| 1 |
#![cfg(not(syn_disable_nightly_tests))] |
| 2 |
#![cfg(not(miri))] |
| 3 |
#![recursion_limit = "1024"] |
| 4 |
#![feature(rustc_private)] |
| 5 |
#![allow( |
| 6 |
clippy::blocks_in_conditions, |
| 7 |
clippy::elidable_lifetime_names, |
| 8 |
clippy::manual_assert, |
| 9 |
clippy::manual_let_else, |
| 10 |
clippy::match_like_matches_macro, |
| 11 |
clippy::needless_lifetimes, |
| 12 |
clippy::uninlined_format_args |
| 13 |
)] |
| 14 |
#![allow(mismatched_lifetime_syntaxes)] |
| 15 |
|
| 16 |
extern crate rustc_ast; |
| 17 |
extern crate rustc_ast_pretty; |
| 18 |
extern crate rustc_data_structures; |
| 19 |
extern crate rustc_driver; |
| 20 |
extern crate rustc_error_messages; |
| 21 |
extern crate rustc_errors; |
| 22 |
extern crate rustc_expand; |
| 23 |
extern crate rustc_parse; |
| 24 |
extern crate rustc_session; |
| 25 |
extern crate rustc_span; |
| 26 |
|
| 27 |
use crate::common::eq::SpanlessEq; |
| 28 |
use quote::quote; |
| 29 |
use rustc_ast::ast::{ |
| 30 |
AngleBracketedArg, Crate, GenericArg, GenericArgs, GenericParamKind, Generics, |
| 31 |
}; |
| 32 |
use rustc_ast::mut_visit::{self, MutVisitor}; |
| 33 |
use rustc_ast_pretty::pprust; |
| 34 |
use rustc_data_structures::flat_map_in_place::FlatMapInPlace; |
| 35 |
use rustc_error_messages::{DiagMessage, LazyFallbackBundle}; |
| 36 |
use rustc_errors::{translation, Diag, PResult}; |
| 37 |
use rustc_parse::lexer::StripTokens; |
| 38 |
use rustc_session::parse::ParseSess; |
| 39 |
use rustc_span::FileName; |
| 40 |
use std::borrow::Cow; |
| 41 |
use std::fs; |
| 42 |
use std::panic; |
| 43 |
use std::path::Path; |
| 44 |
use std::process; |
| 45 |
use std::sync::atomic::{AtomicUsize, Ordering}; |
| 46 |
use std::time::Instant; |
| 47 |
|
| 48 |
#[macro_use] |
| 49 |
mod macros; |
| 50 |
|
| 51 |
mod common; |
| 52 |
mod repo; |
| 53 |
|
| 54 |
#[test] |
| 55 |
fn test_round_trip() { |
| 56 |
repo::rayon_init(); |
| 57 |
repo::clone_rust(); |
| 58 |
let abort_after = repo::abort_after(); |
| 59 |
if abort_after == 0 { |
| 60 |
panic!("skipping all round_trip tests"); |
| 61 |
} |
| 62 |
|
| 63 |
let failed = AtomicUsize::new(0); |
| 64 |
|
| 65 |
repo::for_each_rust_file(|path| test(path, &failed, abort_after)); |
| 66 |
|
| 67 |
let failed = failed.into_inner(); |
| 68 |
if failed > 0 { |
| 69 |
panic!("{} failures", failed); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
fn test(path: &Path, failed: &AtomicUsize, abort_after: usize) { |
| 74 |
let failed = || { |
| 75 |
let prev_failed = failed.fetch_add(1, Ordering::Relaxed); |
| 76 |
if prev_failed + 1 >= abort_after { |
| 77 |
process::exit(1); |
| 78 |
} |
| 79 |
}; |
| 80 |
|
| 81 |
let content = fs::read_to_string(path).unwrap(); |
| 82 |
|
| 83 |
let (back, elapsed) = match panic::catch_unwind(|| { |
| 84 |
let start = Instant::now(); |
| 85 |
let result = syn::parse_file(&content); |
| 86 |
let elapsed = start.elapsed(); |
| 87 |
result.map(|krate| (quote!(#krate).to_string(), elapsed)) |
| 88 |
}) { |
| 89 |
Err(_) => { |
| 90 |
errorf!("=== {}: syn panic\n", path.display()); |
| 91 |
failed(); |
| 92 |
return; |
| 93 |
} |
| 94 |
Ok(Err(msg)) => { |
| 95 |
errorf!("=== {}: syn failed to parse\n{:?}\n", path.display(), msg); |
| 96 |
failed(); |
| 97 |
return; |
| 98 |
} |
| 99 |
Ok(Ok(result)) => result, |
| 100 |
}; |
| 101 |
|
| 102 |
let edition = repo::edition(path).parse().unwrap(); |
| 103 |
|
| 104 |
rustc_span::create_session_if_not_set_then(edition, |_| { |
| 105 |
let equal = match panic::catch_unwind(|| { |
| 106 |
let locale_resources = rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(); |
| 107 |
let sess = ParseSess::new(locale_resources); |
| 108 |
let before = match librustc_parse(content, &sess) { |
| 109 |
Ok(before) => before, |
| 110 |
Err(diagnostic) => { |
| 111 |
errorf!( |
| 112 |
"=== {}: ignore - librustc failed to parse original content: {}\n", |
| 113 |
path.display(), |
| 114 |
translate_message(&diagnostic), |
| 115 |
); |
| 116 |
diagnostic.cancel(); |
| 117 |
return Err(true); |
| 118 |
} |
| 119 |
}; |
| 120 |
let after = match librustc_parse(back, &sess) { |
| 121 |
Ok(after) => after, |
| 122 |
Err(diagnostic) => { |
| 123 |
errorf!("=== {}: librustc failed to parse", path.display()); |
| 124 |
diagnostic.emit(); |
| 125 |
return Err(false); |
| 126 |
} |
| 127 |
}; |
| 128 |
Ok((before, after)) |
| 129 |
}) { |
| 130 |
Err(_) => { |
| 131 |
errorf!("=== {}: ignoring librustc panic\n", path.display()); |
| 132 |
true |
| 133 |
} |
| 134 |
Ok(Err(equal)) => equal, |
| 135 |
Ok(Ok((mut before, mut after))) => { |
| 136 |
normalize(&mut before); |
| 137 |
normalize(&mut after); |
| 138 |
if SpanlessEq::eq(&before, &after) { |
| 139 |
errorf!( |
| 140 |
"=== {}: pass in {}ms\n", |
| 141 |
path.display(), |
| 142 |
elapsed.as_secs() * 1000 + u64::from(elapsed.subsec_nanos()) / 1_000_000 |
| 143 |
); |
| 144 |
true |
| 145 |
} else { |
| 146 |
errorf!( |
| 147 |
"=== {}: FAIL\n{}\n!=\n{}\n", |
| 148 |
path.display(), |
| 149 |
pprust::crate_to_string_for_macros(&before), |
| 150 |
pprust::crate_to_string_for_macros(&after), |
| 151 |
); |
| 152 |
false |
| 153 |
} |
| 154 |
} |
| 155 |
}; |
| 156 |
if !equal { |
| 157 |
failed(); |
| 158 |
} |
| 159 |
}); |
| 160 |
} |
| 161 |
|
| 162 |
fn librustc_parse(content: String, sess: &ParseSess) -> PResult<Crate> { |
| 163 |
static COUNTER: AtomicUsize = AtomicUsize::new(0); |
| 164 |
let counter = COUNTER.fetch_add(1, Ordering::Relaxed); |
| 165 |
let name = FileName::Custom(format!("test_round_trip{}", counter)); |
| 166 |
let mut parser = rustc_parse::new_parser_from_source_str( |
| 167 |
sess, |
| 168 |
name, |
| 169 |
content, |
| 170 |
StripTokens::ShebangAndFrontmatter, |
| 171 |
) |
| 172 |
.unwrap(); |
| 173 |
parser.parse_crate_mod() |
| 174 |
} |
| 175 |
|
| 176 |
fn translate_message(diagnostic: &Diag) -> Cow<'static, str> { |
| 177 |
thread_local! { |
| 178 |
static FLUENT_BUNDLE: LazyFallbackBundle = { |
| 179 |
let locale_resources = rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(); |
| 180 |
let with_directionality_markers = false; |
| 181 |
rustc_error_messages::fallback_fluent_bundle(locale_resources, with_directionality_markers) |
| 182 |
}; |
| 183 |
} |
| 184 |
|
| 185 |
let message = &diagnostic.messages[0].0; |
| 186 |
let args = translation::to_fluent_args(diagnostic.args.iter()); |
| 187 |
|
| 188 |
let (identifier, attr) = match message { |
| 189 |
DiagMessage::Str(msg) | DiagMessage::Translated(msg) => return msg.clone(), |
| 190 |
DiagMessage::FluentIdentifier(identifier, attr) => (identifier, attr), |
| 191 |
}; |
| 192 |
|
| 193 |
FLUENT_BUNDLE.with(|fluent_bundle| { |
| 194 |
let message = fluent_bundle |
| 195 |
.get_message(identifier) |
| 196 |
.expect("missing diagnostic in fluent bundle"); |
| 197 |
let value = match attr { |
| 198 |
Some(attr) => message |
| 199 |
.get_attribute(attr) |
| 200 |
.expect("missing attribute in fluent message") |
| 201 |
.value(), |
| 202 |
None => message.value().expect("missing value in fluent message"), |
| 203 |
}; |
| 204 |
|
| 205 |
let mut err = Vec::new(); |
| 206 |
let translated = fluent_bundle.format_pattern(value, Some(&args), &mut err); |
| 207 |
assert!(err.is_empty()); |
| 208 |
Cow::Owned(translated.into_owned()) |
| 209 |
}) |
| 210 |
} |
| 211 |
|
| 212 |
fn normalize(krate: &mut Crate) { |
| 213 |
struct NormalizeVisitor; |
| 214 |
|
| 215 |
impl MutVisitor for NormalizeVisitor { |
| 216 |
fn visit_generic_args(&mut self, e: &mut GenericArgs) { |
| 217 |
if let GenericArgs::AngleBracketed(e) = e { |
| 218 |
#[derive(Ord, PartialOrd, Eq, PartialEq)] |
| 219 |
enum Group { |
| 220 |
Lifetimes, |
| 221 |
TypesAndConsts, |
| 222 |
Constraints, |
| 223 |
} |
| 224 |
e.args.sort_by_key(|arg| match arg { |
| 225 |
AngleBracketedArg::Arg(arg) => match arg { |
| 226 |
GenericArg::Lifetime(_) => Group::Lifetimes, |
| 227 |
GenericArg::Type(_) | GenericArg::Const(_) => Group::TypesAndConsts, |
| 228 |
}, |
| 229 |
AngleBracketedArg::Constraint(_) => Group::Constraints, |
| 230 |
}); |
| 231 |
} |
| 232 |
mut_visit::walk_generic_args(self, e); |
| 233 |
} |
| 234 |
|
| 235 |
fn visit_generics(&mut self, e: &mut Generics) { |
| 236 |
#[derive(Ord, PartialOrd, Eq, PartialEq)] |
| 237 |
enum Group { |
| 238 |
Lifetimes, |
| 239 |
TypesAndConsts, |
| 240 |
} |
| 241 |
e.params.sort_by_key(|param| match param.kind { |
| 242 |
GenericParamKind::Lifetime => Group::Lifetimes, |
| 243 |
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => { |
| 244 |
Group::TypesAndConsts |
| 245 |
} |
| 246 |
}); |
| 247 |
e.params |
| 248 |
.flat_map_in_place(|param| self.flat_map_generic_param(param)); |
| 249 |
if e.where_clause.predicates.is_empty() { |
| 250 |
e.where_clause.has_where_token = false; |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
NormalizeVisitor.visit_crate(krate); |
| 256 |
} |