@@ -2,8 +2,9 @@ use crate::interface::{Compiler, Result};
2
2
use crate :: proc_macro_decls;
3
3
use crate :: util;
4
4
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} ;
7
8
use rustc_codegen_ssa:: back:: link:: emit_metadata;
8
9
use rustc_codegen_ssa:: traits:: CodegenBackend ;
9
10
use rustc_data_structures:: sync:: { par_iter, Lrc , OnceCell , ParallelIterator , WorkerLocal } ;
@@ -36,6 +37,7 @@ use rustc_span::symbol::Symbol;
36
37
use rustc_span:: { FileName , RealFileName } ;
37
38
use rustc_trait_selection:: traits;
38
39
use rustc_typeck as typeck;
40
+ use smallvec:: SmallVec ;
39
41
use tracing:: { info, warn} ;
40
42
41
43
use rustc_serialize:: json;
@@ -50,6 +52,64 @@ use std::path::PathBuf;
50
52
use std:: rc:: Rc ;
51
53
use std:: { env, fs, iter, mem} ;
52
54
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
+
53
113
pub fn parse < ' a > ( sess : & ' a Session , input : & Input ) -> PResult < ' a , ast:: Crate > {
54
114
let krate = sess. time ( "parse_crate" , || match input {
55
115
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> {
59
119
} ) ?;
60
120
61
121
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) ;
62
126
println ! ( "{}" , json:: as_json( & krate) ) ;
63
127
}
64
128
@@ -379,6 +443,10 @@ fn configure_and_expand_inner<'a>(
379
443
}
380
444
381
445
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) ;
382
450
println ! ( "{}" , json:: as_json( & krate) ) ;
383
451
}
384
452
0 commit comments