diff --git a/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs b/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs index 72aee0267ac1e..68f9a7c5007c8 100644 --- a/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs +++ b/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs @@ -273,13 +273,17 @@ impl<'tcx> BorrowExplanation<'tcx> { _ => {} } } - pub(crate) fn add_lifetime_bound_suggestion_to_diagnostic( + + fn add_lifetime_bound_suggestion_to_diagnostic( &self, err: &mut Diagnostic, category: &ConstraintCategory<'tcx>, span: Span, region_name: &RegionName, ) { + if !span.is_desugaring(DesugaringKind::OpaqueTy) { + return; + } if let ConstraintCategory::OpaqueType = category { let suggestable_name = if region_name.was_named() { region_name.name } else { kw::UnderscoreLifetime }; diff --git a/src/test/ui/regions/do-not-suggest-adding-bound-to-opaque-type.rs b/src/test/ui/regions/do-not-suggest-adding-bound-to-opaque-type.rs new file mode 100644 index 0000000000000..a1e801e392305 --- /dev/null +++ b/src/test/ui/regions/do-not-suggest-adding-bound-to-opaque-type.rs @@ -0,0 +1,12 @@ +pub trait T {} + +struct S<'a>(&'a ()); + +impl<'a> T for S<'a> {} + +fn foo() -> impl T { + let x = (); + S(&x) //~ ERROR `x` does not live long enough +} + +fn main() {} diff --git a/src/test/ui/regions/do-not-suggest-adding-bound-to-opaque-type.stderr b/src/test/ui/regions/do-not-suggest-adding-bound-to-opaque-type.stderr new file mode 100644 index 0000000000000..6ea238f302f3f --- /dev/null +++ b/src/test/ui/regions/do-not-suggest-adding-bound-to-opaque-type.stderr @@ -0,0 +1,14 @@ +error[E0597]: `x` does not live long enough + --> $DIR/do-not-suggest-adding-bound-to-opaque-type.rs:9:7 + | +LL | S(&x) + | --^^- + | | | + | | borrowed value does not live long enough + | opaque type requires that `x` is borrowed for `'static` +LL | } + | - `x` dropped here while still borrowed + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0597`.