@@ -1406,11 +1406,18 @@ fn resolution_failure(
14061406 let in_scope = kinds. iter ( ) . any ( |kind| kind. res ( ) . is_some ( ) ) ;
14071407 let mut reported_not_in_scope = false ;
14081408 let item = |res : Res | {
1409- if let Some ( id) = res. opt_def_id ( ) {
1410- ( format ! ( "the {} `{}`" , res. descr( ) , cx. tcx. item_name( id) . to_string( ) ) , "," )
1411- } else {
1412- ( format ! ( "{} {}" , res. article( ) , res. descr( ) ) , "" )
1413- }
1409+ format ! ( "the {} `{}`" , res. descr( ) , cx. tcx. item_name( res. def_id( ) ) . to_string( ) )
1410+ } ;
1411+ let assoc_item_not_allowed = |res : Res , diag : & mut DiagnosticBuilder < ' _ > | {
1412+ let def_id = res. def_id ( ) ;
1413+ let name = cx. tcx . item_name ( def_id) ;
1414+ let note = format ! (
1415+ "`{}` is {} {}, not a module or type, and cannot have associated items" ,
1416+ name,
1417+ res. article( ) ,
1418+ res. descr( )
1419+ ) ;
1420+ diag. note ( & note) ;
14141421 } ;
14151422 for failure in kinds {
14161423 match failure {
@@ -1425,11 +1432,9 @@ fn resolution_failure(
14251432 }
14261433 ResolutionFailure :: Dummy => continue ,
14271434 ResolutionFailure :: WrongNamespace ( res, expected_ns) => {
1428- let ( item, comma) = item ( res) ;
14291435 let note = format ! (
1430- "this link resolves to {}{} which is not in the {} namespace" ,
1431- item,
1432- comma,
1436+ "this link resolves to {}, which is not in the {} namespace" ,
1437+ item( res) ,
14331438 expected_ns. descr( )
14341439 ) ;
14351440 diag. note ( & note) ;
@@ -1450,10 +1455,9 @@ fn resolution_failure(
14501455 panic ! ( "all intra doc links should have a parent item" )
14511456 }
14521457 ResolutionFailure :: NoPrimitiveImpl ( res, _) => {
1453- let ( item, comma) = item ( res) ;
14541458 let note = format ! (
1455- "this link partially resolves to {}{} which does not have any associated items" ,
1456- item, comma ,
1459+ "this link partially resolves to {}, which does not have any associated items" ,
1460+ item( res ) ,
14571461 ) ;
14581462 diag. note ( & note) ;
14591463 }
@@ -1465,41 +1469,62 @@ fn resolution_failure(
14651469 diag. note ( & note) ;
14661470 }
14671471 ResolutionFailure :: NoAssocItem ( res, assoc_item) => {
1468- let ( item, _) = item ( res) ;
1469- diag. note ( & format ! ( "this link partially resolves to {}" , item) ) ;
1470- // FIXME: when are items neither a primitive nor a Def?
1471- if let Res :: Def ( _, def_id) = res {
1472- let name = cx. tcx . item_name ( def_id) ;
1473- let note = format ! ( "no `{}` in `{}`" , assoc_item, name, ) ;
1474- diag. note ( & note) ;
1475- }
1472+ use DefKind :: * ;
1473+
1474+ let ( kind, def_id) = match res {
1475+ Res :: Def ( kind, def_id) => ( kind, def_id) ,
1476+ _ => unreachable ! (
1477+ "primitives are covered above and other `Res` variants aren't possible at module scope"
1478+ ) ,
1479+ } ;
1480+ let name = cx. tcx . item_name ( def_id) ;
1481+ let path_description = match kind {
1482+ Mod | ForeignMod => "inner item" ,
1483+ Struct => "field or associated item" ,
1484+ Enum | Union => "variant or associated item" ,
1485+ Variant
1486+ | Field
1487+ | Closure
1488+ | Generator
1489+ | AssocTy
1490+ | AssocConst
1491+ | AssocFn
1492+ | Fn
1493+ | Macro ( _)
1494+ | Const
1495+ | ConstParam
1496+ | ExternCrate
1497+ | Use
1498+ | LifetimeParam
1499+ | Ctor ( _, _)
1500+ | AnonConst => return assoc_item_not_allowed ( res, diag) ,
1501+ Trait | TyAlias | ForeignTy | OpaqueTy | TraitAlias | TyParam
1502+ | Static => "associated item" ,
1503+ Impl | GlobalAsm => unreachable ! ( "not a path" ) ,
1504+ } ;
1505+ let note = format ! (
1506+ "the {} `{}` has no {} named `{}`" ,
1507+ res. descr( ) ,
1508+ name,
1509+ path_description,
1510+ assoc_item
1511+ ) ;
1512+ diag. note ( & note) ;
14761513 }
14771514 ResolutionFailure :: CannotHaveAssociatedItems ( res, _) => {
1478- let ( item, _) = item ( res) ;
1479- diag. note ( & format ! ( "this link partially resolves to {}" , item) ) ;
1480- if let Res :: Def ( kind, def_id) = res {
1481- let name = cx. tcx . item_name ( def_id) ;
1482- let note = format ! (
1483- "`{}` is {} {}, not a module or type, and cannot have associated items" ,
1484- name,
1485- kind. article( ) ,
1486- kind. descr( def_id)
1487- ) ;
1488- diag. note ( & note) ;
1489- }
1515+ assoc_item_not_allowed ( res, diag)
14901516 }
14911517 // TODO: is there ever a case where this happens?
14921518 ResolutionFailure :: NotAnEnum ( res) => {
1493- let ( item, comma) = item ( res) ;
14941519 let note =
1495- format ! ( "this link resolves to {}{} which is not an enum" , item, comma ) ;
1520+ format ! ( "this link resolves to {}, which is not an enum" , item( res ) ) ;
14961521 diag. note ( & note) ;
14971522 diag. note ( "if this were an enum, it might have a variant which resolved" ) ;
14981523 }
14991524 ResolutionFailure :: NotAVariant ( res, variant) => {
15001525 let note = format ! (
15011526 "this link partially resolves to {}, but there is no variant named {}" ,
1502- item( res) . 0 ,
1527+ item( res) ,
15031528 variant
15041529 ) ;
15051530 diag. note ( & note) ;
0 commit comments