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

add error_occured field to ConstQualifs, #78809

Merged
merged 5 commits into from
Nov 14, 2020
Merged
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
5 changes: 3 additions & 2 deletions compiler/rustc_middle/src/mir/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_mir/src/const_eval/eval_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_mir/src/transform/check_consts/qualifs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@
//!
//! 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;
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,
}
}

Expand Down
21 changes: 13 additions & 8 deletions compiler/rustc_mir/src/transform/check_consts/validation.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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,
};

Expand Down Expand Up @@ -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,
}
}
}
Expand All @@ -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>,
}

Expand All @@ -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(),
}
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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)
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
}

/// Emits an error if an expression cannot be evaluated in the current context.
Expand Down Expand Up @@ -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();
}

Expand Down
1 change: 0 additions & 1 deletion src/test/compile-fail/issue-52443.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 2 additions & 9 deletions src/test/ui/const-generics/nested-type.full.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 2 previous errors
error: aborting due to previous error

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`.
11 changes: 2 additions & 9 deletions src/test/ui/const-generics/nested-type.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 3 previous errors
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`.
1 change: 0 additions & 1 deletion src/test/ui/const-generics/nested-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
1 change: 0 additions & 1 deletion src/test/ui/consts/const-call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 2 additions & 9 deletions src/test/ui/consts/const-call.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 2 previous errors
error: aborting due to previous error

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`.
1 change: 0 additions & 1 deletion src/test/ui/consts/const-eval/issue-52442.rs
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 2 additions & 9 deletions src/test/ui/consts/const-eval/issue-52442.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ LL | [(); { &loop { break } as *const _ as usize } ];
= note: see issue #51910 <https:/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 2 previous errors
error: aborting due to previous error

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`.
1 change: 0 additions & 1 deletion src/test/ui/consts/const-eval/match-test-ptr-null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
11 changes: 2 additions & 9 deletions src/test/ui/consts/const-eval/match-test-ptr-null.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ LL | match &1 as *const i32 as usize {
= note: see issue #51910 <https:/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 2 previous errors
error: aborting due to previous error

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`.
1 change: 0 additions & 1 deletion src/test/ui/consts/issue-68542-closure-in-array-len.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

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

fn main() {}
11 changes: 2 additions & 9 deletions src/test/ui/consts/issue-68542-closure-in-array-len.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 2 previous errors
error: aborting due to previous error

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`.
3 changes: 3 additions & 0 deletions src/test/ui/consts/issue-76064.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
struct Bug([u8; panic!(1)]); //~ ERROR panicking in constants is unstable
Copy link
Contributor Author

@vn-ki vn-ki Nov 6, 2020

Choose a reason for hiding this comment

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

Not sure what a good test case is, in this case. So I added the one from the issue.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a good test case. This PR does not yet fix struct Bug([u8; {let x; x}]);, so there are no other tests to add


fn main() {}
13 changes: 13 additions & 0 deletions src/test/ui/consts/issue-76064.stderr
Original file line number Diff line number Diff line change
@@ -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:/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`.
2 changes: 0 additions & 2 deletions src/test/ui/issues/issue-39559-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
19 changes: 3 additions & 16 deletions src/test/ui/issues/issue-39559-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 4 previous errors
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`.
1 change: 0 additions & 1 deletion src/test/ui/issues/issue-43105.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 3 additions & 13 deletions src/test/ui/issues/issue-43105.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
1 change: 0 additions & 1 deletion src/test/ui/issues/issue-52023-array-size-pointer-cast.rs
Original file line number Diff line number Diff line change
@@ -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
}
Loading