Skip to content

Commit 3851794

Browse files
committed
Auto merge of #26783 - eddyb:methrec, r=huonw
After #26694, the overloaded operator and "impl not known at method lookup time" cases started triggering the lint. I've also added checks for overloaded autoderef and method calls via paths (i.e. `T::method()`). All new 8 test cases did not trigger the lint before #26694. r? @huonw
2 parents ceded6a + 585f0e9 commit 3851794

File tree

19 files changed

+215
-128
lines changed

19 files changed

+215
-128
lines changed

src/librustc/metadata/decoder.rs

+2-21
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,7 @@ fn item_to_def_like(cdata: Cmd, item: rbml::Doc, did: ast::DefId) -> DefLike {
299299
Constant => {
300300
// Check whether we have an associated const item.
301301
if item_sort(item) == Some('C') {
302-
// Check whether the associated const is from a trait or impl.
303-
// See the comment for methods below.
304-
let provenance = if reader::maybe_get_doc(
305-
item, tag_item_trait_parent_sort).is_some() {
306-
def::FromTrait(item_require_parent_item(cdata, item))
307-
} else {
308-
def::FromImpl(item_require_parent_item(cdata, item))
309-
};
310-
DlDef(def::DefAssociatedConst(did, provenance))
302+
DlDef(def::DefAssociatedConst(did))
311303
} else {
312304
// Regular const item.
313305
DlDef(def::DefConst(did))
@@ -319,18 +311,7 @@ fn item_to_def_like(cdata: Cmd, item: rbml::Doc, did: ast::DefId) -> DefLike {
319311
Fn => DlDef(def::DefFn(did, false)),
320312
CtorFn => DlDef(def::DefFn(did, true)),
321313
Method | StaticMethod => {
322-
// def_static_method carries an optional field of its enclosing
323-
// trait or enclosing impl (if this is an inherent static method).
324-
// So we need to detect whether this is in a trait or not, which
325-
// we do through the mildly hacky way of checking whether there is
326-
// a trait_parent_sort.
327-
let provenance = if reader::maybe_get_doc(
328-
item, tag_item_trait_parent_sort).is_some() {
329-
def::FromTrait(item_require_parent_item(cdata, item))
330-
} else {
331-
def::FromImpl(item_require_parent_item(cdata, item))
332-
};
333-
DlDef(def::DefMethod(did, provenance))
314+
DlDef(def::DefMethod(did))
334315
}
335316
Type => {
336317
if item_sort(item) == Some('t') {

src/librustc/middle/astencode.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,7 @@ impl tr for def::Def {
444444
fn tr(&self, dcx: &DecodeContext) -> def::Def {
445445
match *self {
446446
def::DefFn(did, is_ctor) => def::DefFn(did.tr(dcx), is_ctor),
447-
def::DefMethod(did, p) => {
448-
def::DefMethod(did.tr(dcx), p.map(|did2| did2.tr(dcx)))
449-
}
447+
def::DefMethod(did) => def::DefMethod(did.tr(dcx)),
450448
def::DefSelfTy(opt_did, impl_ids) => { def::DefSelfTy(opt_did.map(|did| did.tr(dcx)),
451449
impl_ids.map(|(nid1, nid2)| {
452450
(dcx.tr_id(nid1),
@@ -456,9 +454,7 @@ impl tr for def::Def {
456454
def::DefForeignMod(did) => { def::DefForeignMod(did.tr(dcx)) }
457455
def::DefStatic(did, m) => { def::DefStatic(did.tr(dcx), m) }
458456
def::DefConst(did) => { def::DefConst(did.tr(dcx)) }
459-
def::DefAssociatedConst(did, p) => {
460-
def::DefAssociatedConst(did.tr(dcx), p.map(|did2| did2.tr(dcx)))
461-
}
457+
def::DefAssociatedConst(did) => def::DefAssociatedConst(did.tr(dcx)),
462458
def::DefLocal(nid) => { def::DefLocal(dcx.tr_id(nid)) }
463459
def::DefVariant(e_did, v_did, is_s) => {
464460
def::DefVariant(e_did.tr(dcx), v_did.tr(dcx), is_s)

src/librustc/middle/check_const.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
650650
}
651651
}
652652
Some(def::DefConst(did)) |
653-
Some(def::DefAssociatedConst(did, _)) => {
653+
Some(def::DefAssociatedConst(did)) => {
654654
if let Some(expr) = const_eval::lookup_const_by_id(v.tcx, did,
655655
Some(e.id)) {
656656
let inner = v.global_expr(Mode::Const, expr);
@@ -696,10 +696,17 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
696696
v.add_qualif(ConstQualif::NON_ZERO_SIZED);
697697
true
698698
}
699-
Some(def::DefMethod(did, def::FromImpl(_))) |
700699
Some(def::DefFn(did, _)) => {
701700
v.handle_const_fn_call(e, did, node_ty)
702701
}
702+
Some(def::DefMethod(did)) => {
703+
match v.tcx.impl_or_trait_item(did).container() {
704+
ty::ImplContainer(_) => {
705+
v.handle_const_fn_call(e, did, node_ty)
706+
}
707+
ty::TraitContainer(_) => false
708+
}
709+
}
703710
_ => false
704711
};
705712
if !is_const {

src/librustc/middle/check_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ impl<'a, 'tcx> Folder for StaticInliner<'a, 'tcx> {
442442
ast::PatIdent(..) | ast::PatEnum(..) | ast::PatQPath(..) => {
443443
let def = self.tcx.def_map.borrow().get(&pat.id).map(|d| d.full_def());
444444
match def {
445-
Some(DefAssociatedConst(did, _)) |
445+
Some(DefAssociatedConst(did)) |
446446
Some(DefConst(did)) => match lookup_const_by_id(self.tcx, did, Some(pat.id)) {
447447
Some(const_expr) => {
448448
const_expr_to_pat(self.tcx, const_expr, pat.span).map(|new_pat| {

src/librustc/middle/check_static_recursion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CheckItemRecursionVisitor<'a, 'ast> {
238238
ast::ExprPath(..) => {
239239
match self.def_map.borrow().get(&e.id).map(|d| d.base_def) {
240240
Some(DefStatic(def_id, _)) |
241-
Some(DefAssociatedConst(def_id, _)) |
241+
Some(DefAssociatedConst(def_id)) |
242242
Some(DefConst(def_id))
243243
if ast_util::is_local(def_id) => {
244244
match self.ast_map.get(def_id.node) {

src/librustc/middle/const_eval.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn lookup_const<'a>(tcx: &'a ty::ctxt, e: &Expr) -> Option<&'a Expr> {
4141
let opt_def = tcx.def_map.borrow().get(&e.id).map(|d| d.full_def());
4242
match opt_def {
4343
Some(def::DefConst(def_id)) |
44-
Some(def::DefAssociatedConst(def_id, _)) => {
44+
Some(def::DefAssociatedConst(def_id)) => {
4545
lookup_const_by_id(tcx, def_id, Some(e.id))
4646
}
4747
Some(def::DefVariant(enum_def, variant_def, _)) => {
@@ -929,10 +929,10 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
929929
(lookup_const_by_id(tcx, def_id, Some(e.id)), None)
930930
}
931931
}
932-
Some(def::DefAssociatedConst(def_id, provenance)) => {
932+
Some(def::DefAssociatedConst(def_id)) => {
933933
if ast_util::is_local(def_id) {
934-
match provenance {
935-
def::FromTrait(trait_id) => match tcx.map.find(def_id.node) {
934+
match tcx.impl_or_trait_item(def_id).container() {
935+
ty::TraitContainer(trait_id) => match tcx.map.find(def_id.node) {
936936
Some(ast_map::NodeTraitItem(ti)) => match ti.node {
937937
ast::ConstTraitItem(ref ty, _) => {
938938
if let ExprTypeChecked = ty_hint {
@@ -950,7 +950,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
950950
},
951951
_ => (None, None)
952952
},
953-
def::FromImpl(_) => match tcx.map.find(def_id.node) {
953+
ty::ImplContainer(_) => match tcx.map.find(def_id.node) {
954954
Some(ast_map::NodeImplItem(ii)) => match ii.node {
955955
ast::ConstImplItem(ref ty, ref expr) => {
956956
(Some(&**expr), Some(&**ty))

src/librustc/middle/def.rs

+3-21
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111
pub use self::Def::*;
12-
pub use self::MethodProvenance::*;
1312

1413
use middle::privacy::LastPrivate;
1514
use middle::subst::ParamSpace;
@@ -28,7 +27,7 @@ pub enum Def {
2827
DefForeignMod(ast::DefId),
2928
DefStatic(ast::DefId, bool /* is_mutbl */),
3029
DefConst(ast::DefId),
31-
DefAssociatedConst(ast::DefId /* const */, MethodProvenance),
30+
DefAssociatedConst(ast::DefId),
3231
DefLocal(ast::NodeId),
3332
DefVariant(ast::DefId /* enum */, ast::DefId /* variant */, bool /* is_structure */),
3433
DefTy(ast::DefId, bool /* is_enum */),
@@ -51,7 +50,7 @@ pub enum Def {
5150
DefStruct(ast::DefId),
5251
DefRegion(ast::NodeId),
5352
DefLabel(ast::NodeId),
54-
DefMethod(ast::DefId /* method */, MethodProvenance),
53+
DefMethod(ast::DefId),
5554
}
5655

5756
/// The result of resolving a path.
@@ -112,23 +111,6 @@ pub struct Export {
112111
pub def_id: ast::DefId, // The definition of the target.
113112
}
114113

115-
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
116-
pub enum MethodProvenance {
117-
FromTrait(ast::DefId),
118-
FromImpl(ast::DefId),
119-
}
120-
121-
impl MethodProvenance {
122-
pub fn map<F>(self, f: F) -> MethodProvenance where
123-
F: FnOnce(ast::DefId) -> ast::DefId,
124-
{
125-
match self {
126-
FromTrait(did) => FromTrait(f(did)),
127-
FromImpl(did) => FromImpl(f(did))
128-
}
129-
}
130-
}
131-
132114
impl Def {
133115
pub fn local_node_id(&self) -> ast::NodeId {
134116
let def_id = self.def_id();
@@ -141,7 +123,7 @@ impl Def {
141123
DefFn(id, _) | DefMod(id) | DefForeignMod(id) | DefStatic(id, _) |
142124
DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(_, id) |
143125
DefTyParam(_, _, id, _) | DefUse(id) | DefStruct(id) | DefTrait(id) |
144-
DefMethod(id, _) | DefConst(id) | DefAssociatedConst(id, _) |
126+
DefMethod(id) | DefConst(id) | DefAssociatedConst(id) |
145127
DefSelfTy(Some(id), None)=> {
146128
id
147129
}

src/librustc_lint/builtin.rs

+53-15
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
use metadata::{csearch, decoder};
3535
use middle::{cfg, def, infer, pat_util, stability, traits};
36-
use middle::def::*;
3736
use middle::subst::Substs;
3837
use middle::ty::{self, Ty};
3938
use middle::const_eval::{eval_const_expr_partial, ConstVal};
@@ -2251,34 +2250,73 @@ impl LintPass for UnconditionalRecursion {
22512250
}
22522251
}
22532252

2254-
// Check if the method call `id` refers to method `method`.
2253+
// Check if the expression `id` performs a call to `method`.
22552254
fn expr_refers_to_this_method(tcx: &ty::ctxt,
22562255
method: &ty::Method,
22572256
id: ast::NodeId) -> bool {
2258-
let method_call = ty::MethodCall::expr(id);
2259-
let callee = match tcx.tables.borrow().method_map.get(&method_call) {
2260-
Some(&m) => m,
2261-
None => return false
2262-
};
2263-
let callee_item = tcx.impl_or_trait_item(callee.def_id);
2257+
let tables = tcx.tables.borrow();
2258+
2259+
// Check for method calls and overloaded operators.
2260+
if let Some(m) = tables.method_map.get(&ty::MethodCall::expr(id)) {
2261+
if method_call_refers_to_method(tcx, method, m.def_id, m.substs, id) {
2262+
return true;
2263+
}
2264+
}
2265+
2266+
// Check for overloaded autoderef method calls.
2267+
if let Some(&ty::AdjustDerefRef(ref adj)) = tables.adjustments.get(&id) {
2268+
for i in 0..adj.autoderefs {
2269+
let method_call = ty::MethodCall::autoderef(id, i as u32);
2270+
if let Some(m) = tables.method_map.get(&method_call) {
2271+
if method_call_refers_to_method(tcx, method, m.def_id, m.substs, id) {
2272+
return true;
2273+
}
2274+
}
2275+
}
2276+
}
2277+
2278+
// Check for calls to methods via explicit paths (e.g. `T::method()`).
2279+
match tcx.map.get(id) {
2280+
ast_map::NodeExpr(&ast::Expr { node: ast::ExprCall(ref callee, _), .. }) => {
2281+
match tcx.def_map.borrow().get(&callee.id).map(|d| d.full_def()) {
2282+
Some(def::DefMethod(def_id)) => {
2283+
let no_substs = &ty::ItemSubsts::empty();
2284+
let ts = tables.item_substs.get(&callee.id).unwrap_or(no_substs);
2285+
method_call_refers_to_method(tcx, method, def_id, &ts.substs, id)
2286+
}
2287+
_ => false
2288+
}
2289+
}
2290+
_ => false
2291+
}
2292+
}
2293+
2294+
// Check if the method call to the method with the ID `callee_id`
2295+
// and instantiated with `callee_substs` refers to method `method`.
2296+
fn method_call_refers_to_method<'tcx>(tcx: &ty::ctxt<'tcx>,
2297+
method: &ty::Method,
2298+
callee_id: ast::DefId,
2299+
callee_substs: &Substs<'tcx>,
2300+
expr_id: ast::NodeId) -> bool {
2301+
let callee_item = tcx.impl_or_trait_item(callee_id);
22642302

22652303
match callee_item.container() {
22662304
// This is an inherent method, so the `def_id` refers
22672305
// directly to the method definition.
22682306
ty::ImplContainer(_) => {
2269-
callee.def_id == method.def_id
2307+
callee_id == method.def_id
22702308
}
22712309

22722310
// A trait method, from any number of possible sources.
22732311
// Attempt to select a concrete impl before checking.
22742312
ty::TraitContainer(trait_def_id) => {
2275-
let trait_substs = callee.substs.clone().method_to_trait();
2313+
let trait_substs = callee_substs.clone().method_to_trait();
22762314
let trait_substs = tcx.mk_substs(trait_substs);
22772315
let trait_ref = ty::TraitRef::new(trait_def_id, trait_substs);
22782316
let trait_ref = ty::Binder(trait_ref);
2279-
let span = tcx.map.span(id);
2317+
let span = tcx.map.span(expr_id);
22802318
let obligation =
2281-
traits::Obligation::new(traits::ObligationCause::misc(span, id),
2319+
traits::Obligation::new(traits::ObligationCause::misc(span, expr_id),
22822320
trait_ref.to_poly_trait_predicate());
22832321

22842322
let param_env = ty::ParameterEnvironment::for_item(tcx, method.def_id.node);
@@ -2289,12 +2327,12 @@ impl LintPass for UnconditionalRecursion {
22892327
// If `T` is `Self`, then this call is inside
22902328
// a default method definition.
22912329
Ok(Some(traits::VtableParam(_))) => {
2292-
let self_ty = callee.substs.self_ty();
2330+
let self_ty = callee_substs.self_ty();
22932331
let on_self = self_ty.map_or(false, |t| t.is_self());
22942332
// We can only be recurring in a default
22952333
// method if we're being called literally
22962334
// on the `Self` type.
2297-
on_self && callee.def_id == method.def_id
2335+
on_self && callee_id == method.def_id
22982336
}
22992337

23002338
// The `impl` is known, so we check that with a
@@ -2454,7 +2492,7 @@ impl LintPass for MutableTransmutes {
24542492
ast::ExprPath(..) => (),
24552493
_ => return None
24562494
}
2457-
if let DefFn(did, _) = cx.tcx.resolve_expr(expr) {
2495+
if let def::DefFn(did, _) = cx.tcx.resolve_expr(expr) {
24582496
if !def_id_is_transmute(cx, did) {
24592497
return None;
24602498
}

src/librustc_resolve/build_reduced_graph.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -545,14 +545,12 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
545545

546546
match trait_item.node {
547547
ast::ConstTraitItem(..) => {
548-
let def = DefAssociatedConst(local_def(trait_item.id),
549-
FromTrait(local_def(item.id)));
548+
let def = DefAssociatedConst(local_def(trait_item.id));
550549
// NB: not DefModifiers::IMPORTABLE
551550
name_bindings.define_value(def, trait_item.span, DefModifiers::PUBLIC);
552551
}
553552
ast::MethodTraitItem(..) => {
554-
let def = DefMethod(local_def(trait_item.id),
555-
FromTrait(local_def(item.id)));
553+
let def = DefMethod(local_def(trait_item.id));
556554
// NB: not DefModifiers::IMPORTABLE
557555
name_bindings.define_value(def, trait_item.span, DefModifiers::PUBLIC);
558556
}

src/librustc_resolve/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3448,7 +3448,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
34483448
// Look for a method in the current self type's impl module.
34493449
if let Some(module) = get_module(self, path.span, &name_path) {
34503450
if let Some(binding) = module.children.borrow().get(&name) {
3451-
if let Some(DefMethod(did, _)) = binding.def_for_namespace(ValueNS) {
3451+
if let Some(DefMethod(did)) = binding.def_for_namespace(ValueNS) {
34523452
if is_static_method(self, did) {
34533453
return StaticMethod(path_names_to_string(&path, 0))
34543454
}

src/librustc_trans/save/dump_csv.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
719719
let def_map = self.tcx.def_map.borrow();
720720
let def = def_map.get(&id).unwrap().full_def();
721721
match def {
722-
def::DefMethod(did, _) => {
722+
def::DefMethod(did) => {
723723
let ti = self.tcx.impl_or_trait_item(did);
724724
if let ty::MethodTraitItem(m) = ti {
725725
if m.explicit_self == ty::StaticExplicitSelfCategory {

src/librustc_trans/save/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -551,20 +551,20 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
551551
scope: self.enclosing_scope(id),
552552
}))
553553
}
554-
def::DefMethod(decl_id, provenence) => {
554+
def::DefMethod(decl_id) => {
555555
let sub_span = self.span_utils.sub_span_for_meth_name(path.span);
556556
let def_id = if decl_id.krate == ast::LOCAL_CRATE {
557557
let ti = self.tcx.impl_or_trait_item(decl_id);
558-
match provenence {
559-
def::FromTrait(def_id) => {
558+
match ti.container() {
559+
ty::TraitContainer(def_id) => {
560560
self.tcx.trait_items(def_id)
561561
.iter()
562562
.find(|mr| {
563563
mr.name() == ti.name() && self.trait_method_has_body(mr)
564564
})
565565
.map(|mr| mr.def_id())
566566
}
567-
def::FromImpl(def_id) => {
567+
ty::ImplContainer(def_id) => {
568568
let impl_items = self.tcx.impl_items.borrow();
569569
Some(impl_items.get(&def_id)
570570
.unwrap()

0 commit comments

Comments
 (0)