Skip to content

Commit af8bfec

Browse files
nnethercotefmease
authored andcommitted
More updates for recent diagnostics changes.
A sequel to rust-lang#1883, this covers diagnostic naming changes from rust-lang/rust/pull/121489, rust-lang/rust/pull/121780, and rust-lang/rust/pull/122132.
1 parent c707805 commit af8bfec

7 files changed

+60
-62
lines changed

examples/rustc-driver-getting-diagnostics.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extern crate rustc_session;
1010
extern crate rustc_span;
1111

1212
use rustc_errors::{
13-
emitter::Emitter, registry, translation::Translate, DiagCtxt, Diagnostic, FluentBundle,
13+
emitter::Emitter, registry, translation::Translate, DiagCtxt, DiagInner, FluentBundle,
1414
};
1515
use rustc_session::config;
1616
use rustc_span::source_map::SourceMap;
@@ -22,7 +22,7 @@ use std::{
2222

2323
struct DebugEmitter {
2424
source_map: Arc<SourceMap>,
25-
diagnostics: Arc<Mutex<Vec<Diagnostic>>>,
25+
diagnostics: Arc<Mutex<Vec<DiagInner>>>,
2626
}
2727

2828
impl Translate for DebugEmitter {
@@ -36,7 +36,7 @@ impl Translate for DebugEmitter {
3636
}
3737

3838
impl Emitter for DebugEmitter {
39-
fn emit_diagnostic(&mut self, diag: &Diagnostic) {
39+
fn emit_diagnostic(&mut self, diag: &DiagInner) {
4040
self.diagnostics.lock().unwrap().push(diag.clone());
4141
}
4242

@@ -52,7 +52,7 @@ fn main() {
5252
.output()
5353
.unwrap();
5454
let sysroot = str::from_utf8(&out.stdout).unwrap().trim();
55-
let buffer: Arc<Mutex<Vec<Diagnostic>>> = Arc::default();
55+
let buffer: Arc<Mutex<Vec<DiagInner>>> = Arc::default();
5656
let diagnostics = buffer.clone();
5757
let config = rustc_interface::Config {
5858
opts: config::Options {

src/appendix/code-index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Item | Kind | Short description | Chapter |
1111
`ast::Crate` | struct | A syntax-level representation of a parsed crate | [The parser] | [compiler/rustc_ast/src/ast.rs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/ast/struct.Crate.html)
1212
`rustc_hir::Crate` | struct | A more abstract, compiler-friendly form of a crate's AST | [The Hir] | [compiler/rustc_hir/src/hir.rs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/struct.Crate.html)
1313
`DefId` | struct | One of four types of HIR node identifiers | [Identifiers in the HIR] | [compiler/rustc_hir/src/def_id.rs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.DefId.html)
14-
`DiagnosticBuilder` | struct | A struct for building up compiler diagnostics, such as errors or lints | [Emitting Diagnostics] | [compiler/rustc_errors/src/diagnostic_builder.rs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.DiagnosticBuilder.html)
14+
`Diag` | struct | A struct for a compiler diagnostic, such as an error or lint | [Emitting Diagnostics] | [compiler/rustc_errors/src/diagnostic.rs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.Diag.html)
1515
`DocContext` | struct | A state container used by rustdoc when crawling through a crate to gather its documentation | [Rustdoc] | [src/librustdoc/core.rs](https://github.com/rust-lang/rust/blob/master/src/librustdoc/core.rs)
1616
`HirId` | struct | One of four types of HIR node identifiers | [Identifiers in the HIR] | [compiler/rustc_hir/src/hir_id.rs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir_id/struct.HirId.html)
1717
`NodeId` | struct | One of four types of HIR node identifiers. Being phased out | [Identifiers in the HIR] | [compiler/rustc_ast/src/ast.rs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/node_id/struct.NodeId.html)

src/diagnostics.md

+14-14
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,10 @@ reporting errors.
315315

316316
[errors]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/index.html
317317

318-
Diagnostics can be implemented as types which implement the `IntoDiagnostic`
318+
Diagnostics can be implemented as types which implement the `Diagnostic`
319319
trait. This is preferred for new diagnostics as it enforces a separation
320320
between diagnostic emitting logic and the main code paths. For less-complex
321-
diagnostics, the `IntoDiagnostic` trait can be derived -- see [Diagnostic
321+
diagnostics, the `Diagnostic` trait can be derived -- see [Diagnostic
322322
structs][diagnostic-structs]. Within the trait implementation, the APIs
323323
described below can be used as normal.
324324

@@ -335,27 +335,27 @@ In general, there are two classes of such methods: ones that emit an error
335335
directly and ones that allow finer control over what to emit. For example,
336336
[`span_err`][spanerr] emits the given error message at the given `Span`, but
337337
[`struct_span_err`][strspanerr] instead returns a
338-
[`DiagnosticBuilder`][diagbuild].
338+
[`Diag`][diag].
339339

340340
Most of these methods will accept strings, but it is recommended that typed
341341
identifiers for translatable diagnostics be used for new diagnostics (see
342342
[Translation][translation]).
343343

344344
[translation]: ./diagnostics/translation.md
345345

346-
`DiagnosticBuilder` allows you to add related notes and suggestions to an error
346+
`Diag` allows you to add related notes and suggestions to an error
347347
before emitting it by calling the [`emit`][emit] method. (Failing to either
348-
emit or [cancel][cancel] a `DiagnosticBuilder` will result in an ICE.) See the
349-
[docs][diagbuild] for more info on what you can do.
348+
emit or [cancel][cancel] a `Diag` will result in an ICE.) See the
349+
[docs][diag] for more info on what you can do.
350350

351351
[spanerr]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.DiagCtxt.html#method.span_err
352352
[strspanerr]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.DiagCtxt.html#method.struct_span_err
353-
[diagbuild]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/diagnostic_builder/struct.DiagnosticBuilder.html
354-
[emit]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/diagnostic_builder/struct.DiagnosticBuilder.html#method.emit
355-
[cancel]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/diagnostic_builder/struct.DiagnosticBuilder.html#method.cancel
353+
[diag]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.Diag.html
354+
[emit]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.Diag.html#method.emit
355+
[cancel]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.Diag.html#method.cancel
356356

357357
```rust,ignore
358-
// Get a DiagnosticBuilder. This does _not_ emit an error yet.
358+
// Get a `Diag`. This does _not_ emit an error yet.
359359
let mut err = sess.dcx.struct_span_err(sp, fluent::example::example_error);
360360
361361
// In some cases, you might need to check if `sp` is generated by a macro to
@@ -385,7 +385,7 @@ example-example-error = oh no! this is an error!
385385

386386
In addition to telling the user exactly _why_ their code is wrong, it's
387387
oftentimes furthermore possible to tell them how to fix it. To this end,
388-
`DiagnosticBuilder` offers a structured suggestions API, which formats code
388+
`Diag` offers a structured suggestions API, which formats code
389389
suggestions pleasingly in the terminal, or (when the `--error-format json` flag
390390
is passed) as JSON for consumption by tools like [`rustfix`][rustfix].
391391

@@ -395,7 +395,7 @@ Not all suggestions should be applied mechanically, they have a degree of
395395
confidence in the suggested code, from high
396396
(`Applicability::MachineApplicable`) to low (`Applicability::MaybeIncorrect`).
397397
Be conservative when choosing the level. Use the
398-
[`span_suggestion`][span_suggestion] method of `DiagnosticBuilder` to
398+
[`span_suggestion`][span_suggestion] method of `Diag` to
399399
make a suggestion. The last argument provides a hint to tools whether
400400
the suggestion is mechanically applicable or not.
401401

@@ -414,7 +414,7 @@ them))
414414
- not shown (used for _very_ obvious cases, but we still want to allow tools to
415415
apply them)
416416

417-
[span_suggestion]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.DiagnosticBuilder.html#method.span_suggestion
417+
[span_suggestion]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.Diag.html#method.span_suggestion
418418

419419
For example, to make our `qux` suggestion machine-applicable, we would do:
420420

@@ -859,7 +859,7 @@ The "human" readable and the json format emitter can be found under
859859
The JSON emitter defines [its own `Diagnostic`
860860
struct](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/json/struct.Diagnostic.html)
861861
(and sub-structs) for the JSON serialization. Don't confuse this with
862-
[`errors::Diagnostic`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.Diagnostic.html)!
862+
[`errors::Diag`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.Diag.html)!
863863

864864
## `#[rustc_on_unimplemented(...)]`
865865

src/diagnostics/diagnostic-structs.md

+21-23
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# Diagnostic and subdiagnostic structs
2-
rustc has three diagnostic derives that can be used to create simple diagnostics,
3-
which are recommended to be used when they are applicable:
4-
`#[derive(Diagnostic)]`, `#[derive(LintDiagnostic)]`, and `#[derive(Subdiagnostic)]`.
2+
rustc has three diagnostic traits that can be used to create diagnostics:
3+
`Diagnostic`, `LintDiagnostic`, and `Subdiagnostic`. For simple diagnostics,
4+
instead of using the `Diag` API to create and emit diagnostics,
5+
derived impls can be used. They are only suitable for simple diagnostics that
6+
don't require much logic in deciding whether or not to add additional
7+
subdiagnostics.
58

6-
Diagnostics created with the derive macros can be translated into different
7-
languages and each has a slug that uniquely identifies the diagnostic.
9+
Such diagnostic can be translated into
10+
different languages and each has a slug that uniquely identifies the
11+
diagnostic.
812

913
## `#[derive(Diagnostic)]` and `#[derive(LintDiagnostic)]`
10-
Instead of using the `DiagnosticBuilder` API to create and emit diagnostics,
11-
these derives can be used. They are only applicable for simple diagnostics that
12-
don't require much logic in deciding whether or not to add additional
13-
subdiagnostics.
1414

1515
Consider the [definition][defn] of the "field already declared" diagnostic
1616
shown below:
@@ -28,14 +28,14 @@ pub struct FieldAlreadyDeclared {
2828
}
2929
```
3030

31-
`Diagnostic` can only be applied to structs and enums.
31+
`Diagnostic` can only be derived on structs and enums.
3232
Attributes that are placed on the type for structs are placed on each
3333
variants for enums (or vice versa). Each `Diagnostic` has to have one
3434
attribute, `#[diag(...)]`, applied to the struct or each enum variant.
3535

3636
If an error has an error code (e.g. "E0624"), then that can be specified using
3737
the `code` sub-attribute. Specifying a `code` isn't mandatory, but if you are
38-
porting a diagnostic that uses `DiagnosticBuilder` to use `Diagnostic`
38+
porting a diagnostic that uses `Diag` to use `Diagnostic`
3939
then you should keep the code if there was one.
4040

4141
`#[diag(..)]` must provide a slug as the first positional argument (a path to an
@@ -110,13 +110,12 @@ the value of the `field_name` field of the struct), not a Fluent identifier.
110110
cannot be used when the field's type contains an `Applicability`.
111111

112112
In the end, the `Diagnostic` derive will generate an implementation of
113-
`IntoDiagnostic` that looks like the following:
113+
`Diagnostic` that looks like the following:
114114

115115
```rust,ignore
116-
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a> for FieldAlreadyDeclared {
117-
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> DiagnosticBuilder<'a, G> {
118-
let mut diag =
119-
DiagnosticBuilder::new(dcx, level, fluent::hir_analysis_field_already_declared);
116+
impl<'a, G: EmissionGuarantee> Diagnostic<'a> for FieldAlreadyDeclared {
117+
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
118+
let mut diag = Diag::new(dcx, level, fluent::hir_analysis_field_already_declared);
120119
diag.set_span(self.span);
121120
diag.span_label(
122121
self.span,
@@ -208,7 +207,7 @@ following attributes:
208207
- String which must be one of `machine-applicable`, `maybe-incorrect`,
209208
`has-placeholders` or `unspecified`.
210209
- `#[subdiagnostic]`
211-
- _Applied to a type that implements `AddToDiagnostic` (from
210+
- _Applied to a type that implements `Subdiagnostic` (from
212211
`#[derive(Subdiagnostic)]`)._
213212
- Adds the subdiagnostic represented by the subdiagnostic struct.
214213
- `#[primary_span]` (_Optional_)
@@ -246,7 +245,7 @@ pub enum ExpectedReturnTypeLabel<'tcx> {
246245
}
247246
```
248247

249-
Like `Diagnostic`, `Subdiagnostic` can be applied to structs or
248+
Like `Diagnostic`, `Subdiagnostic` can be derived for structs or
250249
enums. Attributes that are placed on the type for structs are placed on each
251250
variants for enums (or vice versa). Each `Subdiagnostic` should have one
252251
attribute applied to the struct or each variant, one of:
@@ -307,12 +306,12 @@ Applicabilities can also be specified as a field (of type `Applicability`)
307306
using the `#[applicability]` attribute.
308307

309308
In the end, the `Subdiagnostic` derive will generate an implementation
310-
of `AddToDiagnostic` that looks like the following:
309+
of `Subdiagnostic` that looks like the following:
311310

312311
```rust
313-
impl<'tcx> AddToDiagnostic for ExpectedReturnTypeLabel<'tcx> {
314-
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
315-
use rustc_errors::{Applicability, IntoDiagnosticArg};
312+
impl<'tcx> Subdiagnostic for ExpectedReturnTypeLabel<'tcx> {
313+
fn add_to_diag(self, diag: &mut rustc_errors::Diagnostic) {
314+
use rustc_errors::{Applicability, IntoDiagArg};
316315
match self {
317316
ExpectedReturnTypeLabel::Unit { span } => {
318317
diag.span_label(span, rustc_errors::fluent::hir_analysis_expected_default_return_type)
@@ -321,7 +320,6 @@ impl<'tcx> AddToDiagnostic for ExpectedReturnTypeLabel<'tcx> {
321320
diag.set_arg("expected", expected);
322321
diag.span_label(span, rustc_errors::fluent::hir_analysis_expected_return_type)
323322
}
324-
325323
}
326324
}
327325
}

src/diagnostics/error-guaranteed.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ Thankfully, in most cases, it should be statically impossible to abuse
3030

3131
[errorguar]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.ErrorGuaranteed.html
3232
[rerrors]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/index.html
33-
[emit]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/diagnostic_builder/struct.DiagnosticBuilder.html#method.emit
33+
[emit]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.diag.html#method.emit

src/diagnostics/translation.md

+17-17
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ There are two ways of writing translatable diagnostics:
1212
deciding to emit subdiagnostics and can therefore be represented as
1313
diagnostic structs). See [the diagnostic and subdiagnostic structs
1414
documentation](./diagnostic-structs.md).
15-
2. Using typed identifiers with `DiagnosticBuilder` APIs (in
16-
`IntoDiagnostic` or `AddToDiagnostic` or `DecorateLint` implementations).
15+
2. Using typed identifiers with `Diag` APIs (in
16+
`Diagnostic` or `Subdiagnostic` or `LintDiagnostic` implementations).
1717

1818
When adding or changing a translatable diagnostic,
1919
you don't need to worry about the translations.
@@ -113,28 +113,28 @@ translation.
113113
### Messages
114114

115115
All of rustc's traditional diagnostic APIs (e.g. `struct_span_err` or `note`)
116-
take any message that can be converted into a `DiagnosticMessage` (or
117-
`SubdiagnosticMessage`).
116+
take any message that can be converted into a `DiagMessage` (or
117+
`SubdiagMessage`).
118118

119-
[`rustc_error_messages::DiagnosticMessage`] can represent legacy non-translatable
119+
[`rustc_error_messages::DiagMessage`] can represent legacy non-translatable
120120
diagnostic messages and translatable messages. Non-translatable messages are
121121
just `String`s. Translatable messages are just a `&'static str` with the
122122
identifier of the Fluent message (sometimes with an additional `&'static str`
123123
with an attribute).
124124

125-
`DiagnosticMessage` never needs to be interacted with directly:
126-
`DiagnosticMessage` constants are created for each diagnostic message in a
127-
Fluent resource (described in more detail below), or `DiagnosticMessage`s will
125+
`DiagMessage` never needs to be interacted with directly:
126+
`DiagMessage` constants are created for each diagnostic message in a
127+
Fluent resource (described in more detail below), or `DiagMessage`s will
128128
either be created in the macro-generated code of a diagnostic derive.
129129

130-
`rustc_error_messages::SubdiagnosticMessage` is similar, it can correspond to a
130+
`rustc_error_messages::SubdiagMessage` is similar, it can correspond to a
131131
legacy non-translatable diagnostic message or the name of an attribute to a
132-
Fluent message. Translatable `SubdiagnosticMessage`s must be combined with a
133-
`DiagnosticMessage` (using `DiagnosticMessage::with_subdiagnostic_message`) to
132+
Fluent message. Translatable `SubdiagMessage`s must be combined with a
133+
`DiagMessage` (using `DiagMessage::with_subdiagnostic_message`) to
134134
be emitted (an attribute name on its own is meaningless without a corresponding
135-
message identifier, which is what `DiagnosticMessage` provides).
135+
message identifier, which is what `DiagMessage` provides).
136136

137-
Both `DiagnosticMessage` and `SubdiagnosticMessage` implement `Into` for any
137+
Both `DiagMessage` and `SubdiagMessage` implement `Into` for any
138138
type that can be converted into a string, and converts these into
139139
non-translatable diagnostics - this keeps all existing diagnostic calls
140140
working.
@@ -148,8 +148,8 @@ Diagnostics have a `set_arg` function that can be used to provide this
148148
additional context to a diagnostic.
149149

150150
Arguments have both a name (e.g. "what" in the earlier example) and a value.
151-
Argument values are represented using the `DiagnosticArgValue` type, which is
152-
just a string or a number. rustc types can implement `IntoDiagnosticArg` with
151+
Argument values are represented using the `DiagArgValue` type, which is
152+
just a string or a number. rustc types can implement `IntoDiagArg` with
153153
conversion into a string or a number, and common types like `Ty<'tcx>` already
154154
have such implementations.
155155

@@ -167,7 +167,7 @@ accessing the fallback and primary fluent bundles (`fallback_fluent_bundle` and
167167
`fluent_bundle` respectively).
168168

169169
`Emitter` also has member functions with default implementations for performing
170-
translation of a `DiagnosticMessage` using the results of
170+
translation of a `DiagMessage` using the results of
171171
`fallback_fluent_bundle` and `fluent_bundle`.
172172

173173
All of the emitters in rustc load the fallback Fluent bundle lazily, only
@@ -201,4 +201,4 @@ files are malformed, or a message is duplicated in multiple resources.
201201
[Fluent]: https://projectfluent.org
202202
[`compiler/rustc_borrowck/messages.ftl`]: https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_borrowck/messages.ftl
203203
[`compiler/rustc_parse/messages.ftl`]: https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_parse/messages.ftl
204-
[`rustc_error_messages::DiagnosticMessage`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_error_messages/enum.DiagnosticMessage.html
204+
[`rustc_error_messages::DiagMessage`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_error_messages/enum.DiagMessage.html

src/overview.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Macro-expansion, `AST`-validation, name-resolution, and early linting also take
7979
place during the lexing and parsing stage.
8080

8181
The [`rustc_ast::ast`]::{[`Crate`], [`Expr`], [`Pat`], ...} `AST` nodes are
82-
returned from the parser while the standard [`DiagnosticBuilder`] API is used
82+
returned from the parser while the standard [`Diag`] API is used
8383
for error handling. Generally Rust's compiler will try to recover from errors
8484
by parsing a superset of Rust's grammar, while also emitting an error type.
8585

@@ -137,7 +137,7 @@ the final binary.
137137
[`bump`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/parser/struct.Parser.html#method.bump
138138
[`check`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/parser/struct.Parser.html#method.check
139139
[`Crate`]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_ast/ast/struct.Crate.html
140-
[`DiagnosticBuilder`]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_errors/struct.DiagnosticBuilder.html
140+
[`diag`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.Diag.html
141141
[`eat`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/parser/struct.Parser.html#method.eat
142142
[`expect`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/parser/struct.Parser.html#method.expect
143143
[`Expr`]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_ast/ast/struct.Expr.html

0 commit comments

Comments
 (0)