Skip to content

Commit 3faec69

Browse files
authored
Rollup merge of #70827 - estebank:shorten-sugg-span-add-lt, r=ecstatic-morse
Use smaller span for suggestion restricting lifetime
2 parents 17fa344 + 0664b81 commit 3faec69

File tree

2 files changed

+17
-28
lines changed

2 files changed

+17
-28
lines changed

src/librustc_mir/borrow_check/diagnostics/explain_borrow.rs

+16-27
Original file line numberDiff line numberDiff line change
@@ -208,45 +208,34 @@ impl BorrowExplanation {
208208
);
209209
};
210210

211-
self.add_lifetime_bound_suggestion_to_diagnostic(
212-
tcx,
213-
err,
214-
&category,
215-
span,
216-
region_name,
217-
);
211+
self.add_lifetime_bound_suggestion_to_diagnostic(err, &category, span, region_name);
218212
}
219213
_ => {}
220214
}
221215
}
222-
pub(in crate::borrow_check) fn add_lifetime_bound_suggestion_to_diagnostic<'tcx>(
216+
pub(in crate::borrow_check) fn add_lifetime_bound_suggestion_to_diagnostic(
223217
&self,
224-
tcx: TyCtxt<'tcx>,
225218
err: &mut DiagnosticBuilder<'_>,
226219
category: &ConstraintCategory,
227220
span: Span,
228221
region_name: &RegionName,
229222
) {
230223
if let ConstraintCategory::OpaqueType = category {
231-
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(span) {
232-
let suggestable_name = if region_name.was_named() {
233-
region_name.to_string()
234-
} else {
235-
"'_".to_string()
236-
};
224+
let suggestable_name =
225+
if region_name.was_named() { region_name.to_string() } else { "'_".to_string() };
237226

238-
err.span_suggestion(
239-
span,
240-
&format!(
241-
"you can add a bound to the {}to make it last less than \
242-
`'static` and match `{}`",
243-
category.description(),
244-
region_name,
245-
),
246-
format!("{} + {}", snippet, suggestable_name),
247-
Applicability::Unspecified,
248-
);
249-
}
227+
let msg = format!(
228+
"you can add a bound to the {}to make it last less than `'static` and match `{}`",
229+
category.description(),
230+
region_name,
231+
);
232+
233+
err.span_suggestion_verbose(
234+
span.shrink_to_hi(),
235+
&msg,
236+
format!(" + {}", suggestable_name),
237+
Applicability::Unspecified,
238+
);
250239
}
251240
}
252241
}

src/test/ui/impl-trait/does-not-live-long-enough.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ LL | }
1414
help: you can add a bound to the opaque type to make it last less than `'static` and match `'a`
1515
|
1616
LL | fn started_with<'a>(&'a self, prefix: &'a str) -> impl Iterator<Item=&'a str> + 'a {
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
| ^^^^
1818

1919
error: aborting due to previous error
2020

0 commit comments

Comments
 (0)