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

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

Closed
PlasmaPower opened this issue Jan 11, 2018 · 7 comments
Closed
Assignees
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-macros-1.2 Area: Declarative macros 1.2 A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@PlasmaPower
Copy link
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

@pietroalbini pietroalbini added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-macros-2.0 Area: Declarative macros 2.0 (#39412) labels Feb 6, 2018
@abonander
Copy link
Contributor

I believe that this should be resolved by #48465.

@eddyb
Copy link
Member

eddyb commented Mar 26, 2018

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
Copy link
Contributor

durka commented Mar 26, 2018

Look at that, eager expansion!

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

Yes please!

@eddyb
Copy link
Member

eddyb commented Mar 27, 2018

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

@alexcrichton alexcrichton added the A-macros-1.2 Area: Declarative macros 1.2 label May 22, 2018
@nikomatsakis
Copy link
Contributor

I'm going to remove the I-nominated tag. We're not prioritzing this, but I agree with @alexcrichton that this is something we ought to "figure out' as part of macros 1.2 etc.

@durka
Copy link
Contributor

durka commented Jun 21, 2018

Maybe change the title to "Rethink proc macro expansion order" or something?

@petrochenkov
Copy link
Contributor

I'll close this in favor of #55414 where the discussion regarding cases like this case is happening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-macros-1.2 Area: Declarative macros 1.2 A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants