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`
This commit is contained in:
bors 2020-11-14 18:03:17 +00:00
commit 98d66340d6
29 changed files with 78 additions and 157 deletions

View file

@ -233,14 +233,15 @@ pub struct BorrowCheckResult<'tcx> {
/// The result of the `mir_const_qualif` query.
///
/// Each field corresponds to an implementer of the `Qualif` trait in
/// `librustc_mir/transform/check_consts/qualifs.rs`. See that file for more information on each
/// Each field (except `error_occured`) corresponds to an implementer of the `Qualif` trait in
/// `rustc_mir/src/transform/check_consts/qualifs.rs`. See that file for more information on each
/// `Qualif`.
#[derive(Clone, Copy, Debug, Default, TyEncodable, TyDecodable, HashStable)]
pub struct ConstQualifs {
pub has_mut_interior: bool,
pub needs_drop: bool,
pub custom_eq: bool,
pub error_occured: Option<ErrorReported>,
}
/// After we borrow check a closure, we are left with various

View file

@ -6,6 +6,7 @@ use crate::interpret::{
ScalarMaybeUninit, StackPopCleanup,
};
use rustc_errors::ErrorReported;
use rustc_hir::def::DefKind;
use rustc_middle::mir;
use rustc_middle::mir::interpret::ErrorHandled;
@ -274,6 +275,16 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
return Err(ErrorHandled::Reported(error_reported));
}
}
if !tcx.is_mir_available(def.did) {
tcx.sess.delay_span_bug(
tcx.def_span(def.did),
&format!("no MIR body is available for {:?}", def.did),
);
return Err(ErrorHandled::Reported(ErrorReported {}));
}
if let Some(error_reported) = tcx.mir_const_qualif_opt_const_arg(def).error_occured {
return Err(ErrorHandled::Reported(error_reported));
}
}
let is_static = tcx.is_static(def.did);

View file

@ -2,6 +2,7 @@
//!
//! See the `Qualif` trait for more info.
use rustc_errors::ErrorReported;
use rustc_middle::mir::*;
use rustc_middle::ty::{self, subst::SubstsRef, AdtDef, Ty};
use rustc_span::DUMMY_SP;
@ -9,11 +10,16 @@ use rustc_trait_selection::traits;
use super::ConstCx;
pub fn in_any_value_of_ty(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> ConstQualifs {
pub fn in_any_value_of_ty(
cx: &ConstCx<'_, 'tcx>,
ty: Ty<'tcx>,
error_occured: Option<ErrorReported>,
) -> ConstQualifs {
ConstQualifs {
has_mut_interior: HasMutInterior::in_any_value_of_ty(cx, ty),
needs_drop: NeedsDrop::in_any_value_of_ty(cx, ty),
custom_eq: CustomEq::in_any_value_of_ty(cx, ty),
error_occured,
}
}

View file

@ -1,6 +1,6 @@
//! The `Visitor` responsible for actually checking a `mir::Body` for invalid operations.
use rustc_errors::{struct_span_err, Applicability, Diagnostic};
use rustc_errors::{struct_span_err, Applicability, Diagnostic, ErrorReported};
use rustc_hir::def_id::DefId;
use rustc_hir::{self as hir, HirId, LangItem};
use rustc_infer::infer::TyCtxtInferExt;
@ -123,7 +123,11 @@ impl Qualifs<'mir, 'tcx> {
has_mut_interior.get().contains(local) || self.indirectly_mutable(ccx, local, location)
}
fn in_return_place(&mut self, ccx: &'mir ConstCx<'mir, 'tcx>) -> ConstQualifs {
fn in_return_place(
&mut self,
ccx: &'mir ConstCx<'mir, 'tcx>,
error_occured: Option<ErrorReported>,
) -> ConstQualifs {
// Find the `Return` terminator if one exists.
//
// If no `Return` terminator exists, this MIR is divergent. Just return the conservative
@ -139,7 +143,7 @@ impl Qualifs<'mir, 'tcx> {
.map(|(bb, _)| bb);
let return_block = match return_block {
None => return qualifs::in_any_value_of_ty(ccx, ccx.body.return_ty()),
None => return qualifs::in_any_value_of_ty(ccx, ccx.body.return_ty(), error_occured),
Some(bb) => bb,
};
@ -170,6 +174,7 @@ impl Qualifs<'mir, 'tcx> {
needs_drop: self.needs_drop(ccx, RETURN_PLACE, return_loc),
has_mut_interior: self.has_mut_interior(ccx, RETURN_PLACE, return_loc),
custom_eq,
error_occured,
}
}
}
@ -181,7 +186,7 @@ pub struct Validator<'mir, 'tcx> {
/// The span of the current statement.
span: Span,
error_emitted: bool,
error_emitted: Option<ErrorReported>,
secondary_errors: Vec<Diagnostic>,
}
@ -199,7 +204,7 @@ impl Validator<'mir, 'tcx> {
span: ccx.body.span,
ccx,
qualifs: Default::default(),
error_emitted: false,
error_emitted: None,
secondary_errors: Vec::new(),
}
}
@ -266,7 +271,7 @@ impl Validator<'mir, 'tcx> {
// If we got through const-checking without emitting any "primary" errors, emit any
// "secondary" errors if they occurred.
let secondary_errors = mem::take(&mut self.secondary_errors);
if !self.error_emitted {
if self.error_emitted.is_none() {
for error in secondary_errors {
self.tcx.sess.diagnostic().emit_diagnostic(&error);
}
@ -276,7 +281,7 @@ impl Validator<'mir, 'tcx> {
}
pub fn qualifs_in_return_place(&mut self) -> ConstQualifs {
self.qualifs.in_return_place(self.ccx)
self.qualifs.in_return_place(self.ccx, self.error_emitted)
}
/// Emits an error if an expression cannot be evaluated in the current context.
@ -318,7 +323,7 @@ impl Validator<'mir, 'tcx> {
match op.importance() {
ops::DiagnosticImportance::Primary => {
self.error_emitted = true;
self.error_emitted = Some(ErrorReported);
err.emit();
}

View file

@ -11,5 +11,4 @@ fn main() {
//~| ERROR calls in constants are limited to constant functions
//~| ERROR mutable references are not allowed in constants
//~| ERROR calls in constants are limited to constant functions
//~| ERROR evaluation of constant value failed
}

View file

@ -4,13 +4,6 @@ error[E0015]: calls in constants are limited to constant functions, tuple struct
LL | Foo::<17>::value()
| ^^^^^^^^^^^^^^^^^^
error[E0080]: evaluation of constant value failed
--> $DIR/nested-type.rs:16:5
|
LL | Foo::<17>::value()
| ^^^^^^^^^^^^^^^^^^ calling non-const function `Foo::{constant#0}::Foo::<17_usize>::value`
error: aborting due to previous error
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0015, E0080.
For more information about an error, try `rustc --explain E0015`.
For more information about this error, try `rustc --explain E0015`.

View file

@ -20,13 +20,6 @@ error[E0015]: calls in constants are limited to constant functions, tuple struct
LL | Foo::<17>::value()
| ^^^^^^^^^^^^^^^^^^
error[E0080]: evaluation of constant value failed
--> $DIR/nested-type.rs:16:5
|
LL | Foo::<17>::value()
| ^^^^^^^^^^^^^^^^^^ calling non-const function `Foo::{constant#0}::Foo::<17_usize>::value`
error: aborting due to 2 previous errors
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0015, E0080.
For more information about an error, try `rustc --explain E0015`.
For more information about this error, try `rustc --explain E0015`.

View file

@ -15,7 +15,6 @@ struct Foo<const N: [u8; { //[min]~ ERROR `[u8; _]` is forbidden
Foo::<17>::value()
//~^ ERROR calls in constants are limited to constant functions
//~| ERROR evaluation of constant value failed
}]>;
fn main() {}

View file

@ -5,5 +5,4 @@ fn f(x: usize) -> usize {
fn main() {
let _ = [0; f(2)];
//~^ ERROR calls in constants are limited to constant functions
//~| ERROR evaluation of constant value failed
}

View file

@ -4,13 +4,6 @@ error[E0015]: calls in constants are limited to constant functions, tuple struct
LL | let _ = [0; f(2)];
| ^^^^
error[E0080]: evaluation of constant value failed
--> $DIR/const-call.rs:6:17
|
LL | let _ = [0; f(2)];
| ^^^^ calling non-const function `f`
error: aborting due to previous error
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0015, E0080.
For more information about an error, try `rustc --explain E0015`.
For more information about this error, try `rustc --explain E0015`.

View file

@ -1,5 +1,4 @@
fn main() {
[(); { &loop { break } as *const _ as usize } ];
//~^ ERROR casting pointers to integers in constants is unstable
//~| ERROR evaluation of constant value failed
}

View file

@ -7,13 +7,6 @@ LL | [(); { &loop { break } as *const _ as usize } ];
= note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information
= help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable
error[E0080]: evaluation of constant value failed
--> $DIR/issue-52442.rs:2:13
|
LL | [(); { &loop { break } as *const _ as usize } ];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "pointer-to-integer cast" needs an rfc before being allowed inside constants
error: aborting due to previous error
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0080, E0658.
For more information about an error, try `rustc --explain E0080`.
For more information about this error, try `rustc --explain E0658`.

View file

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

View file

@ -7,13 +7,6 @@ LL | match &1 as *const i32 as usize {
= note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information
= help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable
error[E0080]: evaluation of constant value failed
--> $DIR/match-test-ptr-null.rs:6:15
|
LL | match &1 as *const i32 as usize {
| ^^^^^^^^^^^^^^^^^^^^^^^^^ "pointer-to-integer cast" needs an rfc before being allowed inside constants
error: aborting due to previous error
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0080, E0658.
For more information about an error, try `rustc --explain E0080`.
For more information about this error, try `rustc --explain E0658`.

View file

@ -4,7 +4,6 @@
struct Bug {
a: [(); (|| { 0 })()] //~ ERROR calls in constants are limited to
//~^ ERROR evaluation of constant value failed
}
fn main() {}

View file

@ -4,13 +4,6 @@ error[E0015]: calls in constants are limited to constant functions, tuple struct
LL | a: [(); (|| { 0 })()]
| ^^^^^^^^^^^^
error[E0080]: evaluation of constant value failed
--> $DIR/issue-68542-closure-in-array-len.rs:6:13
|
LL | a: [(); (|| { 0 })()]
| ^^^^^^^^^^^^ calling non-const function `Bug::a::{constant#0}::{closure#0}`
error: aborting due to previous error
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0015, E0080.
For more information about an error, try `rustc --explain E0015`.
For more information about this error, try `rustc --explain E0015`.

View file

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

View file

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

View file

@ -13,8 +13,6 @@ impl Dim for Dim3 {
fn main() {
let array: [usize; Dim3::dim()]
//~^ ERROR E0015
//~| ERROR E0080
= [0; Dim3::dim()];
//~^ ERROR E0015
//~| ERROR E0080
}

View file

@ -4,25 +4,12 @@ error[E0015]: calls in constants are limited to constant functions, tuple struct
LL | let array: [usize; Dim3::dim()]
| ^^^^^^^^^^^
error[E0080]: evaluation of constant value failed
--> $DIR/issue-39559-2.rs:14:24
|
LL | let array: [usize; Dim3::dim()]
| ^^^^^^^^^^^ calling non-const function `<Dim3 as Dim>::dim`
error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants
--> $DIR/issue-39559-2.rs:17:15
--> $DIR/issue-39559-2.rs:16:15
|
LL | = [0; Dim3::dim()];
| ^^^^^^^^^^^
error[E0080]: evaluation of constant value failed
--> $DIR/issue-39559-2.rs:17:15
|
LL | = [0; Dim3::dim()];
| ^^^^^^^^^^^ calling non-const function `<Dim3 as Dim>::dim`
error: aborting due to 2 previous errors
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0015, E0080.
For more information about an error, try `rustc --explain E0015`.
For more information about this error, try `rustc --explain E0015`.

View file

@ -2,7 +2,6 @@ fn xyz() -> u8 { 42 }
const NUM: u8 = xyz();
//~^ ERROR calls in constants are limited to constant functions, tuple structs and tuple variants
//~| ERROR any use of this value will cause an error [const_err]
fn main() {
match 1 {

View file

@ -4,28 +4,18 @@ error[E0015]: calls in constants are limited to constant functions, tuple struct
LL | const NUM: u8 = xyz();
| ^^^^^
error: any use of this value will cause an error
--> $DIR/issue-43105.rs:3:17
|
LL | const NUM: u8 = xyz();
| ----------------^^^^^-
| |
| calling non-const function `xyz`
|
= note: `#[deny(const_err)]` on by default
error: could not evaluate constant pattern
--> $DIR/issue-43105.rs:9:9
--> $DIR/issue-43105.rs:8:9
|
LL | NUM => unimplemented!(),
| ^^^
error: could not evaluate constant pattern
--> $DIR/issue-43105.rs:9:9
--> $DIR/issue-43105.rs:8:9
|
LL | NUM => unimplemented!(),
| ^^^
error: aborting due to 4 previous errors
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0015`.

View file

@ -1,4 +1,3 @@
fn main() {
let _ = [0; (&0 as *const i32) as usize]; //~ ERROR casting pointers to integers in constants
//~^ ERROR evaluation of constant value failed
}

View file

@ -7,13 +7,6 @@ LL | let _ = [0; (&0 as *const i32) as usize];
= note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information
= help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable
error[E0080]: evaluation of constant value failed
--> $DIR/issue-52023-array-size-pointer-cast.rs:2:17
|
LL | let _ = [0; (&0 as *const i32) as usize];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ "pointer-to-integer cast" needs an rfc before being allowed inside constants
error: aborting due to previous error
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0080, E0658.
For more information about an error, try `rustc --explain E0080`.
For more information about this error, try `rustc --explain E0658`.

View file

@ -3,6 +3,5 @@
static A: &'static [u32] = &[1];
static B: [u32; 1] = [0; A.len()];
//~^ ERROR [E0013]
//~| ERROR evaluation of constant value failed
fn main() {}

View file

@ -6,13 +6,6 @@ LL | static B: [u32; 1] = [0; A.len()];
|
= help: consider extracting the value of the `static` to a `const`, and referring to that
error[E0080]: evaluation of constant value failed
--> $DIR/issue-52060.rs:4:26
|
LL | static B: [u32; 1] = [0; A.len()];
| ^ constant accesses static
error: aborting due to previous error
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0013, E0080.
For more information about an error, try `rustc --explain E0013`.
For more information about this error, try `rustc --explain E0013`.

View file

@ -1,8 +1,8 @@
fn main() {
[1; <Multiply<Five, Five>>::VAL]; //~ ERROR evaluation of constant value failed
[1; <Multiply<Five, Five>>::VAL];
}
trait TypeVal<T> {
const VAL: T; //~ ERROR any use of this value will cause an error
const VAL: T;
}
struct Five;
struct Multiply<N, M> {

View file

@ -26,21 +26,7 @@ LL | const VAL: T;
LL | impl<N, M> TypeVal<usize> for Multiply<N, M> where N: TypeVal<VAL> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `VAL` in implementation
error: any use of this value will cause an error
--> $DIR/issue-77919.rs:5:5
|
LL | const VAL: T;
| ^^^^^^^^^^^^^ no MIR body is available for DefId(0:7 ~ issue_77919[317d]::TypeVal::VAL)
|
= note: `#[deny(const_err)]` on by default
error: aborting due to 3 previous errors
error[E0080]: evaluation of constant value failed
--> $DIR/issue-77919.rs:2:9
|
LL | [1; <Multiply<Five, Five>>::VAL];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
error: aborting due to 5 previous errors
Some errors have detailed explanations: E0046, E0080, E0412.
Some errors have detailed explanations: E0046, E0412.
For more information about an error, try `rustc --explain E0046`.

View file

@ -26,21 +26,7 @@ LL | const VAL: T;
LL | impl<N, M> TypeVal<usize> for Multiply<N, M> where N: TypeVal<VAL> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `VAL` in implementation
error: any use of this value will cause an error
--> $DIR/ice-6252.rs:5:5
|
LL | const VAL: T;
| ^^^^^^^^^^^^^ no MIR body is available for DefId(0:5 ~ ice_6252[317d]::TypeVal::VAL)
|
= note: `#[deny(const_err)]` on by default
error: aborting due to 3 previous errors
error[E0080]: evaluation of constant value failed
--> $DIR/ice-6252.rs:14:9
|
LL | [1; <Multiply<Five, Five>>::VAL];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
error: aborting due to 5 previous errors
Some errors have detailed explanations: E0046, E0080, E0412.
Some errors have detailed explanations: E0046, E0412.
For more information about an error, try `rustc --explain E0046`.