Skip to content

Commit 37ea270

Browse files
committed
auto merge of #18811 : pczarn/rust/issue-18763-ice, r=pnkfelix
Fix ICEs introduced in #17830 * fixed get_tt for doc comments * properly handle MatchNt in `quote` Fixes #18763 Fixes #18775
2 parents 82f3838 + a320824 commit 37ea270

File tree

3 files changed

+53
-30
lines changed

3 files changed

+53
-30
lines changed

src/libsyntax/ast.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
// The Rust abstract syntax tree.
1212

13-
use codemap::{Span, Spanned, DUMMY_SP, ExpnId, respan};
13+
use codemap::{Span, Spanned, DUMMY_SP, ExpnId};
1414
use abi::Abi;
1515
use ast_util;
1616
use owned_slice::OwnedSlice;
@@ -783,13 +783,13 @@ impl TokenTree {
783783
TtToken(sp, token::Pound)
784784
}
785785
(&TtToken(sp, token::DocComment(name)), 1) => {
786-
let doc = MetaNameValue(token::intern_and_get_ident("doc"),
787-
respan(sp, LitStr(token::get_name(name), CookedStr)));
788-
let doc = token::NtMeta(P(respan(sp, doc)));
789786
TtDelimited(sp, Rc::new(Delimited {
790787
delim: token::Bracket,
791788
open_span: sp,
792-
tts: vec![TtToken(sp, token::Interpolated(doc))],
789+
tts: vec![TtToken(sp, token::Ident(token::str_to_ident("doc"),
790+
token::Plain)),
791+
TtToken(sp, token::Eq),
792+
TtToken(sp, token::LitStr(name))],
793793
close_span: sp,
794794
}))
795795
}

src/libsyntax/ext/quote.rs

+17-25
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use ptr::P;
2323
*
2424
* This is registered as a set of expression syntax extension called quote!
2525
* that lifts its argument token-tree to an AST representing the
26-
* construction of the same token tree, with ast::TtNonterminal nodes
27-
* interpreted as antiquotes (splices).
26+
* construction of the same token tree, with token::SubstNt interpreted
27+
* as antiquotes (splices).
2828
*
2929
*/
3030

@@ -616,20 +616,6 @@ fn mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> P<ast::Expr> {
616616
vec!(mk_name(cx, sp, ident.ident())));
617617
}
618618

619-
token::MatchNt(name, kind, name_style, kind_style) => {
620-
return cx.expr_call(sp,
621-
mk_token_path(cx, sp, "MatchNt"),
622-
vec![mk_ident(cx, sp, name),
623-
mk_ident(cx, sp, kind),
624-
match name_style {
625-
ModName => mk_token_path(cx, sp, "ModName"),
626-
Plain => mk_token_path(cx, sp, "Plain"),
627-
},
628-
match kind_style {
629-
ModName => mk_token_path(cx, sp, "ModName"),
630-
Plain => mk_token_path(cx, sp, "Plain"),
631-
}]);
632-
}
633619
token::Interpolated(_) => panic!("quote! with interpolated token"),
634620

635621
_ => ()
@@ -666,7 +652,7 @@ fn mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> P<ast::Expr> {
666652
mk_token_path(cx, sp, name)
667653
}
668654

669-
fn mk_tt(cx: &ExtCtxt, _: Span, tt: &ast::TokenTree) -> Vec<P<ast::Stmt>> {
655+
fn mk_tt(cx: &ExtCtxt, tt: &ast::TokenTree) -> Vec<P<ast::Stmt>> {
670656
match *tt {
671657
ast::TtToken(sp, SubstNt(ident, _)) => {
672658
// tt.extend($ident.to_tokens(ext_cx).into_iter())
@@ -687,6 +673,13 @@ fn mk_tt(cx: &ExtCtxt, _: Span, tt: &ast::TokenTree) -> Vec<P<ast::Stmt>> {
687673

688674
vec!(cx.stmt_expr(e_push))
689675
}
676+
ref tt @ ast::TtToken(_, MatchNt(..)) => {
677+
let mut seq = vec![];
678+
for i in range(0, tt.len()) {
679+
seq.push(tt.get_tt(i));
680+
}
681+
mk_tts(cx, seq.as_slice())
682+
}
690683
ast::TtToken(sp, ref tok) => {
691684
let e_sp = cx.expr_ident(sp, id_ext("_sp"));
692685
let e_tok = cx.expr_call(sp,
@@ -699,21 +692,20 @@ fn mk_tt(cx: &ExtCtxt, _: Span, tt: &ast::TokenTree) -> Vec<P<ast::Stmt>> {
699692
vec!(e_tok));
700693
vec!(cx.stmt_expr(e_push))
701694
},
702-
ast::TtDelimited(sp, ref delimed) => {
703-
mk_tt(cx, sp, &delimed.open_tt()).into_iter()
704-
.chain(delimed.tts.iter().flat_map(|tt| mk_tt(cx, sp, tt).into_iter()))
705-
.chain(mk_tt(cx, sp, &delimed.close_tt()).into_iter())
695+
ast::TtDelimited(_, ref delimed) => {
696+
mk_tt(cx, &delimed.open_tt()).into_iter()
697+
.chain(delimed.tts.iter().flat_map(|tt| mk_tt(cx, tt).into_iter()))
698+
.chain(mk_tt(cx, &delimed.close_tt()).into_iter())
706699
.collect()
707700
},
708701
ast::TtSequence(..) => panic!("TtSequence in quote!"),
709702
}
710703
}
711704

712-
fn mk_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::TokenTree])
713-
-> Vec<P<ast::Stmt>> {
705+
fn mk_tts(cx: &ExtCtxt, tts: &[ast::TokenTree]) -> Vec<P<ast::Stmt>> {
714706
let mut ss = Vec::new();
715707
for tt in tts.iter() {
716-
ss.extend(mk_tt(cx, sp, tt).into_iter());
708+
ss.extend(mk_tt(cx, tt).into_iter());
717709
}
718710
ss
719711
}
@@ -775,7 +767,7 @@ fn expand_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::TokenTree])
775767
let stmt_let_tt = cx.stmt_let(sp, true, id_ext("tt"), cx.expr_vec_ng(sp));
776768

777769
let mut vector = vec!(stmt_let_sp, stmt_let_tt);
778-
vector.extend(mk_tts(cx, sp, tts.as_slice()).into_iter());
770+
vector.extend(mk_tts(cx, tts.as_slice()).into_iter());
779771
let block = cx.expr_block(
780772
cx.block_all(sp,
781773
Vec::new(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-android
12+
// ignore-pretty: does not work well with `--test`
13+
14+
#![feature(quote)]
15+
16+
extern crate syntax;
17+
18+
use syntax::ext::base::ExtCtxt;
19+
20+
fn syntax_extension(cx: &ExtCtxt) {
21+
let _toks_1 = vec![quote_tokens!(cx, /** comment */ fn foo() {})];
22+
let name = quote_tokens!(cx, bar);
23+
let _toks_2 = vec![quote_item!(cx, static $name:int = 2;)];
24+
let _toks_3 = vec![quote_item!(cx,
25+
/// comment
26+
fn foo() { let $name:int = 3; }
27+
)];
28+
}
29+
30+
fn main() {
31+
}

0 commit comments

Comments
 (0)