@@ -109,6 +109,45 @@ impl Res {
109
109
Res :: Primitive ( _) => None ,
110
110
}
111
111
}
112
+
113
+ /// Used for error reporting.
114
+ fn disambiguator_suggestion ( self ) -> Suggestion {
115
+ let kind = match self {
116
+ Res :: Primitive ( _) => return Suggestion :: Prefix ( "prim" ) ,
117
+ Res :: Def ( kind, _) => kind,
118
+ } ;
119
+ if kind == DefKind :: Macro ( MacroKind :: Bang ) {
120
+ return Suggestion :: Macro ;
121
+ } else if kind == DefKind :: Fn || kind == DefKind :: AssocFn {
122
+ return Suggestion :: Function ;
123
+ } else if kind == DefKind :: Field {
124
+ return Suggestion :: RemoveDisambiguator ;
125
+ }
126
+
127
+ let prefix = match kind {
128
+ DefKind :: Struct => "struct" ,
129
+ DefKind :: Enum => "enum" ,
130
+ DefKind :: Trait => "trait" ,
131
+ DefKind :: Union => "union" ,
132
+ DefKind :: Mod => "mod" ,
133
+ DefKind :: Const | DefKind :: ConstParam | DefKind :: AssocConst | DefKind :: AnonConst => {
134
+ "const"
135
+ }
136
+ DefKind :: Static => "static" ,
137
+ DefKind :: Macro ( MacroKind :: Derive ) => "derive" ,
138
+ // Now handle things that don't have a specific disambiguator
139
+ _ => match kind
140
+ . ns ( )
141
+ . expect ( "tried to calculate a disambiguator for a def without a namespace?" )
142
+ {
143
+ Namespace :: TypeNS => "type" ,
144
+ Namespace :: ValueNS => "value" ,
145
+ Namespace :: MacroNS => "macro" ,
146
+ } ,
147
+ } ;
148
+
149
+ Suggestion :: Prefix ( prefix)
150
+ }
112
151
}
113
152
114
153
impl TryFrom < ResolveRes > for Res {
@@ -1267,7 +1306,7 @@ impl LinkCollector<'_, '_> {
1267
1306
}
1268
1307
}
1269
1308
1270
- let report_mismatch = |specified : Disambiguator , resolved : Disambiguator | {
1309
+ let report_mismatch = |specified : Disambiguator , resolved : Res | {
1271
1310
// The resolved item did not match the disambiguator; give a better error than 'not found'
1272
1311
let msg = format ! ( "incompatible link kind for `{}`" , path_str) ;
1273
1312
let callback = |diag : & mut DiagnosticBuilder < ' _ > , sp : Option < rustc_span:: Span > | {
@@ -1276,7 +1315,7 @@ impl LinkCollector<'_, '_> {
1276
1315
resolved. article( ) ,
1277
1316
resolved. descr( ) ,
1278
1317
specified. article( ) ,
1279
- specified. descr( )
1318
+ specified. descr( ) ,
1280
1319
) ;
1281
1320
if let Some ( sp) = sp {
1282
1321
diag. span_label ( sp, & note) ;
@@ -1311,7 +1350,7 @@ impl LinkCollector<'_, '_> {
1311
1350
=> { }
1312
1351
( actual, Some ( Disambiguator :: Kind ( expected) ) ) if actual == expected => { }
1313
1352
( _, Some ( specified @ Disambiguator :: Kind ( _) | specified @ Disambiguator :: Primitive ) ) => {
1314
- report_mismatch ( specified, Disambiguator :: Kind ( kind) ) ;
1353
+ report_mismatch ( specified, Res :: Def ( kind, id ) ) ;
1315
1354
return None ;
1316
1355
}
1317
1356
}
@@ -1362,7 +1401,7 @@ impl LinkCollector<'_, '_> {
1362
1401
match disambiguator {
1363
1402
Some ( Disambiguator :: Primitive | Disambiguator :: Namespace ( _) ) | None => { }
1364
1403
Some ( other) => {
1365
- report_mismatch ( other, Disambiguator :: Primitive ) ;
1404
+ report_mismatch ( other, res ) ;
1366
1405
return None ;
1367
1406
}
1368
1407
}
@@ -1676,53 +1715,6 @@ impl Disambiguator {
1676
1715
}
1677
1716
}
1678
1717
1679
- fn from_res ( res : Res ) -> Self {
1680
- match res {
1681
- Res :: Def ( kind, _) => Disambiguator :: Kind ( kind) ,
1682
- Res :: Primitive ( _) => Disambiguator :: Primitive ,
1683
- }
1684
- }
1685
-
1686
- /// Used for error reporting.
1687
- fn suggestion ( self ) -> Suggestion {
1688
- let kind = match self {
1689
- Disambiguator :: Primitive => return Suggestion :: Prefix ( "prim" ) ,
1690
- Disambiguator :: Kind ( kind) => kind,
1691
- Disambiguator :: Namespace ( _) => panic ! ( "display_for cannot be used on namespaces" ) ,
1692
- } ;
1693
- if kind == DefKind :: Macro ( MacroKind :: Bang ) {
1694
- return Suggestion :: Macro ;
1695
- } else if kind == DefKind :: Fn || kind == DefKind :: AssocFn {
1696
- return Suggestion :: Function ;
1697
- } else if kind == DefKind :: Field {
1698
- return Suggestion :: RemoveDisambiguator ;
1699
- }
1700
-
1701
- let prefix = match kind {
1702
- DefKind :: Struct => "struct" ,
1703
- DefKind :: Enum => "enum" ,
1704
- DefKind :: Trait => "trait" ,
1705
- DefKind :: Union => "union" ,
1706
- DefKind :: Mod => "mod" ,
1707
- DefKind :: Const | DefKind :: ConstParam | DefKind :: AssocConst | DefKind :: AnonConst => {
1708
- "const"
1709
- }
1710
- DefKind :: Static => "static" ,
1711
- DefKind :: Macro ( MacroKind :: Derive ) => "derive" ,
1712
- // Now handle things that don't have a specific disambiguator
1713
- _ => match kind
1714
- . ns ( )
1715
- . expect ( "tried to calculate a disambiguator for a def without a namespace?" )
1716
- {
1717
- Namespace :: TypeNS => "type" ,
1718
- Namespace :: ValueNS => "value" ,
1719
- Namespace :: MacroNS => "macro" ,
1720
- } ,
1721
- } ;
1722
-
1723
- Suggestion :: Prefix ( prefix)
1724
- }
1725
-
1726
1718
fn ns ( self ) -> Namespace {
1727
1719
match self {
1728
1720
Self :: Namespace ( n) => n,
@@ -2070,15 +2062,9 @@ fn resolution_failure(
2070
2062
ResolutionFailure :: NotResolved { .. } => unreachable ! ( "handled above" ) ,
2071
2063
ResolutionFailure :: Dummy => continue ,
2072
2064
ResolutionFailure :: WrongNamespace { res, expected_ns } => {
2073
- if let Res :: Def ( kind, _) = res {
2074
- let disambiguator = Disambiguator :: Kind ( kind) ;
2075
- suggest_disambiguator (
2076
- disambiguator,
2077
- diag,
2078
- path_str,
2079
- diag_info. ori_link ,
2080
- sp,
2081
- )
2065
+ // FIXME: does this need to be behind an `if`?
2066
+ if matches ! ( res, Res :: Def ( ..) ) {
2067
+ suggest_disambiguator ( res, diag, path_str, diag_info. ori_link , sp) ;
2082
2068
}
2083
2069
2084
2070
format ! (
@@ -2214,23 +2200,22 @@ fn ambiguity_error(
2214
2200
}
2215
2201
2216
2202
for res in candidates {
2217
- let disambiguator = Disambiguator :: from_res ( res) ;
2218
- suggest_disambiguator ( disambiguator, diag, path_str, diag_info. ori_link , sp) ;
2203
+ suggest_disambiguator ( res, diag, path_str, diag_info. ori_link , sp) ;
2219
2204
}
2220
2205
} ) ;
2221
2206
}
2222
2207
2223
2208
/// In case of an ambiguity or mismatched disambiguator, suggest the correct
2224
2209
/// disambiguator.
2225
2210
fn suggest_disambiguator (
2226
- disambiguator : Disambiguator ,
2211
+ res : Res ,
2227
2212
diag : & mut DiagnosticBuilder < ' _ > ,
2228
2213
path_str : & str ,
2229
2214
ori_link : & str ,
2230
2215
sp : Option < rustc_span:: Span > ,
2231
2216
) {
2232
- let suggestion = disambiguator . suggestion ( ) ;
2233
- let help = format ! ( "to link to the {}, {}" , disambiguator . descr( ) , suggestion. descr( ) ) ;
2217
+ let suggestion = res . disambiguator_suggestion ( ) ;
2218
+ let help = format ! ( "to link to the {}, {}" , res . descr( ) , suggestion. descr( ) ) ;
2234
2219
2235
2220
if let Some ( sp) = sp {
2236
2221
let mut spans = suggestion. as_help_span ( path_str, ori_link, sp) ;
0 commit comments