@@ -2,8 +2,9 @@ use crate::interface::{Compiler, Result};
22use crate :: proc_macro_decls;
33use crate :: util;
44
5- use rustc_ast:: mut_visit:: MutVisitor ;
6- use rustc_ast:: { self as ast, visit} ;
5+ use rustc_ast:: mut_visit:: { self , MutVisitor } ;
6+ use rustc_ast:: ptr:: P ;
7+ use rustc_ast:: { self as ast, token, visit} ;
78use rustc_codegen_ssa:: back:: link:: emit_metadata;
89use rustc_codegen_ssa:: traits:: CodegenBackend ;
910use rustc_data_structures:: sync:: { par_iter, Lrc , OnceCell , ParallelIterator , WorkerLocal } ;
@@ -36,6 +37,7 @@ use rustc_span::symbol::Symbol;
3637use rustc_span:: { FileName , RealFileName } ;
3738use rustc_trait_selection:: traits;
3839use rustc_typeck as typeck;
40+ use smallvec:: SmallVec ;
3941use tracing:: { info, warn} ;
4042
4143use rustc_serialize:: json;
@@ -50,6 +52,64 @@ use std::path::PathBuf;
5052use std:: rc:: Rc ;
5153use std:: { env, fs, iter, mem} ;
5254
55+ /// Remove alls `LazyTokenStreams` from an AST struct
56+ /// Normally, this is done during AST lowering. However,
57+ /// printing the AST JSON requires us to serialize
58+ /// the entire AST, and we don't want to serialize
59+ /// a `LazyTokenStream`.
60+ struct TokenStripper ;
61+ impl mut_visit:: MutVisitor for TokenStripper {
62+ fn flat_map_item ( & mut self , mut i : P < ast:: Item > ) -> SmallVec < [ P < ast:: Item > ; 1 ] > {
63+ i. tokens = None ;
64+ mut_visit:: noop_flat_map_item ( i, self )
65+ }
66+ fn visit_block ( & mut self , b : & mut P < ast:: Block > ) {
67+ b. tokens = None ;
68+ mut_visit:: noop_visit_block ( b, self ) ;
69+ }
70+ fn flat_map_stmt ( & mut self , mut stmt : ast:: Stmt ) -> SmallVec < [ ast:: Stmt ; 1 ] > {
71+ stmt. tokens = None ;
72+ mut_visit:: noop_flat_map_stmt ( stmt, self )
73+ }
74+ fn visit_pat ( & mut self , p : & mut P < ast:: Pat > ) {
75+ p. tokens = None ;
76+ mut_visit:: noop_visit_pat ( p, self ) ;
77+ }
78+ fn visit_ty ( & mut self , ty : & mut P < ast:: Ty > ) {
79+ ty. tokens = None ;
80+ mut_visit:: noop_visit_ty ( ty, self ) ;
81+ }
82+ fn visit_attribute ( & mut self , attr : & mut ast:: Attribute ) {
83+ attr. tokens = None ;
84+ if let ast:: AttrKind :: Normal ( ast:: AttrItem { tokens, .. } ) = & mut attr. kind {
85+ * tokens = None ;
86+ }
87+ mut_visit:: noop_visit_attribute ( attr, self ) ;
88+ }
89+
90+ fn visit_interpolated ( & mut self , nt : & mut token:: Nonterminal ) {
91+ if let token:: Nonterminal :: NtMeta ( meta) = nt {
92+ meta. tokens = None ;
93+ }
94+ // Handles all of the other cases
95+ mut_visit:: noop_visit_interpolated ( nt, self ) ;
96+ }
97+
98+ fn visit_path ( & mut self , p : & mut ast:: Path ) {
99+ p. tokens = None ;
100+ mut_visit:: noop_visit_path ( p, self ) ;
101+ }
102+ fn visit_vis ( & mut self , vis : & mut ast:: Visibility ) {
103+ vis. tokens = None ;
104+ mut_visit:: noop_visit_vis ( vis, self ) ;
105+ }
106+ fn visit_expr ( & mut self , e : & mut P < ast:: Expr > ) {
107+ e. tokens = None ;
108+ mut_visit:: noop_visit_expr ( e, self ) ;
109+ }
110+ fn visit_mac ( & mut self , _mac : & mut ast:: MacCall ) { }
111+ }
112+
53113pub fn parse < ' a > ( sess : & ' a Session , input : & Input ) -> PResult < ' a , ast:: Crate > {
54114 let krate = sess. time ( "parse_crate" , || match input {
55115 Input :: File ( file) => parse_crate_from_file ( file, & sess. parse_sess ) ,
@@ -59,6 +119,10 @@ pub fn parse<'a>(sess: &'a Session, input: &Input) -> PResult<'a, ast::Crate> {
59119 } ) ?;
60120
61121 if sess. opts . debugging_opts . ast_json_noexpand {
122+ // Set any `token` fields to `None` before
123+ // we display the AST.
124+ let mut krate = krate. clone ( ) ;
125+ TokenStripper . visit_crate ( & mut krate) ;
62126 println ! ( "{}" , json:: as_json( & krate) ) ;
63127 }
64128
@@ -379,6 +443,10 @@ fn configure_and_expand_inner<'a>(
379443 }
380444
381445 if sess. opts . debugging_opts . ast_json {
446+ // Set any `token` fields to `None` before
447+ // we display the AST.
448+ let mut krate = krate. clone ( ) ;
449+ TokenStripper . visit_crate ( & mut krate) ;
382450 println ! ( "{}" , json:: as_json( & krate) ) ;
383451 }
384452
0 commit comments