Skip to content
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 4 commits into from
Dec 22, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Switch pretty-printer roundtrip test to better parser
  • Loading branch information
dtolnay committed Dec 22, 2024
commit 822e8063fdc88a61ee82a0c3ffdcd33f4be15f8e
18 changes: 4 additions & 14 deletions tests/ui-fulldeps/pprust-expr-roundtrip.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//@ run-pass
//@ ignore-cross-compile
//@ aux-crate: parser=parser.rs
//@ edition: 2021

// The general idea of this test is to enumerate all "interesting" expressions and check that
// `parse(print(e)) == e` for all `e`. Here's what's interesting, for the purposes of this test:
Expand All @@ -21,7 +23,6 @@

extern crate rustc_ast;
extern crate rustc_ast_pretty;
extern crate rustc_data_structures;
extern crate rustc_parse;
extern crate rustc_session;
extern crate rustc_span;
Expand All @@ -32,28 +33,17 @@ extern crate thin_vec;
#[allow(unused_extern_crates)]
extern crate rustc_driver;

use parser::parse_expr;
use rustc_ast::mut_visit::{visit_clobber, MutVisitor};
use rustc_ast::ptr::P;
use rustc_ast::*;
use rustc_ast_pretty::pprust;
use rustc_parse::{new_parser_from_source_str, unwrap_or_emit_fatal};
use rustc_session::parse::ParseSess;
use rustc_span::source_map::Spanned;
use rustc_span::symbol::Ident;
use rustc_span::{FileName, DUMMY_SP};
use rustc_span::DUMMY_SP;
use thin_vec::{thin_vec, ThinVec};

fn parse_expr(psess: &ParseSess, src: &str) -> Option<P<Expr>> {
let src_as_string = src.to_string();

let mut p = unwrap_or_emit_fatal(new_parser_from_source_str(
psess,
FileName::Custom(src_as_string.clone()),
src_as_string,
));
p.parse_expr().map_err(|e| e.cancel()).ok()
}

// Helper functions for building exprs
fn expr(kind: ExprKind) -> P<Expr> {
P(Expr { id: DUMMY_NODE_ID, kind, span: DUMMY_SP, attrs: AttrVec::new(), tokens: None })
Expand Down