Skip to content

Commit 5fdc849

Browse files
authored
Rollup merge of #97102 - mbartlett21:fn-pointer-error, r=lcnr
Update function pointer call error message It now uses the type of context. (fixes #97082)
2 parents 519b6b4 + cdc12ed commit 5fdc849

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

Diff for: compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ impl<'tcx> NonConstOp<'tcx> for FnCallIndirect {
8989
ccx: &ConstCx<'_, 'tcx>,
9090
span: Span,
9191
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
92-
ccx.tcx.sess.struct_span_err(span, "function pointers are not allowed in const fn")
92+
ccx.tcx.sess.struct_span_err(
93+
span,
94+
&format!("function pointer calls are not allowed in {}s", ccx.const_kind()),
95+
)
9396
}
9497
}
9598

Diff for: src/test/ui/consts/const-fn-ptr.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const fn make_fn_ptr() -> fn() {
2+
|| {}
3+
}
4+
5+
static STAT: () = make_fn_ptr()();
6+
//~^ ERROR function pointer
7+
8+
const CONST: () = make_fn_ptr()();
9+
//~^ ERROR function pointer
10+
11+
const fn call_ptr() {
12+
make_fn_ptr()();
13+
//~^ ERROR function pointer
14+
}
15+
16+
fn main() {}

Diff for: src/test/ui/consts/const-fn-ptr.stderr

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: function pointer calls are not allowed in statics
2+
--> $DIR/const-fn-ptr.rs:5:19
3+
|
4+
LL | static STAT: () = make_fn_ptr()();
5+
| ^^^^^^^^^^^^^^^
6+
7+
error: function pointer calls are not allowed in constants
8+
--> $DIR/const-fn-ptr.rs:8:19
9+
|
10+
LL | const CONST: () = make_fn_ptr()();
11+
| ^^^^^^^^^^^^^^^
12+
13+
error: function pointer calls are not allowed in constant functions
14+
--> $DIR/const-fn-ptr.rs:12:5
15+
|
16+
LL | make_fn_ptr()();
17+
| ^^^^^^^^^^^^^^^
18+
19+
error: aborting due to 3 previous errors
20+

Diff for: src/test/ui/consts/issue-56164.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | const fn foo() { (||{})() }
77
= note: closures need an RFC before allowed to be called in constant functions
88
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
99

10-
error: function pointers are not allowed in const fn
10+
error: function pointer calls are not allowed in constant functions
1111
--> $DIR/issue-56164.rs:7:5
1212
|
1313
LL | input()

0 commit comments

Comments
 (0)