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)
This commit is contained in:
Yuki Okushi 2022-05-17 19:01:35 +09:00 committed by GitHub
commit 5fdc849bdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 2 deletions

View File

@ -89,7 +89,10 @@ fn build_error(
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()),
)
}
}

View File

@ -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() {}

View File

@ -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

View File

@ -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()