@@ -1247,15 +1247,14 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
12471247 ) ;
12481248 let res = res. base_res ( ) ;
12491249 if res != Res :: Err {
1250- new_id = Some ( res. def_id ( ) ) ;
1251- let span = trait_ref. path . span ;
12521250 if let PathResult :: Module ( ModuleOrUniformRoot :: Module ( module) ) = self . resolve_path (
12531251 & path,
12541252 Some ( TypeNS ) ,
1255- false ,
1256- span,
1253+ true ,
1254+ trait_ref . path . span ,
12571255 CrateLint :: SimplePath ( trait_ref. ref_id ) ,
12581256 ) {
1257+ new_id = Some ( res. def_id ( ) ) ;
12591258 new_val = Some ( ( module, trait_ref. clone ( ) ) ) ;
12601259 }
12611260 }
@@ -1324,6 +1323,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
13241323 // If this is a trait impl, ensure the const
13251324 // exists in trait
13261325 this. check_trait_item (
1326+ item. id ,
13271327 item. ident ,
13281328 & item. kind ,
13291329 ValueNS ,
@@ -1359,6 +1359,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
13591359 // If this is a trait impl, ensure the method
13601360 // exists in trait
13611361 this. check_trait_item (
1362+ item. id ,
13621363 item. ident ,
13631364 & item. kind ,
13641365 ValueNS ,
@@ -1386,6 +1387,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
13861387 // If this is a trait impl, ensure the type
13871388 // exists in trait
13881389 this. check_trait_item (
1390+ item. id ,
13891391 item. ident ,
13901392 & item. kind ,
13911393 TypeNS ,
@@ -1416,34 +1418,71 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
14161418
14171419 fn check_trait_item < F > (
14181420 & mut self ,
1419- ident : Ident ,
1421+ id : NodeId ,
1422+ mut ident : Ident ,
14201423 kind : & AssocItemKind ,
14211424 ns : Namespace ,
14221425 span : Span ,
14231426 err : F ,
14241427 ) where
14251428 F : FnOnce ( Ident , & str , Option < Symbol > ) -> ResolutionError < ' _ > ,
14261429 {
1427- // If there is a TraitRef in scope for an impl, then the method must be in the
1428- // trait.
1429- if let Some ( ( module, _) ) = self . current_trait_ref {
1430- if self
1431- . r
1432- . resolve_ident_in_module (
1433- ModuleOrUniformRoot :: Module ( module) ,
1434- ident,
1435- ns,
1436- & self . parent_scope ,
1437- false ,
1438- span,
1439- )
1440- . is_err ( )
1441- {
1442- let candidate = self . find_similarly_named_assoc_item ( ident. name , kind) ;
1443- let path = & self . current_trait_ref . as_ref ( ) . unwrap ( ) . 1 . path ;
1444- self . report_error ( span, err ( ident, & path_names_to_string ( path) , candidate) ) ;
1430+ // If there is a TraitRef in scope for an impl, then the method must be in the trait.
1431+ let Some ( ( module, _) ) = & self . current_trait_ref else { return ; } ;
1432+ ident. span . normalize_to_macros_2_0_and_adjust ( module. expansion ) ;
1433+ let key = self . r . new_key ( ident, ns) ;
1434+ let mut binding = self . r . resolution ( module, key) . try_borrow ( ) . ok ( ) . and_then ( |r| r. binding ) ;
1435+ debug ! ( ?binding) ;
1436+ if binding. is_none ( ) {
1437+ // We could not find the trait item in the correct namespace.
1438+ // Check the other namespace to report an error.
1439+ let ns = match ns {
1440+ ValueNS => TypeNS ,
1441+ TypeNS => ValueNS ,
1442+ _ => ns,
1443+ } ;
1444+ let key = self . r . new_key ( ident, ns) ;
1445+ binding = self . r . resolution ( module, key) . try_borrow ( ) . ok ( ) . and_then ( |r| r. binding ) ;
1446+ debug ! ( ?binding) ;
1447+ }
1448+ let Some ( binding) = binding else {
1449+ // We could not find the method: report an error.
1450+ let candidate = self . find_similarly_named_assoc_item ( ident. name , kind) ;
1451+ let path = & self . current_trait_ref . as_ref ( ) . unwrap ( ) . 1 . path ;
1452+ self . report_error ( span, err ( ident, & path_names_to_string ( path) , candidate) ) ;
1453+ return ;
1454+ } ;
1455+
1456+ let res = binding. res ( ) ;
1457+ let Res :: Def ( def_kind, _) = res else { bug ! ( ) } ;
1458+ match ( def_kind, kind) {
1459+ ( DefKind :: AssocTy , AssocItemKind :: TyAlias ( ..) )
1460+ | ( DefKind :: AssocFn , AssocItemKind :: Fn ( ..) )
1461+ | ( DefKind :: AssocConst , AssocItemKind :: Const ( ..) ) => {
1462+ self . r . record_partial_res ( id, PartialRes :: new ( res) ) ;
1463+ return ;
14451464 }
1465+ _ => { }
14461466 }
1467+
1468+ // The method kind does not correspond to what appeared in the trait, report.
1469+ let path = & self . current_trait_ref . as_ref ( ) . unwrap ( ) . 1 . path ;
1470+ let ( code, kind) = match kind {
1471+ AssocItemKind :: Const ( ..) => ( rustc_errors:: error_code!( E0323 ) , "const" ) ,
1472+ AssocItemKind :: Fn ( ..) => ( rustc_errors:: error_code!( E0324 ) , "method" ) ,
1473+ AssocItemKind :: TyAlias ( ..) => ( rustc_errors:: error_code!( E0325 ) , "type" ) ,
1474+ AssocItemKind :: MacCall ( ..) => span_bug ! ( span, "unexpanded macro" ) ,
1475+ } ;
1476+ self . report_error (
1477+ span,
1478+ ResolutionError :: TraitImplMismatch {
1479+ name : ident. name ,
1480+ kind,
1481+ code,
1482+ trait_path : path_names_to_string ( path) ,
1483+ trait_item_span : binding. span ,
1484+ } ,
1485+ ) ;
14471486 }
14481487
14491488 fn resolve_params ( & mut self , params : & ' ast [ Param ] ) {
0 commit comments