Skip to content
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

tt-macro doesn't work #5846

Closed
jbclements opened this Issue Apr 12, 2013 · 13 comments

Comments

Projects
None yet
8 participants
@jbclements
Copy link
Contributor

jbclements commented Apr 12, 2013

I haven't finished investigating this, so I'm making an issue just so I don't forget it:

macro_rules! foo (
    ($x:tt) => {type t = ($x) int;})

foo!(~)
@jbclements

This comment has been minimized.

Copy link
Contributor Author

jbclements commented May 29, 2013

Actually, I'm really not sure this should work. I'm nominating this for well-defined.

@jbclements

This comment has been minimized.

Copy link
Contributor Author

jbclements commented May 30, 2013

Actually... this definitely shouldn't work in our current system. The basic issue is that a lonely sigil is not a complete "thing". The "tt" macro option is only intended for macros that expand into macro invocations. There are a number of ways around this; at the most basic level, you can insist that the user provide the sigil and the thing it's attached to. This works, but it may be unsatisfying if you want the sigil to be attached to names that are introduced by the macro. In this case, you have a couple of choices.

  • use an unhygienic macro (and tell everyone what name you want to attach to the sigil, ugh)
  • explicitly have three different versions of the macro, one for each sigil (bleah)
  • clean up the second option by nesting one macro definition inside another one
  • make sigils expand into macro invocations.

Anyhow, I would say that as of right now, at least, this is not a bug. I'm going to leave this as nominated mostly because this came up in Vancouver as something that Graydon and I both expected to work.

@huonw

This comment has been minimized.

Copy link
Member

huonw commented Jul 23, 2013

Visiting for triage: still doesn't work, nothing more to add.

@graydon

This comment has been minimized.

Copy link
Contributor

graydon commented Aug 22, 2013

accepted for well-defined milestone

@huonw

This comment has been minimized.

Copy link
Member

huonw commented Jan 31, 2014

You can work around this by using a dummy macro so that you can pass the tt directly to it, e.g. for the example in the issue:

#[feature(macro_rules)];

macro_rules! item { ($i: item) => { $i } }

macro_rules! foo (
    ($x:tt) => { item!(type t = $x int;) }
)

foo!(~)

fn main() {}
@jbclements

This comment has been minimized.

Copy link
Contributor Author

jbclements commented Jul 8, 2014

reading this workaround makes me feel like our macro system is really broken :(.

@steveklabnik

This comment has been minimized.

Copy link
Member

steveklabnik commented Feb 23, 2015

@kmcallister kmcallister added A-macros and removed A-syntaxext labels Feb 23, 2015

@kmcallister

This comment has been minimized.

Copy link
Contributor

kmcallister commented Mar 3, 2015

The modernized example

macro_rules! foo {
    ($x:tt) => (type t = $x<int>;)
}

foo!(Box);

gives me

/tmp/foo.rs:2:26: 2:28 error: expected type, found `an interpolated tt`
/tmp/foo.rs:2     ($x:tt) => (type t = $x<int>;)
                                       ^~

which looks like #22819.

@huonw

This comment has been minimized.

Copy link
Member

huonw commented Oct 27, 2015

#16036 notes that another hacky work-around is a nested macro invocation, which avoids needing to define/export dummy macros (although one will want to be careful about giving that internal macro a fairly unique name to avoid trampling on any other macros in scope, unfortunately 😞 ).

macro_rules! foo {
    ($x:tt) => (
        macro_rules! internal {
            () => { type t = $x<i8>; }
        }
        internal!();
    )
}

foo!(Box);

@brson brson added P-low and removed P-medium labels Jul 14, 2016

@steveklabnik

This comment has been minimized.

Copy link
Member

steveklabnik commented Jul 14, 2016

Updated example:

macro_rules! foo {
    ($x:tt) => (type t = $x<i32>;)
}

foo!(Box);

error:

error: expected type, found `Box`
 --> <anon>:2:26
2 |>     ($x:tt) => (type t = $x<i32>;)
  |>                          ^^
@brson

This comment has been minimized.

Copy link
Contributor

brson commented Jul 14, 2016

Triage: This is an ancient P-low bug but per @sanxiyn it actually comes up in the wild a lot - macro writers have to know how to do this workaround. Can this be escalated? Is there a solution somebody could mentor?

@nrc

This comment has been minimized.

Copy link
Member

nrc commented Jul 14, 2016

Discussed at lang meeting, we believe this should remain p-low since we don't know a very easy fix. It should be fixed by converting macro_rules to expand to pure tokens then parsing, rather than mixing the two steps (which we hop would be back-compat).

cc @jseyfried

@jseyfried

This comment has been minimized.

Copy link
Contributor

jseyfried commented Jul 18, 2016

Fixed in #34908.

bors added a commit that referenced this issue Jul 26, 2016

Auto merge of #34908 - jseyfried:improve_tt_matchers, r=nrc
macros: Improve `tt` matchers

Fixes #5846, fixes #22819.
r? @nrc

bors added a commit that referenced this issue Jul 26, 2016

Auto merge of #34908 - jseyfried:improve_tt_matchers, r=nrc
macros: Improve `tt` matchers

Fixes #5846, fixes #22819.
r? @nrc

bors added a commit that referenced this issue Jul 28, 2016

Auto merge of #34908 - jseyfried:improve_tt_matchers, r=nrc
macros: Improve `tt` matchers

Fixes #5846, fixes #22819.
r? @nrc

bors added a commit that referenced this issue Jul 28, 2016

Auto merge of #34908 - jseyfried:improve_tt_matchers, r=nrc
macros: Improve `tt` matchers

Fixes #5846, fixes #22819.
r? @nrc

bors added a commit that referenced this issue Jul 28, 2016

Auto merge of #34908 - jseyfried:improve_tt_matchers, r=nrc
macros: Improve `tt` matchers

Fixes #5846, fixes #22819.
r? @nrc

@bors bors closed this in #34908 Jul 28, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.