Skip to content

Commit b100d8e

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

File tree

2 files changed

+48
-39
lines changed

2 files changed

+48
-39
lines changed

tests/ui-fulldeps/auxiliary/parser.rs

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

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

+6-39
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ extern crate rustc_parse;
3737
extern crate rustc_session;
3838
extern crate rustc_span;
3939

40+
#[path = "auxiliary/parser.rs"]
41+
mod parser;
42+
4043
use std::mem;
4144
use std::process::ExitCode;
4245

43-
use rustc_ast::ast::{DUMMY_NODE_ID, Expr, ExprKind};
46+
use rustc_ast::ast::{Expr, ExprKind};
4447
use rustc_ast::mut_visit::{self, DummyAstNode as _, MutVisitor};
45-
use rustc_ast::node_id::NodeId;
4648
use rustc_ast::ptr::P;
47-
use rustc_ast::token;
4849
use rustc_ast_pretty::pprust;
49-
use rustc_errors::Diag;
50-
use rustc_parse::parser::Recovery;
5150
use rustc_session::parse::ParseSess;
52-
use rustc_span::{DUMMY_SP, FileName, Span};
51+
52+
use crate::parser::parse_expr;
5353

5454
// Every parenthesis in the following expressions is re-inserted by the
5555
// pretty-printer.
@@ -155,39 +155,6 @@ impl MutVisitor for Unparenthesize {
155155
}
156156
}
157157

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-
191158
fn main() -> ExitCode {
192159
let mut status = ExitCode::SUCCESS;
193160
let mut fail = |description: &str, before: &str, after: &str| {

0 commit comments

Comments
 (0)