11//@ run-pass
22//@ ignore-cross-compile
3+ //@ aux-crate: parser=parser.rs
4+ //@ edition: 2021
35
46// This test covers the AST pretty-printer's automatic insertion of parentheses
57// into unparenthesized syntax trees according to precedence and various grammar
3133
3234extern crate rustc_ast;
3335extern crate rustc_ast_pretty;
34- extern crate rustc_driver;
35- extern crate rustc_errors;
3636extern crate rustc_parse;
3737extern crate rustc_session;
3838extern crate rustc_span;
3939
4040use std:: mem;
4141use std:: process:: ExitCode ;
4242
43- use rustc_ast:: ast:: { DUMMY_NODE_ID , Expr , ExprKind } ;
43+ use parser:: parse_expr;
44+ use rustc_ast:: ast:: { Expr , ExprKind } ;
4445use rustc_ast:: mut_visit:: { self , DummyAstNode as _, MutVisitor } ;
45- use rustc_ast:: node_id:: NodeId ;
4646use rustc_ast:: ptr:: P ;
4747use rustc_ast_pretty:: pprust;
48- use rustc_errors:: Diag ;
49- use rustc_parse:: parser:: Recovery ;
5048use rustc_session:: parse:: ParseSess ;
51- use rustc_span:: { DUMMY_SP , FileName , Span } ;
5249
5350// Every parenthesis in the following expressions is re-inserted by the
5451// pretty-printer.
@@ -156,34 +153,6 @@ impl MutVisitor for Unparenthesize {
156153 }
157154}
158155
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-
187156fn main ( ) -> ExitCode {
188157 let mut status = ExitCode :: SUCCESS ;
189158 let mut fail = |description : & str , before : & str , after : & str | {
@@ -199,7 +168,9 @@ fn main() -> ExitCode {
199168 let psess = & ParseSess :: new ( vec ! [ rustc_parse:: DEFAULT_LOCALE_RESOURCE ] ) ;
200169
201170 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+ } ;
203174
204175 // Check for FALSE POSITIVE: pretty-printer inserting parentheses where not needed.
205176 // Pseudocode:
0 commit comments