forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#134599 - dtolnay:fulldepsparser, r=fmease Detect invalid exprs in parser used by pretty-printer tests This PR fixes a bug in rust-lang#133730 inherited from rust-lang#43742. Before this fix, the test might silently only operate on a prefix of some of the test cases in this table: https://github.com/rust-lang/rust/blob/13170cd787cb733ed24842ee825bcbd98dc01476/tests/ui-fulldeps/pprust-parenthesis-insertion.rs#L57 For example, adding the test case `1 .. 2 .. 3` (a syntactically invalid expression) into the table would unexpectedly succeed the test instead of crashing at this unwrap: https://github.com/rust-lang/rust/blob/13170cd787cb733ed24842ee825bcbd98dc01476/tests/ui-fulldeps/pprust-parenthesis-insertion.rs#L199-L200 because `parse_expr` would successfully parse just `1 .. 2` and disregard the last `.. 3`. This PR adds a check that `parse_expr` reaches `Eof`, ensuring all the test cases actually test the whole expression they look like they are supposed to.
- Loading branch information
Showing
3 changed files
with
62 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#![feature(rustc_private)] | ||
|
||
extern crate rustc_ast; | ||
extern crate rustc_driver; | ||
extern crate rustc_errors; | ||
extern crate rustc_parse; | ||
extern crate rustc_session; | ||
extern crate rustc_span; | ||
|
||
use rustc_ast::ast::{DUMMY_NODE_ID, Expr}; | ||
use rustc_ast::mut_visit::MutVisitor; | ||
use rustc_ast::node_id::NodeId; | ||
use rustc_ast::ptr::P; | ||
use rustc_ast::token; | ||
use rustc_errors::Diag; | ||
use rustc_parse::parser::Recovery; | ||
use rustc_session::parse::ParseSess; | ||
use rustc_span::{DUMMY_SP, FileName, Span}; | ||
|
||
pub fn parse_expr(psess: &ParseSess, source_code: &str) -> Option<P<Expr>> { | ||
let parser = rustc_parse::unwrap_or_emit_fatal(rustc_parse::new_parser_from_source_str( | ||
psess, | ||
FileName::anon_source_code(source_code), | ||
source_code.to_owned(), | ||
)); | ||
|
||
let mut parser = parser.recovery(Recovery::Forbidden); | ||
let mut expr = parser.parse_expr().map_err(Diag::cancel).ok()?; | ||
if parser.token != token::Eof { | ||
return None; | ||
} | ||
|
||
Normalize.visit_expr(&mut expr); | ||
Some(expr) | ||
} | ||
|
||
// Erase Span information that could distinguish between identical expressions | ||
// parsed from different source strings. | ||
struct Normalize; | ||
|
||
impl MutVisitor for Normalize { | ||
const VISIT_TOKENS: bool = true; | ||
|
||
fn visit_id(&mut self, id: &mut NodeId) { | ||
*id = DUMMY_NODE_ID; | ||
} | ||
|
||
fn visit_span(&mut self, span: &mut Span) { | ||
*span = DUMMY_SP; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters