Skip to content

Commit 0e388f2

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 06cf881 commit 0e388f2

File tree

12 files changed

+45
-53
lines changed

12 files changed

+45
-53
lines changed

compiler/rustc_builtin_macros/src/test.rs

+1-1
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

+1-1
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

+2-7
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

+1-1
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

+10-7
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

+1-1
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

+16-16
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ impl DiagCtxt {
738738
#[rustc_lint_diagnostics]
739739
#[track_caller]
740740
pub fn struct_warn(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
741-
DiagnosticBuilder::new(self, Warning(None), msg)
741+
DiagnosticBuilder::new(self, Warning, msg)
742742
}
743743

744744
/// Construct a builder at the `Allow` level with the `msg`.
@@ -1005,7 +1005,7 @@ impl DiagCtxt {
10051005
(0, 0) => return,
10061006
(0, _) => inner
10071007
.emitter
1008-
.emit_diagnostic(&Diagnostic::new(Warning(None), DiagnosticMessage::Str(warnings))),
1008+
.emit_diagnostic(&Diagnostic::new(Warning, DiagnosticMessage::Str(warnings))),
10091009
(_, 0) => {
10101010
inner.emit_diagnostic(Diagnostic::new(Fatal, errors));
10111011
}
@@ -1094,7 +1094,7 @@ impl DiagCtxt {
10941094
&'a self,
10951095
warning: impl IntoDiagnostic<'a, ()>,
10961096
) -> DiagnosticBuilder<'a, ()> {
1097-
warning.into_diagnostic(self, Warning(None))
1097+
warning.into_diagnostic(self, Warning)
10981098
}
10991099

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

1307-
if matches!(diagnostic.level, Warning(_))
1308-
&& !self.flags.can_emit_warnings
1309-
&& !diagnostic.is_force_warn()
1310-
{
1307+
if diagnostic.level == Warning && !self.flags.can_emit_warnings {
13111308
if diagnostic.has_future_breakage() {
13121309
(*TRACK_DIAGNOSTIC)(diagnostic, &mut |_| {});
13131310
}
@@ -1359,7 +1356,7 @@ impl DiagCtxtInner {
13591356
self.emitter.emit_diagnostic(&diagnostic);
13601357
if diagnostic.is_error() {
13611358
self.deduplicated_err_count += 1;
1362-
} else if let Warning(_) = diagnostic.level {
1359+
} else if matches!(diagnostic.level, ForceWarning(_) | Warning) {
13631360
self.deduplicated_warn_count += 1;
13641361
}
13651362
}
@@ -1562,14 +1559,17 @@ pub enum Level {
15621559
/// Its `EmissionGuarantee` is `ErrorGuaranteed`.
15631560
Error,
15641561

1565-
/// A warning about the code being compiled. Does not prevent compilation from finishing.
1562+
/// A `force-warn` lint warning about the code being compiled. Does not prevent compilation
1563+
/// from finishing.
15661564
///
1567-
/// This [`LintExpectationId`] is used for expected lint diagnostics, which should
1568-
/// also emit a warning due to the `force-warn` flag. In all other cases this should
1569-
/// be `None`.
1565+
/// The [`LintExpectationId`] is used for expected lint diagnostics. In all other cases this
1566+
/// should be `None`.
1567+
ForceWarning(Option<LintExpectationId>),
1568+
1569+
/// A warning about the code being compiled. Does not prevent compilation from finishing.
15701570
///
15711571
/// Its `EmissionGuarantee` is `()`.
1572-
Warning(Option<LintExpectationId>),
1572+
Warning,
15731573

15741574
/// A message giving additional context. Rare, because notes are more commonly attached to other
15751575
/// diagnostics such as errors.
@@ -1622,7 +1622,7 @@ impl Level {
16221622
Bug | DelayedBug | Fatal | Error => {
16231623
spec.set_fg(Some(Color::Red)).set_intense(true);
16241624
}
1625-
Warning(_) => {
1625+
ForceWarning(_) | Warning => {
16261626
spec.set_fg(Some(Color::Yellow)).set_intense(cfg!(windows));
16271627
}
16281628
Note | OnceNote => {
@@ -1641,7 +1641,7 @@ impl Level {
16411641
match self {
16421642
Bug | DelayedBug => "error: internal compiler error",
16431643
Fatal | Error => "error",
1644-
Warning(_) => "warning",
1644+
ForceWarning(_) | Warning => "warning",
16451645
Note | OnceNote => "note",
16461646
Help | OnceHelp => "help",
16471647
FailureNote => "failure-note",
@@ -1655,7 +1655,7 @@ impl Level {
16551655

16561656
pub fn get_expectation_id(&self) -> Option<LintExpectationId> {
16571657
match self {
1658-
Expect(id) | Warning(Some(id)) => Some(*id),
1658+
Expect(id) | ForceWarning(Some(id)) => Some(*id),
16591659
_ => None,
16601660
}
16611661
}

compiler/rustc_expand/src/proc_macro_server.rs

+1-1
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

+6-8
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

+1-5
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

+1-1
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

+4-4
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)