Skip to content

Leading :: semantics in macros depend on calling crate, not defining crate. #52848

Closed
@alercah

Description

@alercah

Easiest reproduction is to make a 2018 crate (note: rust_2018_preview is not sufficient) that calls the quote! macro:

#![feature(proc_macro_quote, proc_macro_non_items)]
use proc_macro::{quote, TokenStream}

#[proc_macro]
fn macro(input: TokenStream) -> TokenStream {
    quote! { $input }
}

This gives an error: can't find crate for `TokenStream` . Upon closer inspection, the offender is this line, and specifically, ::TokenStream::new().

The issue is that, even though hygiene dictates that this name should be looked up in the defining create, the semantics of leading :: are not accounted for. In the defining crate, in Rust 2015, it means "crate root". In the using crate, Rust 2018, it means "crate registrar" (i.e., the thing that holds crate names).

@petrochenkov, does this impact the stabilization of Span? I don't understand how hygeine works when a path has multiple pieces (identifiers or colons) from different caller/definer contexts. (Aside: I would prefer some elucidation for eventual documentation purposes.)

See also #44660. I don't think it impacts $crate at all, but I'm not sure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions