From 3855e039a2c095f2e54b2f389f29e200293774ac Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Wed, 24 Aug 2022 11:34:14 +0900 Subject: [PATCH 1/2] do not suggest adding a bound to a opaque type --- .../src/diagnostics/explain_borrow.rs | 6 +++- ...not-suggest-adding-bound-to-opaque-type.rs | 29 +++++++++++++++++++ ...suggest-adding-bound-to-opaque-type.stderr | 14 +++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/regions/do-not-suggest-adding-bound-to-opaque-type.rs create mode 100644 src/test/ui/regions/do-not-suggest-adding-bound-to-opaque-type.stderr 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..5340288051ac9 --- /dev/null +++ b/src/test/ui/regions/do-not-suggest-adding-bound-to-opaque-type.rs @@ -0,0 +1,29 @@ +#![feature(allocator_api)] + +use std::{ + alloc::{AllocError, Allocator, Layout}, + ptr::NonNull, +}; + +struct GhostBump; + +unsafe impl Allocator for &GhostBump { + fn allocate(&self, layout: Layout) -> Result, AllocError> { + todo!() + } + + unsafe fn deallocate(&self, ptr: NonNull, layout: Layout) { + todo!() + } +} + +fn foo() -> impl Iterator { + let arena = GhostBump; + let mut vec = Vec::new_in(&arena); //~ ERROR `arena` does not live long enough + vec.push(1); + vec.push(2); + vec.push(3); + vec.into_iter() +} + +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..677b81956f987 --- /dev/null +++ b/src/test/ui/regions/do-not-suggest-adding-bound-to-opaque-type.stderr @@ -0,0 +1,14 @@ +error[E0597]: `arena` does not live long enough + --> $DIR/do-not-suggest-adding-bound-to-opaque-type.rs:22:31 + | +LL | let mut vec = Vec::new_in(&arena); + | ^^^^^^ borrowed value does not live long enough +... +LL | vec.into_iter() + | --------------- opaque type requires that `arena` is borrowed for `'static` +LL | } + | - `arena` dropped here while still borrowed + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0597`. From c57ecfae0e131d14676e6d1aa535cb8f17dba80b Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Thu, 25 Aug 2022 00:42:34 +0900 Subject: [PATCH 2/2] use a minimized example --- ...not-suggest-adding-bound-to-opaque-type.rs | 29 ++++--------------- ...suggest-adding-bound-to-opaque-type.stderr | 16 +++++----- 2 files changed, 14 insertions(+), 31 deletions(-) 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 index 5340288051ac9..a1e801e392305 100644 --- 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 @@ -1,29 +1,12 @@ -#![feature(allocator_api)] +pub trait T {} -use std::{ - alloc::{AllocError, Allocator, Layout}, - ptr::NonNull, -}; +struct S<'a>(&'a ()); -struct GhostBump; +impl<'a> T for S<'a> {} -unsafe impl Allocator for &GhostBump { - fn allocate(&self, layout: Layout) -> Result, AllocError> { - todo!() - } - - unsafe fn deallocate(&self, ptr: NonNull, layout: Layout) { - todo!() - } -} - -fn foo() -> impl Iterator { - let arena = GhostBump; - let mut vec = Vec::new_in(&arena); //~ ERROR `arena` does not live long enough - vec.push(1); - vec.push(2); - vec.push(3); - vec.into_iter() +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 index 677b81956f987..6ea238f302f3f 100644 --- 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 @@ -1,13 +1,13 @@ -error[E0597]: `arena` does not live long enough - --> $DIR/do-not-suggest-adding-bound-to-opaque-type.rs:22:31 +error[E0597]: `x` does not live long enough + --> $DIR/do-not-suggest-adding-bound-to-opaque-type.rs:9:7 | -LL | let mut vec = Vec::new_in(&arena); - | ^^^^^^ borrowed value does not live long enough -... -LL | vec.into_iter() - | --------------- opaque type requires that `arena` is borrowed for `'static` +LL | S(&x) + | --^^- + | | | + | | borrowed value does not live long enough + | opaque type requires that `x` is borrowed for `'static` LL | } - | - `arena` dropped here while still borrowed + | - `x` dropped here while still borrowed error: aborting due to previous error