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

Test rustc's automatic parenthesis insertion #1788

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
49 changes: 49 additions & 0 deletions tests/test_precedence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,19 @@ use crate::common::eq::SpanlessEq;
use crate::common::parse;
use quote::ToTokens;
use rustc_ast::ast;
use rustc_ast::ast::{Expr, ExprKind};
use rustc_ast::mut_visit::MutVisitor;
use rustc_ast::ptr::P;
use rustc_ast_pretty::pprust;
use rustc_span::edition::Edition;
use rustc_span::DUMMY_SP;
use std::fs;
use std::mem;
use std::path::Path;
use std::process;
use std::sync::atomic::{AtomicUsize, Ordering};
use syn::parse::Parser as _;
use thin_vec::ThinVec;

#[macro_use]
mod macros;
Expand Down Expand Up @@ -188,6 +193,50 @@ fn test_expressions(path: &Path, edition: Edition, exprs: Vec<syn::Expr>) -> (us
continue;
}

struct FlattenParens;
impl MutVisitor for FlattenParens {
fn visit_expr(&mut self, e: &mut P<Expr>) {
while let ExprKind::Paren(paren) = &mut e.kind {
*e = mem::replace(
paren,
P(Expr {
id: ast::DUMMY_NODE_ID,
kind: ExprKind::Dummy,
span: DUMMY_SP,
attrs: ThinVec::new(),
tokens: None,
}),
);
}
rustc_ast::mut_visit::walk_expr(self, e);
}
}

let mut expr = parse::librustc_expr(&source_code).unwrap();
FlattenParens.visit_expr(&mut expr);
let printed = pprust::expr_to_string(&expr);
let Some(mut expr2) = parse::librustc_expr(&printed) else {
failed += 1;
errorf!(
"\nFAIL {}\nBEFORE:\n{}\nAFTER:\n{}\n",
path.display(),
source_code,
printed,
);
continue;
};
FlattenParens.visit_expr(&mut expr2);
if !SpanlessEq::eq(&expr, &expr2) {
failed += 1;
errorf!(
"\nFAIL {}\nBEFORE:\n{}\nAFTER:\n{}\n",
path.display(),
source_code,
printed,
);
continue;
}

passed += 1;
}
});
Expand Down
Loading