Skip to content

Commit f6d7109

Browse files
authored
Rollup merge of #56007 - RalfJung:non-const-call, r=oli-obk
CTFE: dynamically make sure we do not call non-const-fn I'd love to have a test case for this, but I don't know how. I am also really surprised by this test case that changed behavior: Why did it even start execution if it already determined that it shouldn't?!? r? @oli-obk
2 parents 34ab7ce + 0c0478d commit f6d7109

File tree

9 files changed

+60
-11
lines changed

9 files changed

+60
-11
lines changed

src/librustc_mir/const_eval.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,19 @@ impl<'a, 'mir, 'tcx> interpret::Machine<'a, 'mir, 'tcx>
365365
ret: Option<mir::BasicBlock>,
366366
) -> EvalResult<'tcx, Option<&'mir mir::Mir<'tcx>>> {
367367
debug!("eval_fn_call: {:?}", instance);
368-
if !ecx.tcx.is_const_fn(instance.def_id()) {
368+
// Execution might have wandered off into other crates, so we cannot to a stability-
369+
// sensitive check here. But we can at least rule out functions that are not const
370+
// at all.
371+
if !ecx.tcx.is_const_fn_raw(instance.def_id()) {
369372
// Some functions we support even if they are non-const -- but avoid testing
370-
// that for const fn!
371-
if ecx.hook_fn(instance, args, dest)? {
373+
// that for const fn! We certainly do *not* want to actually call the fn
374+
// though, so be sure we return here.
375+
return if ecx.hook_fn(instance, args, dest)? {
372376
ecx.goto_block(ret)?; // fully evaluated and done
373-
return Ok(None);
374-
}
377+
Ok(None)
378+
} else {
379+
err!(MachineError(format!("calling non-const function `{}`", instance)))
380+
};
375381
}
376382
// This is a const fn. Call it.
377383
Ok(Some(match ecx.load_mir(instance.def) {

src/test/compile-fail/const-fn-error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const fn f(x: usize) -> usize {
1919
for i in 0..x {
2020
//~^ ERROR E0015
2121
//~| ERROR E0019
22+
//~| ERROR E0080
2223
sum += i;
2324
}
2425
sum

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ fn main() {
1414
[(); {while true {break}; 0}]; //~ ERROR constant contains unimplemented expression type
1515
[(); { for _ in 0usize.. {}; 0}]; //~ ERROR calls in constants are limited to constant functions
1616
//~^ ERROR constant contains unimplemented expression type
17+
//~| ERROR evaluation of constant value failed
1718
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ fn f(x: usize) -> usize {
1515
fn main() {
1616
let _ = [0; f(2)];
1717
//~^ ERROR calls in constants are limited to constant functions
18+
//~| ERROR evaluation of constant value failed
1819
}

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

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

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

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

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ impl Dim for Dim3 {
2323
fn main() {
2424
let array: [usize; Dim3::dim()]
2525
//~^ ERROR E0015
26+
//~| ERROR E0080
2627
= [0; Dim3::dim()];
2728
//~^ ERROR E0015
29+
//~| ERROR E0080
2830
}

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,25 @@ 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:24:24
9+
|
10+
LL | let array: [usize; Dim3::dim()]
11+
| ^^^^^^^^^^^ calling non-const function `<Dim3 as Dim>::dim`
12+
713
error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants
8-
--> $DIR/issue-39559-2.rs:26:15
14+
--> $DIR/issue-39559-2.rs:27:15
915
|
1016
LL | = [0; Dim3::dim()];
1117
| ^^^^^^^^^^^
1218

13-
error: aborting due to 2 previous errors
19+
error[E0080]: evaluation of constant value failed
20+
--> $DIR/issue-39559-2.rs:27: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
1426

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

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ fn xyz() -> u8 { 42 }
1212

1313
const NUM: u8 = xyz();
1414
//~^ ERROR calls in constants are limited to constant functions, tuple structs and tuple variants
15+
//~| ERROR any use of this value will cause an error [const_err]
1516

1617
fn main() {
1718
match 1 {
1819
NUM => unimplemented!(),
20+
//~^ ERROR could not evaluate constant pattern
1921
_ => unimplemented!(),
2022
}
2123
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ error[E0015]: calls in constants are limited to constant functions, tuple struct
44
LL | const NUM: u8 = xyz();
55
| ^^^^^
66

7-
error: aborting due to previous error
7+
error: any use of this value will cause an error
8+
--> $DIR/issue-43105.rs:13:1
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+
17+
error: could not evaluate constant pattern
18+
--> $DIR/issue-43105.rs:19:9
19+
|
20+
LL | NUM => unimplemented!(),
21+
| ^^^
22+
23+
error: aborting due to 3 previous errors
824

925
For more information about this error, try `rustc --explain E0015`.

0 commit comments

Comments
 (0)