-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Error using quote_tokens: expected identifier, found keyword self
#18775
Comments
I also hit what I believe to be the same issue. The following code: (http://is.gd/Z0B3a3) #![feature(macro_rules)]
struct Foo { x: u32, y: u32 }
macro_rules! make_copy(
($Self:ident { $($field:ident),+ }) => (
fn copy(&self) -> $Self {
//let me = self;
$Self {
$($field: self.$field),+
}
}
)
)
impl Foo {
make_copy!(Foo {x, y})
}
fn main() {
let x = Foo { x: 0, y: 2 };
let _y = x.copy();
} Gives this error:
Enabling the commented out line and changing |
* fixed get_tt for doc comments * properly handle MatchNt in `quote` Fixes rust-lang#18763 Fixes rust-lang#18775
Please reopen this issue. The original error still occurs on current master. Error:
Rust version:
@yuriks Is your code working now? |
@hannobraun The test case I gave now works, but my original program, when changed back to not use the workaround, still doesn't. I'm going to have to create a new testcase. |
A complete working reproduction of this regression is below: #![feature(quote)]
extern crate syntax;
use syntax::print::pprust;
use syntax::codemap::DUMMY_SP;
use syntax::ast;
use syntax::ext::base::ExtCtxt;
use syntax::parse::token;
#[allow(unused_imports)]
fn main() {
let var_name = ast::Ident::new(
token::intern("my_var")
);
let collection = ast::Ident::new(
token::intern("my_collection")
);
with_fake_extctxt(|cx| {
let field_set = quote_tokens!(cx,
$var_name: self.$collection.pop(id),
);
println!("{}", pprust::tts_to_string(field_set.deref()));
});
}
fn with_fake_extctxt<T>(f: |&syntax::ext::base::ExtCtxt| -> T) -> T {
let ps = syntax::parse::new_parse_sess();
let mut cx = syntax::ext::base::ExtCtxt::new(&ps, Vec::new(), syntax::ext::expand::ExpansionConfig {
deriving_hash_type_parameter: false,
crate_name: from_str("test").unwrap(),
enable_quotes: true,
recursion_limit: 100
});
cx.bt_push(syntax::codemap::ExpnInfo{
call_site: DUMMY_SP,
callee: syntax::codemap::NameAndSpan {
name: "test".to_string(),
format: syntax::codemap::MacroBang,
span: None,
}
});
f(&cx)
} This currently works on http://play.rust-lang.org/ probably because the rustc version there is behind. It reproduces this bug in |
Thanks, @jakerr, for taking care of this. I meant to re-check and update my reduced example but didn't get to it so far. Things have been a bit crazy those last few weeks |
@hannobraun no worries! I got as far as finding what is wrong, but not as far as figuring out how to fix it. ...
} else {
// A nonterminal that matches or not
let namep = match p.token { token::Ident(_, p) => p, _ => token::Plain };
let name = p.parse_ident();
if p.token == token::Colon && p.look_ahead(1, |t| t.is_ident()) {
p.bump();
let kindp = match p.token { token::Ident(_, p) => p, _ => token::Plain };
let nt_kind = p.parse_ident();
let m = TtToken(sp, MatchNt(name, nt_kind, namep, kindp));
m
} else {
TtToken(sp, SubstNt(name, namep))
}
} There's code that looks for I think I'll leave it to the experts. @pczarn do you mind taking a look? |
The error appears when this code is looking ahead after a colon and finds an ident, but that ident is also a keyword. A solution could use |
True, but if you only eliminate the error for keywords you will get errors when trying to treat the ident after the colon as a matcher kind later right? For example quote_tokens!($foo: bar.baz) Would tokenize to MatchNt with name foo and kind bar which would eventually lead to a parse error in macro_parser.rs
Isn't it? Sorry for lack of good links, I'm mobile. |
That'd be good intuition, but the code in It seems I'll have to add |
Prevents breaking down `$name` tokens into separate `$` and `name`. Reports unknown macro variables. Fixes rust-lang#18775 Fixes rust-lang#18839 Fixes rust-lang#15640
Prevents breaking down `$name` tokens into separate `$` and `name`. Reports unknown macro variables. Fixes rust-lang#18775 Fixes rust-lang#18839 Fixes rust-lang#15640
Since upgrading to the latest nightly, I'm getting a compiler error for the following piece of code:
This code is available in the Rustecs repository: https://github.com/hannobraun/rustecs/blob/cd844f90b3073626e4252ff46781bed6ab0b337e/rustecs_macros/src/generate/intermediate.rs#L73
The error:
I was able to create this reduced example:
While there are many reasons this code should not compile, it fails with the same error message. If I
self
to something else or:
or$
in$var_name
the error goes away and the compile fails with a different error (as expected).
I can reproduce the issue with the latest nightly. I'm not sure about the exact version, since
rustc --version
only producesrustc 0.13.0-nightly
. I don't know which version broke it, but it must have been recent. The last successful Travis CI build was using0.13.0-nightly (45cbdec41 2014-11-07 00:02:18 +0000)
. See https://travis-ci.org/hannobraun/rustecs/builds/40199720More info about my system:
The text was updated successfully, but these errors were encountered: