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

Custom derive: can't use const as attribute value #52393

Open
srijs opened this issue Jul 15, 2018 · 4 comments
Open

Custom derive: can't use const as attribute value #52393

srijs opened this issue Jul 15, 2018 · 4 comments
Labels
A-attributes Area: #[attributes(..)] A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-proc-macros Area: Procedural macros C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@srijs
Copy link
Contributor

srijs commented Jul 15, 2018

I'm using serde to implement deserialization for a custom protocol. To keep the code dry, I've pulled all relevant strings into constants, but that creates a problem when trying to use the constants as attributes values in serde's derive macro.

Here's a playground with a minimal example to reproduce the problem: http://play.rust-lang.org/?gist=47230ae9671b24b312bef9540e520ddf&version=stable&mode=debug&edition=2015

The check that seems to produce the error is part of libsyntax:

let found = self.this_token_to_string();
let msg = format!("expected unsuffixed literal or identifier, found {}", found);
Err(self.diagnostic().struct_span_err(lo, &msg))

With my very basic knowledge of the rust compiler, I can see two potential ways this could be made possible:

  1. Run derive macros after the constants have been expanded. This would be transparent to the macros (they could treat the expanded constants as literals), however this might be a no-go due to the way that the passes are ordered within rustc?

  2. Relax the check in libsyntax (and possibly other places) to allow for identifiers to be valid attribute values on the syntax level, so that custom derive macros could use the identifier as part of their code gen (this wouldn't work for all macros, but at least for serde it looks like it'd be good enough).

If possible, could you give an indication as to whether this is something that you'd like in the compiler (in which case I'd be interested in working on a PR or RFC for it), or whether you think this is either impossible or undesirable for any reason?

@srijs srijs changed the title can't use const as attribute value Custom derive: can't use const as attribute value Jul 15, 2018
@jonas-schievink jonas-schievink added A-attributes Area: #[attributes(..)] A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) labels Mar 23, 2019
@JohnTitor JohnTitor added C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 10, 2019
@Aaron1011 Aaron1011 added the A-proc-macros Area: Procedural macros label May 21, 2020
@webern
Copy link

webern commented Aug 12, 2021

I'm surprised this issue doesn't have more upvotes. Is this issue represented somewhere else where it has more of a following? What I want to do is this:

const SOME_CONST: &str = "foobar";

#[some_derive_macro(name = SOME_CONST)]
struct Whatever {

}

@thomcc
Copy link
Member

thomcc commented Aug 12, 2021

Yeah, you can't put consts in attributes, but it would be very nice. I'm not sure if solving this requires an RFC, but it might.

EDIT: For proc macros, the macro could probably solve it in many cases, but would have to be changed.

@oldgalileo
Copy link

The include_str! macro seems related, but I'm curious if there's been any work unlinked here that enables the usage of a const _: &str in an attribute yet?

@glyh
Copy link

glyh commented Apr 11, 2024

Potential use case: pomsky + logos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: #[attributes(..)] A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-proc-macros Area: Procedural macros C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

8 participants