1
1
//@ run-pass
2
2
//@ ignore-cross-compile
3
+ //@ aux-crate: parser=parser.rs
4
+ //@ edition: 2021
3
5
4
6
// This test covers the AST pretty-printer's automatic insertion of parentheses
5
7
// into unparenthesized syntax trees according to precedence and various grammar
31
33
32
34
extern crate rustc_ast;
33
35
extern crate rustc_ast_pretty;
34
- extern crate rustc_driver;
35
- extern crate rustc_errors;
36
36
extern crate rustc_parse;
37
37
extern crate rustc_session;
38
38
extern crate rustc_span;
39
39
40
40
use std:: mem;
41
41
use std:: process:: ExitCode ;
42
42
43
- use rustc_ast:: ast:: { DUMMY_NODE_ID , Expr , ExprKind } ;
43
+ use parser:: parse_expr;
44
+ use rustc_ast:: ast:: { Expr , ExprKind } ;
44
45
use rustc_ast:: mut_visit:: { self , DummyAstNode as _, MutVisitor } ;
45
- use rustc_ast:: node_id:: NodeId ;
46
46
use rustc_ast:: ptr:: P ;
47
47
use rustc_ast_pretty:: pprust;
48
- use rustc_errors:: Diag ;
49
- use rustc_parse:: parser:: Recovery ;
50
48
use rustc_session:: parse:: ParseSess ;
51
- use rustc_span:: { DUMMY_SP , FileName , Span } ;
52
49
53
50
// Every parenthesis in the following expressions is re-inserted by the
54
51
// pretty-printer.
@@ -156,34 +153,6 @@ impl MutVisitor for Unparenthesize {
156
153
}
157
154
}
158
155
159
- // Erase Span information that could distinguish between identical expressions
160
- // parsed from different source strings.
161
- struct Normalize ;
162
-
163
- impl MutVisitor for Normalize {
164
- const VISIT_TOKENS : bool = true ;
165
-
166
- fn visit_id ( & mut self , id : & mut NodeId ) {
167
- * id = DUMMY_NODE_ID ;
168
- }
169
-
170
- fn visit_span ( & mut self , span : & mut Span ) {
171
- * span = DUMMY_SP ;
172
- }
173
- }
174
-
175
- fn parse_expr ( psess : & ParseSess , source_code : & str ) -> Option < P < Expr > > {
176
- let parser = rustc_parse:: unwrap_or_emit_fatal ( rustc_parse:: new_parser_from_source_str (
177
- psess,
178
- FileName :: anon_source_code ( source_code) ,
179
- source_code. to_owned ( ) ,
180
- ) ) ;
181
-
182
- let mut expr = parser. recovery ( Recovery :: Forbidden ) . parse_expr ( ) . map_err ( Diag :: cancel) . ok ( ) ?;
183
- Normalize . visit_expr ( & mut expr) ;
184
- Some ( expr)
185
- }
186
-
187
156
fn main ( ) -> ExitCode {
188
157
let mut status = ExitCode :: SUCCESS ;
189
158
let mut fail = |description : & str , before : & str , after : & str | {
@@ -199,7 +168,9 @@ fn main() -> ExitCode {
199
168
let psess = & ParseSess :: new ( vec ! [ rustc_parse:: DEFAULT_LOCALE_RESOURCE ] ) ;
200
169
201
170
for & source_code in EXPRS {
202
- let expr = parse_expr ( psess, source_code) . unwrap ( ) ;
171
+ let Some ( expr) = parse_expr ( psess, source_code) else {
172
+ panic ! ( "Failed to parse original test case: {source_code}" ) ;
173
+ } ;
203
174
204
175
// Check for FALSE POSITIVE: pretty-printer inserting parentheses where not needed.
205
176
// Pseudocode:
0 commit comments