Skip to content

Commit 90ff38b

Browse files
committed
Point at the enclosing const context
``` error[E0015]: cannot call non-const associated function `Foo::{constant#0}::Foo::<17>::value` in constants --> $DIR/nested-type.rs:15:5 | LL | struct Foo<const N: [u8; { | __________________________- LL | | struct Foo<const N: usize>; LL | | LL | | impl<const N: usize> Foo<N> { ... | LL | | Foo::<17>::value() | | ^^^^^^^^^^^^^^^^^^ LL | | LL | | }]>; | |_- calls in constants are limited to constant functions, tuple structs and tuple variants ```
1 parent 8b89cb3 commit 90ff38b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+176
-95
lines changed

compiler/rustc_const_eval/src/check_consts/ops.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ fn build_error_for_const_call<'tcx>(
212212

213213
debug!(?call_kind);
214214

215+
let mut note = true;
215216
let mut err = match call_kind {
216217
CallKind::Normal { desugaring: Some((kind, self_ty)), .. } => {
217218
macro_rules! error {
@@ -365,6 +366,12 @@ fn build_error_for_const_call<'tcx>(
365366
kind: ccx.const_kind(),
366367
non_or_conditionally,
367368
});
369+
let context_span = ccx.tcx.def_span(ccx.def_id());
370+
err.span_label(context_span, format!(
371+
"calls in {}s are limited to constant functions, tuple structs and tuple variants",
372+
ccx.const_kind(),
373+
));
374+
note = false;
368375
let def_kind = ccx.tcx.def_kind(callee);
369376
if let DefKind::AssocTy | DefKind::AssocConst | DefKind::AssocFn = def_kind {
370377
let parent = ccx.tcx.parent(callee);
@@ -412,11 +419,12 @@ fn build_error_for_const_call<'tcx>(
412419
}
413420
};
414421

415-
err.note(format!(
416-
"calls in {}s are limited to constant functions, \
417-
tuple structs and tuple variants",
418-
ccx.const_kind(),
419-
));
422+
if note {
423+
err.note(format!(
424+
"calls in {}s are limited to constant functions, tuple structs and tuple variants",
425+
ccx.const_kind(),
426+
));
427+
}
420428

421429
err
422430
}

tests/ui/asm/non-const.stderr

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ error[E0015]: cannot call non-const function `non_const_fn` in constants
22
--> $DIR/non-const.rs:10:31
33
|
44
LL | global_asm!("/* {} */", const non_const_fn(0));
5-
| ^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^ calls in constants are limited to constant functions, tuple structs and tuple variants
66
|
77
note: function `non_const_fn` is not const
88
--> $DIR/non-const.rs:8:1
99
|
1010
LL | fn non_const_fn(x: i32) -> i32 { x }
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
1312

1413
error: aborting due to 1 previous error
1514

tests/ui/borrowck/issue-64453.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ error[E0015]: cannot call non-const function `format` in statics
22
--> $DIR/issue-64453.rs:4:31
33
|
44
LL | static settings_dir: String = format!("");
5-
| ^^^^^^^^^^^
5+
| --------------------------- ^^^^^^^^^^^
6+
| |
7+
| calls in statics are limited to constant functions, tuple structs and tuple variants
68
|
79
note: function `format` is not const
810
--> $SRC_DIR/alloc/src/fmt.rs:LL:COL
9-
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
1011
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
1112
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
1213

tests/ui/const-generics/nested-type.full.stderr

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
error[E0015]: cannot call non-const associated function `Foo::{constant#0}::Foo::<17>::value` in constants
22
--> $DIR/nested-type.rs:15:5
33
|
4-
LL | Foo::<17>::value()
5-
| ^^^^^^^^^^^^^^^^^^
6-
|
7-
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
4+
LL | struct Foo<const N: [u8; {
5+
| __________________________-
6+
LL | | struct Foo<const N: usize>;
7+
LL | |
8+
LL | | impl<const N: usize> Foo<N> {
9+
... |
10+
LL | | Foo::<17>::value()
11+
| | ^^^^^^^^^^^^^^^^^^
12+
LL | |
13+
LL | | }]>;
14+
| |_- calls in constants are limited to constant functions, tuple structs and tuple variants
815

916
error: aborting due to 1 previous error
1017

tests/ui/const-generics/nested-type.min.stderr

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
error[E0015]: cannot call non-const associated function `Foo::{constant#0}::Foo::<17>::value` in constants
22
--> $DIR/nested-type.rs:15:5
33
|
4-
LL | Foo::<17>::value()
5-
| ^^^^^^^^^^^^^^^^^^
6-
|
7-
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
4+
LL | struct Foo<const N: [u8; {
5+
| __________________________-
6+
LL | | struct Foo<const N: usize>;
7+
LL | |
8+
LL | | impl<const N: usize> Foo<N> {
9+
... |
10+
LL | | Foo::<17>::value()
11+
| | ^^^^^^^^^^^^^^^^^^
12+
LL | |
13+
LL | | }]>;
14+
| |_- calls in constants are limited to constant functions, tuple structs and tuple variants
815

916
error: `[u8; {
1017
struct Foo<const N: usize>;

tests/ui/consts/const-call.stderr

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ error[E0015]: cannot call non-const function `f` in constants
22
--> $DIR/const-call.rs:6:17
33
|
44
LL | let _ = [0; f(2)];
5-
| ^^^^
5+
| ^^^^ calls in constants are limited to constant functions, tuple structs and tuple variants
66
|
77
note: function `f` is not const
88
--> $DIR/const-call.rs:1:1
99
|
1010
LL | fn f(x: usize) -> usize {
1111
| ^^^^^^^^^^^^^^^^^^^^^^^
12-
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
1312

1413
error: aborting due to 1 previous error
1514

tests/ui/consts/const-eval/format.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ LL | println!("{:?}", 0);
1818
error[E0015]: cannot call non-const function `_print` in constant functions
1919
--> $DIR/format.rs:7:5
2020
|
21+
LL | const fn print() {
22+
| ---------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants
2123
LL | println!("{:?}", 0);
2224
| ^^^^^^^^^^^^^^^^^^^
2325
|
2426
note: function `_print` is not const
2527
--> $SRC_DIR/std/src/io/stdio.rs:LL:COL
26-
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
2728
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
2829

2930
error[E0015]: cannot call non-const formatting macro in constant functions

tests/ui/consts/const-extern-fn/const-extern-fn-call-extern-fn.stderr

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0015]: cannot call non-const function `regular_in_block` in constant functions
22
--> $DIR/const-extern-fn-call-extern-fn.rs:7:9
33
|
4+
LL | const extern "C" fn bar() {
5+
| ------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants
6+
LL | unsafe {
47
LL | regular_in_block();
58
| ^^^^^^^^^^^^^^^^^^
69
|
@@ -9,11 +12,13 @@ note: function `regular_in_block` is not const
912
|
1013
LL | fn regular_in_block();
1114
| ^^^^^^^^^^^^^^^^^^^^^^
12-
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
1315

1416
error[E0015]: cannot call non-const function `regular` in constant functions
1517
--> $DIR/const-extern-fn-call-extern-fn.rs:16:9
1618
|
19+
LL | const extern "C" fn foo() {
20+
| ------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants
21+
LL | unsafe {
1722
LL | regular();
1823
| ^^^^^^^^^
1924
|
@@ -22,7 +27,6 @@ note: function `regular` is not const
2227
|
2328
LL | extern "C" fn regular() {}
2429
| ^^^^^^^^^^^^^^^^^^^^^^^
25-
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
2630

2731
error: aborting due to 2 previous errors
2832

tests/ui/consts/const-fn-not-safe-for-const.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0015]: cannot call non-const function `random` in constant functions
22
--> $DIR/const-fn-not-safe-for-const.rs:14:5
33
|
4+
LL | const fn sub1() -> u32 {
5+
| ---------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants
46
LL | random()
57
| ^^^^^^^^
68
|
@@ -9,7 +11,6 @@ note: function `random` is not const
911
|
1012
LL | fn random() -> u32 {
1113
| ^^^^^^^^^^^^^^^^^^
12-
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
1314

1415
error: aborting due to 1 previous error
1516

tests/ui/consts/control-flow/issue-46843.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ error[E0015]: cannot call non-const function `non_const` in constants
22
--> $DIR/issue-46843.rs:10:26
33
|
44
LL | pub const Q: i32 = match non_const() {
5-
| ^^^^^^^^^^^
5+
| ---------------- ^^^^^^^^^^^
6+
| |
7+
| calls in constants are limited to constant functions, tuple structs and tuple variants
68
|
79
note: function `non_const` is not const
810
--> $DIR/issue-46843.rs:6:1
911
|
1012
LL | fn non_const() -> Thing {
1113
| ^^^^^^^^^^^^^^^^^^^^^^^
12-
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
1314

1415
error: aborting due to 1 previous error
1516

0 commit comments

Comments
 (0)