Skip to content

Commit 65ba6ac

Browse files
committed
Extract ui-fulldeps expression parser into module
1 parent 3f98f76 commit 65ba6ac

File tree

2 files changed

+55
-41
lines changed

2 files changed

+55
-41
lines changed

tests/ui-fulldeps/auxiliary/parser.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#![feature(rustc_private)]
2+
3+
extern crate rustc_ast;
4+
extern crate rustc_driver;
5+
extern crate rustc_errors;
6+
extern crate rustc_parse;
7+
extern crate rustc_session;
8+
extern crate rustc_span;
9+
10+
use rustc_ast::ast::{DUMMY_NODE_ID, Expr};
11+
use rustc_ast::mut_visit::MutVisitor;
12+
use rustc_ast::node_id::NodeId;
13+
use rustc_ast::ptr::P;
14+
use rustc_ast::token;
15+
use rustc_errors::Diag;
16+
use rustc_parse::parser::Recovery;
17+
use rustc_session::parse::ParseSess;
18+
use rustc_span::{DUMMY_SP, FileName, Span};
19+
20+
pub fn parse_expr(psess: &ParseSess, source_code: &str) -> Option<P<Expr>> {
21+
let parser = rustc_parse::unwrap_or_emit_fatal(rustc_parse::new_parser_from_source_str(
22+
psess,
23+
FileName::anon_source_code(source_code),
24+
source_code.to_owned(),
25+
));
26+
27+
let mut parser = parser.recovery(Recovery::Forbidden);
28+
let mut expr = parser.parse_expr().map_err(Diag::cancel).ok()?;
29+
if parser.token != token::Eof {
30+
return None;
31+
}
32+
33+
Normalize.visit_expr(&mut expr);
34+
Some(expr)
35+
}
36+
37+
// Erase Span information that could distinguish between identical expressions
38+
// parsed from different source strings.
39+
struct Normalize;
40+
41+
impl MutVisitor for Normalize {
42+
const VISIT_TOKENS: bool = true;
43+
44+
fn visit_id(&mut self, id: &mut NodeId) {
45+
*id = DUMMY_NODE_ID;
46+
}
47+
48+
fn visit_span(&mut self, span: &mut Span) {
49+
*span = DUMMY_SP;
50+
}
51+
}

tests/ui-fulldeps/pprust-parenthesis-insertion.rs

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
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
@@ -31,25 +33,19 @@
3133

3234
extern crate rustc_ast;
3335
extern crate rustc_ast_pretty;
34-
extern crate rustc_driver;
35-
extern crate rustc_errors;
3636
extern crate rustc_parse;
3737
extern crate rustc_session;
3838
extern crate rustc_span;
3939

4040
use std::mem;
4141
use 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};
4445
use rustc_ast::mut_visit::{self, DummyAstNode as _, MutVisitor};
45-
use rustc_ast::node_id::NodeId;
4646
use rustc_ast::ptr::P;
47-
use rustc_ast::token;
4847
use rustc_ast_pretty::pprust;
49-
use rustc_errors::Diag;
50-
use rustc_parse::parser::Recovery;
5148
use rustc_session::parse::ParseSess;
52-
use rustc_span::{DUMMY_SP, FileName, Span};
5349

5450
// Every parenthesis in the following expressions is re-inserted by the
5551
// pretty-printer.
@@ -155,39 +151,6 @@ impl MutVisitor for Unparenthesize {
155151
}
156152
}
157153

158-
// Erase Span information that could distinguish between identical expressions
159-
// parsed from different source strings.
160-
struct Normalize;
161-
162-
impl MutVisitor for Normalize {
163-
const VISIT_TOKENS: bool = true;
164-
165-
fn visit_id(&mut self, id: &mut NodeId) {
166-
*id = DUMMY_NODE_ID;
167-
}
168-
169-
fn visit_span(&mut self, span: &mut Span) {
170-
*span = DUMMY_SP;
171-
}
172-
}
173-
174-
fn parse_expr(psess: &ParseSess, source_code: &str) -> Option<P<Expr>> {
175-
let parser = rustc_parse::unwrap_or_emit_fatal(rustc_parse::new_parser_from_source_str(
176-
psess,
177-
FileName::anon_source_code(source_code),
178-
source_code.to_owned(),
179-
));
180-
181-
let mut parser = parser.recovery(Recovery::Forbidden);
182-
let mut expr = parser.parse_expr().map_err(Diag::cancel).ok()?;
183-
if parser.token != token::Eof {
184-
return None;
185-
}
186-
187-
Normalize.visit_expr(&mut expr);
188-
Some(expr)
189-
}
190-
191154
fn main() -> ExitCode {
192155
let mut status = ExitCode::SUCCESS;
193156
let mut fail = |description: &str, before: &str, after: &str| {

0 commit comments

Comments
 (0)