Skip to content

Commit f3eaf16

Browse files
committed
Split ExpectationLintId off Level
1 parent b6d74b5 commit f3eaf16

File tree

5 files changed

+48
-34
lines changed

5 files changed

+48
-34
lines changed

compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ fn annotation_level_for_level(level: Level) -> annotate_snippets::Level {
9191
Level::Bug | Level::Fatal | Level::Error | Level::DelayedBug => {
9292
annotate_snippets::Level::Error
9393
}
94-
Level::ForceWarning(_) | Level::Warning => annotate_snippets::Level::Warning,
94+
Level::ForceWarning | Level::Warning => annotate_snippets::Level::Warning,
9595
Level::Note | Level::OnceNote => annotate_snippets::Level::Note,
9696
Level::Help | Level::OnceHelp => annotate_snippets::Level::Help,
9797
// FIXME(#59346): Not sure how to map this level
9898
Level::FailureNote => annotate_snippets::Level::Error,
9999
Level::Allow => panic!("Should not call with Allow"),
100-
Level::Expect(_) => panic!("Should not call with Expect"),
100+
Level::Expect => panic!("Should not call with Expect"),
101101
}
102102
}
103103

compiler/rustc_errors/src/diagnostic.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::thread::panicking;
99

1010
use rustc_data_structures::fx::FxIndexMap;
1111
use rustc_error_messages::{FluentValue, fluent_value_from_str_list_sep_by_and};
12-
use rustc_lint_defs::Applicability;
12+
use rustc_lint_defs::{Applicability, LintExpectationId};
1313
use rustc_macros::{Decodable, Encodable};
1414
use rustc_span::source_map::Spanned;
1515
use rustc_span::{DUMMY_SP, Span, Symbol};
@@ -296,6 +296,7 @@ pub struct DiagInner {
296296

297297
pub messages: Vec<(DiagMessage, Style)>,
298298
pub code: Option<ErrCode>,
299+
pub lint_id: Option<LintExpectationId>,
299300
pub span: MultiSpan,
300301
pub children: Vec<Subdiag>,
301302
pub suggestions: Suggestions,
@@ -324,6 +325,7 @@ impl DiagInner {
324325
pub fn new_with_messages(level: Level, messages: Vec<(DiagMessage, Style)>) -> Self {
325326
DiagInner {
326327
level,
328+
lint_id: None,
327329
messages,
328330
code: None,
329331
span: MultiSpan::new(),
@@ -346,15 +348,15 @@ impl DiagInner {
346348
match self.level {
347349
Level::Bug | Level::Fatal | Level::Error | Level::DelayedBug => true,
348350

349-
Level::ForceWarning(_)
351+
Level::ForceWarning
350352
| Level::Warning
351353
| Level::Note
352354
| Level::OnceNote
353355
| Level::Help
354356
| Level::OnceHelp
355357
| Level::FailureNote
356358
| Level::Allow
357-
| Level::Expect(_) => false,
359+
| Level::Expect => false,
358360
}
359361
}
360362

@@ -365,7 +367,7 @@ impl DiagInner {
365367

366368
pub(crate) fn is_force_warn(&self) -> bool {
367369
match self.level {
368-
Level::ForceWarning(_) => {
370+
Level::ForceWarning => {
369371
assert!(self.is_lint.is_some());
370372
true
371373
}
@@ -1259,6 +1261,17 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
12591261
self
12601262
} }
12611263

1264+
with_fn! { with_lint_id,
1265+
/// Add an argument.
1266+
#[rustc_lint_diagnostics]
1267+
pub fn lint_id(
1268+
&mut self,
1269+
id: LintExpectationId,
1270+
) -> &mut Self {
1271+
self.lint_id = Some(id);
1272+
self
1273+
} }
1274+
12621275
with_fn! { with_primary_message,
12631276
/// Add a primary message.
12641277
#[rustc_lint_diagnostics]

compiler/rustc_errors/src/json.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl Emitter for JsonEmitter {
144144
//
145145
// So to avoid ICEs and confused users we "upgrade" the lint level for
146146
// those `FutureBreakageItem` to warn.
147-
if matches!(diag.level, crate::Level::Allow | crate::Level::Expect(..)) {
147+
if matches!(diag.level, crate::Level::Allow | crate::Level::Expect) {
148148
diag.level = crate::Level::Warning;
149149
}
150150
FutureBreakageItem {

compiler/rustc_errors/src/lib.rs

+19-20
Original file line numberDiff line numberDiff line change
@@ -905,8 +905,8 @@ impl<'a> DiagCtxtHandle<'a> {
905905
DelayedBug => {
906906
return self.inner.borrow_mut().emit_diagnostic(diag, self.tainted_with_errors);
907907
}
908-
ForceWarning(_) | Warning | Note | OnceNote | Help | OnceHelp | FailureNote | Allow
909-
| Expect(_) => None,
908+
ForceWarning | Warning | Note | OnceNote | Help | OnceHelp | FailureNote | Allow
909+
| Expect => None,
910910
};
911911

912912
// FIXME(Centril, #69537): Consider reintroducing panic on overwriting a stashed diagnostic
@@ -1045,7 +1045,7 @@ impl<'a> DiagCtxtHandle<'a> {
10451045
// Use `ForceWarning` rather than `Warning` to guarantee emission, e.g. with a
10461046
// configuration like `--cap-lints allow --force-warn bare_trait_objects`.
10471047
inner.emit_diagnostic(
1048-
DiagInner::new(ForceWarning(None), DiagMessage::Str(warnings)),
1048+
DiagInner::new(ForceWarning, DiagMessage::Str(warnings)),
10491049
None,
10501050
);
10511051
}
@@ -1450,7 +1450,7 @@ impl<'a> DiagCtxtHandle<'a> {
14501450
#[rustc_lint_diagnostics]
14511451
#[track_caller]
14521452
pub fn struct_expect(self, msg: impl Into<DiagMessage>, id: LintExpectationId) -> Diag<'a, ()> {
1453-
Diag::new(self, Expect(id), msg)
1453+
Diag::new(self, Expect, msg).with_lint_id(id)
14541454
}
14551455
}
14561456

@@ -1510,7 +1510,7 @@ impl DiagCtxtInner {
15101510
// Future breakages aren't emitted if they're `Level::Allow` or
15111511
// `Level::Expect`, but they still need to be constructed and
15121512
// stashed below, so they'll trigger the must_produce_diag check.
1513-
assert_matches!(diagnostic.level, Error | Warning | Allow | Expect(_));
1513+
assert_matches!(diagnostic.level, Error | Warning | Allow | Expect);
15141514
self.future_breakage_diagnostics.push(diagnostic.clone());
15151515
}
15161516

@@ -1558,7 +1558,7 @@ impl DiagCtxtInner {
15581558
};
15591559
}
15601560
}
1561-
ForceWarning(None) => {} // `ForceWarning(Some(...))` is below, with `Expect`
1561+
ForceWarning if diagnostic.lint_id.is_none() => {} // `ForceWarning(Some(...))` is below, with `Expect`
15621562
Warning => {
15631563
if !self.flags.can_emit_warnings {
15641564
// We are not emitting warnings.
@@ -1580,9 +1580,9 @@ impl DiagCtxtInner {
15801580
}
15811581
return None;
15821582
}
1583-
Expect(expect_id) | ForceWarning(Some(expect_id)) => {
1584-
self.fulfilled_expectations.insert(expect_id);
1585-
if let Expect(_) = diagnostic.level {
1583+
Expect | ForceWarning => {
1584+
self.fulfilled_expectations.insert(diagnostic.lint_id.unwrap());
1585+
if let Expect = diagnostic.level {
15861586
// Nothing emitted here for expected lints.
15871587
TRACK_DIAGNOSTIC(diagnostic, &mut |_| None);
15881588
self.suppressed_expected_diag = true;
@@ -1631,7 +1631,7 @@ impl DiagCtxtInner {
16311631

16321632
if is_error {
16331633
self.deduplicated_err_count += 1;
1634-
} else if matches!(diagnostic.level, ForceWarning(_) | Warning) {
1634+
} else if matches!(diagnostic.level, ForceWarning | Warning) {
16351635
self.deduplicated_warn_count += 1;
16361636
}
16371637
self.has_printed = true;
@@ -1899,9 +1899,9 @@ pub enum Level {
18991899
/// A `force-warn` lint warning about the code being compiled. Does not prevent compilation
19001900
/// from finishing.
19011901
///
1902-
/// The [`LintExpectationId`] is used for expected lint diagnostics. In all other cases this
1902+
/// Requires a [`LintExpectationId`] for expected lint diagnostics. In all other cases this
19031903
/// should be `None`.
1904-
ForceWarning(Option<LintExpectationId>),
1904+
ForceWarning,
19051905

19061906
/// A warning about the code being compiled. Does not prevent compilation from finishing.
19071907
/// Will be skipped if `can_emit_warnings` is false.
@@ -1926,8 +1926,8 @@ pub enum Level {
19261926
/// Only used for lints.
19271927
Allow,
19281928

1929-
/// Only used for lints.
1930-
Expect(LintExpectationId),
1929+
/// Only used for lints. Requires a [`LintExpectationId`] for silencing the lints.
1930+
Expect,
19311931
}
19321932

19331933
impl fmt::Display for Level {
@@ -1943,7 +1943,7 @@ impl Level {
19431943
Bug | Fatal | Error | DelayedBug => {
19441944
spec.set_fg(Some(Color::Red)).set_intense(true);
19451945
}
1946-
ForceWarning(_) | Warning => {
1946+
ForceWarning | Warning => {
19471947
spec.set_fg(Some(Color::Yellow)).set_intense(cfg!(windows));
19481948
}
19491949
Note | OnceNote => {
@@ -1953,7 +1953,7 @@ impl Level {
19531953
spec.set_fg(Some(Color::Cyan)).set_intense(true);
19541954
}
19551955
FailureNote => {}
1956-
Allow | Expect(_) => unreachable!(),
1956+
Allow | Expect => unreachable!(),
19571957
}
19581958
spec
19591959
}
@@ -1962,11 +1962,11 @@ impl Level {
19621962
match self {
19631963
Bug | DelayedBug => "error: internal compiler error",
19641964
Fatal | Error => "error",
1965-
ForceWarning(_) | Warning => "warning",
1965+
ForceWarning | Warning => "warning",
19661966
Note | OnceNote => "note",
19671967
Help | OnceHelp => "help",
19681968
FailureNote => "failure-note",
1969-
Allow | Expect(_) => unreachable!(),
1969+
Allow | Expect => unreachable!(),
19701970
}
19711971
}
19721972

@@ -1977,8 +1977,7 @@ impl Level {
19771977
// Can this level be used in a subdiagnostic message?
19781978
fn can_be_subdiag(&self) -> bool {
19791979
match self {
1980-
Bug | DelayedBug | Fatal | Error | ForceWarning(_) | FailureNote | Allow
1981-
| Expect(_) => false,
1980+
Bug | DelayedBug | Fatal | Error | ForceWarning | FailureNote | Allow | Expect => false,
19821981

19831982
Warning | Note | Help | OnceNote | OnceHelp => true,
19841983
}

compiler/rustc_middle/src/lint.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,10 @@ pub fn lint_level(
293293
);
294294

295295
// Convert lint level to error level.
296-
let err_level = match level {
296+
let (err_level, lint_id) = match level {
297297
Level::Allow => {
298298
if has_future_breakage {
299-
rustc_errors::Level::Allow
299+
(rustc_errors::Level::Allow, None)
300300
} else {
301301
return;
302302
}
@@ -309,17 +309,19 @@ pub fn lint_level(
309309
// We can also not mark the lint expectation as fulfilled here right away, as it
310310
// can still be cancelled in the decorate function. All of this means that we simply
311311
// create a `Diag` and continue as we would for warnings.
312-
rustc_errors::Level::Expect(expect_id)
312+
(rustc_errors::Level::Expect, Some(expect_id))
313313
}
314-
Level::ForceWarn(Some(expect_id)) => rustc_errors::Level::ForceWarning(Some(expect_id)),
315-
Level::ForceWarn(None) => rustc_errors::Level::ForceWarning(None),
316-
Level::Warn => rustc_errors::Level::Warning,
317-
Level::Deny | Level::Forbid => rustc_errors::Level::Error,
314+
Level::ForceWarn(expect_id) => (rustc_errors::Level::ForceWarning, expect_id),
315+
Level::Warn => (rustc_errors::Level::Warning, None),
316+
Level::Deny | Level::Forbid => (rustc_errors::Level::Error, None),
318317
};
319318
let mut err = Diag::new(sess.dcx(), err_level, "");
320319
if let Some(span) = span {
321320
err.span(span);
322321
}
322+
if let Some(lint_id) = lint_id {
323+
err.lint_id(lint_id);
324+
}
323325

324326
// If this code originates in a foreign macro, aka something that this crate
325327
// did not itself author, then it's likely that there's nothing this crate

0 commit comments

Comments
 (0)