Skip to content

Commit 0d2f38e

Browse files
committed
compiletest: Always preserve kind for compiler diagnostics
Those that didn't previously preserved kind are now marked as not requiring annotations to keep the previous behavior. Also, do not lose diagnostics with an empty message.
1 parent a295908 commit 0d2f38e

File tree

5 files changed

+43
-26
lines changed

5 files changed

+43
-26
lines changed

src/tools/compiletest/src/errors.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ pub struct Error {
5353
/// `None` if not specified or unknown message kind.
5454
pub kind: Option<ErrorKind>,
5555
pub msg: String,
56+
/// For some `Error`s, like secondary lines of multi-line diagnostics, line annotations
57+
/// are not mandatory, even if they would otherwise be mandatory for primary errors.
58+
/// Only makes sense for "actual" errors, not for "expected" errors.
59+
pub require_annotation: bool,
5660
}
5761

5862
impl Error {
@@ -182,7 +186,7 @@ fn parse_expected(
182186
kind,
183187
msg
184188
);
185-
Some((follow_prev, Error { line_num, kind, msg }))
189+
Some((follow_prev, Error { line_num, kind, msg, require_annotation: true }))
186190
}
187191

188192
#[cfg(test)]

src/tools/compiletest/src/json.rs

+30-21
Original file line numberDiff line numberDiff line change
@@ -231,36 +231,42 @@ fn push_actual_errors(
231231
// We expect to replace these with something more structured anyhow.
232232
let mut message_lines = diagnostic.message.lines();
233233
let kind = ErrorKind::from_str(&diagnostic.level).ok();
234-
if let Some(first_line) = message_lines.next() {
235-
let ignore = |s| {
236-
static RE: OnceLock<Regex> = OnceLock::new();
237-
RE.get_or_init(|| {
238-
Regex::new(r"aborting due to \d+ previous errors?|\d+ warnings? emitted").unwrap()
239-
})
240-
.is_match(s)
241-
};
242-
243-
if primary_spans.is_empty() && !ignore(first_line) {
244-
errors.push(Error { line_num: None, kind, msg: with_code(None, first_line) });
245-
} else {
246-
for span in primary_spans {
247-
errors.push(Error {
248-
line_num: Some(span.line_start),
249-
kind,
250-
msg: with_code(Some(span), first_line),
251-
});
252-
}
234+
let first_line = message_lines.next().unwrap_or(&diagnostic.message);
235+
if primary_spans.is_empty() {
236+
static RE: OnceLock<Regex> = OnceLock::new();
237+
let re_init =
238+
|| Regex::new(r"aborting due to \d+ previous errors?|\d+ warnings? emitted").unwrap();
239+
errors.push(Error {
240+
line_num: None,
241+
kind,
242+
msg: with_code(None, first_line),
243+
require_annotation: !RE.get_or_init(re_init).is_match(first_line),
244+
});
245+
} else {
246+
for span in primary_spans {
247+
errors.push(Error {
248+
line_num: Some(span.line_start),
249+
kind,
250+
msg: with_code(Some(span), first_line),
251+
require_annotation: true,
252+
});
253253
}
254254
}
255255
for next_line in message_lines {
256256
if primary_spans.is_empty() {
257-
errors.push(Error { line_num: None, kind: None, msg: with_code(None, next_line) });
257+
errors.push(Error {
258+
line_num: None,
259+
kind,
260+
msg: with_code(None, next_line),
261+
require_annotation: false,
262+
});
258263
} else {
259264
for span in primary_spans {
260265
errors.push(Error {
261266
line_num: Some(span.line_start),
262-
kind: None,
267+
kind,
263268
msg: with_code(Some(span), next_line),
269+
require_annotation: false,
264270
});
265271
}
266272
}
@@ -274,6 +280,7 @@ fn push_actual_errors(
274280
line_num: Some(span.line_start + index),
275281
kind: Some(ErrorKind::Suggestion),
276282
msg: line.to_string(),
283+
require_annotation: true,
277284
});
278285
}
279286
}
@@ -292,6 +299,7 @@ fn push_actual_errors(
292299
line_num: Some(span.line_start),
293300
kind: Some(ErrorKind::Note),
294301
msg: span.label.clone().unwrap(),
302+
require_annotation: true,
295303
});
296304
}
297305

@@ -311,6 +319,7 @@ fn push_backtrace(
311319
line_num: Some(expansion.span.line_start),
312320
kind: Some(ErrorKind::Note),
313321
msg: format!("in this expansion of {}", expansion.macro_decl_name),
322+
require_annotation: true,
314323
});
315324
}
316325

src/tools/compiletest/src/runtest.rs

+1
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ impl<'test> TestCx<'test> {
811811
expect_note: bool,
812812
) -> bool {
813813
!actual_error.msg.is_empty()
814+
&& actual_error.require_annotation
814815
&& match actual_error.kind {
815816
Some(ErrorKind::Help) => expect_help,
816817
Some(ErrorKind::Note) => expect_note,

tests/ui/proc-macro/issue-91800.rs

+3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ extern crate issue_91800_macro;
66
#[derive(MyTrait)]
77
//~^ ERROR macros that expand to items must be delimited with braces or followed by a semicolon
88
//~| ERROR proc-macro derive produced unparsable tokens
9+
//~| ERROR
910
#[attribute_macro]
1011
//~^ ERROR macros that expand to items must be delimited with braces or followed by a semicolon
12+
//~| ERROR
1113
struct MyStruct;
1214

1315
fn_macro! {}
1416
//~^ ERROR macros that expand to items must be delimited with braces or followed by a semicolon
17+
//~| ERROR
1518

1619
fn main() {}

tests/ui/proc-macro/issue-91800.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,31 @@ LL | #[derive(MyTrait)]
2121
= note: this error originates in the derive macro `MyTrait` (in Nightly builds, run with -Z macro-backtrace for more info)
2222

2323
error: macros that expand to items must be delimited with braces or followed by a semicolon
24-
--> $DIR/issue-91800.rs:9:1
24+
--> $DIR/issue-91800.rs:10:1
2525
|
2626
LL | #[attribute_macro]
2727
| ^^^^^^^^^^^^^^^^^^
2828
|
2929
= note: this error originates in the attribute macro `attribute_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
3030

3131
error:
32-
--> $DIR/issue-91800.rs:9:1
32+
--> $DIR/issue-91800.rs:10:1
3333
|
3434
LL | #[attribute_macro]
3535
| ^^^^^^^^^^^^^^^^^^
3636
|
3737
= note: this error originates in the attribute macro `attribute_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
3838

3939
error: macros that expand to items must be delimited with braces or followed by a semicolon
40-
--> $DIR/issue-91800.rs:13:1
40+
--> $DIR/issue-91800.rs:15:1
4141
|
4242
LL | fn_macro! {}
4343
| ^^^^^^^^^^^^
4444
|
4545
= note: this error originates in the macro `fn_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
4646

4747
error:
48-
--> $DIR/issue-91800.rs:13:1
48+
--> $DIR/issue-91800.rs:15:1
4949
|
5050
LL | fn_macro! {}
5151
| ^^^^^^^^^^^^

0 commit comments

Comments
 (0)