Skip to content

Derive fails when using non-literal const generic default #2449

Closed
dtolnay/syn
#1483
@cmyr

Description

@cmyr

I imagine this is simplest with an illustration. The following code does not compile for me:

const MY_DEFAULT: usize = 4;

#[derive(serde::Deserialize)]
struct MyStruct<const N: usize = MY_DEFAULT>(bool);

but the following seems to be fine:

#[derive(serde::Deserialize)]
struct MyStruct<const N: usize = 4>(bool);

The error I see in the first case is that MY_DEFAULT is an unexpected token. I poked around the derive source here for a few minutes and didn't see anything jump out at me (it looks like syn parses this as an expression, not a literal, so it should at least parse?) but it looks like something isn't right.

If anyone has any insight, that would be much appreciated!

rustc 1.69.0 (84c898d65 2023-04-16)
serde v1.0.162

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions