Skip to content

Update function pointer call error message #97102

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

Merged
merged 2 commits into from
May 17, 2022
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: 4 additions & 1 deletion compiler/rustc_const_eval/src/transform/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ impl<'tcx> NonConstOp<'tcx> for FnCallIndirect {
ccx: &ConstCx<'_, 'tcx>,
span: Span,
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
ccx.tcx.sess.struct_span_err(span, "function pointers are not allowed in const fn")
ccx.tcx.sess.struct_span_err(
span,
&format!("function pointer calls are not allowed in {}s", ccx.const_kind()),
)
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/test/ui/consts/const-fn-ptr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const fn make_fn_ptr() -> fn() {
|| {}
}

static STAT: () = make_fn_ptr()();
//~^ ERROR function pointer

const CONST: () = make_fn_ptr()();
//~^ ERROR function pointer

const fn call_ptr() {
make_fn_ptr()();
//~^ ERROR function pointer
}

fn main() {}
20 changes: 20 additions & 0 deletions src/test/ui/consts/const-fn-ptr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: function pointer calls are not allowed in statics
--> $DIR/const-fn-ptr.rs:5:19
|
LL | static STAT: () = make_fn_ptr()();
| ^^^^^^^^^^^^^^^

error: function pointer calls are not allowed in constants
--> $DIR/const-fn-ptr.rs:8:19
|
LL | const CONST: () = make_fn_ptr()();
| ^^^^^^^^^^^^^^^

error: function pointer calls are not allowed in constant functions
--> $DIR/const-fn-ptr.rs:12:5
|
LL | make_fn_ptr()();
| ^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors

2 changes: 1 addition & 1 deletion src/test/ui/consts/issue-56164.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | const fn foo() { (||{})() }
= note: closures need an RFC before allowed to be called in constant functions
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

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