Skip to content

Commit e2abf06

Browse files
committed
Fix lifetime generics in <T<..> as Trait>::try_from suggestion.
1 parent a4f6d3e commit e2abf06

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

compiler/rustc_typeck/src/check/method/prelude2021.rs

+21-17
Original file line numberDiff line numberDiff line change
@@ -262,31 +262,35 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
262262
method_name.name
263263
));
264264

265-
let self_ty_name = self
265+
let mut self_ty_name = self
266266
.sess()
267267
.source_map()
268268
.span_to_snippet(self_ty_span)
269269
.unwrap_or_else(|_| self_ty.to_string());
270270

271-
let self_ty_generics_count = match self_ty.kind() {
272-
// Get the number of generics the self type has (if an Adt) unless we can determine that
273-
// the user has written the self type with generics already which we (naively) do by looking
274-
// for a "<" in `self_ty_name`.
275-
Adt(def, _) if !self_ty_name.contains('<') => self.tcx.generics_of(def.did).count(),
276-
_ => 0,
277-
};
278-
let self_ty_generics = if self_ty_generics_count > 0 {
279-
format!("<{}>", vec!["_"; self_ty_generics_count].join(", "))
280-
} else {
281-
String::new()
282-
};
271+
// Get the number of generics the self type has (if an Adt) unless we can determine that
272+
// the user has written the self type with generics already which we (naively) do by looking
273+
// for a "<" in `self_ty_name`.
274+
if !self_ty_name.contains('<') {
275+
if let Adt(def, _) = self_ty.kind() {
276+
let generics = self.tcx.generics_of(def.did);
277+
if !generics.params.is_empty() {
278+
let counts = generics.own_counts();
279+
self_ty_name += &format!(
280+
"<{}>",
281+
std::iter::repeat("'_")
282+
.take(counts.lifetimes)
283+
.chain(std::iter::repeat("_").take(counts.types + counts.consts))
284+
.collect::<Vec<_>>()
285+
.join(", ")
286+
);
287+
}
288+
}
289+
}
283290
lint.span_suggestion(
284291
span,
285292
"disambiguate the associated function",
286-
format!(
287-
"<{}{} as {}>::{}",
288-
self_ty_name, self_ty_generics, trait_name, method_name.name,
289-
),
293+
format!("<{} as {}>::{}", self_ty_name, trait_name, method_name.name,),
290294
Applicability::MachineApplicable,
291295
);
292296

0 commit comments

Comments
 (0)