Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't evaluate constants if borrowck fails #86325

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/mir/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ pub struct BorrowCheckResult<'tcx> {
pub concrete_opaque_types: VecMap<OpaqueTypeKey<'tcx>, Ty<'tcx>>,
pub closure_requirements: Option<ClosureRegionRequirements<'tcx>>,
pub used_mut_upvars: SmallVec<[Field; 8]>,
pub errored: bool,
Copy link
Member

@eddyb eddyb Jul 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm tempted to suggest making the mir_borrowck query return Result<BorrowCheckResult, ErrorReported> - I don't know if that would regress anything, but if there are any errors I wouldn't want to trust the other fields tbh.

}

/// The result of the `mir_const_qualif` query.
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_mir/src/borrow_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions compiler/rustc_mir/src/const_eval/eval_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -292,6 +294,18 @@ 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));
}

if cid.promoted.is_none() {
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
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);
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/consts/const-eval/borrowck_error_no_ice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
struct Bug {
A: [(); {
let x;
x
//~^ error: use of possibly-uninitialized variable
}],
}

fn main() {}
9 changes: 9 additions & 0 deletions src/test/ui/consts/const-eval/borrowck_error_no_ice.stderr
Original file line number Diff line number Diff line change
@@ -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`.
17 changes: 3 additions & 14 deletions src/test/ui/consts/const-mut-refs/issue-76510.32bit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
17 changes: 3 additions & 14 deletions src/test/ui/consts/const-mut-refs/issue-76510.64bit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
7 changes: 3 additions & 4 deletions src/test/ui/consts/const-mut-refs/issue-76510.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<str>>((S.as_ptr(), 3));
0
}] {
let s = transmute::<(*const u8, usize), &ManuallyDrop<str>>((S.as_ptr(), 3));
0
}] {
[(); 0]
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/consts/issue-78655.rs
Original file line number Diff line number Diff line change
@@ -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`
};
Expand Down
11 changes: 1 addition & 10 deletions src/test/ui/consts/issue-78655.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand All @@ -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`.