Skip to content

Commit db63611

Browse files
committed
Helper function for formatting with LifetimeSuggestionPosition
1 parent 263a3ae commit db63611

File tree

3 files changed

+15
-29
lines changed

3 files changed

+15
-29
lines changed

Diff for: 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,

Diff for: 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(

Diff for: compiler/rustc_trait_selection/src/error_reporting/infer/region.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -844,18 +844,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
844844
impl<'hir, 'tcx> hir::intravisit::Visitor<'hir> for LifetimeReplaceVisitor<'tcx, '_> {
845845
fn visit_lifetime(&mut self, lt: &'hir hir::Lifetime) {
846846
if lt.res == self.needle {
847-
let (pos, span) = lt.suggestion_position();
848-
let new_lt = &self.new_lt;
849-
let sugg = match pos {
850-
hir::LifetimeSuggestionPosition::Normal => format!("{new_lt}"),
851-
hir::LifetimeSuggestionPosition::Ampersand => format!("{new_lt} "),
852-
hir::LifetimeSuggestionPosition::ElidedPath => format!("<{new_lt}>"),
853-
hir::LifetimeSuggestionPosition::ElidedPathArgument => {
854-
format!("{new_lt}, ")
855-
}
856-
hir::LifetimeSuggestionPosition::ObjectDefault => format!("+ {new_lt}"),
857-
};
858-
self.add_lt_suggs.push((span, sugg));
847+
self.add_lt_suggs.push(lt.suggestion(self.new_lt));
859848
}
860849
}
861850

0 commit comments

Comments
 (0)