Skip to content

Commit 57740d6

Browse files
committed
don't evaluate constants if borrowck fails
1 parent 9fef8d9 commit 57740d6

File tree

7 files changed

+33
-11
lines changed

7 files changed

+33
-11
lines changed

compiler/rustc_middle/src/mir/query.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ pub struct BorrowCheckResult<'tcx> {
214214
pub concrete_opaque_types: VecMap<OpaqueTypeKey<'tcx>, Ty<'tcx>>,
215215
pub closure_requirements: Option<ClosureRegionRequirements<'tcx>>,
216216
pub used_mut_upvars: SmallVec<[Field; 8]>,
217+
pub errored: bool,
217218
}
218219

219220
/// The result of the `mir_const_qualif` query.

compiler/rustc_mir/src/borrow_check/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,12 @@ fn do_mir_borrowck<'a, 'tcx>(
432432
diag.buffer(&mut mbcx.errors_buffer);
433433
}
434434

435+
let mut errored = false;
435436
if !mbcx.errors_buffer.is_empty() {
436437
mbcx.errors_buffer.sort_by_key(|diag| diag.sort_span);
437438

438439
for diag in mbcx.errors_buffer.drain(..) {
440+
errored |= diag.is_error();
439441
mbcx.infcx.tcx.sess.diagnostic().emit_diagnostic(&diag);
440442
}
441443
}
@@ -444,6 +446,7 @@ fn do_mir_borrowck<'a, 'tcx>(
444446
concrete_opaque_types: opaque_type_values,
445447
closure_requirements: opt_closure_req,
446448
used_mut_upvars: mbcx.used_mut_upvars,
449+
errored,
447450
};
448451

449452
debug!("do_mir_borrowck: result = {:#?}", result);

compiler/rustc_mir/src/const_eval/eval_queries.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,15 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
292292
if let Some(error_reported) = tcx.mir_const_qualif_opt_const_arg(def).error_occured {
293293
return Err(ErrorHandled::Reported(error_reported));
294294
}
295+
296+
let borrowck_results = if let Some(param_did) = def.const_param_did {
297+
tcx.mir_borrowck_const_arg((def.did, param_did))
298+
} else {
299+
tcx.mir_borrowck(def.did)
300+
};
301+
if borrowck_results.errored {
302+
return Err(ErrorHandled::Reported(ErrorReported {}));
303+
}
295304
}
296305

297306
let is_static = tcx.is_static(def.did);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
struct Bug {
2+
A: [(); {
3+
let x;
4+
x
5+
//~^ error: use of possibly-uninitialized variable
6+
}],
7+
}
8+
9+
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0381]: use of possibly-uninitialized variable: `x`
2+
--> $DIR/borrowck_error_no_ice.rs:4:9
3+
|
4+
LL | x
5+
| ^ use of possibly-uninitialized `x`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0381`.

src/test/ui/consts/issue-78655.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const FOO: *const u32 = { //~ ERROR encountered dangling pointer in final constant
1+
const FOO: *const u32 = {
22
let x;
33
&x //~ ERROR borrow of possibly-uninitialized variable: `x`
44
};

src/test/ui/consts/issue-78655.stderr

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@ error[E0381]: borrow of possibly-uninitialized variable: `x`
44
LL | &x
55
| ^^ use of possibly-uninitialized `x`
66

7-
error: encountered dangling pointer in final constant
8-
--> $DIR/issue-78655.rs:1:1
9-
|
10-
LL | / const FOO: *const u32 = {
11-
LL | | let x;
12-
LL | | &x
13-
LL | | };
14-
| |__^
15-
167
error: could not evaluate constant pattern
178
--> $DIR/issue-78655.rs:7:9
189
|
@@ -25,6 +16,6 @@ error: could not evaluate constant pattern
2516
LL | let FOO = FOO;
2617
| ^^^
2718

28-
error: aborting due to 4 previous errors
19+
error: aborting due to 3 previous errors
2920

3021
For more information about this error, try `rustc --explain E0381`.

0 commit comments

Comments
 (0)