Skip to content

How to determine hygienic context for the "crate root" in absolute-by-default paths #50376

Closed
@petrochenkov

Description

@petrochenkov

Procedural macros generate tokens, but sometimes the information is kept not in tokens, but rather in the lack of them (compare with #50130).

In particular, paths in use are resolved as absolute-by-default.
Effectively, they have one extra segment added at the start during name resolution

use a::b::c;

=>

use {{root}}::a::b::c;
// Or using the syntax from https://github.com/rust-lang/rust/issues/44660
use crate::a::b::c;

The added root segment has its hygienic context and may be resolved at definition site (similarly to use $crate::a::b::c from Macros 1.0) or at call site (similarly to use a::b::c in Macros 1.0).

The question is how to determine this context, given that the root segment doesn't have its own explicit token?
Without its own token the crate root needs to inherit context from some other token that really exists (compare with #50122), but what token it should be exactly is a pretty tough choice.

macro m() {
    use a::b::c; // ctxt(root) is most probably def-site because *everything* here has def-site context
    use a::$b; // ctxt(root) is probably def-site?
    use ::$a; // ctxt(root) is probably def-site??
    use $a; // ctxt(root) is ???
    $use_passed_from_the_outside a::b::c; // ctxt(root) is call-site??? should it be inherited from `use`?
    $use_passed_from_the_outside $a; // this is not even funny
}

I hope this issue doesn't affect Procedural Macros 1.2 (crossing my fingers).
Since all tokens generated and accepted by those macros are supposed to have the same call-site context, implicitly created crate roots will have the same call-site context anyway regardless of the token from which it's going to inherit it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-decl-macros-2-0Area: Declarative macros 2.0 (#39412)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

    No branches or pull requests

    Issue actions