Skip to content

Reference is incorrect about macro expanding expressions in builtin attributes #1806

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

Open
mejrs opened this issue Apr 30, 2025 · 0 comments
Open
Labels
A-attributes Area: Attributes A-macros Area: Macros

Comments

@mejrs
Copy link
Contributor

mejrs commented Apr 30, 2025

See https://doc.rust-lang.org/nightly/reference/attributes.html#r-attributes.meta.literal-expr

Syntax
MetaItem :
      [SimplePath]
   | [SimplePath] = [Expression]
   | [SimplePath] ( MetaSeq? )

MetaSeq :
   MetaItemInner ( , MetaItemInner )* ,?

MetaItemInner :
      MetaItem
   | [Expression]

Expressions in meta items must macro-expand to literal expressions, which must not
include integer or float type suffixes. Expressions which are not literal expressions
will be syntactically accepted (and can be passed to proc-macros), but will be rejected after parsing.

To me this implies that #[attr(name= literal_expr!())]is valid, but it is not. It appears that macro expanding expressions in attributes only happens for #[attr = literal_expr!()].

For example (to take a random attribute that accepts multiple flavors of metaitem syntax):

#![feature(rustc_attrs)]
#![allow(internal_features)]

#[rustc_on_unimplemented = concat!("the", " ", "message")]
pub trait A {}

#[rustc_on_unimplemented(message = concat!("the", " ", "message"))]
pub trait B {}

Results in:

error: expected unsuffixed literal, found `concat`
 --> src/lib.rs:7:36
  |
7 | #[rustc_on_unimplemented(message = concat!("the", " ", "message"))]
  |                                    ^^^^^^
  |
help: surround the identifier with quotation marks to make it into a string literal
  |
7 | #[rustc_on_unimplemented(message = "concat"!("the", " ", "message"))]
  |                                    +      +

error: could not compile `playground` (lib) due to 1 previous error
@ehuss ehuss added A-attributes Area: Attributes A-macros Area: Macros labels Apr 30, 2025
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: Macros
Projects
None yet
Development

No branches or pull requests

2 participants