Skip to content

Lifetimes in procedural macros aren't being parsed correctly #50942

Description

@alexcrichton

Given this procedural macro:

#![crate_type = "proc-macro"]
#![feature(proc_macro)]

extern crate proc_macro;

use proc_macro::*;

#[proc_macro]
pub fn bar(_input: TokenStream) -> TokenStream {
    let mut ret = Vec::<TokenTree>::new();
    ret.push(Ident::new("static", Span::call_site()).into());
    ret.push(Ident::new("FOO", Span::call_site()).into());
    ret.push(Punct::new(':', Spacing::Alone).into());
    ret.push(Punct::new('&', Spacing::Alone).into());
    ret.push(Punct::new('\'', Spacing::Joint).into());
    ret.push(Ident::new("static", Span::call_site()).into());
    ret.push(Ident::new("i32", Span::call_site()).into());
    ret.push(Punct::new('=', Spacing::Alone).into());
    ret.push(Punct::new('&', Spacing::Alone).into());
    ret.push(Literal::i32_unsuffixed(1).into());
    ret.push(Punct::new(';', Spacing::Alone).into());
    return ret.into_iter().collect()
}

and this invocation:

#![crate_type = "rlib"]
#![feature(proc_macro)]

extern crate bar;

bar::bar!();

we get:

$ rustc +nightly bar.rs
$ rustc +nightly foo.rs -L .
error: lifetimes cannot use keyword names
 --> foo.rs:6:1
  |
6 | bar::bar!();
  | ^^^^^^^^^^^^

error[E0261]: use of undeclared lifetime name `static`
 --> foo.rs:6:1
  |
6 | bar::bar!();
  | ^^^^^^^^^^^^ undeclared lifetime

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0261`.

Activity

  1. petrochenkov commented on May 21, 2018

    @petrochenkov
    Contributor

    Note the missing ' in "undeclared lifetime name static", that's why it's undeclared.

  2. petrochenkov commented on May 21, 2018

    @petrochenkov
    Contributor

    Oh crap, fn glue doesn't add ' to the identifier string.
    My test with lifetimes in #50473 passed accidentally because all the lifetimes lost their 's so their names were still equal and resolution succeeded.

  3. petrochenkov commented on May 21, 2018

    @petrochenkov
    Contributor

    Sorry.

  4. alexcrichton commented on May 21, 2018

    @alexcrichton
    MemberAuthor

    Aha that'd do it! You found it much faster than I did :)

    And no worries! Would you like to send a PR? Or shall I?

  5. petrochenkov commented on May 21, 2018

    @petrochenkov
    Contributor

    It'll happen sooner if you do it.

  6. alexcrichton commented on May 21, 2018

    @alexcrichton
    MemberAuthor

    Ok! I'll take care of this

  7. added a commit that references this issue on May 21, 2018
    3b8f791
  8. alexcrichton commented on May 21, 2018

    @alexcrichton
    MemberAuthor

    I've posted a fix for this at #50946

  9. added a commit that references this issue on May 22, 2018
    aa63dce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-decl-macros-2-0Area: Declarative macros 2.0 (#39412)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions