Skip to content

Commit d94a57e

Browse files
committed
XXX: Use &'static Nonterminal in TokenKind::Interpolated.
1 parent 8771282 commit d94a57e

File tree

4 files changed

+153
-8
lines changed

4 files changed

+153
-8
lines changed

compiler/rustc_ast/src/mut_visit.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,10 @@ pub fn visit_token<T: MutVisitor>(t: &mut Token, vis: &mut T) {
756756
return; // Avoid visiting the span for the second time.
757757
}
758758
token::Interpolated(nt) => {
759-
visit_nonterminal(Lrc::make_mut(nt), vis);
759+
let mut nt2 = nt.clone();
760+
visit_nonterminal(&mut nt2, vis);
761+
// njn: skip this step if nt == nt2?
762+
*nt = Box::leak(Box::new(nt2));
760763
}
761764
_ => {}
762765
}

compiler/rustc_ast/src/token.rs

+143-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use crate::ptr::P;
88
use crate::util::case::Case;
99

1010
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
11-
use rustc_data_structures::sync::Lrc;
1211
use rustc_macros::HashStable_Generic;
12+
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
1313
use rustc_span::symbol::{kw, sym};
1414
#[allow(hidden_glob_reexports)]
1515
use rustc_span::symbol::{Ident, Symbol};
@@ -226,7 +226,23 @@ fn ident_can_begin_type(name: Symbol, span: Span, is_raw: bool) -> bool {
226226
.contains(&name)
227227
}
228228

229-
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
229+
// njn: comment
230+
// rustc_index::newtype_index! {
231+
// pub struct NtIndex {
232+
// ENCODABLE = custom
233+
// DEBUG_FORMAT = "NtIndex({})"
234+
// }
235+
// }
236+
237+
// njn: comment
238+
// njn: name?
239+
//#[derive(Clone, Copy)]
240+
//pub struct Nt(NtIndex);
241+
242+
//impl Nt {
243+
//}
244+
245+
#[derive(Clone, Copy, PartialEq, Debug, HashStable_Generic)]
230246
pub enum TokenKind {
231247
/* Expression-operator symbols. */
232248
Eq,
@@ -280,6 +296,7 @@ pub enum TokenKind {
280296
/// treat regular and interpolated lifetime identifiers in the same way.
281297
Lifetime(Symbol),
282298

299+
// njn: update comment
283300
/// An embedded AST node, as produced by a macro. This only exists for
284301
/// historical reasons. We'd like to get rid of it, for multiple reasons.
285302
/// - It's conceptually very strange. Saying a token can contain an AST
@@ -289,7 +306,8 @@ pub enum TokenKind {
289306
/// - It prevents `Token` from implementing `Copy`.
290307
/// It adds complexity and likely slows things down. Please don't add new
291308
/// occurrences of this token kind!
292-
Interpolated(Lrc<Nonterminal>),
309+
//Interpolated(Nt),
310+
Interpolated(&'static Nonterminal),
293311

294312
/// A doc comment token.
295313
/// `Symbol` is the doc comment's data excluding its "quotes" (`///`, `/**`, etc)
@@ -299,7 +317,128 @@ pub enum TokenKind {
299317
Eof,
300318
}
301319

302-
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
320+
// njn: necessary?
321+
impl<S: Encoder> Encodable<S> for TokenKind {
322+
fn encode(&self, s: &mut S) {
323+
match self {
324+
Eq => s.emit_usize(0),
325+
Lt => s.emit_usize(1),
326+
Le => s.emit_usize(2),
327+
EqEq => s.emit_usize(3),
328+
Ne => s.emit_usize(4),
329+
Ge => s.emit_usize(5),
330+
Gt => s.emit_usize(6),
331+
AndAnd => s.emit_usize(7),
332+
OrOr => s.emit_usize(8),
333+
Not => s.emit_usize(9),
334+
Tilde => s.emit_usize(10),
335+
BinOp(tok) => {
336+
s.emit_usize(11);
337+
tok.encode(s)
338+
}
339+
BinOpEq(tok) => {
340+
s.emit_usize(12);
341+
tok.encode(s)
342+
}
343+
At => s.emit_usize(13),
344+
Dot => s.emit_usize(14),
345+
DotDot => s.emit_usize(15),
346+
DotDotDot => s.emit_usize(16),
347+
DotDotEq => s.emit_usize(17),
348+
Comma => s.emit_usize(18),
349+
Semi => s.emit_usize(19),
350+
Colon => s.emit_usize(20),
351+
ModSep => s.emit_usize(21),
352+
RArrow => s.emit_usize(22),
353+
LArrow => s.emit_usize(23),
354+
FatArrow => s.emit_usize(24),
355+
Pound => s.emit_usize(25),
356+
Dollar => s.emit_usize(26),
357+
Question => s.emit_usize(27),
358+
SingleQuote => s.emit_usize(28),
359+
OpenDelim(delim) => {
360+
s.emit_usize(29);
361+
delim.encode(s)
362+
}
363+
CloseDelim(delim) => {
364+
s.emit_usize(30);
365+
delim.encode(s)
366+
}
367+
Literal(lit) => {
368+
s.emit_usize(31);
369+
lit.encode(s)
370+
}
371+
Ident(name, is_raw) => {
372+
s.emit_usize(32);
373+
name.encode(s);
374+
is_raw.encode(s)
375+
}
376+
Lifetime(name) => {
377+
s.emit_usize(33);
378+
name.encode(s)
379+
}
380+
Interpolated(_nt) => {
381+
s.emit_usize(34);
382+
panic!("njn: impossible?");
383+
}
384+
DocComment(kind, style, sym) => {
385+
s.emit_usize(35);
386+
kind.encode(s);
387+
style.encode(s);
388+
sym.encode(s)
389+
}
390+
Eof => s.emit_usize(36),
391+
}
392+
}
393+
}
394+
395+
// njn: necessary?
396+
impl<D: Decoder> Decodable<D> for TokenKind {
397+
fn decode(d: &mut D) -> TokenKind {
398+
match d.read_usize() {
399+
0 => Eq,
400+
1 => Lt,
401+
2 => Le,
402+
3 => EqEq,
403+
4 => Ne,
404+
5 => Ge,
405+
6 => Gt,
406+
7 => AndAnd,
407+
8 => OrOr,
408+
9 => Not,
409+
10 => Tilde,
410+
11 => BinOp(Decodable::decode(d)),
411+
12 => BinOpEq(Decodable::decode(d)),
412+
13 => At,
413+
14 => Dot,
414+
15 => DotDot,
415+
16 => DotDotDot,
416+
17 => DotDotEq,
417+
18 => Comma,
418+
19 => Semi,
419+
20 => Colon,
420+
21 => ModSep,
421+
22 => RArrow,
422+
23 => LArrow,
423+
24 => FatArrow,
424+
25 => Pound,
425+
26 => Dollar,
426+
27 => Question,
427+
28 => SingleQuote,
428+
29 => OpenDelim(Decodable::decode(d)),
429+
30 => CloseDelim(Decodable::decode(d)),
430+
31 => Literal(Decodable::decode(d)),
431+
32 => Ident(Decodable::decode(d), Decodable::decode(d)),
432+
33 => Lifetime(Decodable::decode(d)),
433+
34 => panic!("njn: unreachable?"),
434+
35 => DocComment(Decodable::decode(d), Decodable::decode(d), Decodable::decode(d)),
435+
36 => Eof,
436+
_ => panic!("njn: bad"),
437+
}
438+
}
439+
}
440+
441+
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
303442
pub struct Token {
304443
pub kind: TokenKind,
305444
pub span: Span,

compiler/rustc_expand/src/mbe/transcribe.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,11 @@ pub(super) fn transcribe<'a>(
228228
// `Delimiter::Invisible` to maintain parsing priorities.
229229
// `Interpolated` is currently used for such groups in rustc parser.
230230
marker.visit_span(&mut sp);
231-
let token = TokenTree::token_alone(token::Interpolated(nt.clone()), sp);
231+
let token = TokenTree::token_alone(
232+
// njn: nt.clone()?
233+
token::Interpolated(Box::leak(Box::new(nt.clone()))),
234+
sp,
235+
);
232236
result.push(token);
233237
}
234238
MatchedSeq(..) => {

compiler/rustc_expand/src/proc_macro.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_ast as ast;
66
use rustc_ast::ptr::P;
77
use rustc_ast::token;
88
use rustc_ast::tokenstream::TokenStream;
9-
use rustc_data_structures::sync::Lrc;
109
use rustc_errors::ErrorGuaranteed;
1110
use rustc_parse::parser::ForceCollect;
1211
use rustc_session::config::ProcMacroExecutionStrategy;
@@ -126,7 +125,7 @@ impl MultiItemModifier for DeriveProcMacro {
126125
Annotatable::Stmt(stmt) => token::NtStmt(stmt),
127126
_ => unreachable!(),
128127
};
129-
TokenStream::token_alone(token::Interpolated(Lrc::new(nt)), DUMMY_SP)
128+
TokenStream::token_alone(token::Interpolated(Box::leak(Box::new(nt))), DUMMY_SP)
130129
} else {
131130
item.to_tokens()
132131
};

0 commit comments

Comments
 (0)