@@ -220,80 +220,40 @@ enum MalformedGenerics {
220
220
221
221
#[ derive( Clone , Debug , Hash , PartialEq , Eq ) ]
222
222
pub ( crate ) enum UrlFragment {
223
- Item ( ItemFragment ) ,
223
+ Item ( DefId ) ,
224
224
UserWritten ( String ) ,
225
225
}
226
226
227
227
impl UrlFragment {
228
228
/// Render the fragment, including the leading `#`.
229
229
pub ( crate ) fn render ( & self , s : & mut String , tcx : TyCtxt < ' _ > ) -> std:: fmt:: Result {
230
+ s. push ( '#' ) ;
230
231
match self {
231
- UrlFragment :: Item ( frag) => frag. render ( s, tcx) ,
232
- UrlFragment :: UserWritten ( raw) => write ! ( s, "#{}" , raw) ,
233
- }
234
- }
235
- }
236
-
237
- #[ derive( Copy , Clone , Debug , Hash , PartialEq , Eq ) ]
238
- pub ( crate ) struct ItemFragment ( FragmentKind , DefId ) ;
239
-
240
- #[ derive( Copy , Clone , Debug , Hash , PartialEq , Eq ) ]
241
- pub ( crate ) enum FragmentKind {
242
- Method ,
243
- TyMethod ,
244
- AssociatedConstant ,
245
- AssociatedType ,
246
-
247
- StructField ,
248
- Variant ,
249
- VariantField ,
250
- }
251
-
252
- impl FragmentKind {
253
- fn from_def_id ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> FragmentKind {
254
- match tcx. def_kind ( def_id) {
255
- DefKind :: AssocFn => {
256
- if tcx. associated_item ( def_id) . defaultness . has_value ( ) {
257
- FragmentKind :: Method
258
- } else {
259
- FragmentKind :: TyMethod
260
- }
261
- }
262
- DefKind :: AssocConst => FragmentKind :: AssociatedConstant ,
263
- DefKind :: AssocTy => FragmentKind :: AssociatedType ,
264
- DefKind :: Variant => FragmentKind :: Variant ,
265
- DefKind :: Field => {
266
- if tcx. def_kind ( tcx. parent ( def_id) ) == DefKind :: Variant {
267
- FragmentKind :: VariantField
268
- } else {
269
- FragmentKind :: StructField
270
- }
271
- }
272
- kind => bug ! ( "unexpected associated item kind: {:?}" , kind) ,
273
- }
274
- }
275
- }
276
-
277
- impl ItemFragment {
278
- /// Render the fragment, including the leading `#`.
279
- pub ( crate ) fn render ( & self , s : & mut String , tcx : TyCtxt < ' _ > ) -> std:: fmt:: Result {
280
- write ! ( s, "#" ) ?;
281
- match * self {
282
- ItemFragment ( kind, def_id) => {
232
+ & UrlFragment :: Item ( def_id) => {
283
233
let name = tcx. item_name ( def_id) ;
284
- match kind {
285
- FragmentKind :: Method => write ! ( s, "method.{}" , name) ,
286
- FragmentKind :: TyMethod => write ! ( s, "tymethod.{}" , name) ,
287
- FragmentKind :: AssociatedConstant => write ! ( s, "associatedconstant.{}" , name) ,
288
- FragmentKind :: AssociatedType => write ! ( s, "associatedtype.{}" , name) ,
289
- FragmentKind :: StructField => write ! ( s, "structfield.{}" , name) ,
290
- FragmentKind :: Variant => write ! ( s, "variant.{}" , name) ,
291
- FragmentKind :: VariantField => {
292
- let variant = tcx. item_name ( tcx. parent ( def_id) ) ;
293
- write ! ( s, "variant.{}.field.{}" , variant, name)
234
+ match tcx. def_kind ( def_id) {
235
+ DefKind :: AssocFn => {
236
+ if tcx. associated_item ( def_id) . defaultness . has_value ( ) {
237
+ write ! ( s, "method.{}" , name)
238
+ } else {
239
+ write ! ( s, "tymethod.{}" , name)
240
+ }
294
241
}
242
+ DefKind :: AssocConst => write ! ( s, "associatedconstant.{}" , name) ,
243
+ DefKind :: AssocTy => write ! ( s, "associatedtype.{}" , name) ,
244
+ DefKind :: Variant => write ! ( s, "variant.{}" , name) ,
245
+ DefKind :: Field => {
246
+ let parent_id = tcx. parent ( def_id) ;
247
+ if tcx. def_kind ( parent_id) == DefKind :: Variant {
248
+ write ! ( s, "variant.{}.field.{}" , tcx. item_name( parent_id) , name)
249
+ } else {
250
+ write ! ( s, "structfield.{}" , name)
251
+ }
252
+ }
253
+ kind => bug ! ( "unexpected associated item kind: {:?}" , kind) ,
295
254
}
296
255
}
256
+ UrlFragment :: UserWritten ( raw) => Ok ( s. push_str ( & raw ) ) ,
297
257
}
298
258
}
299
259
}
@@ -1124,7 +1084,7 @@ impl LinkCollector<'_, '_> {
1124
1084
1125
1085
match res {
1126
1086
Res :: Primitive ( prim) => {
1127
- if let Some ( UrlFragment :: Item ( ItemFragment ( _ , id ) ) ) = fragment {
1087
+ if let Some ( UrlFragment :: Item ( id ) ) = fragment {
1128
1088
// We're actually resolving an associated item of a primitive, so we need to
1129
1089
// verify the disambiguator (if any) matches the type of the associated item.
1130
1090
// This case should really follow the same flow as the `Res::Def` branch below,
@@ -1172,12 +1132,11 @@ impl LinkCollector<'_, '_> {
1172
1132
} )
1173
1133
}
1174
1134
Res :: Def ( kind, id) => {
1175
- let ( kind_for_dis, id_for_dis) =
1176
- if let Some ( UrlFragment :: Item ( ItemFragment ( _, id) ) ) = fragment {
1177
- ( self . cx . tcx . def_kind ( id) , id)
1178
- } else {
1179
- ( kind, id)
1180
- } ;
1135
+ let ( kind_for_dis, id_for_dis) = if let Some ( UrlFragment :: Item ( id) ) = fragment {
1136
+ ( self . cx . tcx . def_kind ( id) , id)
1137
+ } else {
1138
+ ( kind, id)
1139
+ } ;
1181
1140
self . verify_disambiguator (
1182
1141
path_str,
1183
1142
& ori_link,
@@ -1318,10 +1277,7 @@ impl LinkCollector<'_, '_> {
1318
1277
return None ;
1319
1278
}
1320
1279
( Some ( u_frag) , None ) => Some ( UrlFragment :: UserWritten ( u_frag. clone ( ) ) ) ,
1321
- ( None , Some ( def_id) ) => Some ( UrlFragment :: Item ( ItemFragment (
1322
- FragmentKind :: from_def_id ( self . cx . tcx , def_id) ,
1323
- def_id,
1324
- ) ) ) ,
1280
+ ( None , Some ( def_id) ) => Some ( UrlFragment :: Item ( def_id) ) ,
1325
1281
( None , None ) => None ,
1326
1282
} ;
1327
1283
Some ( ( res, fragment) )
0 commit comments