Skip to content

Provide more helpful error message when trying to use const generic defaults #80507

Closed
@XAMPPRocky

Description

@XAMPPRocky

Defaults are currently not implemented in const generics. With min_const_generics stabilising soon, I've been using them more and one thing that is surprising is the lack of defaults. Especially when you try to declare a default, the error message provides a unexpected syntax error, when I would expect a "defaults in const generics is not implemented" error, leading you to think that there's another syntax that you need to use as opposed to it just not being available.

#![feature(min_const_generics)]

fn foo<const NUM: usize = 0>() -> usize {
    NUM
}

Error Message

error: expected one of `!`, `(`, `+`, `,`, `::`, `<`, or `>`, found `=`
 --> src/lib.rs:3:25
  |
3 | fn foo<const NUM: usize = 0>() -> usize {
  |                         ^ expected one of 7 possible tokens

error: aborting due to previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.const-generics-bad-diagnosticsAn error is correctly emitted, but is confusing, for `min_const_generics`.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions