Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d2eadb7

Browse files
committedApr 30, 2025·
Auto merge of #139720 - petrochenkov:errkind2, r=jieyouxu
compiletest: Make diagnostic kind mandatory on line annotations (take 2) Compiletest currently accepts line annotations without kind in UI tests. ``` let a = b + c; //~ my message ``` Such annotations have two effects. - First, they match any compiler-produced diagnostic kind. This functionality is never used in practice, there are no target-dependent diagnostic kinds of something like that. - Second, they are not "viral". For example, any explicit `//~ NOTE my msg` in a test requires all other `NOTE` diagnostics in the same test to be annotated. Implicit `//~ my msg` will just match the note and won't require other annotations. The second functionality has a replacement since recently - directive `//@ dont-require-annotations: NOTE`. This PR removes support for `//~ my message` and makes the explicit diagnostic kind mandatory. Unwanted additional annotations are suppressed using the `dont-require-annotations` directive. Closes rust-lang/compiler-team#862. Previous attempt - #139427. r? `@jieyouxu`
2 parents 427288b + 20faf85 commit d2eadb7

File tree

372 files changed

+2377
-2107
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

372 files changed

+2377
-2107
lines changed
 

‎src/doc/rustc-dev-guide/src/tests/ui.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,9 @@ E.g. use `//@ dont-require-annotations: NOTE` to annotate notes selectively.
372372
Avoid using this directive for `ERROR`s and `WARN`ings, unless there's a serious reason, like
373373
target-dependent compiler output.
374374

375-
Missing diagnostic kinds (`//~ message`) are currently accepted, but are being phased away.
376-
They will match any compiler output kind, but will not force exhaustive annotations for that kind.
377-
Prefer explicit kind and `//@ dont-require-annotations` to achieve the same effect.
375+
Some diagnostics are never required to be line-annotated, regardless of their kind or directives,
376+
for example secondary lines of multiline diagnostics,
377+
or ubiquitous diagnostics like `aborting due to N previous errors`.
378378

379379
UI tests use the `-A unused` flag by default to ignore all unused warnings, as
380380
unused warnings are usually not the focus of a test. However, simple code

‎src/tools/compiletest/src/errors.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,20 @@ impl ErrorKind {
3030

3131
/// Either the canonical uppercase string, or some additional versions for compatibility.
3232
/// FIXME: consider keeping only the canonical versions here.
33-
fn from_user_str(s: &str) -> Option<ErrorKind> {
34-
Some(match s {
33+
pub fn from_user_str(s: &str) -> ErrorKind {
34+
match s {
3535
"HELP" | "help" => ErrorKind::Help,
3636
"ERROR" | "error" => ErrorKind::Error,
37-
"NOTE" | "note" => ErrorKind::Note,
37+
// `MONO_ITEM` makes annotations in `codegen-units` tests syntactically correct,
38+
// but those tests never use the error kind later on.
39+
"NOTE" | "note" | "MONO_ITEM" => ErrorKind::Note,
3840
"SUGGESTION" => ErrorKind::Suggestion,
3941
"WARN" | "WARNING" | "warn" | "warning" => ErrorKind::Warning,
40-
_ => return None,
41-
})
42-
}
43-
44-
pub fn expect_from_user_str(s: &str) -> ErrorKind {
45-
ErrorKind::from_user_str(s).unwrap_or_else(|| {
46-
panic!(
42+
_ => panic!(
4743
"unexpected diagnostic kind `{s}`, expected \
4844
`ERROR`, `WARN`, `NOTE`, `HELP` or `SUGGESTION`"
49-
)
50-
})
45+
),
46+
}
5147
}
5248
}
5349

@@ -67,8 +63,7 @@ impl fmt::Display for ErrorKind {
6763
pub struct Error {
6864
pub line_num: Option<usize>,
6965
/// What kind of message we expect (e.g., warning, error, suggestion).
70-
/// `None` if not specified or unknown message kind.
71-
pub kind: Option<ErrorKind>,
66+
pub kind: ErrorKind,
7267
pub msg: String,
7368
/// For some `Error`s, like secondary lines of multi-line diagnostics, line annotations
7469
/// are not mandatory, even if they would otherwise be mandatory for primary errors.
@@ -79,12 +74,7 @@ pub struct Error {
7974
impl Error {
8075
pub fn render_for_expected(&self) -> String {
8176
use colored::Colorize;
82-
format!(
83-
"{: <10}line {: >3}: {}",
84-
self.kind.map(|kind| kind.to_string()).unwrap_or_default(),
85-
self.line_num_str(),
86-
self.msg.cyan(),
87-
)
77+
format!("{: <10}line {: >3}: {}", self.kind, self.line_num_str(), self.msg.cyan())
8878
}
8979

9080
pub fn line_num_str(&self) -> String {
@@ -173,9 +163,10 @@ fn parse_expected(
173163
// Get the part of the comment after the sigil (e.g. `~^^` or ~|).
174164
let tag = captures.get(0).unwrap();
175165
let rest = line[tag.end()..].trim_start();
176-
let (kind_str, _) = rest.split_once(|c: char| !c.is_ascii_alphabetic()).unwrap_or((rest, ""));
166+
let (kind_str, _) =
167+
rest.split_once(|c: char| c != '_' && !c.is_ascii_alphabetic()).unwrap_or((rest, ""));
177168
let kind = ErrorKind::from_user_str(kind_str);
178-
let untrimmed_msg = if kind.is_some() { &rest[kind_str.len()..] } else { rest };
169+
let untrimmed_msg = &rest[kind_str.len()..];
179170
let msg = untrimmed_msg.strip_prefix(':').unwrap_or(untrimmed_msg).trim().to_owned();
180171

181172
let line_num_adjust = &captures["adjust"];

0 commit comments

Comments
 (0)