Skip to content

Define ExpansionContext trait for the quote_foo! macros to use #12265

Closed
@pnkfelix

Description

@pnkfelix

The current quote_foo! macros (quote_expr!, quote_pat!, etc) all inject method calls on the first parameter to the macro invocation, which represents the expansion contexts.

The method calls are to methods like cfg (for getting the ast::CrateConfig) and ident_of (to map a string to an ast::Ident).

But there is no documentation for these methods, nor any standard trait defining how they should be packaged together to work with an invocation of quote_foo!. (The unit test shows examples of how you might do this; see e.g. the sample code I put on the description for #12264.)

Plus, when you omit the first argument, or plug in a random value for the first argument, the error message you get is less than ideal (since the compiler just complains about one method not existing, and this is one method among many).

Maybe I'm mistaken, but I think we can do better; in particular, if we make the macro expand into something that calls out to another function parameterized over <C:ExpansionContext> where the trait-bound defines all the necessarily methods that the macro(s) expect, then we will get much better error messages from the compiler, since the user will see that there is a particular trait that they need to implement on the first argument to the macro.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions