Skip to content

Commit 16f4b02

Browse files
committedJan 17, 2024
Auto merge of #119922 - nnethercote:fix-Diag-code-is_lint, r=oli-obk
Rework how diagnostic lints are stored. `Diagnostic::code` has the type `DiagnosticId`, which has `Error` and `Lint` variants. Plus `Diagnostic::is_lint` is a bool, which should be redundant w.r.t. `Diagnostic::code`. Seems simple. Except it's possible for a lint to have an error code, in which case its `code` field is recorded as `Error`, and `is_lint` is required to indicate that it's a lint. This is what happens with `derive(LintDiagnostic)` lints. Which means those lints don't have a lint name or a `has_future_breakage` field because those are stored in the `DiagnosticId::Lint`. It's all a bit messy and confused and seems unintentional. This commit: - removes `DiagnosticId`; - changes `Diagnostic::code` to `Option<String>`, which means both errors and lints can straightforwardly have an error code; - changes `Diagnostic::is_lint` to `Option<IsLint>`, where `IsLint` is a new type containing a lint name and a `has_future_breakage` bool, so all lints can have those, error code or not. r? `@oli-obk`
·
1.90.01.77.0
2 parents f45fe57 + d71f535 commit 16f4b02

File tree

24 files changed

+111
-135
lines changed

24 files changed

+111
-135
lines changed
 

‎compiler/rustc_codegen_ssa/src/back/write.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_data_structures::memmap::Mmap;
1414
use rustc_data_structures::profiling::{SelfProfilerRef, VerboseTimingGuard};
1515
use rustc_data_structures::sync::Lrc;
1616
use rustc_errors::emitter::Emitter;
17-
use rustc_errors::{translation::Translate, DiagCtxt, DiagnosticId, FatalError, Level};
17+
use rustc_errors::{translation::Translate, DiagCtxt, FatalError, Level};
1818
use rustc_errors::{DiagnosticBuilder, DiagnosticMessage, Style};
1919
use rustc_fs_util::link_or_copy;
2020
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
@@ -1000,7 +1000,7 @@ type DiagnosticArgName<'source> = Cow<'source, str>;
10001000
struct Diagnostic {
10011001
msgs: Vec<(DiagnosticMessage, Style)>,
10021002
args: FxHashMap<DiagnosticArgName<'static>, rustc_errors::DiagnosticArgValue<'static>>,
1003-
code: Option<DiagnosticId>,
1003+
code: Option<String>,
10041004
lvl: Level,
10051005
}
10061006

‎compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use crate::emitter::FileWithAnnotatedLines;
99
use crate::snippet::Line;
1010
use crate::translation::{to_fluent_args, Translate};
1111
use crate::{
12-
CodeSuggestion, Diagnostic, DiagnosticId, DiagnosticMessage, Emitter, FluentBundle,
13-
LazyFallbackBundle, Level, MultiSpan, Style, SubDiagnostic,
12+
CodeSuggestion, Diagnostic, DiagnosticMessage, Emitter, FluentBundle, LazyFallbackBundle,
13+
Level, MultiSpan, Style, SubDiagnostic,
1414
};
1515
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation};
1616
use rustc_data_structures::sync::Lrc;
@@ -127,7 +127,7 @@ impl AnnotateSnippetEmitter {
127127
level: &Level,
128128
messages: &[(DiagnosticMessage, Style)],
129129
args: &FluentArgs<'_>,
130-
code: &Option<DiagnosticId>,
130+
code: &Option<String>,
131131
msp: &MultiSpan,
132132
_children: &[SubDiagnostic],
133133
_suggestions: &[CodeSuggestion],
@@ -181,11 +181,7 @@ impl AnnotateSnippetEmitter {
181181
let snippet = Snippet {
182182
title: Some(Annotation {
183183
label: Some(&message),
184-
id: code.as_ref().map(|c| match c {
185-
DiagnosticId::Error(val) | DiagnosticId::Lint { name: val, .. } => {
186-
val.as_str()
187-
}
188-
}),
184+
id: code.as_deref(),
189185
annotation_type: annotation_type_for_level(*level),
190186
}),
191187
footer: vec![],

‎compiler/rustc_errors/src/diagnostic.rs‎

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub struct Diagnostic {
104104
pub(crate) level: Level,
105105

106106
pub messages: Vec<(DiagnosticMessage, Style)>,
107-
pub code: Option<DiagnosticId>,
107+
pub code: Option<String>,
108108
pub span: MultiSpan,
109109
pub children: Vec<SubDiagnostic>,
110110
pub suggestions: Result<Vec<CodeSuggestion>, SuggestionsDisabled>,
@@ -115,9 +115,9 @@ pub struct Diagnostic {
115115
/// `span` if there is one. Otherwise, it is `DUMMY_SP`.
116116
pub sort_span: Span,
117117

118-
/// If diagnostic is from Lint, custom hash function ignores notes
119-
/// otherwise hash is based on the all the fields
120-
pub is_lint: bool,
118+
/// If diagnostic is from Lint, custom hash function ignores children.
119+
/// Otherwise hash is based on the all the fields.
120+
pub is_lint: Option<IsLint>,
121121

122122
/// With `-Ztrack_diagnostics` enabled,
123123
/// we print where in rustc this error was emitted.
@@ -146,13 +146,11 @@ impl fmt::Display for DiagnosticLocation {
146146
}
147147

148148
#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
149-
pub enum DiagnosticId {
150-
Error(String),
151-
Lint {
152-
name: String,
153-
/// Indicates whether this lint should show up in cargo's future breakage report.
154-
has_future_breakage: bool,
155-
},
149+
pub struct IsLint {
150+
/// The lint name.
151+
pub(crate) name: String,
152+
/// Indicates whether this lint should show up in cargo's future breakage report.
153+
has_future_breakage: bool,
156154
}
157155

158156
/// A "sub"-diagnostic attached to a parent diagnostic.
@@ -231,7 +229,7 @@ impl Diagnostic {
231229
suggestions: Ok(vec![]),
232230
args: Default::default(),
233231
sort_span: DUMMY_SP,
234-
is_lint: false,
232+
is_lint: None,
235233
emitted_at: DiagnosticLocation::caller(),
236234
}
237235
}
@@ -288,16 +286,13 @@ impl Diagnostic {
288286

289287
/// Indicates whether this diagnostic should show up in cargo's future breakage report.
290288
pub(crate) fn has_future_breakage(&self) -> bool {
291-
match self.code {
292-
Some(DiagnosticId::Lint { has_future_breakage, .. }) => has_future_breakage,
293-
_ => false,
294-
}
289+
matches!(self.is_lint, Some(IsLint { has_future_breakage: true, .. }))
295290
}
296291

297292
pub(crate) fn is_force_warn(&self) -> bool {
298293
match self.level {
299294
Level::ForceWarning(_) => {
300-
assert!(self.is_lint);
295+
assert!(self.is_lint.is_some());
301296
true
302297
}
303298
_ => false,
@@ -893,12 +888,12 @@ impl Diagnostic {
893888
self
894889
}
895890

896-
pub fn is_lint(&mut self) -> &mut Self {
897-
self.is_lint = true;
891+
pub fn is_lint(&mut self, name: String, has_future_breakage: bool) -> &mut Self {
892+
self.is_lint = Some(IsLint { name, has_future_breakage });
898893
self
899894
}
900895

901-
pub fn code(&mut self, s: DiagnosticId) -> &mut Self {
896+
pub fn code(&mut self, s: String) -> &mut Self {
902897
self.code = Some(s);
903898
self
904899
}
@@ -908,8 +903,8 @@ impl Diagnostic {
908903
self
909904
}
910905

911-
pub fn get_code(&self) -> Option<DiagnosticId> {
912-
self.code.clone()
906+
pub fn get_code(&self) -> Option<&str> {
907+
self.code.as_deref()
913908
}
914909

915910
pub fn primary_message(&mut self, msg: impl Into<DiagnosticMessage>) -> &mut Self {
@@ -995,7 +990,8 @@ impl Diagnostic {
995990
&Level,
996991
&[(DiagnosticMessage, Style)],
997992
Vec<(&Cow<'static, str>, &DiagnosticArgValue<'static>)>,
998-
&Option<DiagnosticId>,
993+
&Option<String>,
994+
&Option<IsLint>,
999995
&MultiSpan,
1000996
&Result<Vec<CodeSuggestion>, SuggestionsDisabled>,
1001997
Option<&[SubDiagnostic]>,
@@ -1005,9 +1001,10 @@ impl Diagnostic {
10051001
&self.messages,
10061002
self.args().collect(),
10071003
&self.code,
1004+
&self.is_lint,
10081005
&self.span,
10091006
&self.suggestions,
1010-
(if self.is_lint { None } else { Some(&self.children) }),
1007+
(if self.is_lint.is_some() { None } else { Some(&self.children) }),
10111008
)
10121009
}
10131010
}

‎compiler/rustc_errors/src/diagnostic_builder.rs‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::diagnostic::IntoDiagnosticArg;
22
use crate::{DiagCtxt, Level, MultiSpan, StashKey};
33
use crate::{
4-
Diagnostic, DiagnosticId, DiagnosticMessage, DiagnosticStyledString, ErrorGuaranteed,
5-
ExplicitBug, SubdiagnosticMessage,
4+
Diagnostic, DiagnosticMessage, DiagnosticStyledString, ErrorGuaranteed, ExplicitBug,
5+
SubdiagnosticMessage,
66
};
77
use rustc_lint_defs::Applicability;
88
use rustc_span::source_map::Spanned;
@@ -395,8 +395,11 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
395395
forward!((span, with_span)(
396396
sp: impl Into<MultiSpan>,
397397
));
398+
forward!((is_lint, with_is_lint)(
399+
name: String, has_future_breakage: bool,
400+
));
398401
forward!((code, with_code)(
399-
s: DiagnosticId,
402+
s: String,
400403
));
401404
forward!((arg, with_arg)(
402405
name: impl Into<Cow<'static, str>>, arg: impl IntoDiagnosticArg,
@@ -437,15 +440,11 @@ impl<G: EmissionGuarantee> Drop for DiagnosticBuilder<'_, G> {
437440
#[macro_export]
438441
macro_rules! struct_span_code_err {
439442
($dcx:expr, $span:expr, $code:ident, $($message:tt)*) => ({
440-
$dcx.struct_span_err(
441-
$span,
442-
format!($($message)*),
443-
)
444-
.with_code($crate::error_code!($code))
443+
$dcx.struct_span_err($span, format!($($message)*)).with_code($crate::error_code!($code))
445444
})
446445
}
447446

448447
#[macro_export]
449448
macro_rules! error_code {
450-
($code:ident) => {{ $crate::DiagnosticId::Error(stringify!($code).to_owned()) }};
449+
($code:ident) => {{ stringify!($code).to_owned() }};
451450
}

‎compiler/rustc_errors/src/emitter.rs‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ use crate::snippet::{
1616
use crate::styled_buffer::StyledBuffer;
1717
use crate::translation::{to_fluent_args, Translate};
1818
use crate::{
19-
diagnostic::DiagnosticLocation, CodeSuggestion, DiagCtxt, Diagnostic, DiagnosticId,
20-
DiagnosticMessage, FluentBundle, LazyFallbackBundle, Level, MultiSpan, SubDiagnostic,
21-
SubstitutionHighlight, SuggestionStyle, TerminalUrl,
19+
diagnostic::DiagnosticLocation, CodeSuggestion, DiagCtxt, Diagnostic, DiagnosticMessage,
20+
FluentBundle, LazyFallbackBundle, Level, MultiSpan, SubDiagnostic, SubstitutionHighlight,
21+
SuggestionStyle, TerminalUrl,
2222
};
2323
use rustc_lint_defs::pluralize;
2424

@@ -1309,7 +1309,7 @@ impl HumanEmitter {
13091309
msp: &MultiSpan,
13101310
msgs: &[(DiagnosticMessage, Style)],
13111311
args: &FluentArgs<'_>,
1312-
code: &Option<DiagnosticId>,
1312+
code: &Option<String>,
13131313
level: &Level,
13141314
max_line_num_len: usize,
13151315
is_secondary: bool,
@@ -1336,14 +1336,13 @@ impl HumanEmitter {
13361336
buffer.append(0, level.to_str(), Style::Level(*level));
13371337
label_width += level.to_str().len();
13381338
}
1339-
// only render error codes, not lint codes
1340-
if let Some(DiagnosticId::Error(ref code)) = *code {
1339+
if let Some(code) = code {
13411340
buffer.append(0, "[", Style::Level(*level));
13421341
let code = if let TerminalUrl::Yes = self.terminal_url {
13431342
let path = "https://doc.rust-lang.org/error_codes";
1344-
format!("\x1b]8;;{path}/{code}.html\x07{code}\x1b]8;;\x07")
1343+
Cow::Owned(format!("\x1b]8;;{path}/{code}.html\x07{code}\x1b]8;;\x07"))
13451344
} else {
1346-
code.clone()
1345+
Cow::Borrowed(code)
13471346
};
13481347
buffer.append(0, &code, Style::Level(*level));
13491348
buffer.append(0, "]", Style::Level(*level));
@@ -2077,7 +2076,7 @@ impl HumanEmitter {
20772076
level: &Level,
20782077
messages: &[(DiagnosticMessage, Style)],
20792078
args: &FluentArgs<'_>,
2080-
code: &Option<DiagnosticId>,
2079+
code: &Option<String>,
20812080
span: &MultiSpan,
20822081
children: &[SubDiagnostic],
20832082
suggestions: &[CodeSuggestion],

‎compiler/rustc_errors/src/json.rs‎

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ use termcolor::{ColorSpec, WriteColor};
1515
use crate::emitter::{should_show_source_code, Emitter, HumanReadableErrorType};
1616
use crate::registry::Registry;
1717
use crate::translation::{to_fluent_args, Translate};
18-
use crate::DiagnosticId;
1918
use crate::{
20-
CodeSuggestion, FluentBundle, LazyFallbackBundle, MultiSpan, SpanLabel, SubDiagnostic,
21-
TerminalUrl,
19+
diagnostic::IsLint, CodeSuggestion, FluentBundle, LazyFallbackBundle, MultiSpan, SpanLabel,
20+
SubDiagnostic, TerminalUrl,
2221
};
2322
use rustc_lint_defs::Applicability;
2423

@@ -301,7 +300,8 @@ struct DiagnosticSpanMacroExpansion {
301300

302301
#[derive(Serialize)]
303302
struct DiagnosticCode {
304-
/// The code itself.
303+
/// The error code (e.g. "E1234"), if the diagnostic has one. Or the lint
304+
/// name, if it's a lint without an error code.
305305
code: String,
306306
/// An explanation for the code.
307307
explanation: Option<&'static str>,
@@ -399,9 +399,21 @@ impl Diagnostic {
399399
let output = String::from_utf8(output).unwrap();
400400

401401
let translated_message = je.translate_messages(&diag.messages, &args);
402+
403+
let code = if let Some(code) = &diag.code {
404+
Some(DiagnosticCode {
405+
code: code.to_string(),
406+
explanation: je.registry.as_ref().unwrap().try_find_description(&code).ok(),
407+
})
408+
} else if let Some(IsLint { name, .. }) = &diag.is_lint {
409+
Some(DiagnosticCode { code: name.to_string(), explanation: None })
410+
} else {
411+
None
412+
};
413+
402414
Diagnostic {
403415
message: translated_message.to_string(),
404-
code: DiagnosticCode::map_opt_string(diag.code.clone(), je),
416+
code,
405417
level: diag.level.to_str(),
406418
spans: DiagnosticSpan::from_multispan(&diag.span, &args, je),
407419
children: diag
@@ -592,18 +604,3 @@ impl DiagnosticSpanLine {
592604
.unwrap_or_else(|_| vec![])
593605
}
594606
}
595-
596-
impl DiagnosticCode {
597-
fn map_opt_string(s: Option<DiagnosticId>, je: &JsonEmitter) -> Option<DiagnosticCode> {
598-
s.map(|s| {
599-
let s = match s {
600-
DiagnosticId::Error(s) => s,
601-
DiagnosticId::Lint { name, .. } => name,
602-
};
603-
let je_result =
604-
je.registry.as_ref().map(|registry| registry.try_find_description(&s)).unwrap();
605-
606-
DiagnosticCode { code: s, explanation: je_result.ok() }
607-
})
608-
}
609-
}

‎compiler/rustc_errors/src/lib.rs‎

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extern crate tracing;
3030
extern crate self as rustc_errors;
3131

3232
pub use diagnostic::{
33-
AddToDiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgValue, DiagnosticId,
33+
AddToDiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgValue,
3434
DiagnosticStyledString, IntoDiagnosticArg, SubDiagnostic,
3535
};
3636
pub use diagnostic_builder::{
@@ -442,13 +442,13 @@ struct DiagCtxtInner {
442442
/// This is used for the `good_path_delayed_bugs` check.
443443
suppressed_expected_diag: bool,
444444

445-
/// This set contains the `DiagnosticId` of all emitted diagnostics to avoid
445+
/// This set contains the code of all emitted diagnostics to avoid
446446
/// emitting the same diagnostic with extended help (`--teach`) twice, which
447447
/// would be unnecessary repetition.
448-
taught_diagnostics: FxHashSet<DiagnosticId>,
448+
taught_diagnostics: FxHashSet<String>,
449449

450450
/// Used to suggest rustc --explain `<error code>`
451-
emitted_diagnostic_codes: FxIndexSet<DiagnosticId>,
451+
emitted_diagnostic_codes: FxIndexSet<String>,
452452

453453
/// This set contains a hash of every diagnostic that has been emitted by
454454
/// this `DiagCtxt`. These hashes is used to avoid emitting the same error
@@ -676,7 +676,7 @@ impl DiagCtxt {
676676
let key = (span.with_parent(None), key);
677677

678678
if diag.is_error() {
679-
if diag.is_lint {
679+
if diag.is_lint.is_some() {
680680
inner.lint_err_count += 1;
681681
} else {
682682
inner.err_count += 1;
@@ -695,7 +695,7 @@ impl DiagCtxt {
695695
let key = (span.with_parent(None), key);
696696
let diag = inner.stashed_diagnostics.remove(&key)?;
697697
if diag.is_error() {
698-
if diag.is_lint {
698+
if diag.is_lint.is_some() {
699699
inner.lint_err_count -= 1;
700700
} else {
701701
inner.err_count -= 1;
@@ -715,9 +715,7 @@ impl DiagCtxt {
715715

716716
/// Construct a builder at the `Warning` level at the given `span` and with the `msg`.
717717
///
718-
/// Attempting to `.emit()` the builder will only emit if either:
719-
/// * `can_emit_warnings` is `true`
720-
/// * `is_force_warn` was set in `DiagnosticId::Lint`
718+
/// An `emit` call on the builder will only emit if `can_emit_warnings` is `true`.
721719
#[rustc_lint_diagnostics]
722720
#[track_caller]
723721
pub fn struct_span_warn(
@@ -730,9 +728,7 @@ impl DiagCtxt {
730728

731729
/// Construct a builder at the `Warning` level with the `msg`.
732730
///
733-
/// Attempting to `.emit()` the builder will only emit if either:
734-
/// * `can_emit_warnings` is `true`
735-
/// * `is_force_warn` was set in `DiagnosticId::Lint`
731+
/// An `emit` call on the builder will only emit if `can_emit_warnings` is `true`.
736732
#[rustc_lint_diagnostics]
737733
#[track_caller]
738734
pub fn struct_warn(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
@@ -1011,11 +1007,12 @@ impl DiagCtxt {
10111007
let mut error_codes = inner
10121008
.emitted_diagnostic_codes
10131009
.iter()
1014-
.filter_map(|x| match &x {
1015-
DiagnosticId::Error(s) if registry.try_find_description(s).is_ok() => {
1016-
Some(s.clone())
1010+
.filter_map(|code| {
1011+
if registry.try_find_description(code).is_ok().clone() {
1012+
Some(code.clone())
1013+
} else {
1014+
None
10171015
}
1018-
_ => None,
10191016
})
10201017
.collect::<Vec<_>>();
10211018
if !error_codes.is_empty() {
@@ -1058,8 +1055,8 @@ impl DiagCtxt {
10581055
///
10591056
/// Used to suppress emitting the same error multiple times with extended explanation when
10601057
/// calling `-Zteach`.
1061-
pub fn must_teach(&self, code: &DiagnosticId) -> bool {
1062-
self.inner.borrow_mut().taught_diagnostics.insert(code.clone())
1058+
pub fn must_teach(&self, code: &str) -> bool {
1059+
self.inner.borrow_mut().taught_diagnostics.insert(code.to_string())
10631060
}
10641061

10651062
pub fn force_print_diagnostic(&self, db: Diagnostic) {
@@ -1231,7 +1228,7 @@ impl DiagCtxtInner {
12311228
for diag in diags {
12321229
// Decrement the count tracking the stash; emitting will increment it.
12331230
if diag.is_error() {
1234-
if diag.is_lint {
1231+
if diag.is_lint.is_some() {
12351232
self.lint_err_count -= 1;
12361233
} else {
12371234
self.err_count -= 1;
@@ -1363,7 +1360,7 @@ impl DiagCtxtInner {
13631360
self.has_printed = true;
13641361
}
13651362
if diagnostic.is_error() {
1366-
if diagnostic.is_lint {
1363+
if diagnostic.is_lint.is_some() {
13671364
self.lint_err_count += 1;
13681365
} else {
13691366
self.err_count += 1;

‎compiler/rustc_hir_analysis/src/check/compare_impl_item.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::potentially_plural_count;
22
use crate::errors::LifetimesOrBoundsMismatchOnTrait;
33
use hir::def_id::{DefId, DefIdMap, LocalDefId};
44
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
5-
use rustc_errors::{pluralize, struct_span_code_err, Applicability, DiagnosticId, ErrorGuaranteed};
5+
use rustc_errors::{pluralize, struct_span_code_err, Applicability, ErrorGuaranteed};
66
use rustc_hir as hir;
77
use rustc_hir::def::{DefKind, Res};
88
use rustc_hir::intravisit;
@@ -1382,7 +1382,7 @@ fn compare_number_of_generics<'tcx>(
13821382
kind = kind,
13831383
),
13841384
);
1385-
err.code(DiagnosticId::Error("E0049".into()));
1385+
err.code("E0049".into());
13861386

13871387
let msg =
13881388
format!("expected {trait_count} {kind} parameter{}", pluralize!(trait_count),);

‎compiler/rustc_hir_analysis/src/structured_errors.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ pub use self::{
66
missing_cast_for_variadic_arg::*, sized_unsized_cast::*, wrong_number_of_generic_args::*,
77
};
88

9-
use rustc_errors::{DiagnosticBuilder, DiagnosticId};
9+
use rustc_errors::DiagnosticBuilder;
1010
use rustc_session::Session;
1111

1212
pub trait StructuredDiagnostic<'tcx> {
1313
fn session(&self) -> &Session;
1414

15-
fn code(&self) -> DiagnosticId;
15+
fn code(&self) -> String;
1616

1717
fn diagnostic(&self) -> DiagnosticBuilder<'tcx> {
1818
let err = self.diagnostic_common();

‎compiler/rustc_hir_analysis/src/structured_errors/missing_cast_for_variadic_arg.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{errors, structured_errors::StructuredDiagnostic};
2-
use rustc_errors::{DiagnosticBuilder, DiagnosticId};
2+
use rustc_errors::DiagnosticBuilder;
33
use rustc_middle::ty::{Ty, TypeVisitableExt};
44
use rustc_session::Session;
55
use rustc_span::Span;
@@ -16,7 +16,7 @@ impl<'tcx> StructuredDiagnostic<'tcx> for MissingCastForVariadicArg<'tcx, '_> {
1616
self.sess
1717
}
1818

19-
fn code(&self) -> DiagnosticId {
19+
fn code(&self) -> String {
2020
rustc_errors::error_code!(E0617)
2121
}
2222

‎compiler/rustc_hir_analysis/src/structured_errors/sized_unsized_cast.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{errors, structured_errors::StructuredDiagnostic};
2-
use rustc_errors::{DiagnosticBuilder, DiagnosticId};
2+
use rustc_errors::DiagnosticBuilder;
33
use rustc_middle::ty::{Ty, TypeVisitableExt};
44
use rustc_session::Session;
55
use rustc_span::Span;
@@ -16,7 +16,7 @@ impl<'tcx> StructuredDiagnostic<'tcx> for SizedUnsizedCast<'tcx> {
1616
self.sess
1717
}
1818

19-
fn code(&self) -> DiagnosticId {
19+
fn code(&self) -> String {
2020
rustc_errors::error_code!(E0607)
2121
}
2222

‎compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use crate::structured_errors::StructuredDiagnostic;
2-
use rustc_errors::{
3-
pluralize, Applicability, Diagnostic, DiagnosticBuilder, DiagnosticId, MultiSpan,
4-
};
2+
use rustc_errors::{pluralize, Applicability, Diagnostic, DiagnosticBuilder, MultiSpan};
53
use rustc_hir as hir;
64
use rustc_middle::ty::{self as ty, AssocItems, AssocKind, TyCtxt};
75
use rustc_session::Session;
@@ -1107,7 +1105,7 @@ impl<'tcx> StructuredDiagnostic<'tcx> for WrongNumberOfGenericArgs<'_, 'tcx> {
11071105
self.tcx.sess
11081106
}
11091107

1110-
fn code(&self) -> DiagnosticId {
1108+
fn code(&self) -> String {
11111109
rustc_errors::error_code!(E0107)
11121110
}
11131111

‎compiler/rustc_hir_typeck/src/expr.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2626
use rustc_data_structures::stack::ensure_sufficient_stack;
2727
use rustc_errors::{
2828
pluralize, struct_span_code_err, AddToDiagnostic, Applicability, Diagnostic, DiagnosticBuilder,
29-
DiagnosticId, ErrorGuaranteed, StashKey,
29+
ErrorGuaranteed, StashKey,
3030
};
3131
use rustc_hir as hir;
3232
use rustc_hir::def::{CtorKind, DefKind, Res};
@@ -941,7 +941,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
941941
}
942942

943943
let mut err = self.dcx().struct_span_err(op_span, "invalid left-hand side of assignment");
944-
err.code(DiagnosticId::Error(err_code.into()));
944+
err.code(err_code.into());
945945
err.span_label(lhs.span, "cannot assign to this expression");
946946

947947
self.comes_from_while_condition(lhs.hir_id, |expr| {

‎compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ use crate::{
1212
use itertools::Itertools;
1313
use rustc_ast as ast;
1414
use rustc_data_structures::fx::FxIndexSet;
15-
use rustc_errors::{
16-
pluralize, Applicability, Diagnostic, DiagnosticId, ErrorGuaranteed, MultiSpan, StashKey,
17-
};
15+
use rustc_errors::{pluralize, Applicability, Diagnostic, ErrorGuaranteed, MultiSpan, StashKey};
1816
use rustc_hir as hir;
1917
use rustc_hir::def::{CtorOf, DefKind, Res};
2018
use rustc_hir::def_id::DefId;
@@ -679,7 +677,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
679677
pluralize!("was", provided_args.len())
680678
),
681679
);
682-
err.code(DiagnosticId::Error(err_code.to_owned()));
680+
err.code(err_code.to_owned());
683681
err.multipart_suggestion_verbose(
684682
"wrap these arguments in parentheses to construct a tuple",
685683
vec![
@@ -829,7 +827,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
829827
pluralize!("was", provided_args.len())
830828
),
831829
)
832-
.with_code(DiagnosticId::Error(err_code.to_owned()))
830+
.with_code(err_code.to_owned())
833831
};
834832

835833
// As we encounter issues, keep track of what we want to provide for the suggestion

‎compiler/rustc_hir_typeck/src/lib.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use crate::expectation::Expectation;
5252
use crate::fn_ctxt::RawTy;
5353
use crate::gather_locals::GatherLocalsVisitor;
5454
use rustc_data_structures::unord::UnordSet;
55-
use rustc_errors::{struct_span_code_err, DiagnosticId, ErrorGuaranteed};
55+
use rustc_errors::{struct_span_code_err, ErrorGuaranteed};
5656
use rustc_hir as hir;
5757
use rustc_hir::def::{DefKind, Res};
5858
use rustc_hir::intravisit::Visitor;
@@ -369,7 +369,7 @@ fn report_unexpected_variant_res(
369369
let err = tcx
370370
.dcx()
371371
.struct_span_err(span, format!("expected {expected}, found {res_descr} `{path_str}`"))
372-
.with_code(DiagnosticId::Error(err_code.into()));
372+
.with_code(err_code.into());
373373
match res {
374374
Res::Def(DefKind::Fn | DefKind::AssocFn, _) if err_code == "E0164" => {
375375
let patterns_url = "https://doc.rust-lang.org/book/ch18-00-patterns.html";

‎compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ pub enum TypeAnnotationNeeded {
4141
E0284,
4242
}
4343

44-
impl Into<rustc_errors::DiagnosticId> for TypeAnnotationNeeded {
45-
fn into(self) -> rustc_errors::DiagnosticId {
44+
impl Into<String> for TypeAnnotationNeeded {
45+
fn into(self) -> String {
4646
match self {
4747
Self::E0282 => rustc_errors::error_code!(E0282),
4848
Self::E0283 => rustc_errors::error_code!(E0283),

‎compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl DiagnosticDeriveVariantBuilder {
211211

212212
let code = nested.parse::<syn::LitStr>()?;
213213
tokens.extend(quote! {
214-
diag.code(rustc_errors::DiagnosticId::Error(#code.to_string()));
214+
diag.code(#code.to_string());
215215
});
216216
} else {
217217
span_err(path.span().unwrap(), "unknown argument")

‎compiler/rustc_middle/src/lint.rs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::cmp;
22

33
use rustc_data_structures::fx::FxIndexMap;
44
use rustc_data_structures::sorted_map::SortedMap;
5-
use rustc_errors::{Diagnostic, DiagnosticBuilder, DiagnosticId, DiagnosticMessage, MultiSpan};
5+
use rustc_errors::{Diagnostic, DiagnosticBuilder, DiagnosticMessage, MultiSpan};
66
use rustc_hir::{HirId, ItemLocalId};
77
use rustc_session::lint::{
88
builtin::{self, FORBIDDEN_LINT_GROUPS},
@@ -322,8 +322,6 @@ pub fn struct_lint_level(
322322
err.span(span);
323323
}
324324

325-
err.is_lint();
326-
327325
// If this code originates in a foreign macro, aka something that this crate
328326
// did not itself author, then it's likely that there's nothing this crate
329327
// can do about it. We probably want to skip the lint entirely.
@@ -351,8 +349,7 @@ pub fn struct_lint_level(
351349
// suppressed the lint due to macros.
352350
err.primary_message(msg);
353351

354-
let name = lint.name_lower();
355-
err.code(DiagnosticId::Lint { name, has_future_breakage });
352+
err.is_lint(lint.name_lower(), has_future_breakage);
356353

357354
// Lint diagnostics that are covered by the expect level will not be emitted outside
358355
// the compiler. It is therefore not necessary to add any information for the user.

‎compiler/rustc_mir_transform/src/errors.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for RequiresUnsafe {
6666
#[track_caller]
6767
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> DiagnosticBuilder<'a, G> {
6868
let mut diag = DiagnosticBuilder::new(dcx, level, fluent::mir_transform_requires_unsafe);
69-
diag.code(rustc_errors::DiagnosticId::Error("E0133".to_string()));
69+
diag.code("E0133".to_string());
7070
diag.span(self.span);
7171
diag.span_label(self.span, self.details.label());
7272
let desc = dcx.eagerly_translate_to_string(self.details.label(), [].into_iter());

‎compiler/rustc_passes/src/entry.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_>) {
180180
Default::default()
181181
});
182182
let main_def_opt = tcx.resolutions(()).main_def;
183-
let diagnostic_id = error_code!(E0601);
184-
let add_teach_note = tcx.sess.teach(&diagnostic_id);
183+
let code = error_code!(E0601);
184+
let add_teach_note = tcx.sess.teach(&code);
185185
// The file may be empty, which leads to the diagnostic machinery not emitting this
186186
// note. This is a relatively simple way to detect that case and emit a span-less
187187
// note instead.

‎compiler/rustc_resolve/src/late.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_ast::ptr::P;
1616
use rustc_ast::visit::{self, AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor};
1717
use rustc_ast::*;
1818
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
19-
use rustc_errors::{Applicability, DiagnosticArgValue, DiagnosticId, IntoDiagnosticArg};
19+
use rustc_errors::{Applicability, DiagnosticArgValue, IntoDiagnosticArg};
2020
use rustc_hir::def::Namespace::{self, *};
2121
use rustc_hir::def::{self, CtorKind, DefKind, LifetimeRes, NonMacroAttrKind, PartialRes, PerNS};
2222
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
@@ -533,7 +533,7 @@ impl<'a> PathSource<'a> {
533533
}
534534
}
535535

536-
fn error_code(self, has_unexpected_resolution: bool) -> DiagnosticId {
536+
fn error_code(self, has_unexpected_resolution: bool) -> String {
537537
use rustc_errors::error_code;
538538
match (self, has_unexpected_resolution) {
539539
(PathSource::Trait(_), true) => error_code!(E0404),
@@ -3821,7 +3821,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
38213821
swap(&mut err.span, &mut parent_err.span);
38223822
err.children = take(&mut parent_err.children);
38233823
err.sort_span = parent_err.sort_span;
3824-
err.is_lint = parent_err.is_lint;
3824+
err.is_lint = parent_err.is_lint.clone();
38253825

38263826
// merge the parent's suggestions with the typo suggestions
38273827
fn append_result<T, E>(res1: &mut Result<Vec<T>, E>, res2: Result<Vec<T>, E>) {

‎compiler/rustc_resolve/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ enum ResolutionError<'a> {
257257
kind: &'static str,
258258
trait_path: String,
259259
trait_item_span: Span,
260-
code: rustc_errors::DiagnosticId,
260+
code: String,
261261
},
262262
/// Error E0201: multiple impl items for the same trait item.
263263
TraitImplDuplicate { name: Symbol, trait_item_span: Span, old_span: Span },

‎compiler/rustc_session/src/parse.rs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1515
use rustc_data_structures::sync::{AppendOnlyVec, Lock, Lrc};
1616
use rustc_errors::{emitter::SilentEmitter, DiagCtxt};
1717
use rustc_errors::{
18-
fallback_fluent_bundle, Diagnostic, DiagnosticBuilder, DiagnosticId, DiagnosticMessage,
19-
MultiSpan, StashKey,
18+
fallback_fluent_bundle, Diagnostic, DiagnosticBuilder, DiagnosticMessage, MultiSpan, StashKey,
2019
};
2120
use rustc_feature::{find_feature_issue, GateIssue, UnstableFeatures};
2221
use rustc_span::edition::Edition;
@@ -148,7 +147,7 @@ pub fn feature_warn_issue(
148147
// Decorate this as a future-incompatibility lint as in rustc_middle::lint::struct_lint_level
149148
let lint = UNSTABLE_SYNTAX_PRE_EXPANSION;
150149
let future_incompatible = lint.future_incompatible.as_ref().unwrap();
151-
err.code(DiagnosticId::Lint { name: lint.name_lower(), has_future_breakage: false });
150+
err.is_lint(lint.name_lower(), /* has_future_breakage */ false);
152151
err.warn(lint.desc);
153152
err.note(format!("for more information, see {}", future_incompatible.reference));
154153

‎compiler/rustc_session/src/session.rs‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ use rustc_errors::emitter::{DynEmitter, HumanEmitter, HumanReadableErrorType};
2020
use rustc_errors::json::JsonEmitter;
2121
use rustc_errors::registry::Registry;
2222
use rustc_errors::{
23-
error_code, fallback_fluent_bundle, DiagCtxt, DiagnosticBuilder, DiagnosticId,
24-
DiagnosticMessage, ErrorGuaranteed, FatalAbort, FluentBundle, IntoDiagnostic,
25-
LazyFallbackBundle, TerminalUrl,
23+
error_code, fallback_fluent_bundle, DiagCtxt, DiagnosticBuilder, DiagnosticMessage,
24+
ErrorGuaranteed, FatalAbort, FluentBundle, IntoDiagnostic, LazyFallbackBundle, TerminalUrl,
2625
};
2726
use rustc_macros::HashStable_Generic;
2827
pub use rustc_span::def_id::StableCrateId;
@@ -906,7 +905,7 @@ impl Session {
906905
CodegenUnits::Default(16)
907906
}
908907

909-
pub fn teach(&self, code: &DiagnosticId) -> bool {
908+
pub fn teach(&self, code: &str) -> bool {
910909
self.opts.unstable_opts.teach && self.dcx().must_teach(code)
911910
}
912911

0 commit comments

Comments
 (0)
Please sign in to comment.