Skip to content

Commit 68ae3b2

Browse files
Rollup merge of #130149 - GrigorenkoPV:lifetime-suggestion, r=cjgillot
Helper function for formatting with `LifetimeSuggestionPosition`
2 parents 57273d8 + db63611 commit 68ae3b2

File tree

3 files changed

+15
-29
lines changed

3 files changed

+15
-29
lines changed

compiler/rustc_hir/src/hir.rs

+13
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,19 @@ impl Lifetime {
168168
(LifetimeSuggestionPosition::Normal, self.ident.span)
169169
}
170170
}
171+
172+
pub fn suggestion(&self, new_lifetime: &str) -> (Span, String) {
173+
debug_assert!(new_lifetime.starts_with('\''));
174+
let (pos, span) = self.suggestion_position();
175+
let code = match pos {
176+
LifetimeSuggestionPosition::Normal => format!("{new_lifetime}"),
177+
LifetimeSuggestionPosition::Ampersand => format!("{new_lifetime} "),
178+
LifetimeSuggestionPosition::ElidedPath => format!("<{new_lifetime}>"),
179+
LifetimeSuggestionPosition::ElidedPathArgument => format!("{new_lifetime}, "),
180+
LifetimeSuggestionPosition::ObjectDefault => format!("+ {new_lifetime}"),
181+
};
182+
(span, code)
183+
}
171184
}
172185

173186
/// A `Path` is essentially Rust's notion of a name; for instance,

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -1191,23 +1191,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
11911191
(generics.span, "<'a>".to_owned())
11921192
};
11931193

1194-
let lifetime_sugg = match lifetime_ref.suggestion_position() {
1195-
(hir::LifetimeSuggestionPosition::Normal, span) => {
1196-
(span, "'a".to_owned())
1197-
}
1198-
(hir::LifetimeSuggestionPosition::Ampersand, span) => {
1199-
(span, "'a ".to_owned())
1200-
}
1201-
(hir::LifetimeSuggestionPosition::ElidedPath, span) => {
1202-
(span, "<'a>".to_owned())
1203-
}
1204-
(hir::LifetimeSuggestionPosition::ElidedPathArgument, span) => {
1205-
(span, "'a, ".to_owned())
1206-
}
1207-
(hir::LifetimeSuggestionPosition::ObjectDefault, span) => {
1208-
(span, "+ 'a".to_owned())
1209-
}
1210-
};
1194+
let lifetime_sugg = lifetime_ref.suggestion("'a");
12111195
let suggestions = vec![lifetime_sugg, new_param_sugg];
12121196

12131197
diag.span_label(

compiler/rustc_trait_selection/src/error_reporting/infer/region.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -852,18 +852,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
852852
impl<'hir, 'tcx> hir::intravisit::Visitor<'hir> for LifetimeReplaceVisitor<'tcx, '_> {
853853
fn visit_lifetime(&mut self, lt: &'hir hir::Lifetime) {
854854
if lt.res == self.needle {
855-
let (pos, span) = lt.suggestion_position();
856-
let new_lt = &self.new_lt;
857-
let sugg = match pos {
858-
hir::LifetimeSuggestionPosition::Normal => format!("{new_lt}"),
859-
hir::LifetimeSuggestionPosition::Ampersand => format!("{new_lt} "),
860-
hir::LifetimeSuggestionPosition::ElidedPath => format!("<{new_lt}>"),
861-
hir::LifetimeSuggestionPosition::ElidedPathArgument => {
862-
format!("{new_lt}, ")
863-
}
864-
hir::LifetimeSuggestionPosition::ObjectDefault => format!("+ {new_lt}"),
865-
};
866-
self.add_lt_suggs.push((span, sugg));
855+
self.add_lt_suggs.push(lt.suggestion(self.new_lt));
867856
}
868857
}
869858

0 commit comments

Comments
 (0)