Skip to content

Commit 98d6634

Browse files
committed
Auto merge of #78809 - vn-ki:fix-issue-76064, r=oli-obk
add error_occured field to ConstQualifs, fix #76064 I wasn't sure what `in_return_place` actually did and not sure why it returns `ConstQualifs` while it's sibling functions return `bool`. So I tried to make as minimal changes to the structure as possible. Please point out whether I have to refactor it or not. r? `@oli-obk` cc `@RalfJung`
2 parents 30e49a9 + f026d0f commit 98d6634

29 files changed

+78
-157
lines changed

compiler/rustc_middle/src/mir/query.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,15 @@ pub struct BorrowCheckResult<'tcx> {
233233

234234
/// The result of the `mir_const_qualif` query.
235235
///
236-
/// Each field corresponds to an implementer of the `Qualif` trait in
237-
/// `librustc_mir/transform/check_consts/qualifs.rs`. See that file for more information on each
236+
/// Each field (except `error_occured`) corresponds to an implementer of the `Qualif` trait in
237+
/// `rustc_mir/src/transform/check_consts/qualifs.rs`. See that file for more information on each
238238
/// `Qualif`.
239239
#[derive(Clone, Copy, Debug, Default, TyEncodable, TyDecodable, HashStable)]
240240
pub struct ConstQualifs {
241241
pub has_mut_interior: bool,
242242
pub needs_drop: bool,
243243
pub custom_eq: bool,
244+
pub error_occured: Option<ErrorReported>,
244245
}
245246

246247
/// After we borrow check a closure, we are left with various

compiler/rustc_mir/src/const_eval/eval_queries.rs

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::interpret::{
66
ScalarMaybeUninit, StackPopCleanup,
77
};
88

9+
use rustc_errors::ErrorReported;
910
use rustc_hir::def::DefKind;
1011
use rustc_middle::mir;
1112
use rustc_middle::mir::interpret::ErrorHandled;
@@ -274,6 +275,16 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
274275
return Err(ErrorHandled::Reported(error_reported));
275276
}
276277
}
278+
if !tcx.is_mir_available(def.did) {
279+
tcx.sess.delay_span_bug(
280+
tcx.def_span(def.did),
281+
&format!("no MIR body is available for {:?}", def.did),
282+
);
283+
return Err(ErrorHandled::Reported(ErrorReported {}));
284+
}
285+
if let Some(error_reported) = tcx.mir_const_qualif_opt_const_arg(def).error_occured {
286+
return Err(ErrorHandled::Reported(error_reported));
287+
}
277288
}
278289

279290
let is_static = tcx.is_static(def.did);

compiler/rustc_mir/src/transform/check_consts/qualifs.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22
//!
33
//! See the `Qualif` trait for more info.
44
5+
use rustc_errors::ErrorReported;
56
use rustc_middle::mir::*;
67
use rustc_middle::ty::{self, subst::SubstsRef, AdtDef, Ty};
78
use rustc_span::DUMMY_SP;
89
use rustc_trait_selection::traits;
910

1011
use super::ConstCx;
1112

12-
pub fn in_any_value_of_ty(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> ConstQualifs {
13+
pub fn in_any_value_of_ty(
14+
cx: &ConstCx<'_, 'tcx>,
15+
ty: Ty<'tcx>,
16+
error_occured: Option<ErrorReported>,
17+
) -> ConstQualifs {
1318
ConstQualifs {
1419
has_mut_interior: HasMutInterior::in_any_value_of_ty(cx, ty),
1520
needs_drop: NeedsDrop::in_any_value_of_ty(cx, ty),
1621
custom_eq: CustomEq::in_any_value_of_ty(cx, ty),
22+
error_occured,
1723
}
1824
}
1925

compiler/rustc_mir/src/transform/check_consts/validation.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! The `Visitor` responsible for actually checking a `mir::Body` for invalid operations.
22
3-
use rustc_errors::{struct_span_err, Applicability, Diagnostic};
3+
use rustc_errors::{struct_span_err, Applicability, Diagnostic, ErrorReported};
44
use rustc_hir::def_id::DefId;
55
use rustc_hir::{self as hir, HirId, LangItem};
66
use rustc_infer::infer::TyCtxtInferExt;
@@ -123,7 +123,11 @@ impl Qualifs<'mir, 'tcx> {
123123
has_mut_interior.get().contains(local) || self.indirectly_mutable(ccx, local, location)
124124
}
125125

126-
fn in_return_place(&mut self, ccx: &'mir ConstCx<'mir, 'tcx>) -> ConstQualifs {
126+
fn in_return_place(
127+
&mut self,
128+
ccx: &'mir ConstCx<'mir, 'tcx>,
129+
error_occured: Option<ErrorReported>,
130+
) -> ConstQualifs {
127131
// Find the `Return` terminator if one exists.
128132
//
129133
// If no `Return` terminator exists, this MIR is divergent. Just return the conservative
@@ -139,7 +143,7 @@ impl Qualifs<'mir, 'tcx> {
139143
.map(|(bb, _)| bb);
140144

141145
let return_block = match return_block {
142-
None => return qualifs::in_any_value_of_ty(ccx, ccx.body.return_ty()),
146+
None => return qualifs::in_any_value_of_ty(ccx, ccx.body.return_ty(), error_occured),
143147
Some(bb) => bb,
144148
};
145149

@@ -170,6 +174,7 @@ impl Qualifs<'mir, 'tcx> {
170174
needs_drop: self.needs_drop(ccx, RETURN_PLACE, return_loc),
171175
has_mut_interior: self.has_mut_interior(ccx, RETURN_PLACE, return_loc),
172176
custom_eq,
177+
error_occured,
173178
}
174179
}
175180
}
@@ -181,7 +186,7 @@ pub struct Validator<'mir, 'tcx> {
181186
/// The span of the current statement.
182187
span: Span,
183188

184-
error_emitted: bool,
189+
error_emitted: Option<ErrorReported>,
185190
secondary_errors: Vec<Diagnostic>,
186191
}
187192

@@ -199,7 +204,7 @@ impl Validator<'mir, 'tcx> {
199204
span: ccx.body.span,
200205
ccx,
201206
qualifs: Default::default(),
202-
error_emitted: false,
207+
error_emitted: None,
203208
secondary_errors: Vec::new(),
204209
}
205210
}
@@ -266,7 +271,7 @@ impl Validator<'mir, 'tcx> {
266271
// If we got through const-checking without emitting any "primary" errors, emit any
267272
// "secondary" errors if they occurred.
268273
let secondary_errors = mem::take(&mut self.secondary_errors);
269-
if !self.error_emitted {
274+
if self.error_emitted.is_none() {
270275
for error in secondary_errors {
271276
self.tcx.sess.diagnostic().emit_diagnostic(&error);
272277
}
@@ -276,7 +281,7 @@ impl Validator<'mir, 'tcx> {
276281
}
277282

278283
pub fn qualifs_in_return_place(&mut self) -> ConstQualifs {
279-
self.qualifs.in_return_place(self.ccx)
284+
self.qualifs.in_return_place(self.ccx, self.error_emitted)
280285
}
281286

282287
/// Emits an error if an expression cannot be evaluated in the current context.
@@ -318,7 +323,7 @@ impl Validator<'mir, 'tcx> {
318323

319324
match op.importance() {
320325
ops::DiagnosticImportance::Primary => {
321-
self.error_emitted = true;
326+
self.error_emitted = Some(ErrorReported);
322327
err.emit();
323328
}
324329

src/test/compile-fail/issue-52443.rs

-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ fn main() {
1111
//~| ERROR calls in constants are limited to constant functions
1212
//~| ERROR mutable references are not allowed in constants
1313
//~| ERROR calls in constants are limited to constant functions
14-
//~| ERROR evaluation of constant value failed
1514
}

src/test/ui/const-generics/nested-type.full.stderr

+2-9
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@ error[E0015]: calls in constants are limited to constant functions, tuple struct
44
LL | Foo::<17>::value()
55
| ^^^^^^^^^^^^^^^^^^
66

7-
error[E0080]: evaluation of constant value failed
8-
--> $DIR/nested-type.rs:16:5
9-
|
10-
LL | Foo::<17>::value()
11-
| ^^^^^^^^^^^^^^^^^^ calling non-const function `Foo::{constant#0}::Foo::<17_usize>::value`
12-
13-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
148

15-
Some errors have detailed explanations: E0015, E0080.
16-
For more information about an error, try `rustc --explain E0015`.
9+
For more information about this error, try `rustc --explain E0015`.

src/test/ui/const-generics/nested-type.min.stderr

+2-9
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ error[E0015]: calls in constants are limited to constant functions, tuple struct
2020
LL | Foo::<17>::value()
2121
| ^^^^^^^^^^^^^^^^^^
2222

23-
error[E0080]: evaluation of constant value failed
24-
--> $DIR/nested-type.rs:16:5
25-
|
26-
LL | Foo::<17>::value()
27-
| ^^^^^^^^^^^^^^^^^^ calling non-const function `Foo::{constant#0}::Foo::<17_usize>::value`
28-
29-
error: aborting due to 3 previous errors
23+
error: aborting due to 2 previous errors
3024

31-
Some errors have detailed explanations: E0015, E0080.
32-
For more information about an error, try `rustc --explain E0015`.
25+
For more information about this error, try `rustc --explain E0015`.

src/test/ui/const-generics/nested-type.rs

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ struct Foo<const N: [u8; { //[min]~ ERROR `[u8; _]` is forbidden
1515

1616
Foo::<17>::value()
1717
//~^ ERROR calls in constants are limited to constant functions
18-
//~| ERROR evaluation of constant value failed
1918
}]>;
2019

2120
fn main() {}

src/test/ui/consts/const-call.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ fn f(x: usize) -> usize {
55
fn main() {
66
let _ = [0; f(2)];
77
//~^ ERROR calls in constants are limited to constant functions
8-
//~| ERROR evaluation of constant value failed
98
}

src/test/ui/consts/const-call.stderr

+2-9
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@ error[E0015]: calls in constants are limited to constant functions, tuple struct
44
LL | let _ = [0; f(2)];
55
| ^^^^
66

7-
error[E0080]: evaluation of constant value failed
8-
--> $DIR/const-call.rs:6:17
9-
|
10-
LL | let _ = [0; f(2)];
11-
| ^^^^ calling non-const function `f`
12-
13-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
148

15-
Some errors have detailed explanations: E0015, E0080.
16-
For more information about an error, try `rustc --explain E0015`.
9+
For more information about this error, try `rustc --explain E0015`.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
fn main() {
22
[(); { &loop { break } as *const _ as usize } ];
33
//~^ ERROR casting pointers to integers in constants is unstable
4-
//~| ERROR evaluation of constant value failed
54
}

src/test/ui/consts/const-eval/issue-52442.stderr

+2-9
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ LL | [(); { &loop { break } as *const _ as usize } ];
77
= note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information
88
= help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable
99

10-
error[E0080]: evaluation of constant value failed
11-
--> $DIR/issue-52442.rs:2:13
12-
|
13-
LL | [(); { &loop { break } as *const _ as usize } ];
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "pointer-to-integer cast" needs an rfc before being allowed inside constants
15-
16-
error: aborting due to 2 previous errors
10+
error: aborting due to previous error
1711

18-
Some errors have detailed explanations: E0080, E0658.
19-
For more information about an error, try `rustc --explain E0080`.
12+
For more information about this error, try `rustc --explain E0658`.

src/test/ui/consts/const-eval/match-test-ptr-null.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ fn main() {
55
let _: [u8; 0] = [4; {
66
match &1 as *const i32 as usize {
77
//~^ ERROR casting pointers to integers in constants
8-
//~| ERROR evaluation of constant value failed
98
0 => 42,
109
n => n,
1110
}

src/test/ui/consts/const-eval/match-test-ptr-null.stderr

+2-9
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ LL | match &1 as *const i32 as usize {
77
= note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information
88
= help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable
99

10-
error[E0080]: evaluation of constant value failed
11-
--> $DIR/match-test-ptr-null.rs:6:15
12-
|
13-
LL | match &1 as *const i32 as usize {
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ "pointer-to-integer cast" needs an rfc before being allowed inside constants
15-
16-
error: aborting due to 2 previous errors
10+
error: aborting due to previous error
1711

18-
Some errors have detailed explanations: E0080, E0658.
19-
For more information about an error, try `rustc --explain E0080`.
12+
For more information about this error, try `rustc --explain E0658`.

src/test/ui/consts/issue-68542-closure-in-array-len.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
struct Bug {
66
a: [(); (|| { 0 })()] //~ ERROR calls in constants are limited to
7-
//~^ ERROR evaluation of constant value failed
87
}
98

109
fn main() {}

src/test/ui/consts/issue-68542-closure-in-array-len.stderr

+2-9
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@ error[E0015]: calls in constants are limited to constant functions, tuple struct
44
LL | a: [(); (|| { 0 })()]
55
| ^^^^^^^^^^^^
66

7-
error[E0080]: evaluation of constant value failed
8-
--> $DIR/issue-68542-closure-in-array-len.rs:6:13
9-
|
10-
LL | a: [(); (|| { 0 })()]
11-
| ^^^^^^^^^^^^ calling non-const function `Bug::a::{constant#0}::{closure#0}`
12-
13-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
148

15-
Some errors have detailed explanations: E0015, E0080.
16-
For more information about an error, try `rustc --explain E0015`.
9+
For more information about this error, try `rustc --explain E0015`.

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

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
struct Bug([u8; panic!(1)]); //~ ERROR panicking in constants is unstable
2+
3+
fn main() {}

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

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0658]: panicking in constants is unstable
2+
--> $DIR/issue-76064.rs:1:17
3+
|
4+
LL | struct Bug([u8; panic!(1)]);
5+
| ^^^^^^^^^
6+
|
7+
= note: see issue #51999 <https://github.com/rust-lang/rust/issues/51999> for more information
8+
= help: add `#![feature(const_panic)]` to the crate attributes to enable
9+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
10+
11+
error: aborting due to previous error
12+
13+
For more information about this error, try `rustc --explain E0658`.

src/test/ui/issues/issue-39559-2.rs

-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ impl Dim for Dim3 {
1313
fn main() {
1414
let array: [usize; Dim3::dim()]
1515
//~^ ERROR E0015
16-
//~| ERROR E0080
1716
= [0; Dim3::dim()];
1817
//~^ ERROR E0015
19-
//~| ERROR E0080
2018
}

src/test/ui/issues/issue-39559-2.stderr

+3-16
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,12 @@ error[E0015]: calls in constants are limited to constant functions, tuple struct
44
LL | let array: [usize; Dim3::dim()]
55
| ^^^^^^^^^^^
66

7-
error[E0080]: evaluation of constant value failed
8-
--> $DIR/issue-39559-2.rs:14:24
9-
|
10-
LL | let array: [usize; Dim3::dim()]
11-
| ^^^^^^^^^^^ calling non-const function `<Dim3 as Dim>::dim`
12-
137
error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants
14-
--> $DIR/issue-39559-2.rs:17:15
8+
--> $DIR/issue-39559-2.rs:16:15
159
|
1610
LL | = [0; Dim3::dim()];
1711
| ^^^^^^^^^^^
1812

19-
error[E0080]: evaluation of constant value failed
20-
--> $DIR/issue-39559-2.rs:17:15
21-
|
22-
LL | = [0; Dim3::dim()];
23-
| ^^^^^^^^^^^ calling non-const function `<Dim3 as Dim>::dim`
24-
25-
error: aborting due to 4 previous errors
13+
error: aborting due to 2 previous errors
2614

27-
Some errors have detailed explanations: E0015, E0080.
28-
For more information about an error, try `rustc --explain E0015`.
15+
For more information about this error, try `rustc --explain E0015`.

src/test/ui/issues/issue-43105.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ fn xyz() -> u8 { 42 }
22

33
const NUM: u8 = xyz();
44
//~^ ERROR calls in constants are limited to constant functions, tuple structs and tuple variants
5-
//~| ERROR any use of this value will cause an error [const_err]
65

76
fn main() {
87
match 1 {

src/test/ui/issues/issue-43105.stderr

+3-13
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,18 @@ error[E0015]: calls in constants are limited to constant functions, tuple struct
44
LL | const NUM: u8 = xyz();
55
| ^^^^^
66

7-
error: any use of this value will cause an error
8-
--> $DIR/issue-43105.rs:3:17
9-
|
10-
LL | const NUM: u8 = xyz();
11-
| ----------------^^^^^-
12-
| |
13-
| calling non-const function `xyz`
14-
|
15-
= note: `#[deny(const_err)]` on by default
16-
177
error: could not evaluate constant pattern
18-
--> $DIR/issue-43105.rs:9:9
8+
--> $DIR/issue-43105.rs:8:9
199
|
2010
LL | NUM => unimplemented!(),
2111
| ^^^
2212

2313
error: could not evaluate constant pattern
24-
--> $DIR/issue-43105.rs:9:9
14+
--> $DIR/issue-43105.rs:8:9
2515
|
2616
LL | NUM => unimplemented!(),
2717
| ^^^
2818

29-
error: aborting due to 4 previous errors
19+
error: aborting due to 3 previous errors
3020

3121
For more information about this error, try `rustc --explain E0015`.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
fn main() {
22
let _ = [0; (&0 as *const i32) as usize]; //~ ERROR casting pointers to integers in constants
3-
//~^ ERROR evaluation of constant value failed
43
}

0 commit comments

Comments
 (0)