Skip to content

Commit 08e46fa

Browse files
committed
Change how force-warn lint diagnostics are recorded.
`is_force_warn` is only possible for diagnostics with `Level::Warning`, but it is currently stored in `Diagnostic::code`, which every diagnostic has. This commit: - removes the boolean `DiagnosticId::Lint::is_force_warn` field; - adds a `ForceWarning` variant to `Level`. Benefits: - The common `Level::Warning` case now has no arguments, replacing lots of `Warning(None)` occurrences. - `rustc_session::lint::Level` and `rustc_errors::Level` are more similar, both having `ForceWarning` and `Warning`.
1 parent 1c30987 commit 08e46fa

File tree

12 files changed

+45
-53
lines changed

12 files changed

+45
-53
lines changed

compiler/rustc_builtin_macros/src/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ fn not_testable_error(cx: &ExtCtxt<'_>, attr_sp: Span, item: Option<&ast::Item>)
394394
let level = match item.map(|i| &i.kind) {
395395
// These were a warning before #92959 and need to continue being that to avoid breaking
396396
// stable user code (#94508).
397-
Some(ast::ItemKind::MacCall(_)) => Level::Warning(None),
397+
Some(ast::ItemKind::MacCall(_)) => Level::Warning,
398398
_ => Level::Error,
399399
};
400400
let mut err = DiagnosticBuilder::<()>::new(dcx, level, msg);

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ fn report_inline_asm(
417417
}
418418
let level = match level {
419419
llvm::DiagnosticLevel::Error => Level::Error,
420-
llvm::DiagnosticLevel::Warning => Level::Warning(None),
420+
llvm::DiagnosticLevel::Warning => Level::Warning,
421421
llvm::DiagnosticLevel::Note | llvm::DiagnosticLevel::Remark => Level::Note,
422422
};
423423
cgcx.diag_emitter.inline_asm_error(cookie as u32, msg, level, source);

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,14 +1847,9 @@ impl SharedEmitterMain {
18471847
dcx.emit_diagnostic(d);
18481848
}
18491849
Ok(SharedEmitterMessage::InlineAsmError(cookie, msg, level, source)) => {
1850-
let err_level = match level {
1851-
Level::Error => Level::Error,
1852-
Level::Warning(_) => Level::Warning(None),
1853-
Level::Note => Level::Note,
1854-
_ => bug!("Invalid inline asm diagnostic level"),
1855-
};
1850+
assert!(matches!(level, Level::Error | Level::Warning | Level::Note));
18561851
let msg = msg.strip_prefix("error: ").unwrap_or(&msg).to_string();
1857-
let mut err = DiagnosticBuilder::<()>::new(sess.dcx(), err_level, msg);
1852+
let mut err = DiagnosticBuilder::<()>::new(sess.dcx(), level, msg);
18581853

18591854
// If the cookie is 0 then we don't have span information.
18601855
if cookie != 0 {

compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn source_string(file: Lrc<SourceFile>, line: &Line) -> String {
8787
fn annotation_type_for_level(level: Level) -> AnnotationType {
8888
match level {
8989
Level::Bug | Level::DelayedBug | Level::Fatal | Level::Error => AnnotationType::Error,
90-
Level::Warning(_) => AnnotationType::Warning,
90+
Level::ForceWarning(_) | Level::Warning => AnnotationType::Warning,
9191
Level::Note | Level::OnceNote => AnnotationType::Note,
9292
Level::Help | Level::OnceHelp => AnnotationType::Help,
9393
// FIXME(#59346): Not sure how to map this level

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ pub enum DiagnosticId {
152152
name: String,
153153
/// Indicates whether this lint should show up in cargo's future breakage report.
154154
has_future_breakage: bool,
155-
is_force_warn: bool,
156155
},
157156
}
158157

@@ -248,7 +247,8 @@ impl Diagnostic {
248247
true
249248
}
250249

251-
Level::Warning(_)
250+
Level::ForceWarning(_)
251+
| Level::Warning
252252
| Level::Note
253253
| Level::OnceNote
254254
| Level::Help
@@ -262,7 +262,7 @@ impl Diagnostic {
262262
&mut self,
263263
unstable_to_stable: &FxIndexMap<LintExpectationId, LintExpectationId>,
264264
) {
265-
if let Level::Expect(expectation_id) | Level::Warning(Some(expectation_id)) =
265+
if let Level::Expect(expectation_id) | Level::ForceWarning(Some(expectation_id)) =
266266
&mut self.level
267267
{
268268
if expectation_id.is_stable() {
@@ -292,8 +292,11 @@ impl Diagnostic {
292292
}
293293

294294
pub(crate) fn is_force_warn(&self) -> bool {
295-
match self.code {
296-
Some(DiagnosticId::Lint { is_force_warn, .. }) => is_force_warn,
295+
match self.level {
296+
Level::ForceWarning(_) => {
297+
assert!(self.is_lint);
298+
true
299+
}
297300
_ => false,
298301
}
299302
}
@@ -472,7 +475,7 @@ impl Diagnostic {
472475
/// Add a warning attached to this diagnostic.
473476
#[rustc_lint_diagnostics]
474477
pub fn warn(&mut self, msg: impl Into<SubdiagnosticMessage>) -> &mut Self {
475-
self.sub(Level::Warning(None), msg, MultiSpan::new());
478+
self.sub(Level::Warning, msg, MultiSpan::new());
476479
self
477480
}
478481

@@ -484,7 +487,7 @@ impl Diagnostic {
484487
sp: S,
485488
msg: impl Into<SubdiagnosticMessage>,
486489
) -> &mut Self {
487-
self.sub(Level::Warning(None), msg, sp.into());
490+
self.sub(Level::Warning, msg, sp.into());
488491
self
489492
}
490493

compiler/rustc_errors/src/json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl Emitter for JsonEmitter {
198198
.into_iter()
199199
.map(|mut diag| {
200200
if diag.level == crate::Level::Allow {
201-
diag.level = crate::Level::Warning(None);
201+
diag.level = crate::Level::Warning;
202202
}
203203
FutureBreakageItem {
204204
diagnostic: EmitTyped::Diagnostic(Diagnostic::from_errors_diagnostic(

compiler/rustc_errors/src/lib.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ impl DiagCtxt {
741741
#[rustc_lint_diagnostics]
742742
#[track_caller]
743743
pub fn struct_warn(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
744-
DiagnosticBuilder::new(self, Warning(None), msg)
744+
DiagnosticBuilder::new(self, Warning, msg)
745745
}
746746

747747
/// Construct a builder at the `Allow` level with the `msg`.
@@ -1004,7 +1004,7 @@ impl DiagCtxt {
10041004
(0, 0) => return,
10051005
(0, _) => inner
10061006
.emitter
1007-
.emit_diagnostic(&Diagnostic::new(Warning(None), DiagnosticMessage::Str(warnings))),
1007+
.emit_diagnostic(&Diagnostic::new(Warning, DiagnosticMessage::Str(warnings))),
10081008
(_, 0) => {
10091009
inner.emit_diagnostic(Diagnostic::new(Fatal, errors));
10101010
}
@@ -1093,7 +1093,7 @@ impl DiagCtxt {
10931093
&'a self,
10941094
warning: impl IntoDiagnostic<'a, ()>,
10951095
) -> DiagnosticBuilder<'a, ()> {
1096-
warning.into_diagnostic(self, Warning(None))
1096+
warning.into_diagnostic(self, Warning)
10971097
}
10981098

10991099
#[track_caller]
@@ -1303,10 +1303,7 @@ impl DiagCtxtInner {
13031303
self.fulfilled_expectations.insert(expectation_id.normalize());
13041304
}
13051305

1306-
if matches!(diagnostic.level, Warning(_))
1307-
&& !self.flags.can_emit_warnings
1308-
&& !diagnostic.is_force_warn()
1309-
{
1306+
if diagnostic.level == Warning && !self.flags.can_emit_warnings {
13101307
return None;
13111308
}
13121309

@@ -1354,7 +1351,7 @@ impl DiagCtxtInner {
13541351
self.emitter.emit_diagnostic(&diagnostic);
13551352
if diagnostic.is_error() {
13561353
self.deduplicated_err_count += 1;
1357-
} else if let Warning(_) = diagnostic.level {
1354+
} else if matches!(diagnostic.level, ForceWarning(_) | Warning) {
13581355
self.deduplicated_warn_count += 1;
13591356
}
13601357
}
@@ -1557,14 +1554,17 @@ pub enum Level {
15571554
/// Its `EmissionGuarantee` is `ErrorGuaranteed`.
15581555
Error,
15591556

1560-
/// A warning about the code being compiled. Does not prevent compilation from finishing.
1557+
/// A `force-warn` lint warning about the code being compiled. Does not prevent compilation
1558+
/// from finishing.
15611559
///
1562-
/// This [`LintExpectationId`] is used for expected lint diagnostics, which should
1563-
/// also emit a warning due to the `force-warn` flag. In all other cases this should
1564-
/// be `None`.
1560+
/// The [`LintExpectationId`] is used for expected lint diagnostics. In all other cases this
1561+
/// should be `None`.
1562+
ForceWarning(Option<LintExpectationId>),
1563+
1564+
/// A warning about the code being compiled. Does not prevent compilation from finishing.
15651565
///
15661566
/// Its `EmissionGuarantee` is `()`.
1567-
Warning(Option<LintExpectationId>),
1567+
Warning,
15681568

15691569
/// A message giving additional context. Rare, because notes are more commonly attached to other
15701570
/// diagnostics such as errors.
@@ -1617,7 +1617,7 @@ impl Level {
16171617
Bug | DelayedBug | Fatal | Error => {
16181618
spec.set_fg(Some(Color::Red)).set_intense(true);
16191619
}
1620-
Warning(_) => {
1620+
ForceWarning(_) | Warning => {
16211621
spec.set_fg(Some(Color::Yellow)).set_intense(cfg!(windows));
16221622
}
16231623
Note | OnceNote => {
@@ -1636,7 +1636,7 @@ impl Level {
16361636
match self {
16371637
Bug | DelayedBug => "error: internal compiler error",
16381638
Fatal | Error => "error",
1639-
Warning(_) => "warning",
1639+
ForceWarning(_) | Warning => "warning",
16401640
Note | OnceNote => "note",
16411641
Help | OnceHelp => "help",
16421642
FailureNote => "failure-note",
@@ -1650,7 +1650,7 @@ impl Level {
16501650

16511651
pub fn get_expectation_id(&self) -> Option<LintExpectationId> {
16521652
match self {
1653-
Expect(id) | Warning(Some(id)) => Some(*id),
1653+
Expect(id) | ForceWarning(Some(id)) => Some(*id),
16541654
_ => None,
16551655
}
16561656
}

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ impl ToInternal<rustc_errors::Level> for Level {
380380
fn to_internal(self) -> rustc_errors::Level {
381381
match self {
382382
Level::Error => rustc_errors::Level::Error,
383-
Level::Warning => rustc_errors::Level::Warning(None),
383+
Level::Warning => rustc_errors::Level::Warning,
384384
Level::Note => rustc_errors::Level::Note,
385385
Level::Help => rustc_errors::Level::Help,
386386
_ => unreachable!("unknown proc_macro::Level variant: {:?}", self),

compiler/rustc_middle/src/lint.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,9 @@ pub fn struct_lint_level(
312312
// create a `DiagnosticBuilder` and continue as we would for warnings.
313313
rustc_errors::Level::Expect(expect_id)
314314
}
315-
Level::ForceWarn(Some(expect_id)) => rustc_errors::Level::Warning(Some(expect_id)),
316-
Level::Warn | Level::ForceWarn(None) => rustc_errors::Level::Warning(None),
315+
Level::ForceWarn(Some(expect_id)) => rustc_errors::Level::ForceWarning(Some(expect_id)),
316+
Level::ForceWarn(None) => rustc_errors::Level::ForceWarning(None),
317+
Level::Warn => rustc_errors::Level::Warning,
317318
Level::Deny | Level::Forbid => rustc_errors::Level::Error,
318319
};
319320
let mut err = DiagnosticBuilder::new(sess.dcx(), err_level, "");
@@ -350,22 +351,19 @@ pub fn struct_lint_level(
350351
// suppressed the lint due to macros.
351352
err.primary_message(msg);
352353

354+
let name = lint.name_lower();
355+
err.code(DiagnosticId::Lint { name, has_future_breakage });
356+
353357
// Lint diagnostics that are covered by the expect level will not be emitted outside
354358
// the compiler. It is therefore not necessary to add any information for the user.
355359
// This will therefore directly call the decorate function which will in turn emit
356360
// the `Diagnostic`.
357361
if let Level::Expect(_) = level {
358-
let name = lint.name_lower();
359-
err.code(DiagnosticId::Lint { name, has_future_breakage, is_force_warn: false });
360362
decorate(&mut err);
361363
err.emit();
362364
return;
363365
}
364366

365-
let name = lint.name_lower();
366-
let is_force_warn = matches!(level, Level::ForceWarn(_));
367-
err.code(DiagnosticId::Lint { name, has_future_breakage, is_force_warn });
368-
369367
if let Some(future_incompatible) = future_incompatible {
370368
let explanation = match future_incompatible.reason {
371369
FutureIncompatibilityReason::FutureReleaseErrorDontReportInDeps

compiler/rustc_session/src/parse.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,7 @@ pub fn feature_warn_issue(
143143
// Decorate this as a future-incompatibility lint as in rustc_middle::lint::struct_lint_level
144144
let lint = UNSTABLE_SYNTAX_PRE_EXPANSION;
145145
let future_incompatible = lint.future_incompatible.as_ref().unwrap();
146-
err.code(DiagnosticId::Lint {
147-
name: lint.name_lower(),
148-
has_future_breakage: false,
149-
is_force_warn: false,
150-
});
146+
err.code(DiagnosticId::Lint { name: lint.name_lower(), has_future_breakage: false });
151147
err.warn(lint.desc);
152148
err.note(format!("for more information, see {}", future_incompatible.reference));
153149

src/tools/miri/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ pub fn report_msg<'tcx>(
455455
let sess = machine.tcx.sess;
456456
let level = match diag_level {
457457
DiagLevel::Error => Level::Error,
458-
DiagLevel::Warning => Level::Warning(None),
458+
DiagLevel::Warning => Level::Warning,
459459
DiagLevel::Note => Level::Note,
460460
};
461461
let mut err = DiagnosticBuilder::<()>::new(sess.dcx(), level, title);

src/tools/rustfmt/src/parse/session.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ mod tests {
446446
Some(ignore_list),
447447
);
448448
let span = MultiSpan::from_span(mk_sp(BytePos(0), BytePos(1)));
449-
let non_fatal_diagnostic = build_diagnostic(DiagnosticLevel::Warning(None), Some(span));
449+
let non_fatal_diagnostic = build_diagnostic(DiagnosticLevel::Warning, Some(span));
450450
emitter.emit_diagnostic(&non_fatal_diagnostic);
451451
assert_eq!(num_emitted_errors.load(Ordering::Acquire), 0);
452452
assert_eq!(can_reset_errors.load(Ordering::Acquire), true);
@@ -470,7 +470,7 @@ mod tests {
470470
None,
471471
);
472472
let span = MultiSpan::from_span(mk_sp(BytePos(0), BytePos(1)));
473-
let non_fatal_diagnostic = build_diagnostic(DiagnosticLevel::Warning(None), Some(span));
473+
let non_fatal_diagnostic = build_diagnostic(DiagnosticLevel::Warning, Some(span));
474474
emitter.emit_diagnostic(&non_fatal_diagnostic);
475475
assert_eq!(num_emitted_errors.load(Ordering::Acquire), 1);
476476
assert_eq!(can_reset_errors.load(Ordering::Acquire), false);
@@ -507,8 +507,8 @@ mod tests {
507507
);
508508
let bar_span = MultiSpan::from_span(mk_sp(BytePos(0), BytePos(1)));
509509
let foo_span = MultiSpan::from_span(mk_sp(BytePos(21), BytePos(22)));
510-
let bar_diagnostic = build_diagnostic(DiagnosticLevel::Warning(None), Some(bar_span));
511-
let foo_diagnostic = build_diagnostic(DiagnosticLevel::Warning(None), Some(foo_span));
510+
let bar_diagnostic = build_diagnostic(DiagnosticLevel::Warning, Some(bar_span));
511+
let foo_diagnostic = build_diagnostic(DiagnosticLevel::Warning, Some(foo_span));
512512
let fatal_diagnostic = build_diagnostic(DiagnosticLevel::Fatal, None);
513513
emitter.emit_diagnostic(&bar_diagnostic);
514514
emitter.emit_diagnostic(&foo_diagnostic);

0 commit comments

Comments
 (0)