From a491f51218f93cc6373b28b2f0f753c5c1a5b295 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Sun, 14 Feb 2021 23:59:45 -0500 Subject: [PATCH 1/3] Use delay_span_bug for mismatched subst/hir arg --- .../borrow_check/diagnostics/region_name.rs | 11 ++--- .../issue-82126-mismatched-subst-and-hir.rs | 25 +++++++++++ ...ssue-82126-mismatched-subst-and-hir.stderr | 43 +++++++++++++++++++ 3 files changed, 72 insertions(+), 7 deletions(-) create mode 100644 src/test/ui/borrowck/issue-82126-mismatched-subst-and-hir.rs create mode 100644 src/test/ui/borrowck/issue-82126-mismatched-subst-and-hir.stderr diff --git a/compiler/rustc_mir/src/borrow_check/diagnostics/region_name.rs b/compiler/rustc_mir/src/borrow_check/diagnostics/region_name.rs index cbca012824f82..77297ded40d75 100644 --- a/compiler/rustc_mir/src/borrow_check/diagnostics/region_name.rs +++ b/compiler/rustc_mir/src/borrow_check/diagnostics/region_name.rs @@ -634,14 +634,11 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { | GenericArgKind::Const(_), _, ) => { - // I *think* that HIR lowering should ensure this - // doesn't happen, even in erroneous - // programs. Else we should use delay-span-bug. - span_bug!( + // HIR lowering is insufficient in erroneous programs, so + // we need to use delay_span_bug here. See #82126. + self.infcx.tcx.sess.delay_span_bug( hir_arg.span(), - "unmatched subst and hir arg: found {:?} vs {:?}", - kind, - hir_arg, + &format!("unmatched subst and hir arg: found {:?} vs {:?}", kind, hir_arg), ); } } diff --git a/src/test/ui/borrowck/issue-82126-mismatched-subst-and-hir.rs b/src/test/ui/borrowck/issue-82126-mismatched-subst-and-hir.rs new file mode 100644 index 0000000000000..b6cb617e2f0e7 --- /dev/null +++ b/src/test/ui/borrowck/issue-82126-mismatched-subst-and-hir.rs @@ -0,0 +1,25 @@ +// Regression test for #82087. Checks that mismatched lifetimes and types are +// properly handled. + +// edition:2018 + +use std::sync::Mutex; + +struct MarketMultiplier {} + +impl MarketMultiplier { + fn buy(&mut self) -> &mut usize { + todo!() + } +} + +async fn buy_lock(generator: &Mutex) -> LockedMarket<'_> { + //~^ ERROR this struct takes 0 lifetime arguments but 1 lifetime argument was supplied + //~^^ ERROR this struct takes 1 type argument but 0 type arguments were supplied + LockedMarket(generator.lock().unwrap().buy()) + //~^ ERROR cannot return value referencing temporary value +} + +struct LockedMarket(T); + +fn main() {} diff --git a/src/test/ui/borrowck/issue-82126-mismatched-subst-and-hir.stderr b/src/test/ui/borrowck/issue-82126-mismatched-subst-and-hir.stderr new file mode 100644 index 0000000000000..b6844f50488e1 --- /dev/null +++ b/src/test/ui/borrowck/issue-82126-mismatched-subst-and-hir.stderr @@ -0,0 +1,43 @@ +error[E0107]: this struct takes 0 lifetime arguments but 1 lifetime argument was supplied + --> $DIR/issue-82126-mismatched-subst-and-hir.rs:16:59 + | +LL | async fn buy_lock(generator: &Mutex) -> LockedMarket<'_> { + | ^^^^^^^^^^^^---- help: remove these generics + | | + | expected 0 lifetime arguments + | +note: struct defined here, with 0 lifetime parameters + --> $DIR/issue-82126-mismatched-subst-and-hir.rs:23:8 + | +LL | struct LockedMarket(T); + | ^^^^^^^^^^^^ + +error[E0107]: this struct takes 1 type argument but 0 type arguments were supplied + --> $DIR/issue-82126-mismatched-subst-and-hir.rs:16:59 + | +LL | async fn buy_lock(generator: &Mutex) -> LockedMarket<'_> { + | ^^^^^^^^^^^^ expected 1 type argument + | +note: struct defined here, with 1 type parameter: `T` + --> $DIR/issue-82126-mismatched-subst-and-hir.rs:23:8 + | +LL | struct LockedMarket(T); + | ^^^^^^^^^^^^ - +help: add missing type argument + | +LL | async fn buy_lock(generator: &Mutex) -> LockedMarket<'_, T> { + | ^^^ + +error[E0515]: cannot return value referencing temporary value + --> $DIR/issue-82126-mismatched-subst-and-hir.rs:19:5 + | +LL | LockedMarket(generator.lock().unwrap().buy()) + | ^^^^^^^^^^^^^-------------------------^^^^^^^ + | | | + | | temporary value created here + | returns a value referencing data owned by the current function + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0107, E0515. +For more information about an error, try `rustc --explain E0107`. From f856224e16ef378968114c3811d99bf5cd0e64cb Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Mon, 15 Feb 2021 00:03:57 -0500 Subject: [PATCH 2/3] Fix test issue reference --- src/test/ui/borrowck/issue-82126-mismatched-subst-and-hir.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/borrowck/issue-82126-mismatched-subst-and-hir.rs b/src/test/ui/borrowck/issue-82126-mismatched-subst-and-hir.rs index b6cb617e2f0e7..2e6b88a4beba8 100644 --- a/src/test/ui/borrowck/issue-82126-mismatched-subst-and-hir.rs +++ b/src/test/ui/borrowck/issue-82126-mismatched-subst-and-hir.rs @@ -1,4 +1,4 @@ -// Regression test for #82087. Checks that mismatched lifetimes and types are +// Regression test for #82126. Checks that mismatched lifetimes and types are // properly handled. // edition:2018 From a4b2fafcc19801e858afc59ed3b319c39adefc20 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Mon, 15 Feb 2021 00:28:58 -0500 Subject: [PATCH 3/3] Revise HIR lowering comment --- .../rustc_mir/src/borrow_check/diagnostics/region_name.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_mir/src/borrow_check/diagnostics/region_name.rs b/compiler/rustc_mir/src/borrow_check/diagnostics/region_name.rs index 77297ded40d75..c8b45f87639a2 100644 --- a/compiler/rustc_mir/src/borrow_check/diagnostics/region_name.rs +++ b/compiler/rustc_mir/src/borrow_check/diagnostics/region_name.rs @@ -634,8 +634,8 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { | GenericArgKind::Const(_), _, ) => { - // HIR lowering is insufficient in erroneous programs, so - // we need to use delay_span_bug here. See #82126. + // HIR lowering sometimes doesn't catch this in erroneous + // programs, so we need to use delay_span_bug here. See #82126. self.infcx.tcx.sess.delay_span_bug( hir_arg.span(), &format!("unmatched subst and hir arg: found {:?} vs {:?}", kind, hir_arg),