Skip to content

quote_expr, quote_tokens, etc. should not require an "ext_cx" to be in scope -- HOW TO FIX THEM #7727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jbclements opened this issue Jul 11, 2013 · 4 comments
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-syntaxext Area: Syntax extensions C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@jbclements
Copy link
Contributor

There are a number of problems with our current quote_expr!, quote_tokens!, etc. macros. The first one is that they're cloaking/unhygienic. Fixing this is easy--just change the macro definition so that you have to supply an identifier as the first argument.

The second problem is that they really shouldn't work in the way that they do.

Specifically, these macros currently expand into ASTs that represent calls to the parser, thereby generating an AST. This discards all context/hygiene information, and is also inefficient, since you already have the AST that you're trying to protect.

Languages such as Racket just provide a language primitive called "quote-syntax" whose evaluation rule is literally the trivial one--a use of quote-syntax simply evaluates to the AST on the right hand side of the call.

What would it take to add this to Rust?

  1. You'd need a new AST node called (say) quote_expr.
  2. The typechecker would have to know that any quote_ast node trivially has the type ast::expr -- no checking required.

This should be less than a day of work for someone who really understands the system well. One question is whether you want to duplicate this work for each of the quote_... thingies, or make one that covers them all, with some kind of simple enum.

It appears to me that it would also allow us to throw out a huge amount of code--all of the quoting stuff that's in there currently.

@huonw
Copy link
Member

huonw commented Jul 26, 2013

It seems like this might lend itself to a solution for two problems: quoting and macros being only able to expand to expressions or (one) item (e.g. #4621). Specifically, something like:

enum SyntaxWrapper<T> {
    Plain(T),
    Quote(T),
    MacroInvocation(Mac)
}

type Expr = SyntaxWrapper<Exp_r>;
type Item = SyntaxWrapper<Item_>;
type Method = SyntaxWrapper<Method_>;
type Ty = SyntaxWrapper<Ty_>;
// ...

(It'd be nice to have GADTs to be able to express that MacroInvocation is impossible after expansion in the types, but such is life.)

However, I would imagine that this would have a big negative effect on downstream code, and we appear to be moving away from the spanned wrappers too, so this pattern may be deprecated.

@jbclements
Copy link
Contributor Author

Since issues are forever, I should add that @graydon indicates that Rust used to have something like this, but that as he quite correctly points out, it doesn't solve quasiquoting. This is true; quasiquoting highlights the value of simplicity in ASTs. So, I still think adding a quoted-syntax AST node makes sense, but I see that you still need a nice way to mix computed and quoted elements to get quasiquote.

sfackler added a commit to sfackler/rust that referenced this issue Aug 18, 2013
They previously required one called "ext_cx" to be in scope.

Fixes part of rust-lang#7727
bors added a commit that referenced this issue Aug 19, 2013
They previously required one called "ext_cx" to be in scope.

Fixes part of #7727
@steveklabnik steveklabnik added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label Jan 21, 2015
@steveklabnik
Copy link
Member

I'm not sure what the situation is today. @kmcallister ?

@steveklabnik
Copy link
Member

Triage: with no updates since 2013, and big changes planned to the syntax extension system, i'm going to give this one a close.

flip1995 pushed a commit to flip1995/rust that referenced this issue Sep 28, 2021
Update changelog to 1.56

As expected a pretty short changelog, because of the missed sync last release cycle.

[Rendered](https://github.com/flip1995/rust-clippy/blob/changelog/CHANGELOG.md)

changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-syntaxext Area: Syntax extensions C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

3 participants