-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detect invalid exprs in parser used by pretty-printer tests #134599
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3f98f76
Check that pretty-printer parenthesis test operates on the whole test…
dtolnay 65ba6ac
Extract ui-fulldeps expression parser into module
dtolnay 822e806
Switch pretty-printer roundtrip test to better parser
dtolnay 1f2028f
Show which test case was found to be meaningless
dtolnay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Extract ui-fulldeps expression parser into module
- Loading branch information
commit 65ba6ac9a3314b6ad31634f13a02d2e9705a405e
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In both
pprust-*.rs
tests, I think I would rather use//@ aux-crate: parser=parser.rs
//@ edition: 2021
(aux-crate
puts the crate into the extern prelude contrary toaux-build
) instead of fighting against compiletest (#[path = "auxiliary/parser.rs"] mod parser;
). The extra overhead is not really relevant.