@@ -236,10 +236,23 @@ enum AnchorFailure {
236
236
237
237
#[ derive( Clone , Debug , Hash , PartialEq , Eq ) ]
238
238
crate enum UrlFragment {
239
- Def ( FragmentKind , DefId ) ,
239
+ Item ( ItemFragment ) ,
240
240
UserWritten ( String ) ,
241
241
}
242
242
243
+ impl UrlFragment {
244
+ /// Render the fragment, including the leading `#`.
245
+ crate fn render ( & self , s : & mut String , tcx : TyCtxt < ' _ > ) -> std:: fmt:: Result {
246
+ match self {
247
+ UrlFragment :: Item ( frag) => frag. render ( s, tcx) ,
248
+ UrlFragment :: UserWritten ( raw) => write ! ( s, "#{}" , raw) ,
249
+ }
250
+ }
251
+ }
252
+
253
+ #[ derive( Copy , Clone , Debug , Hash , PartialEq , Eq ) ]
254
+ crate struct ItemFragment ( FragmentKind , DefId ) ;
255
+
243
256
#[ derive( Copy , Clone , Debug , Hash , PartialEq , Eq ) ]
244
257
crate enum FragmentKind {
245
258
Method ,
@@ -252,7 +265,7 @@ crate enum FragmentKind {
252
265
VariantField ,
253
266
}
254
267
255
- impl UrlFragment {
268
+ impl ItemFragment {
256
269
/// Create a fragment for an associated item.
257
270
///
258
271
/// `is_prototype` is whether this associated item is a trait method
@@ -261,21 +274,21 @@ impl UrlFragment {
261
274
match kind {
262
275
ty:: AssocKind :: Fn => {
263
276
if is_prototype {
264
- UrlFragment :: Def ( FragmentKind :: TyMethod , def_id)
277
+ ItemFragment ( FragmentKind :: TyMethod , def_id)
265
278
} else {
266
- UrlFragment :: Def ( FragmentKind :: Method , def_id)
279
+ ItemFragment ( FragmentKind :: Method , def_id)
267
280
}
268
281
}
269
- ty:: AssocKind :: Const => UrlFragment :: Def ( FragmentKind :: AssociatedConstant , def_id) ,
270
- ty:: AssocKind :: Type => UrlFragment :: Def ( FragmentKind :: AssociatedType , def_id) ,
282
+ ty:: AssocKind :: Const => ItemFragment ( FragmentKind :: AssociatedConstant , def_id) ,
283
+ ty:: AssocKind :: Type => ItemFragment ( FragmentKind :: AssociatedType , def_id) ,
271
284
}
272
285
}
273
286
274
287
/// Render the fragment, including the leading `#`.
275
288
crate fn render ( & self , s : & mut String , tcx : TyCtxt < ' _ > ) -> std:: fmt:: Result {
276
289
write ! ( s, "#" ) ?;
277
290
match * self {
278
- UrlFragment :: Def ( kind, def_id) => {
291
+ ItemFragment ( kind, def_id) => {
279
292
let name = tcx. item_name ( def_id) ;
280
293
match kind {
281
294
FragmentKind :: Method => write ! ( s, "method.{}" , name) ,
@@ -290,7 +303,6 @@ impl UrlFragment {
290
303
}
291
304
}
292
305
}
293
- UrlFragment :: UserWritten ( ref raw) => write ! ( s, "{}" , raw) ,
294
306
}
295
307
}
296
308
}
@@ -300,7 +312,7 @@ struct ResolutionInfo {
300
312
module_id : DefId ,
301
313
dis : Option < Disambiguator > ,
302
314
path_str : String ,
303
- extra_fragment : Option < UrlFragment > ,
315
+ extra_fragment : Option < String > ,
304
316
}
305
317
306
318
#[ derive( Clone ) ]
@@ -339,7 +351,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
339
351
& self ,
340
352
path_str : & ' path str ,
341
353
module_id : DefId ,
342
- ) -> Result < ( Res , Option < UrlFragment > ) , ErrorKind < ' path > > {
354
+ ) -> Result < ( Res , Option < ItemFragment > ) , ErrorKind < ' path > > {
343
355
let tcx = self . cx . tcx ;
344
356
let no_res = || ResolutionFailure :: NotResolved {
345
357
module_id,
@@ -389,10 +401,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
389
401
if let Some ( field) =
390
402
def. all_fields ( ) . find ( |f| f. ident . name == variant_field_name)
391
403
{
392
- Ok ( (
393
- ty_res,
394
- Some ( UrlFragment :: Def ( FragmentKind :: VariantField , field. did ) ) ,
395
- ) )
404
+ Ok ( ( ty_res, Some ( ItemFragment ( FragmentKind :: VariantField , field. did ) ) ) )
396
405
} else {
397
406
Err ( ResolutionFailure :: NotResolved {
398
407
module_id,
@@ -420,15 +429,15 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
420
429
prim_ty : PrimitiveType ,
421
430
ns : Namespace ,
422
431
item_name : Symbol ,
423
- ) -> Option < ( Res , UrlFragment ) > {
432
+ ) -> Option < ( Res , ItemFragment ) > {
424
433
let tcx = self . cx . tcx ;
425
434
426
435
prim_ty. impls ( tcx) . into_iter ( ) . find_map ( |& impl_| {
427
436
tcx. associated_items ( impl_)
428
437
. find_by_name_and_namespace ( tcx, Ident :: with_dummy_span ( item_name) , ns, impl_)
429
438
. map ( |item| {
430
439
let kind = item. kind ;
431
- let fragment = UrlFragment :: from_assoc_item ( item. def_id , kind, false ) ;
440
+ let fragment = ItemFragment :: from_assoc_item ( item. def_id , kind, false ) ;
432
441
( Res :: Primitive ( prim_ty) , fragment)
433
442
} )
434
443
} )
@@ -503,21 +512,19 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
503
512
path_str : & ' path str ,
504
513
ns : Namespace ,
505
514
module_id : DefId ,
506
- user_fragment : & Option < UrlFragment > ,
515
+ user_fragment : & Option < String > ,
507
516
) -> Result < ( Res , Option < UrlFragment > ) , ErrorKind < ' path > > {
508
517
let ( res, rustdoc_fragment) = self . resolve_inner ( path_str, ns, module_id) ?;
509
518
let chosen_fragment = match ( user_fragment, rustdoc_fragment) {
510
519
( Some ( _) , Some ( r_frag) ) => {
511
520
let diag_res = match r_frag {
512
- UrlFragment :: Def ( _, did) => Res :: Def ( self . cx . tcx . def_kind ( did) , did) ,
513
- // FIXME: eliminate this branch somehow
514
- UrlFragment :: UserWritten ( _) => unreachable ! ( ) ,
521
+ ItemFragment ( _, did) => Res :: Def ( self . cx . tcx . def_kind ( did) , did) ,
515
522
} ;
516
523
let failure = AnchorFailure :: RustdocAnchorConflict ( diag_res) ;
517
524
return Err ( ErrorKind :: AnchorFailure ( failure) ) ;
518
525
}
519
- ( Some ( u_frag) , None ) => Some ( u_frag. clone ( ) ) ,
520
- ( None , Some ( r_frag) ) => Some ( r_frag) ,
526
+ ( Some ( u_frag) , None ) => Some ( UrlFragment :: UserWritten ( u_frag. clone ( ) ) ) ,
527
+ ( None , Some ( r_frag) ) => Some ( UrlFragment :: Item ( r_frag) ) ,
521
528
( None , None ) => None ,
522
529
} ;
523
530
Ok ( ( res, chosen_fragment) )
@@ -528,7 +535,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
528
535
path_str : & ' path str ,
529
536
ns : Namespace ,
530
537
module_id : DefId ,
531
- ) -> Result < ( Res , Option < UrlFragment > ) , ErrorKind < ' path > > {
538
+ ) -> Result < ( Res , Option < ItemFragment > ) , ErrorKind < ' path > > {
532
539
if let Some ( res) = self . resolve_path ( path_str, ns, module_id) {
533
540
match res {
534
541
// FIXME(#76467): make this fallthrough to lookup the associated
@@ -670,7 +677,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
670
677
item_name : Symbol ,
671
678
ns : Namespace ,
672
679
module_id : DefId ,
673
- ) -> Option < ( Res , UrlFragment ) > {
680
+ ) -> Option < ( Res , ItemFragment ) > {
674
681
let tcx = self . cx . tcx ;
675
682
676
683
match root_res {
@@ -685,7 +692,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
685
692
686
693
assoc_item. map ( |item| {
687
694
let kind = item. kind ;
688
- let fragment = UrlFragment :: from_assoc_item ( item. def_id , kind, false ) ;
695
+ let fragment = ItemFragment :: from_assoc_item ( item. def_id , kind, false ) ;
689
696
( root_res, fragment)
690
697
} )
691
698
} )
@@ -736,7 +743,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
736
743
737
744
if let Some ( item) = assoc_item {
738
745
let kind = item. kind ;
739
- let fragment = UrlFragment :: from_assoc_item ( item. def_id , kind, false ) ;
746
+ let fragment = ItemFragment :: from_assoc_item ( item. def_id , kind, false ) ;
740
747
return Some ( ( root_res, fragment) ) ;
741
748
}
742
749
@@ -768,13 +775,13 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
768
775
. fields
769
776
. iter ( )
770
777
. find ( |item| item. ident . name == item_name) ?;
771
- Some ( ( root_res, UrlFragment :: Def ( FragmentKind :: StructField , field. did ) ) )
778
+ Some ( ( root_res, ItemFragment ( FragmentKind :: StructField , field. did ) ) )
772
779
}
773
780
Res :: Def ( DefKind :: Trait , did) => tcx
774
781
. associated_items ( did)
775
782
. find_by_name_and_namespace ( tcx, Ident :: with_dummy_span ( item_name) , ns, did)
776
783
. map ( |item| {
777
- let fragment = UrlFragment :: from_assoc_item (
784
+ let fragment = ItemFragment :: from_assoc_item (
778
785
item. def_id ,
779
786
item. kind ,
780
787
!item. defaultness . has_value ( ) ,
@@ -797,7 +804,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
797
804
ns : Namespace ,
798
805
path_str : & str ,
799
806
module_id : DefId ,
800
- extra_fragment : & Option < UrlFragment > ,
807
+ extra_fragment : & Option < String > ,
801
808
) -> Option < Res > {
802
809
// resolve() can't be used for macro namespace
803
810
let result = match ns {
@@ -812,7 +819,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
812
819
813
820
let res = match result {
814
821
Ok ( ( res, frag) ) => {
815
- if let Some ( UrlFragment :: Def ( _, id) ) = frag {
822
+ if let Some ( UrlFragment :: Item ( ItemFragment ( _, id) ) ) = frag {
816
823
Some ( Res :: Def ( self . cx . tcx . def_kind ( id) , id) )
817
824
} else {
818
825
Some ( res)
@@ -1039,7 +1046,7 @@ impl From<AnchorFailure> for PreprocessingError<'_> {
1039
1046
struct PreprocessingInfo {
1040
1047
path_str : String ,
1041
1048
disambiguator : Option < Disambiguator > ,
1042
- extra_fragment : Option < UrlFragment > ,
1049
+ extra_fragment : Option < String > ,
1043
1050
link_text : String ,
1044
1051
}
1045
1052
@@ -1125,7 +1132,7 @@ fn preprocess_link<'a>(
1125
1132
Some ( Ok ( PreprocessingInfo {
1126
1133
path_str,
1127
1134
disambiguator,
1128
- extra_fragment : extra_fragment. map ( |frag| UrlFragment :: UserWritten ( frag. to_owned ( ) ) ) ,
1135
+ extra_fragment : extra_fragment. map ( |frag| frag. to_owned ( ) ) ,
1129
1136
link_text : link_text. to_owned ( ) ,
1130
1137
} ) )
1131
1138
}
@@ -1292,7 +1299,7 @@ impl LinkCollector<'_, '_> {
1292
1299
} ;
1293
1300
1294
1301
let verify = |kind : DefKind , id : DefId | {
1295
- let ( kind, id) = if let Some ( UrlFragment :: Def ( _, id) ) = fragment {
1302
+ let ( kind, id) = if let Some ( UrlFragment :: Item ( ItemFragment ( _, id) ) ) = fragment {
1296
1303
( self . cx . tcx . def_kind ( id) , id)
1297
1304
} else {
1298
1305
( kind, id)
@@ -1340,7 +1347,7 @@ impl LinkCollector<'_, '_> {
1340
1347
1341
1348
match res {
1342
1349
Res :: Primitive ( prim) => {
1343
- if let Some ( UrlFragment :: Def ( _, id) ) = fragment {
1350
+ if let Some ( UrlFragment :: Item ( ItemFragment ( _, id) ) ) = fragment {
1344
1351
let kind = self . cx . tcx . def_kind ( id) ;
1345
1352
1346
1353
// We're actually resolving an associated item of a primitive, so we need to
@@ -1488,7 +1495,7 @@ impl LinkCollector<'_, '_> {
1488
1495
let mut candidates = PerNS {
1489
1496
macro_ns : self
1490
1497
. resolve_macro ( path_str, base_node)
1491
- . map ( |res| ( res, extra_fragment. clone ( ) ) ) ,
1498
+ . map ( |res| ( res, extra_fragment. clone ( ) . map ( UrlFragment :: UserWritten ) ) ) ,
1492
1499
type_ns : match self . resolve ( path_str, TypeNS , base_node, extra_fragment) {
1493
1500
Ok ( res) => {
1494
1501
debug ! ( "got res in TypeNS: {:?}" , res) ;
@@ -1520,7 +1527,10 @@ impl LinkCollector<'_, '_> {
1520
1527
// Shouldn't happen but who knows?
1521
1528
Ok ( ( res, Some ( fragment) ) )
1522
1529
}
1523
- ( fragment, None ) | ( None , fragment) => Ok ( ( res, fragment) ) ,
1530
+ ( fragment, None ) => Ok ( ( res, fragment) ) ,
1531
+ ( None , fragment) => {
1532
+ Ok ( ( res, fragment. map ( UrlFragment :: UserWritten ) ) )
1533
+ }
1524
1534
}
1525
1535
}
1526
1536
}
@@ -1557,7 +1567,7 @@ impl LinkCollector<'_, '_> {
1557
1567
}
1558
1568
Some ( MacroNS ) => {
1559
1569
match self . resolve_macro ( path_str, base_node) {
1560
- Ok ( res) => Some ( ( res, extra_fragment. clone ( ) ) ) ,
1570
+ Ok ( res) => Some ( ( res, extra_fragment. clone ( ) . map ( UrlFragment :: UserWritten ) ) ) ,
1561
1571
Err ( mut kind) => {
1562
1572
// `resolve_macro` only looks in the macro namespace. Try to give a better error if possible.
1563
1573
for ns in [ TypeNS , ValueNS ] {
@@ -2276,13 +2286,13 @@ fn privacy_error(cx: &DocContext<'_>, diag_info: &DiagnosticInfo<'_>, path_str:
2276
2286
fn handle_variant (
2277
2287
cx : & DocContext < ' _ > ,
2278
2288
res : Res ,
2279
- ) -> Result < ( Res , Option < UrlFragment > ) , ErrorKind < ' static > > {
2289
+ ) -> Result < ( Res , Option < ItemFragment > ) , ErrorKind < ' static > > {
2280
2290
cx. tcx
2281
2291
. parent ( res. def_id ( cx. tcx ) )
2282
2292
. map ( |parent| {
2283
2293
let parent_def = Res :: Def ( DefKind :: Enum , parent) ;
2284
2294
let variant = cx. tcx . expect_variant_res ( res. as_hir_res ( ) . unwrap ( ) ) ;
2285
- ( parent_def, Some ( UrlFragment :: Def ( FragmentKind :: Variant , variant. def_id ) ) )
2295
+ ( parent_def, Some ( ItemFragment ( FragmentKind :: Variant , variant. def_id ) ) )
2286
2296
} )
2287
2297
. ok_or_else ( || ResolutionFailure :: NoParentItem . into ( ) )
2288
2298
}
0 commit comments