Skip to content

Rust passes invalid syntax to procedural macros when expanding other macros #47358

Closed
@PlasmaPower

Description

@PlasmaPower
Contributor

Here's an example of the issue: https://play.rust-lang.org/?gist=1fcdc0a76f5c4dd0f2da855e497ea298&version=stable

In that code I use #[doc=$doc], which is expanded to invalid syntax #[doc=concat!("Hello", "world", "!")] before being passed to the serde Deserialize procedural macro.

This uses the same expansion as #42164

Activity

added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
A-decl-macros-2-0Area: Declarative macros 2.0 (#39412)
on Feb 6, 2018
abonander

abonander commented on Mar 8, 2018

@abonander
Contributor

I believe that this should be resolved by #48465.

eddyb

eddyb commented on Mar 26, 2018

@eddyb
Member
macro_rules! foo {
    ($x:expr) => {
        #[export_name = $x] pub fn foo() {}
    }
}

foo!(concat!("ma", "in"));

// Errors about `concat!` not being parseable.
// #[export_name = concat!("ma", "in")] pub fn foo() {}

fn main() {}

This showcases that we actually expand macro invocations within a NtExpr which was probably never intended. Are attributes the only place where we embed a TokenStream like that?

Should we just support macro invocations in attributes at the top-level and expanding them?
There's probably a good reason to avoid expanding macros nested arbitrarily just because it went through $x:expr in a macro_rules, but it needs some discussion.

cc @rust-lang/compiler @jseyfried @petrochenkov

durka

durka commented on Mar 26, 2018

@durka
Contributor

Look at that, eager expansion!

Should we just support macro invocations in attributes at the top-level and expanding them?

Yes please!

eddyb

eddyb commented on Mar 27, 2018

@eddyb
Member

@durka It's not eager! The invocation stays in there until macro expansion reaches it accidentally-ish.

7 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-decl-macros-1-2Area: Declarative macros 1.2A-decl-macros-2-0Area: Declarative macros 2.0 (#39412)A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-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.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @durka@alexcrichton@eddyb@nikomatsakis@pietroalbini

    Issue actions

      Rust passes invalid syntax to procedural macros when expanding other macros · Issue #47358 · rust-lang/rust