Skip to content

Empty TokenStream is provided to attribute procedural macro on trait alias #79825

Closed
@ghost

Description

I tried this code:

use proc_macro::TokenStream;

#[proc_macro_attribute]
pub fn give_me_trait_alias(_: TokenStream, input: TokenStream) -> TokenStream {
    assert!(!input.is_empty());
    Default::default()
}

and

#[give_me_trait_alias]
trait Alias = Sized;

I expected to see this happen: the compiler complained #![feature(trait_alias)] was missing, or on nightly, after adding #![feature(trait_alias)], compiled successfully, just like built-in #[allow] on trait alias.

Instead, this happened: the assertion failed. The compiler gave an empty TokenStream as input to the procedural macro:

error: custom attribute panicked
 --> tests/trait_alias.rs:3:1
  |
3 | #[give_me_trait_alias]
  | ^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: message: assertion failed: !input.is_empty()

Edit: I found this in the compiler's log:

INFO rustc_parse cached tokens found, but they're not "probably equal", going with stringified version
INFO rustc_parse cached   tokens: trait Alias = Sized ;
INFO rustc_parse reparsed tokens: 
INFO rustc_parse cached   tokens debug: TokenStream([(Token(Token { kind: Ident("trait", false), span: tests/trait_alias.rs:4:1: 4:6 (#0) }), Alone), (Token(Token { kind: Ident("Alias", false), span: tests/trait_alias.rs:4:7: 4:12 (#0) }), Alone), (Token(Token { kind: Eq, span: tests/trait_alias.rs:4:13: 4:14 (#0) }), Alone), (Token(Token { kind: Ident("Sized", false), span: tests/trait_alias.rs:4:15: 4:20 (#0) }), Joint), (Token(Token { kind: Semi, span: tests/trait_alias.rs:4:20: 4:21 (#0) }), Alone)])
INFO rustc_parse reparsed tokens debug: TokenStream([])

info!(
"cached tokens found, but they're not \"probably equal\", \
going with stringified version"
);

Meta

I tried this with:

cc #41517

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-proc-macrosArea: Procedural macrosC-bugCategory: This is a bug.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.F-trait_alias`#![feature(trait_alias)]`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

    No branches or pull requests

    Issue actions