@@ -1247,15 +1247,14 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1247
1247
) ;
1248
1248
let res = res. base_res ( ) ;
1249
1249
if res != Res :: Err {
1250
- new_id = Some ( res. def_id ( ) ) ;
1251
- let span = trait_ref. path . span ;
1252
1250
if let PathResult :: Module ( ModuleOrUniformRoot :: Module ( module) ) = self . resolve_path (
1253
1251
& path,
1254
1252
Some ( TypeNS ) ,
1255
- false ,
1256
- span,
1253
+ true ,
1254
+ trait_ref . path . span ,
1257
1255
CrateLint :: SimplePath ( trait_ref. ref_id ) ,
1258
1256
) {
1257
+ new_id = Some ( res. def_id ( ) ) ;
1259
1258
new_val = Some ( ( module, trait_ref. clone ( ) ) ) ;
1260
1259
}
1261
1260
}
@@ -1324,6 +1323,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1324
1323
// If this is a trait impl, ensure the const
1325
1324
// exists in trait
1326
1325
this. check_trait_item (
1326
+ item. id ,
1327
1327
item. ident ,
1328
1328
& item. kind ,
1329
1329
ValueNS ,
@@ -1359,6 +1359,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1359
1359
// If this is a trait impl, ensure the method
1360
1360
// exists in trait
1361
1361
this. check_trait_item (
1362
+ item. id ,
1362
1363
item. ident ,
1363
1364
& item. kind ,
1364
1365
ValueNS ,
@@ -1386,6 +1387,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1386
1387
// If this is a trait impl, ensure the type
1387
1388
// exists in trait
1388
1389
this. check_trait_item (
1390
+ item. id ,
1389
1391
item. ident ,
1390
1392
& item. kind ,
1391
1393
TypeNS ,
@@ -1416,34 +1418,71 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
1416
1418
1417
1419
fn check_trait_item < F > (
1418
1420
& mut self ,
1419
- ident : Ident ,
1421
+ id : NodeId ,
1422
+ mut ident : Ident ,
1420
1423
kind : & AssocItemKind ,
1421
1424
ns : Namespace ,
1422
1425
span : Span ,
1423
1426
err : F ,
1424
1427
) where
1425
1428
F : FnOnce ( Ident , & str , Option < Symbol > ) -> ResolutionError < ' _ > ,
1426
1429
{
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 ;
1445
1464
}
1465
+ _ => { }
1446
1466
}
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
+ ) ;
1447
1486
}
1448
1487
1449
1488
fn resolve_params ( & mut self , params : & ' ast [ Param ] ) {
0 commit comments