Skip to content

rust complains about "const fn" which was never declared as such #97082

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

Closed
matthiaskrgr opened this issue May 16, 2022 · 2 comments · Fixed by #97102
Closed

rust complains about "const fn" which was never declared as such #97082

matthiaskrgr opened this issue May 16, 2022 · 2 comments · Fixed by #97102
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented May 16, 2022

Given the following code:

fn f() { dbg!(); }
static A: fn() = f;
static B: () = A();

fn main() {
    println!("Hello, world!");
    B
}

The current output is:

error: function pointers are not allowed in const fn
 --> src/main.rs:3:16
  |
3 | static B: () = A();
  |                ^^^

error: could not compile `f` due to previous error

IMO it's a bit weird that rust tells me about a "const fn" since there is none in the code snippet...? 😅

@matthiaskrgr matthiaskrgr added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 16, 2022
@tmiasko tmiasko added the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label May 16, 2022
@mbartlett21
Copy link
Contributor

@rustbot claim

Here's the relevant code:

pub struct FnCallIndirect;
impl<'tcx> NonConstOp<'tcx> for FnCallIndirect {
fn build_error(
&self,
ccx: &ConstCx<'_, 'tcx>,
span: Span,
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
ccx.tcx.sess.struct_span_err(span, "function pointers are not allowed in const fn")
}
}

But then saying that function pointers aren't allowed in statics is just false, since there is a function pointer assigned to A.

@mbartlett21
Copy link
Contributor

Note: The error message is now "function pointer calls are not allowed in statics"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants