@@ -220,80 +220,40 @@ enum MalformedGenerics {
220220
221221#[ derive( Clone , Debug , Hash , PartialEq , Eq ) ]
222222pub ( crate ) enum UrlFragment {
223- Item ( ItemFragment ) ,
223+ Item ( DefId ) ,
224224 UserWritten ( String ) ,
225225}
226226
227227impl UrlFragment {
228228 /// Render the fragment, including the leading `#`.
229229 pub ( crate ) fn render ( & self , s : & mut String , tcx : TyCtxt < ' _ > ) -> std:: fmt:: Result {
230+ s. push ( '#' ) ;
230231 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) => {
283233 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+ }
294241 }
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) ,
295254 }
296255 }
256+ UrlFragment :: UserWritten ( raw) => Ok ( s. push_str ( & raw ) ) ,
297257 }
298258 }
299259}
@@ -1124,7 +1084,7 @@ impl LinkCollector<'_, '_> {
11241084
11251085 match res {
11261086 Res :: Primitive ( prim) => {
1127- if let Some ( UrlFragment :: Item ( ItemFragment ( _ , id ) ) ) = fragment {
1087+ if let Some ( UrlFragment :: Item ( id ) ) = fragment {
11281088 // We're actually resolving an associated item of a primitive, so we need to
11291089 // verify the disambiguator (if any) matches the type of the associated item.
11301090 // This case should really follow the same flow as the `Res::Def` branch below,
@@ -1172,12 +1132,11 @@ impl LinkCollector<'_, '_> {
11721132 } )
11731133 }
11741134 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+ } ;
11811140 self . verify_disambiguator (
11821141 path_str,
11831142 & ori_link,
@@ -1318,10 +1277,7 @@ impl LinkCollector<'_, '_> {
13181277 return None ;
13191278 }
13201279 ( 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) ) ,
13251281 ( None , None ) => None ,
13261282 } ;
13271283 Some ( ( res, fragment) )
0 commit comments