@@ -181,16 +181,14 @@ enum ResolutionFailure<'a> {
181
181
/// In `[std::io::Error::x]`, `x` would be unresolved.
182
182
unresolved : Cow < ' a , str > ,
183
183
} ,
184
- /// This link has malformed generic parameters; e.g., the angle brackets are unbalanced.
185
- MalformedGenerics ( MalformedGenerics ) ,
186
184
/// Used to communicate that this should be ignored, but shouldn't be reported to the user.
187
185
///
188
186
/// This happens when there is no disambiguator and one of the namespaces
189
187
/// failed to resolve.
190
188
Dummy ,
191
189
}
192
190
193
- #[ derive( Clone , Debug ) ]
191
+ #[ derive( Clone , Copy , Debug ) ]
194
192
enum MalformedGenerics {
195
193
/// This link has unbalanced angle brackets.
196
194
///
@@ -1088,12 +1086,20 @@ impl<'a, 'tcx> DocVisitor for LinkCollector<'a, 'tcx> {
1088
1086
enum PreprocessingError {
1089
1087
Anchor ( AnchorFailure ) ,
1090
1088
Disambiguator ( Range < usize > , String ) ,
1091
- Resolution ( ResolutionFailure < ' static > , String , Option < Disambiguator > ) ,
1089
+ MalformedGenerics ( MalformedGenerics , String ) ,
1092
1090
}
1093
1091
1094
- impl From < AnchorFailure > for PreprocessingError {
1095
- fn from ( err : AnchorFailure ) -> Self {
1096
- Self :: Anchor ( err)
1092
+ impl PreprocessingError {
1093
+ fn report ( & self , cx : & DocContext < ' _ > , diag_info : DiagnosticInfo < ' _ > ) {
1094
+ match self {
1095
+ PreprocessingError :: Anchor ( err) => anchor_failure ( cx, diag_info, * err) ,
1096
+ PreprocessingError :: Disambiguator ( range, msg) => {
1097
+ disambiguator_error ( cx, diag_info, range. clone ( ) , msg)
1098
+ }
1099
+ PreprocessingError :: MalformedGenerics ( err, path_str) => {
1100
+ report_malformed_generics ( cx, diag_info, * err, path_str)
1101
+ }
1102
+ }
1097
1103
}
1098
1104
}
1099
1105
@@ -1138,7 +1144,7 @@ fn preprocess_link(
1138
1144
let extra_fragment = parts. next ( ) ;
1139
1145
if parts. next ( ) . is_some ( ) {
1140
1146
// A valid link can't have multiple #'s
1141
- return Some ( Err ( AnchorFailure :: MultipleAnchors . into ( ) ) ) ;
1147
+ return Some ( Err ( PreprocessingError :: Anchor ( AnchorFailure :: MultipleAnchors ) ) ) ;
1142
1148
}
1143
1149
1144
1150
// Parse and strip the disambiguator from the link, if present.
@@ -1166,13 +1172,9 @@ fn preprocess_link(
1166
1172
let path_str = if path_str. contains ( [ '<' , '>' ] . as_slice ( ) ) {
1167
1173
match strip_generics_from_path ( path_str) {
1168
1174
Ok ( path) => path,
1169
- Err ( err_kind ) => {
1175
+ Err ( err ) => {
1170
1176
debug ! ( "link has malformed generics: {}" , path_str) ;
1171
- return Some ( Err ( PreprocessingError :: Resolution (
1172
- err_kind,
1173
- path_str. to_owned ( ) ,
1174
- disambiguator,
1175
- ) ) ) ;
1177
+ return Some ( Err ( PreprocessingError :: MalformedGenerics ( err, path_str. to_owned ( ) ) ) ) ;
1176
1178
}
1177
1179
}
1178
1180
} else {
@@ -1222,32 +1224,10 @@ impl LinkCollector<'_, '_> {
1222
1224
link_range : ori_link. range . clone ( ) ,
1223
1225
} ;
1224
1226
1225
- let PreprocessingInfo { path_str, disambiguator, extra_fragment, link_text } = match pp_link
1226
- {
1227
- Ok ( x) => x,
1228
- Err ( err) => {
1229
- match err {
1230
- PreprocessingError :: Anchor ( err) => anchor_failure ( self . cx , diag_info, * err) ,
1231
- PreprocessingError :: Disambiguator ( range, msg) => {
1232
- disambiguator_error ( self . cx , diag_info, range. clone ( ) , msg)
1233
- }
1234
- PreprocessingError :: Resolution ( err, path_str, disambiguator) => {
1235
- resolution_failure (
1236
- self ,
1237
- diag_info,
1238
- path_str,
1239
- * disambiguator,
1240
- smallvec ! [ err. clone( ) ] ,
1241
- ) ;
1242
- }
1243
- }
1244
- return None ;
1245
- }
1246
- } ;
1227
+ let PreprocessingInfo { path_str, disambiguator, extra_fragment, link_text } =
1228
+ pp_link. as_ref ( ) . map_err ( |err| err. report ( self . cx , diag_info. clone ( ) ) ) . ok ( ) ?;
1247
1229
let disambiguator = * disambiguator;
1248
1230
1249
- let inner_docs = item. inner_docs ( self . cx . tcx ) ;
1250
-
1251
1231
// In order to correctly resolve intra-doc links we need to
1252
1232
// pick a base AST node to work from. If the documentation for
1253
1233
// this module came from an inner comment (//!) then we anchor
@@ -1259,6 +1239,7 @@ impl LinkCollector<'_, '_> {
1259
1239
// we've already pushed this node onto the resolution stack but
1260
1240
// for outer comments we explicitly try and resolve against the
1261
1241
// parent_node first.
1242
+ let inner_docs = item. inner_docs ( self . cx . tcx ) ;
1262
1243
let base_node =
1263
1244
if item. is_mod ( ) && inner_docs { self . mod_ids . last ( ) . copied ( ) } else { parent_node } ;
1264
1245
let module_id = base_node. expect ( "doc link without parent module" ) ;
@@ -2121,27 +2102,6 @@ fn resolution_failure(
2121
2102
expected_ns. descr( )
2122
2103
)
2123
2104
}
2124
- ResolutionFailure :: MalformedGenerics ( variant) => match variant {
2125
- MalformedGenerics :: UnbalancedAngleBrackets => {
2126
- String :: from ( "unbalanced angle brackets" )
2127
- }
2128
- MalformedGenerics :: MissingType => {
2129
- String :: from ( "missing type for generic parameters" )
2130
- }
2131
- MalformedGenerics :: HasFullyQualifiedSyntax => {
2132
- diag. note ( "see https://github.com/rust-lang/rust/issues/74563 for more information" ) ;
2133
- String :: from ( "fully-qualified syntax is unsupported" )
2134
- }
2135
- MalformedGenerics :: InvalidPathSeparator => {
2136
- String :: from ( "has invalid path separator" )
2137
- }
2138
- MalformedGenerics :: TooManyAngleBrackets => {
2139
- String :: from ( "too many angle brackets" )
2140
- }
2141
- MalformedGenerics :: EmptyAngleBrackets => {
2142
- String :: from ( "empty angle brackets" )
2143
- }
2144
- } ,
2145
2105
} ;
2146
2106
if let Some ( span) = sp {
2147
2107
diag. span_label ( span, & note) ;
@@ -2205,6 +2165,40 @@ fn disambiguator_error(
2205
2165
} ) ;
2206
2166
}
2207
2167
2168
+ fn report_malformed_generics (
2169
+ cx : & DocContext < ' _ > ,
2170
+ diag_info : DiagnosticInfo < ' _ > ,
2171
+ err : MalformedGenerics ,
2172
+ path_str : & str ,
2173
+ ) {
2174
+ report_diagnostic (
2175
+ cx. tcx ,
2176
+ BROKEN_INTRA_DOC_LINKS ,
2177
+ & format ! ( "unresolved link to `{}`" , path_str) ,
2178
+ & diag_info,
2179
+ |diag, sp| {
2180
+ let note = match err {
2181
+ MalformedGenerics :: UnbalancedAngleBrackets => "unbalanced angle brackets" ,
2182
+ MalformedGenerics :: MissingType => "missing type for generic parameters" ,
2183
+ MalformedGenerics :: HasFullyQualifiedSyntax => {
2184
+ diag. note (
2185
+ "see https://github.com/rust-lang/rust/issues/74563 for more information" ,
2186
+ ) ;
2187
+ "fully-qualified syntax is unsupported"
2188
+ }
2189
+ MalformedGenerics :: InvalidPathSeparator => "has invalid path separator" ,
2190
+ MalformedGenerics :: TooManyAngleBrackets => "too many angle brackets" ,
2191
+ MalformedGenerics :: EmptyAngleBrackets => "empty angle brackets" ,
2192
+ } ;
2193
+ if let Some ( span) = sp {
2194
+ diag. span_label ( span, note) ;
2195
+ } else {
2196
+ diag. note ( note) ;
2197
+ }
2198
+ } ,
2199
+ ) ;
2200
+ }
2201
+
2208
2202
/// Report an ambiguity error, where there were multiple possible resolutions.
2209
2203
fn ambiguity_error (
2210
2204
cx : & DocContext < ' _ > ,
@@ -2340,7 +2334,7 @@ fn resolve_primitive(path_str: &str, ns: Namespace) -> Option<Res> {
2340
2334
Some ( Res :: Primitive ( prim) )
2341
2335
}
2342
2336
2343
- fn strip_generics_from_path ( path_str : & str ) -> Result < String , ResolutionFailure < ' static > > {
2337
+ fn strip_generics_from_path ( path_str : & str ) -> Result < String , MalformedGenerics > {
2344
2338
let mut stripped_segments = vec ! [ ] ;
2345
2339
let mut path = path_str. chars ( ) . peekable ( ) ;
2346
2340
let mut segment = Vec :: new ( ) ;
@@ -2355,24 +2349,18 @@ fn strip_generics_from_path(path_str: &str) -> Result<String, ResolutionFailure<
2355
2349
stripped_segments. push ( stripped_segment) ;
2356
2350
}
2357
2351
} else {
2358
- return Err ( ResolutionFailure :: MalformedGenerics (
2359
- MalformedGenerics :: InvalidPathSeparator ,
2360
- ) ) ;
2352
+ return Err ( MalformedGenerics :: InvalidPathSeparator ) ;
2361
2353
}
2362
2354
}
2363
2355
'<' => {
2364
2356
segment. push ( chr) ;
2365
2357
2366
2358
match path. next ( ) {
2367
2359
Some ( '<' ) => {
2368
- return Err ( ResolutionFailure :: MalformedGenerics (
2369
- MalformedGenerics :: TooManyAngleBrackets ,
2370
- ) ) ;
2360
+ return Err ( MalformedGenerics :: TooManyAngleBrackets ) ;
2371
2361
}
2372
2362
Some ( '>' ) => {
2373
- return Err ( ResolutionFailure :: MalformedGenerics (
2374
- MalformedGenerics :: EmptyAngleBrackets ,
2375
- ) ) ;
2363
+ return Err ( MalformedGenerics :: EmptyAngleBrackets ) ;
2376
2364
}
2377
2365
Some ( chr) => {
2378
2366
segment. push ( chr) ;
@@ -2400,16 +2388,10 @@ fn strip_generics_from_path(path_str: &str) -> Result<String, ResolutionFailure<
2400
2388
2401
2389
let stripped_path = stripped_segments. join ( "::" ) ;
2402
2390
2403
- if !stripped_path. is_empty ( ) {
2404
- Ok ( stripped_path)
2405
- } else {
2406
- Err ( ResolutionFailure :: MalformedGenerics ( MalformedGenerics :: MissingType ) )
2407
- }
2391
+ if !stripped_path. is_empty ( ) { Ok ( stripped_path) } else { Err ( MalformedGenerics :: MissingType ) }
2408
2392
}
2409
2393
2410
- fn strip_generics_from_path_segment (
2411
- segment : Vec < char > ,
2412
- ) -> Result < String , ResolutionFailure < ' static > > {
2394
+ fn strip_generics_from_path_segment ( segment : Vec < char > ) -> Result < String , MalformedGenerics > {
2413
2395
let mut stripped_segment = String :: new ( ) ;
2414
2396
let mut param_depth = 0 ;
2415
2397
@@ -2424,9 +2406,7 @@ fn strip_generics_from_path_segment(
2424
2406
if latest_generics_chunk. contains ( " as " ) {
2425
2407
// The segment tries to use fully-qualified syntax, which is currently unsupported.
2426
2408
// Give a helpful error message instead of completely ignoring the angle brackets.
2427
- return Err ( ResolutionFailure :: MalformedGenerics (
2428
- MalformedGenerics :: HasFullyQualifiedSyntax ,
2429
- ) ) ;
2409
+ return Err ( MalformedGenerics :: HasFullyQualifiedSyntax ) ;
2430
2410
}
2431
2411
} else {
2432
2412
if param_depth == 0 {
@@ -2441,6 +2421,6 @@ fn strip_generics_from_path_segment(
2441
2421
Ok ( stripped_segment)
2442
2422
} else {
2443
2423
// The segment has unbalanced angle brackets, e.g. `Vec<T` or `Vec<T>>`
2444
- Err ( ResolutionFailure :: MalformedGenerics ( MalformedGenerics :: UnbalancedAngleBrackets ) )
2424
+ Err ( MalformedGenerics :: UnbalancedAngleBrackets )
2445
2425
}
2446
2426
}
0 commit comments