Skip to content
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
13 changes: 10 additions & 3 deletions compiler/rustc_const_eval/src/const_eval/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,16 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
// all. That said, we have to allow calling functions inside a `const trait`. These
// *are* const-checked!
if !ecx.tcx.is_const_fn(def) || find_attr!(ecx.tcx, def, RustcDoNotConstCheck) {
// We certainly do *not* want to actually call the fn
// though, so be sure we return here.
throw_unsup_format!("calling non-const function `{}`", instance)
// We certainly do *not* want to actually call this fn. const-checking should have
// prevented this, but this can be reached with unleashed-miri, so make testing
// simpler by avoiding ICEs there. (Technically this can still ICE if miri-unleash
// was set in a dependency rather than the current crate. That's okay, it's an
// internal unstable feature.)
if ecx.tcx.sess.opts.unstable_opts.unleash_the_miri_inside_of_you {
throw_unsup_format!("calling non-const function `{}`", instance)
} else {
span_bug!(ecx.cur_span(), "calling non-const function `{}`", instance)
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/ui/consts/const-eval/do_not_const_check.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
//! Ensure that we refuse to run a do_not_const_check function, even if the body *would* const-check
//! at the moment.

//@ rustc-env:RUSTC_ICE=0
//@ failure-status: 101
//@ normalize-stderr: "note: compiler flags.*\n\n" -> ""
//@ normalize-stderr: "note: rustc.*running on.*" -> "note: rustc {version} running on {platform}"
//@ normalize-stderr: "thread 'rustc'.*panicked.*:\n.*\n" -> ""
//@ normalize-stderr: " +\d{1,}: .*\n" -> ""
//@ normalize-stderr: " + at .*\n" -> ""
//@ normalize-stderr: " +.*omitted.*frames?.*\n" -> ""
//@ normalize-stderr: ".*note: Some details are omitted.*\n" -> ""
//@ normalize-stderr: "(internal compiler error: ).*(compiler/[^:]+):\d+:\d+: " -> "$1$2:LL:CC: "

#![feature(rustc_attrs, intrinsics)]

#[rustc_do_not_const_check]
Expand Down
41 changes: 32 additions & 9 deletions tests/ui/consts/const-eval/do_not_const_check.stderr
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
error[E0080]: calling non-const function `mostly_harmless`
--> $DIR/do_not_const_check.rs:9:5
error: internal compiler error: compiler/rustc_const_eval/src/const_eval/machine.rs:LL:CC: calling non-const function `mostly_harmless`
--> $DIR/do_not_const_check.rs:21:5
|
LL | const _: () = {
LL | mostly_harmless();
| ^^^^^^^^^^^^^^^^^ evaluation of `_` failed here
| ^^^^^^^^^^^^^^^^^

error[E0080]: calling non-const function `integer_min::<i32>`
--> $DIR/do_not_const_check.rs:22:5

stack backtrace:

note: using internal features is not supported and expected to cause internal compiler errors when used incorrectly

note: rustc {version} running on {platform}

query stack during panic:
#0 [eval_to_allocation_raw] const-evaluating + checking `_`
#1 [eval_to_const_value_raw] simplifying constant for the type system `_`
#2 [eval_to_const_value_raw] simplifying constant for the type system `_`
#3 [analysis] running analysis passes on crate `do_not_const_check`
end of query stack
error: internal compiler error: compiler/rustc_const_eval/src/const_eval/machine.rs:LL:CC: calling non-const function `integer_min::<i32>`
--> $DIR/do_not_const_check.rs:34:5
|
LL | const _: () = {
LL | integer_min(0, 1);
| ^^^^^^^^^^^^^^^^^ evaluation of `_` failed here
| ^^^^^^^^^^^^^^^^^


stack backtrace:

note: using internal features is not supported and expected to cause internal compiler errors when used incorrectly

note: rustc {version} running on {platform}

query stack during panic:
#0 [eval_to_allocation_raw] const-evaluating + checking `_`
#1 [eval_to_const_value_raw] simplifying constant for the type system `_`
#2 [eval_to_const_value_raw] simplifying constant for the type system `_`
#3 [analysis] running analysis passes on crate `do_not_const_check`
end of query stack
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0080`.
42 changes: 23 additions & 19 deletions tests/ui/consts/qualif-indirect-mutation-fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,30 @@ pub const fn f() {
let _ = &mut a.1;
}

// Mutable borrow of a type with drop impl.
pub const A1: () = {
let mut x = None; //~ ERROR destructor of
let mut y = Some(NotConstDestruct);
let a = &mut x;
let b = &mut y;
std::mem::swap(a, b);
std::mem::forget(y);
}; //~ ERROR calling non-const function `<NotConstDestruct as Drop>::drop`
// FIXME: we make these associated consts to work around
// <https://github.com/rust-lang/rust/issues/163973>.
impl NotConstDestruct {
// Mutable borrow of a type with drop impl.
pub const A1: () = {
let mut x = None; //~ ERROR destructor of
let mut y = Some(NotConstDestruct);
let a = &mut x;
let b = &mut y;
std::mem::swap(a, b);
std::mem::forget(y);
};

// Mutable borrow of a type with drop impl.
pub const A2: () = {
let mut x = None;
let mut y = Some(NotConstDestruct);
let a = &mut x;
let b = &mut y;
std::mem::swap(a, b);
std::mem::forget(y);
let _z = x; //~ ERROR destructor of
}; //~ ERROR calling non-const function `<NotConstDestruct as Drop>::drop`
// Mutable borrow of a type with drop impl.
pub const A2: () = {
let mut x = None;
let mut y = Some(NotConstDestruct);
let a = &mut x;
let b = &mut y;
std::mem::swap(a, b);
std::mem::forget(y);
let _z = x; //~ ERROR destructor of
};
}

// Shared borrow of a type that might be !Freeze and Drop.
pub const fn g1<T>() {
Expand Down
77 changes: 25 additions & 52 deletions tests/ui/consts/qualif-indirect-mutation-fail.stderr
Original file line number Diff line number Diff line change
@@ -1,46 +1,3 @@
error[E0493]: destructor of `Option<NotConstDestruct>` cannot be evaluated at compile-time
--> $DIR/qualif-indirect-mutation-fail.rs:18:9
|
LL | let mut x = None;
| ^^^^^ the destructor for this type cannot be evaluated in constants
...
LL | };
| - value is dropped here

error[E0080]: calling non-const function `<NotConstDestruct as Drop>::drop`
--> $DIR/qualif-indirect-mutation-fail.rs:24:1
|
LL | pub const A1: () = {
...
LL | };
| ^ evaluation of `A1` failed inside this call
|
note: inside `std::ptr::drop_glue::<Option<NotConstDestruct>> - shim(Some(Option<NotConstDestruct>))`
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
note: inside `std::ptr::drop_glue::<NotConstDestruct> - shim(Some(NotConstDestruct))`
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL

error[E0493]: destructor of `Option<NotConstDestruct>` cannot be evaluated at compile-time
--> $DIR/qualif-indirect-mutation-fail.rs:34:9
|
LL | let _z = x;
| ^^ the destructor for this type cannot be evaluated in constants
LL | };
| - value is dropped here

error[E0080]: calling non-const function `<NotConstDestruct as Drop>::drop`
--> $DIR/qualif-indirect-mutation-fail.rs:35:1
|
LL | pub const A2: () = {
...
LL | };
| ^ evaluation of `A2` failed inside this call
|
note: inside `std::ptr::drop_glue::<Option<NotConstDestruct>> - shim(Some(Option<NotConstDestruct>))`
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
note: inside `std::ptr::drop_glue::<NotConstDestruct> - shim(Some(NotConstDestruct))`
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL

error[E0493]: destructor of `(u32, Option<NotConstDestruct>)` cannot be evaluated at compile-time
--> $DIR/qualif-indirect-mutation-fail.rs:12:9
|
Expand All @@ -50,8 +7,25 @@ LL | let _ = &mut a.1;
LL | }
| - value is dropped here

error[E0493]: destructor of `Option<NotConstDestruct>` cannot be evaluated at compile-time
--> $DIR/qualif-indirect-mutation-fail.rs:21:13
|
LL | let mut x = None;
| ^^^^^ the destructor for this type cannot be evaluated in constants
...
LL | };
| - value is dropped here

error[E0493]: destructor of `Option<NotConstDestruct>` cannot be evaluated at compile-time
--> $DIR/qualif-indirect-mutation-fail.rs:37:13
|
LL | let _z = x;
| ^^ the destructor for this type cannot be evaluated in constants
LL | };
| - value is dropped here

error[E0493]: destructor of `Option<T>` cannot be evaluated at compile-time
--> $DIR/qualif-indirect-mutation-fail.rs:39:9
--> $DIR/qualif-indirect-mutation-fail.rs:43:9
|
LL | let x: Option<T> = None;
| ^ the destructor for this type cannot be evaluated in constant functions
Expand All @@ -60,15 +34,15 @@ LL | }
| - value is dropped here

error[E0493]: destructor of `Option<T>` cannot be evaluated at compile-time
--> $DIR/qualif-indirect-mutation-fail.rs:47:9
--> $DIR/qualif-indirect-mutation-fail.rs:51:9
|
LL | let _y = x;
| ^^ the destructor for this type cannot be evaluated in constant functions
LL | }
| - value is dropped here

error[E0493]: destructor of `Option<NotConstDestruct>` cannot be evaluated at compile-time
--> $DIR/qualif-indirect-mutation-fail.rs:55:9
--> $DIR/qualif-indirect-mutation-fail.rs:59:9
|
LL | let mut y: Option<NotConstDestruct> = None;
| ^^^^^ the destructor for this type cannot be evaluated in constant functions
Expand All @@ -77,7 +51,7 @@ LL | }
| - value is dropped here

error[E0493]: destructor of `Option<NotConstDestruct>` cannot be evaluated at compile-time
--> $DIR/qualif-indirect-mutation-fail.rs:52:9
--> $DIR/qualif-indirect-mutation-fail.rs:56:9
|
LL | let mut x: Option<NotConstDestruct> = None;
| ^^^^^ the destructor for this type cannot be evaluated in constant functions
Expand All @@ -86,7 +60,7 @@ LL | }
| - value is dropped here

error[E0493]: destructor of `Option<NotConstDestruct>` cannot be evaluated at compile-time
--> $DIR/qualif-indirect-mutation-fail.rs:65:9
--> $DIR/qualif-indirect-mutation-fail.rs:69:9
|
LL | let y: Option<NotConstDestruct> = None;
| ^ the destructor for this type cannot be evaluated in constant functions
Expand All @@ -95,15 +69,14 @@ LL | }
| - value is dropped here

error[E0493]: destructor of `Option<NotConstDestruct>` cannot be evaluated at compile-time
--> $DIR/qualif-indirect-mutation-fail.rs:62:9
--> $DIR/qualif-indirect-mutation-fail.rs:66:9
|
LL | let x: Option<NotConstDestruct> = None;
| ^ the destructor for this type cannot be evaluated in constant functions
...
LL | }
| - value is dropped here

error: aborting due to 11 previous errors
error: aborting due to 9 previous errors

Some errors have detailed explanations: E0080, E0493.
For more information about an error, try `rustc --explain E0080`.
For more information about this error, try `rustc --explain E0493`.
Loading