Skip to content

Commit 3b7a4cb

Browse files
committed
compiletest: Stricter parsing for diagnostic kinds
1 parent 0d2f38e commit 3b7a4cb

16 files changed

+74
-72
lines changed

src/tools/compiletest/src/errors.rs

+34-32
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::fs::File;
33
use std::io::BufReader;
44
use std::io::prelude::*;
55
use std::path::Path;
6-
use std::str::FromStr;
76
use std::sync::OnceLock;
87

98
use regex::Regex;
@@ -18,30 +17,39 @@ pub enum ErrorKind {
1817
Warning,
1918
}
2019

21-
impl FromStr for ErrorKind {
22-
type Err = ();
23-
fn from_str(s: &str) -> Result<Self, Self::Err> {
24-
let s = s.to_uppercase();
25-
let part0: &str = s.split(':').next().unwrap();
26-
match part0 {
27-
"HELP" => Ok(ErrorKind::Help),
28-
"ERROR" => Ok(ErrorKind::Error),
29-
"NOTE" => Ok(ErrorKind::Note),
30-
"SUGGESTION" => Ok(ErrorKind::Suggestion),
31-
"WARN" | "WARNING" => Ok(ErrorKind::Warning),
32-
_ => Err(()),
20+
impl ErrorKind {
21+
pub fn from_compiler_str(s: &str) -> ErrorKind {
22+
match s {
23+
"help" => ErrorKind::Help,
24+
"error" | "error: internal compiler error" => ErrorKind::Error,
25+
"note" | "failure-note" => ErrorKind::Note,
26+
"warning" => ErrorKind::Warning,
27+
_ => panic!("unexpected compiler diagnostic kind `{s}`"),
3328
}
3429
}
30+
31+
/// Either the canonical uppercase string, or some additional versions for compatibility.
32+
/// FIXME: consider keeping only the canonical versions here.
33+
fn from_user_str(s: &str) -> Option<ErrorKind> {
34+
Some(match s {
35+
"HELP" | "help" => ErrorKind::Help,
36+
"ERROR" | "error" => ErrorKind::Error,
37+
"NOTE" | "note" => ErrorKind::Note,
38+
"SUGGESTION" => ErrorKind::Suggestion,
39+
"WARN" | "WARNING" | "warn" | "warning" => ErrorKind::Warning,
40+
_ => return None,
41+
})
42+
}
3543
}
3644

3745
impl fmt::Display for ErrorKind {
3846
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3947
match *self {
40-
ErrorKind::Help => write!(f, "help message"),
41-
ErrorKind::Error => write!(f, "error"),
42-
ErrorKind::Note => write!(f, "note"),
43-
ErrorKind::Suggestion => write!(f, "suggestion"),
44-
ErrorKind::Warning => write!(f, "warning"),
48+
ErrorKind::Help => write!(f, "HELP"),
49+
ErrorKind::Error => write!(f, "ERROR"),
50+
ErrorKind::Note => write!(f, "NOTE"),
51+
ErrorKind::Suggestion => write!(f, "SUGGESTION"),
52+
ErrorKind::Warning => write!(f, "WARN"),
4553
}
4654
}
4755
}
@@ -64,7 +72,7 @@ impl Error {
6472
use colored::Colorize;
6573
format!(
6674
"{: <10}line {: >3}: {}",
67-
self.kind.map(|kind| kind.to_string()).unwrap_or_default().to_uppercase(),
75+
self.kind.map(|kind| kind.to_string()).unwrap_or_default(),
6876
self.line_num_str(),
6977
self.msg.cyan(),
7078
)
@@ -154,18 +162,12 @@ fn parse_expected(
154162
}
155163

156164
// Get the part of the comment after the sigil (e.g. `~^^` or ~|).
157-
let whole_match = captures.get(0).unwrap();
158-
let (_, mut msg) = line.split_at(whole_match.end());
159-
160-
let first_word = msg.split_whitespace().next().expect("Encountered unexpected empty comment");
161-
162-
// If we find `//~ ERROR foo` or something like that, skip the first word.
163-
let kind = first_word.parse::<ErrorKind>().ok();
164-
if kind.is_some() {
165-
msg = &msg.trim_start().split_at(first_word.len()).1;
166-
}
167-
168-
let msg = msg.trim().to_owned();
165+
let tag = captures.get(0).unwrap();
166+
let rest = line[tag.end()..].trim_start();
167+
let (kind_str, _) = rest.split_once(|c: char| !c.is_ascii_alphabetic()).unwrap_or((rest, ""));
168+
let kind = ErrorKind::from_user_str(kind_str);
169+
let untrimmed_msg = if kind.is_some() { &rest[kind_str.len()..] } else { rest };
170+
let msg = untrimmed_msg.strip_prefix(':').unwrap_or(untrimmed_msg).trim().to_owned();
169171

170172
let line_num_adjust = &captures["adjust"];
171173
let (follow_prev, line_num) = if line_num_adjust == "|" {
@@ -181,7 +183,7 @@ fn parse_expected(
181183
debug!(
182184
"line={:?} tag={:?} follow_prev={:?} kind={:?} msg={:?}",
183185
line_num,
184-
whole_match.as_str(),
186+
tag.as_str(),
185187
follow_prev,
186188
kind,
187189
msg

src/tools/compiletest/src/json.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! These structs are a subset of the ones found in `rustc_errors::json`.
22
33
use std::path::{Path, PathBuf};
4-
use std::str::FromStr;
54
use std::sync::OnceLock;
65

76
use regex::Regex;
@@ -230,7 +229,7 @@ fn push_actual_errors(
230229
// Convert multi-line messages into multiple errors.
231230
// We expect to replace these with something more structured anyhow.
232231
let mut message_lines = diagnostic.message.lines();
233-
let kind = ErrorKind::from_str(&diagnostic.level).ok();
232+
let kind = Some(ErrorKind::from_compiler_str(&diagnostic.level));
234233
let first_line = message_lines.next().unwrap_or(&diagnostic.message);
235234
if primary_spans.is_empty() {
236235
static RE: OnceLock<Regex> = OnceLock::new();
@@ -240,7 +239,8 @@ fn push_actual_errors(
240239
line_num: None,
241240
kind,
242241
msg: with_code(None, first_line),
243-
require_annotation: !RE.get_or_init(re_init).is_match(first_line),
242+
require_annotation: diagnostic.level != "failure-note"
243+
&& !RE.get_or_init(re_init).is_match(first_line),
244244
});
245245
} else {
246246
for span in primary_spans {

tests/ui/async-await/issue-70818.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::future::Future;
44
fn foo<T: Send, U>(ty: T, ty1: U) -> impl Future<Output = (T, U)> + Send {
5-
//~^ Error future cannot be sent between threads safely
5+
//~^ ERROR future cannot be sent between threads safely
66
async { (ty, ty1) }
77
}
88

tests/ui/async-await/issue-71137.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ async fn wrong_mutex() {
1919
}
2020

2121
fn main() {
22-
fake_spawn(wrong_mutex()); //~ Error future cannot be sent between threads safely
22+
fake_spawn(wrong_mutex()); //~ ERROR future cannot be sent between threads safely
2323
}

tests/ui/const-generics/defaults/mismatch.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ pub struct Example4<const N: usize = 13, const M: usize = 4>;
55

66
fn main() {
77
let e: Example<13> = ();
8-
//~^ Error: mismatched types
8+
//~^ ERROR mismatched types
99
//~| expected struct `Example`
1010
let e: Example2<u32, 13> = ();
11-
//~^ Error: mismatched types
11+
//~^ ERROR mismatched types
1212
//~| expected struct `Example2`
1313
let e: Example3<13, u32> = ();
14-
//~^ Error: mismatched types
14+
//~^ ERROR mismatched types
1515
//~| expected struct `Example3`
1616
let e: Example3<7> = ();
17-
//~^ Error: mismatched types
17+
//~^ ERROR mismatched types
1818
//~| expected struct `Example3<7>`
1919
let e: Example4<7> = ();
20-
//~^ Error: mismatched types
20+
//~^ ERROR mismatched types
2121
//~| expected struct `Example4<7>`
2222
}

tests/ui/const-generics/dont-evaluate-array-len-on-err-1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ trait Foo {
1313
[Adt; std::mem::size_of::<Self::Assoc>()]: ,
1414
{
1515
<[Adt; std::mem::size_of::<Self::Assoc>()] as Foo>::bar()
16-
//~^ Error: the trait bound
16+
//~^ ERROR the trait bound
1717
}
1818

1919
fn bar() {}

tests/ui/const-generics/generic_const_exprs/abstract-const-as-cast-3.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ where
1515

1616
// errors are bad but seems to be pre-existing issue #86198
1717
assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as u128 }>>();
18-
//~^ Error: mismatched types
19-
//~^^ Error: unconstrained generic constant
18+
//~^ ERROR mismatched types
19+
//~^^ ERROR unconstrained generic constant
2020
assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as _ }>>();
21-
//~^ Error: mismatched types
22-
//~^^ Error: unconstrained generic constant
21+
//~^ ERROR mismatched types
22+
//~^^ ERROR unconstrained generic constant
2323
assert_impl::<HasCastInTraitImpl<13, { 12 as u128 }>>();
24-
//~^ Error: mismatched types
24+
//~^ ERROR mismatched types
2525
assert_impl::<HasCastInTraitImpl<14, 13>>();
26-
//~^ Error: mismatched types
26+
//~^ ERROR mismatched types
2727
}
2828
pub fn use_trait_impl_2<const N: usize>()
2929
where
@@ -33,15 +33,15 @@ where
3333

3434
// errors are bad but seems to be pre-existing issue #86198
3535
assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as u128 }>>();
36-
//~^ Error: mismatched types
37-
//~^^ Error: unconstrained generic constant
36+
//~^ ERROR mismatched types
37+
//~^^ ERROR unconstrained generic constant
3838
assert_impl::<HasCastInTraitImpl<{ N + 1 }, { N as _ }>>();
39-
//~^ Error: mismatched types
40-
//~^^ Error: unconstrained generic constant
39+
//~^ ERROR mismatched types
40+
//~^^ ERROR unconstrained generic constant
4141
assert_impl::<HasCastInTraitImpl<13, { 12 as u128 }>>();
42-
//~^ Error: mismatched types
42+
//~^ ERROR mismatched types
4343
assert_impl::<HasCastInTraitImpl<14, 13>>();
44-
//~^ Error: mismatched types
44+
//~^ ERROR mismatched types
4545
}
4646

4747
fn main() {}

tests/ui/const-generics/generic_const_exprs/issue-72787.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ pub trait True {}
99

1010
impl<const LHS: u32, const RHS: u32> True for IsLessOrEqual<LHS, RHS> where
1111
Condition<{ LHS <= RHS }>: True
12-
//[min]~^ Error generic parameters may not be used in const operations
13-
//[min]~| Error generic parameters may not be used in const operations
12+
//[min]~^ ERROR generic parameters may not be used in const operations
13+
//[min]~| ERROR generic parameters may not be used in const operations
1414
{
1515
}
1616
impl True for Condition<true> {}
@@ -21,8 +21,8 @@ where
2121
IsLessOrEqual<I, 8>: True,
2222
IsLessOrEqual<J, 8>: True,
2323
IsLessOrEqual<{ 8 - I }, { 8 - J }>: True,
24-
//[min]~^ Error generic parameters may not be used in const operations
25-
//[min]~| Error generic parameters may not be used in const operations
24+
//[min]~^ ERROR generic parameters may not be used in const operations
25+
//[min]~| ERROR generic parameters may not be used in const operations
2626
// Condition<{ 8 - I <= 8 - J }>: True,
2727
{
2828
fn print() {

tests/ui/const-generics/generic_const_exprs/issue-79518-default_trait_method_normalization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ trait Foo {
1414
[(); std::mem::size_of::<Self::Assoc>()]: ,
1515
{
1616
Self::AssocInstance == [(); std::mem::size_of::<Self::Assoc>()];
17-
//~^ Error: mismatched types
17+
//~^ ERROR mismatched types
1818
}
1919
}
2020

tests/ui/impl-trait/impl-generic-mismatch.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ trait Foo {
66

77
impl Foo for () {
88
fn foo<U: Debug>(&self, _: &U) { }
9-
//~^ Error method `foo` has incompatible signature for trait
9+
//~^ ERROR method `foo` has incompatible signature for trait
1010
}
1111

1212
trait Bar {
@@ -15,7 +15,7 @@ trait Bar {
1515

1616
impl Bar for () {
1717
fn bar(&self, _: &impl Debug) { }
18-
//~^ Error method `bar` has incompatible signature for trait
18+
//~^ ERROR method `bar` has incompatible signature for trait
1919
}
2020

2121
trait Baz {
@@ -24,7 +24,7 @@ trait Baz {
2424

2525
impl Baz for () {
2626
fn baz<T: Debug>(&self, _: &impl Debug, _: &T) { }
27-
//~^ Error method `baz` has incompatible signature for trait
27+
//~^ ERROR method `baz` has incompatible signature for trait
2828
}
2929

3030
// With non-local trait (#49841):
@@ -35,7 +35,7 @@ struct X;
3535

3636
impl Hash for X {
3737
fn hash(&self, hasher: &mut impl Hasher) {}
38-
//~^ Error method `hash` has incompatible signature for trait
38+
//~^ ERROR method `hash` has incompatible signature for trait
3939
}
4040

4141
fn main() {}

tests/ui/mismatched_types/assignment-operator-unimplemented.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ struct Foo;
33
fn main() {
44
let mut a = Foo;
55
let ref b = Foo;
6-
a += *b; //~ Error: binary assignment operation `+=` cannot be applied to type `Foo`
6+
a += *b; //~ ERROR binary assignment operation `+=` cannot be applied to type `Foo`
77
}

tests/ui/modules/issue-107649.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,5 @@ fn main() {
102102
();
103103
();
104104
();
105-
dbg!(lib::Dummy); //~ Error: `Dummy` doesn't implement `Debug`
105+
dbg!(lib::Dummy); //~ ERROR `Dummy` doesn't implement `Debug`
106106
}

tests/ui/parallel-rustc/cache-after-waiting-issue-111528.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub fn a() {
1010

1111
#[export_name="fail"]
1212
pub fn b() {
13-
//~^ Error symbol `fail` is already defined
13+
//~^ ERROR symbol `fail` is already defined
1414
}
1515

1616
fn main() {}

tests/ui/resolve/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ struct A {
44

55
impl A {
66
fn new(cofig: String) -> Self {
7-
Self { config } //~ Error cannot find value `config` in this scope
7+
Self { config } //~ ERROR cannot find value `config` in this scope
88
}
99

1010
fn do_something(cofig: String) {
11-
println!("{config}"); //~ Error cannot find value `config` in this scope
11+
println!("{config}"); //~ ERROR cannot find value `config` in this scope
1212
}
1313

1414
fn self_is_available(self, cofig: String) {
15-
println!("{config}"); //~ Error cannot find value `config` in this scope
15+
println!("{config}"); //~ ERROR cannot find value `config` in this scope
1616
}
1717
}
1818

tests/ui/suggestions/issue-103646.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ trait Cat {
55
fn uwu<T: Cat>(c: T) {
66
c.nya();
77
//~^ ERROR no method named `nya` found for type parameter `T` in the current scope
8-
//~| Suggestion T::nya()
8+
//~| SUGGESTION T::nya()
99
}
1010

1111
fn main() {}

tests/ui/type-alias-impl-trait/issue-53598.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl Foo for S2 {
1717
type Item = impl Debug;
1818

1919
fn foo<T: Debug>(_: T) -> Self::Item {
20-
//~^ Error type parameter `T` is part of concrete type but not used in parameter list for the `impl Trait` type alias
20+
//~^ ERROR type parameter `T` is part of concrete type but not used in parameter list for the `impl Trait` type alias
2121
S::<T>(Default::default())
2222
}
2323
}

0 commit comments

Comments
 (0)