From 57740d6e928517431b8bf46a93a66d7a130a3d83 Mon Sep 17 00:00:00 2001 From: Ellen Date: Tue, 15 Jun 2021 11:56:36 +0100 Subject: [PATCH 1/5] don't evaluate constants if borrowck fails --- compiler/rustc_middle/src/mir/query.rs | 1 + compiler/rustc_mir/src/borrow_check/mod.rs | 3 +++ compiler/rustc_mir/src/const_eval/eval_queries.rs | 9 +++++++++ .../ui/consts/const-eval/borrowck_error_no_ice.rs | 9 +++++++++ .../ui/consts/const-eval/borrowck_error_no_ice.stderr | 9 +++++++++ src/test/ui/consts/issue-78655.rs | 2 +- src/test/ui/consts/issue-78655.stderr | 11 +---------- 7 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 src/test/ui/consts/const-eval/borrowck_error_no_ice.rs create mode 100644 src/test/ui/consts/const-eval/borrowck_error_no_ice.stderr diff --git a/compiler/rustc_middle/src/mir/query.rs b/compiler/rustc_middle/src/mir/query.rs index 4fb737f463a86..2819418bffc82 100644 --- a/compiler/rustc_middle/src/mir/query.rs +++ b/compiler/rustc_middle/src/mir/query.rs @@ -214,6 +214,7 @@ pub struct BorrowCheckResult<'tcx> { pub concrete_opaque_types: VecMap, Ty<'tcx>>, pub closure_requirements: Option>, pub used_mut_upvars: SmallVec<[Field; 8]>, + pub errored: bool, } /// The result of the `mir_const_qualif` query. diff --git a/compiler/rustc_mir/src/borrow_check/mod.rs b/compiler/rustc_mir/src/borrow_check/mod.rs index 36eb8a4baa830..bc4644fae2d39 100644 --- a/compiler/rustc_mir/src/borrow_check/mod.rs +++ b/compiler/rustc_mir/src/borrow_check/mod.rs @@ -432,10 +432,12 @@ fn do_mir_borrowck<'a, 'tcx>( diag.buffer(&mut mbcx.errors_buffer); } + let mut errored = false; if !mbcx.errors_buffer.is_empty() { mbcx.errors_buffer.sort_by_key(|diag| diag.sort_span); for diag in mbcx.errors_buffer.drain(..) { + errored |= diag.is_error(); mbcx.infcx.tcx.sess.diagnostic().emit_diagnostic(&diag); } } @@ -444,6 +446,7 @@ fn do_mir_borrowck<'a, 'tcx>( concrete_opaque_types: opaque_type_values, closure_requirements: opt_closure_req, used_mut_upvars: mbcx.used_mut_upvars, + errored, }; debug!("do_mir_borrowck: result = {:#?}", result); diff --git a/compiler/rustc_mir/src/const_eval/eval_queries.rs b/compiler/rustc_mir/src/const_eval/eval_queries.rs index 460fea37461e8..aeeb9e2de7adc 100644 --- a/compiler/rustc_mir/src/const_eval/eval_queries.rs +++ b/compiler/rustc_mir/src/const_eval/eval_queries.rs @@ -292,6 +292,15 @@ pub fn eval_to_allocation_raw_provider<'tcx>( if let Some(error_reported) = tcx.mir_const_qualif_opt_const_arg(def).error_occured { return Err(ErrorHandled::Reported(error_reported)); } + + let borrowck_results = if let Some(param_did) = def.const_param_did { + tcx.mir_borrowck_const_arg((def.did, param_did)) + } else { + tcx.mir_borrowck(def.did) + }; + if borrowck_results.errored { + return Err(ErrorHandled::Reported(ErrorReported {})); + } } let is_static = tcx.is_static(def.did); diff --git a/src/test/ui/consts/const-eval/borrowck_error_no_ice.rs b/src/test/ui/consts/const-eval/borrowck_error_no_ice.rs new file mode 100644 index 0000000000000..5478ba727bbc6 --- /dev/null +++ b/src/test/ui/consts/const-eval/borrowck_error_no_ice.rs @@ -0,0 +1,9 @@ +struct Bug { + A: [(); { + let x; + x + //~^ error: use of possibly-uninitialized variable + }], +} + +fn main() {} \ No newline at end of file diff --git a/src/test/ui/consts/const-eval/borrowck_error_no_ice.stderr b/src/test/ui/consts/const-eval/borrowck_error_no_ice.stderr new file mode 100644 index 0000000000000..72d08bcb17ecd --- /dev/null +++ b/src/test/ui/consts/const-eval/borrowck_error_no_ice.stderr @@ -0,0 +1,9 @@ +error[E0381]: use of possibly-uninitialized variable: `x` + --> $DIR/borrowck_error_no_ice.rs:4:9 + | +LL | x + | ^ use of possibly-uninitialized `x` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0381`. diff --git a/src/test/ui/consts/issue-78655.rs b/src/test/ui/consts/issue-78655.rs index 066764bc46fc4..b85e612992549 100644 --- a/src/test/ui/consts/issue-78655.rs +++ b/src/test/ui/consts/issue-78655.rs @@ -1,4 +1,4 @@ -const FOO: *const u32 = { //~ ERROR encountered dangling pointer in final constant +const FOO: *const u32 = { let x; &x //~ ERROR borrow of possibly-uninitialized variable: `x` }; diff --git a/src/test/ui/consts/issue-78655.stderr b/src/test/ui/consts/issue-78655.stderr index cf3fe18f802fb..734266a3453b5 100644 --- a/src/test/ui/consts/issue-78655.stderr +++ b/src/test/ui/consts/issue-78655.stderr @@ -4,15 +4,6 @@ error[E0381]: borrow of possibly-uninitialized variable: `x` LL | &x | ^^ use of possibly-uninitialized `x` -error: encountered dangling pointer in final constant - --> $DIR/issue-78655.rs:1:1 - | -LL | / const FOO: *const u32 = { -LL | | let x; -LL | | &x -LL | | }; - | |__^ - error: could not evaluate constant pattern --> $DIR/issue-78655.rs:7:9 | @@ -25,6 +16,6 @@ error: could not evaluate constant pattern LL | let FOO = FOO; | ^^^ -error: aborting due to 4 previous errors +error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0381`. From 150cf3e5e83fa1293346067defba5386556ef5ed Mon Sep 17 00:00:00 2001 From: Ellen Date: Tue, 15 Jun 2021 12:09:14 +0100 Subject: [PATCH 2/5] fmt --- compiler/rustc_mir/src/borrow_check/mod.rs | 2 +- compiler/rustc_mir/src/const_eval/eval_queries.rs | 2 +- src/test/ui/consts/const-eval/borrowck_error_no_ice.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_mir/src/borrow_check/mod.rs b/compiler/rustc_mir/src/borrow_check/mod.rs index bc4644fae2d39..27fbab9fbbfd2 100644 --- a/compiler/rustc_mir/src/borrow_check/mod.rs +++ b/compiler/rustc_mir/src/borrow_check/mod.rs @@ -437,7 +437,7 @@ fn do_mir_borrowck<'a, 'tcx>( mbcx.errors_buffer.sort_by_key(|diag| diag.sort_span); for diag in mbcx.errors_buffer.drain(..) { - errored |= diag.is_error(); + errored |= diag.is_error(); mbcx.infcx.tcx.sess.diagnostic().emit_diagnostic(&diag); } } diff --git a/compiler/rustc_mir/src/const_eval/eval_queries.rs b/compiler/rustc_mir/src/const_eval/eval_queries.rs index aeeb9e2de7adc..363a3cc3a83da 100644 --- a/compiler/rustc_mir/src/const_eval/eval_queries.rs +++ b/compiler/rustc_mir/src/const_eval/eval_queries.rs @@ -292,7 +292,7 @@ pub fn eval_to_allocation_raw_provider<'tcx>( if let Some(error_reported) = tcx.mir_const_qualif_opt_const_arg(def).error_occured { return Err(ErrorHandled::Reported(error_reported)); } - + let borrowck_results = if let Some(param_did) = def.const_param_did { tcx.mir_borrowck_const_arg((def.did, param_did)) } else { diff --git a/src/test/ui/consts/const-eval/borrowck_error_no_ice.rs b/src/test/ui/consts/const-eval/borrowck_error_no_ice.rs index 5478ba727bbc6..ef240b4f03512 100644 --- a/src/test/ui/consts/const-eval/borrowck_error_no_ice.rs +++ b/src/test/ui/consts/const-eval/borrowck_error_no_ice.rs @@ -6,4 +6,4 @@ struct Bug { }], } -fn main() {} \ No newline at end of file +fn main() {} From 8231b173ab9b72b156b8fedea8aeb18faa066927 Mon Sep 17 00:00:00 2001 From: Ellen Date: Wed, 16 Jun 2021 16:05:39 +0100 Subject: [PATCH 3/5] somehow i expect this is *not* the correct solution --- .../rustc_mir/src/const_eval/eval_queries.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_mir/src/const_eval/eval_queries.rs b/compiler/rustc_mir/src/const_eval/eval_queries.rs index 363a3cc3a83da..05cfee281bf5d 100644 --- a/compiler/rustc_mir/src/const_eval/eval_queries.rs +++ b/compiler/rustc_mir/src/const_eval/eval_queries.rs @@ -276,6 +276,8 @@ pub fn eval_to_allocation_raw_provider<'tcx>( let cid = key.value; let def = cid.instance.def.with_opt_param(); + debug!("promoted={:?}", cid.promoted); + if let Some(def) = def.as_local() { if tcx.has_typeck_results(def.did) { if let Some(error_reported) = tcx.typeck_opt_const_arg(def).tainted_by_errors { @@ -293,13 +295,16 @@ pub fn eval_to_allocation_raw_provider<'tcx>( return Err(ErrorHandled::Reported(error_reported)); } - let borrowck_results = if let Some(param_did) = def.const_param_did { - tcx.mir_borrowck_const_arg((def.did, param_did)) - } else { - tcx.mir_borrowck(def.did) - }; - if borrowck_results.errored { - return Err(ErrorHandled::Reported(ErrorReported {})); + if cid.promoted.is_none() { + let borrowck_results = if let Some(param_did) = def.const_param_did { + tcx.mir_borrowck_const_arg((def.did, param_did)) + } else { + tcx.mir_borrowck(def.did) + }; + + if borrowck_results.errored { + return Err(ErrorHandled::Reported(ErrorReported {})); + } } } From e4f9d1f09c77a00f84f9eceb45d229b8d466b506 Mon Sep 17 00:00:00 2001 From: Ellen Date: Wed, 16 Jun 2021 16:19:05 +0100 Subject: [PATCH 4/5] fmt --- compiler/rustc_mir/src/const_eval/eval_queries.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_mir/src/const_eval/eval_queries.rs b/compiler/rustc_mir/src/const_eval/eval_queries.rs index 05cfee281bf5d..0b002e76beea5 100644 --- a/compiler/rustc_mir/src/const_eval/eval_queries.rs +++ b/compiler/rustc_mir/src/const_eval/eval_queries.rs @@ -301,7 +301,7 @@ pub fn eval_to_allocation_raw_provider<'tcx>( } else { tcx.mir_borrowck(def.did) }; - + if borrowck_results.errored { return Err(ErrorHandled::Reported(ErrorReported {})); } From e35dd1ae0fa1011d92e307ebfe44c5593397b469 Mon Sep 17 00:00:00 2001 From: Ellen Date: Wed, 16 Jun 2021 20:43:36 +0100 Subject: [PATCH 5/5] *undoes your test* --- .../const-mut-refs/issue-76510.32bit.stderr | 17 +++-------------- .../const-mut-refs/issue-76510.64bit.stderr | 17 +++-------------- .../ui/consts/const-mut-refs/issue-76510.rs | 7 +++---- 3 files changed, 9 insertions(+), 32 deletions(-) diff --git a/src/test/ui/consts/const-mut-refs/issue-76510.32bit.stderr b/src/test/ui/consts/const-mut-refs/issue-76510.32bit.stderr index 965bc67a381ae..61b00be345fee 100644 --- a/src/test/ui/consts/const-mut-refs/issue-76510.32bit.stderr +++ b/src/test/ui/consts/const-mut-refs/issue-76510.32bit.stderr @@ -19,18 +19,7 @@ error[E0596]: cannot borrow data in a `&` reference as mutable LL | const S: &'static mut str = &mut " hello "; | ^^^^^^^^^^^^^^ cannot borrow as mutable -error[E0080]: it is undefined behavior to use this value - --> $DIR/issue-76510.rs:5:1 - | -LL | const S: &'static mut str = &mut " hello "; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered mutable reference in a `const` - | - = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. - = note: the raw bytes of the constant (size: 8, align: 4) { - ╾─alloc2──╼ 07 00 00 00 │ ╾──╼.... - } - -error: aborting due to 4 previous errors +error: aborting due to 3 previous errors -Some errors have detailed explanations: E0080, E0596, E0658, E0764. -For more information about an error, try `rustc --explain E0080`. +Some errors have detailed explanations: E0596, E0658, E0764. +For more information about an error, try `rustc --explain E0596`. diff --git a/src/test/ui/consts/const-mut-refs/issue-76510.64bit.stderr b/src/test/ui/consts/const-mut-refs/issue-76510.64bit.stderr index ac7d5993585e8..61b00be345fee 100644 --- a/src/test/ui/consts/const-mut-refs/issue-76510.64bit.stderr +++ b/src/test/ui/consts/const-mut-refs/issue-76510.64bit.stderr @@ -19,18 +19,7 @@ error[E0596]: cannot borrow data in a `&` reference as mutable LL | const S: &'static mut str = &mut " hello "; | ^^^^^^^^^^^^^^ cannot borrow as mutable -error[E0080]: it is undefined behavior to use this value - --> $DIR/issue-76510.rs:5:1 - | -LL | const S: &'static mut str = &mut " hello "; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered mutable reference in a `const` - | - = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. - = note: the raw bytes of the constant (size: 16, align: 8) { - ╾───────alloc2────────╼ 07 00 00 00 00 00 00 00 │ ╾──────╼........ - } - -error: aborting due to 4 previous errors +error: aborting due to 3 previous errors -Some errors have detailed explanations: E0080, E0596, E0658, E0764. -For more information about an error, try `rustc --explain E0080`. +Some errors have detailed explanations: E0596, E0658, E0764. +For more information about an error, try `rustc --explain E0596`. diff --git a/src/test/ui/consts/const-mut-refs/issue-76510.rs b/src/test/ui/consts/const-mut-refs/issue-76510.rs index 892f6c98116c2..44f5e9b72798e 100644 --- a/src/test/ui/consts/const-mut-refs/issue-76510.rs +++ b/src/test/ui/consts/const-mut-refs/issue-76510.rs @@ -6,12 +6,11 @@ const S: &'static mut str = &mut " hello "; //~^ ERROR: mutable references are not allowed in the final value of constants //~| ERROR: mutation through a reference is not allowed in constants //~| ERROR: cannot borrow data in a `&` reference as mutable -//~| ERROR: it is undefined behavior to use this value const fn trigger() -> [(); unsafe { - let s = transmute::<(*const u8, usize), &ManuallyDrop>((S.as_ptr(), 3)); - 0 - }] { + let s = transmute::<(*const u8, usize), &ManuallyDrop>((S.as_ptr(), 3)); + 0 +}] { [(); 0] }