Skip to content
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

Suggest surrounding with braces when using a const function with arguments with const generics. #91020

Closed
hollmmax opened this issue Nov 18, 2021 · 3 comments · Fixed by #94731
Assignees
Labels
A-const-generics Area: const generics (parameters and arguments) 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.

Comments

@hollmmax
Copy link

hollmmax commented Nov 18, 2021

Given the following code: playground

fn foo<const N: i32>() -> i32 {
    N
}

const fn bar() -> i32 {
    1
}

const fn baz(n: i32) -> i32 {
    n
}

pub fn main() {
    foo::<bar()>();
    foo::<baz(1)>();
}

The current output is:

error: expected type, found `1`
  --> src/main.rs:15:15
   |
15 |     foo::<baz(1)>();
   |               ^ expected type

error[E0573]: expected type, found function `bar`
  --> src/main.rs:14:11
   |
14 |     foo::<bar()>();
   |           ^^^^^ not a type

error[E0747]: unresolved item provided when a constant was expected
  --> src/main.rs:14:11
   |
14 |     foo::<bar()>();
   |           ^^^^^
   |
help: if this generic argument was intended as a const parameter, surround it with braces
   |
14 |     foo::<{ bar() }>();
   |           +       +

Some errors have detailed explanations: E0573, E0747.
For more information about an error, try `rustc --explain E0573`.

Ideally the output should also suggest adding braces to foo::<baz(1)>()

@hollmmax hollmmax 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 Nov 18, 2021
@BoxyUwU BoxyUwU added the A-const-generics Area: const generics (parameters and arguments) label Nov 18, 2021
@Noratrieb
Copy link
Member

@rustbot claim

@Noratrieb
Copy link
Member

@rustbot release-assignment

@Noratrieb
Copy link
Member

I don't have enough time for that right now, someone else can do it

But some infos I was able to find:

For the call with no params, that error comes from rustc_resolve, since bar() is successfully parsed as a Type.

The second case gets rejected in the parser, because it's not a valid Type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) 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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants