Repository navigation
Add support for span context to derive(Diagnostic) and use it - #163414
Merged
rust-bors[bot] merged 5 commits intoSep 30, 2026
Merged
Conversation
Collaborator
|
|
Collaborator
|
rustbot has assigned @petrochenkov. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
Collaborator
|
HIR ty lowering was modified cc @fmease |
derive(Diagnostic)derive(Diagnostic) and use it
Contributor
|
It would be easier to understand what's going on here with a link to the previous work like #162874. |
Contributor
|
r=me after squashing the review changes into the main changes. |
Collaborator
|
Reminder, once the PR becomes ready for a review, use |
Allow writing
```rust
struct Diagnostic {
#[primary_span]
#[label("label text")]
span: Span,
#[context]
context: Span,
}
```
So that the code pointed at by `context` is shown in the rendered diagnostic without an underline.
```
error[E0201]: duplicate definitions with name `Bar`:
--> $DIR/associated-item-duplicate-names-3.rs:14:5
|
LL | trait Foo {
LL | type Bar;
| --------- item in trait
...
LL | impl Foo for Baz {
LL | type Bar = i16;
| --------------- previous definition here
LL | type Bar = u16;
| ^^^^^^^^^^^^^^^ duplicate definition
```
```
error[E0324]: item `RESOLVED_WRONG` is an associated method, which doesn't match its trait `MyTrait`
--> $DIR/unresolved-trait-impl-item.rs:12:5
|
LL | trait MyTrait {
LL | async fn resolved(&self);
LL | const RESOLVED_WRONG: u8 = 0;
| ----------------------------- item in trait
...
LL | impl MyTrait for i32 {
...
LL | async fn RESOLVED_WRONG() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ does not match trait
```
```
error: `impl` item signature doesn't match `trait` item signature
--> $DIR/self-without-lifetime-constraint.rs:46:5
|
LL | pub trait FromSql: Sized {
LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self, &Self>;
| -------------------------------------------------------------------- expected `fn(ValueRef<'1>) -> Result<(&'2 str, &'1 &'2 str), _>`
...
LL | impl FromSql for &str {
LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult<&str, &&str> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(ValueRef<'1>) -> Result<(&'1 str, &'1 &'1 str), _>`
|
= note: expected signature `fn(ValueRef<'1>) -> Result<(&'2 str, &'1 &'2 str), FromSqlError>`
found signature `fn(ValueRef<'1>) -> Result<(&'1 str, &'1 &'1 str), FromSqlError>`
help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
--> $DIR/self-without-lifetime-constraint.rs:42:60
|
LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self, &Self>;
| ^^^^ consider borrowing this type parameter in the trait
```
estebank
force-pushed
the
derive-diagnostic-context
branch
from
September 29, 2026 16:37
577968c to
246eb14
Compare
Contributor
Author
|
@bors r=petrochenkov |
Contributor
JonathanBrouwer
added a commit
to JonathanBrouwer/rust
that referenced
this pull request
Sep 29, 2026
…, r=petrochenkov
Add support for span context to `derive(Diagnostic)` and use it
Allow writing
```rust
struct Diagnostic {
#[primary_span]
#[label("label text")]
span: Span,
#[context]
context: Span,
}
```
So that the code pointed at by `context` is shown in the rendered diagnostic without an underline.
Point at enclosing item(s) on unexpected item type:
```
error: expected type, found constant
--> $DIR/consts.rs:2:19
|
LL | pub fn accept(
LL | _: impl Trait<K: Copy>
| ^------ bounds are not allowed on associated constants
| |
| unexpected constant
|
note: the associated constant is defined here
--> $DIR/consts.rs:7:5
|
LL | const K: i32;
| ^^^^^^^^^^
```
Show trait/impl for duplicate impl associated items
```
error[E0201]: duplicate definitions with name `Bar`:
--> $DIR/associated-item-duplicate-names-3.rs:14:5
|
LL | trait Foo {
LL | type Bar;
| --------- item in trait
...
LL | impl Foo for Baz {
LL | type Bar = i16;
| --------------- previous definition here
LL | type Bar = u16;
| ^^^^^^^^^^^^^^^ duplicate definition
```
Point at trait/impl on associated item mismatch
```
error[E0324]: item `RESOLVED_WRONG` is an associated method, which doesn't match its trait `MyTrait`
--> $DIR/unresolved-trait-impl-item.rs:12:5
|
LL | trait MyTrait {
LL | async fn resolved(&self);
LL | const RESOLVED_WRONG: u8 = 0;
| ----------------------------- item in trait
...
LL | impl MyTrait for i32 {
...
LL | async fn RESOLVED_WRONG() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ does not match trait
```
On `impl` item signature mismatch, point at impl and trait
```
error: `impl` item signature doesn't match `trait` item signature
--> $DIR/self-without-lifetime-constraint.rs:46:5
|
LL | pub trait FromSql: Sized {
LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self, &Self>;
| -------------------------------------------------------------------- expected `fn(ValueRef<'1>) -> Result<(&'2 str, &'1 &'2 str), _>`
...
LL | impl FromSql for &str {
LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult<&str, &&str> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(ValueRef<'1>) -> Result<(&'1 str, &'1 &'1 str), _>`
|
= note: expected signature `fn(ValueRef<'1>) -> Result<(&'2 str, &'1 &'2 str), FromSqlError>`
found signature `fn(ValueRef<'1>) -> Result<(&'1 str, &'1 &'1 str), FromSqlError>`
help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
--> $DIR/self-without-lifetime-constraint.rs:42:60
|
LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self, &Self>;
| ^^^^ consider borrowing this type parameter in the trait
```
rust-bors Bot
pushed a commit
that referenced
this pull request
Sep 29, 2026
…uwer Rollup of 5 pull requests Successful merges: - #158102 (When compiling without a specified `--edition`, emit a message) - #158936 (Add `std::fs::{Home|Media}Dirs`) - #163414 (Add support for span context to `derive(Diagnostic)` and use it) - #163405 (Remove some #[linkage] options) - #163498 (Revert "Rollup merge of #120589 - devnexen:cpuaff_fbsd_upd, r=clarfonthey")
tgross35
added a commit
to tgross35/rust
that referenced
this pull request
Sep 29, 2026
…, r=petrochenkov
Add support for span context to `derive(Diagnostic)` and use it
Allow writing
```rust
struct Diagnostic {
#[primary_span]
#[label("label text")]
span: Span,
#[context]
context: Span,
}
```
So that the code pointed at by `context` is shown in the rendered diagnostic without an underline.
Point at enclosing item(s) on unexpected item type:
```
error: expected type, found constant
--> $DIR/consts.rs:2:19
|
LL | pub fn accept(
LL | _: impl Trait<K: Copy>
| ^------ bounds are not allowed on associated constants
| |
| unexpected constant
|
note: the associated constant is defined here
--> $DIR/consts.rs:7:5
|
LL | const K: i32;
| ^^^^^^^^^^
```
Show trait/impl for duplicate impl associated items
```
error[E0201]: duplicate definitions with name `Bar`:
--> $DIR/associated-item-duplicate-names-3.rs:14:5
|
LL | trait Foo {
LL | type Bar;
| --------- item in trait
...
LL | impl Foo for Baz {
LL | type Bar = i16;
| --------------- previous definition here
LL | type Bar = u16;
| ^^^^^^^^^^^^^^^ duplicate definition
```
Point at trait/impl on associated item mismatch
```
error[E0324]: item `RESOLVED_WRONG` is an associated method, which doesn't match its trait `MyTrait`
--> $DIR/unresolved-trait-impl-item.rs:12:5
|
LL | trait MyTrait {
LL | async fn resolved(&self);
LL | const RESOLVED_WRONG: u8 = 0;
| ----------------------------- item in trait
...
LL | impl MyTrait for i32 {
...
LL | async fn RESOLVED_WRONG() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ does not match trait
```
On `impl` item signature mismatch, point at impl and trait
```
error: `impl` item signature doesn't match `trait` item signature
--> $DIR/self-without-lifetime-constraint.rs:46:5
|
LL | pub trait FromSql: Sized {
LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self, &Self>;
| -------------------------------------------------------------------- expected `fn(ValueRef<'1>) -> Result<(&'2 str, &'1 &'2 str), _>`
...
LL | impl FromSql for &str {
LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult<&str, &&str> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(ValueRef<'1>) -> Result<(&'1 str, &'1 &'1 str), _>`
|
= note: expected signature `fn(ValueRef<'1>) -> Result<(&'2 str, &'1 &'2 str), FromSqlError>`
found signature `fn(ValueRef<'1>) -> Result<(&'1 str, &'1 &'1 str), FromSqlError>`
help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
--> $DIR/self-without-lifetime-constraint.rs:42:60
|
LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self, &Self>;
| ^^^^ consider borrowing this type parameter in the trait
```
rust-bors Bot
pushed a commit
that referenced
this pull request
Sep 29, 2026
Rollup of 8 pull requests Successful merges: - #158102 (When compiling without a specified `--edition`, emit a message) - #158936 (Add `std::fs::{Home|Media}Dirs`) - #163414 (Add support for span context to `derive(Diagnostic)` and use it) - #163481 (triagebot: use r-l/r zulip linkifier for r-l/r rustfmt backport nominations) - #163502 (arch::breakpoint: update docs) - #163505 (Sync cg_gcc subtree 2026-09-29) - #163405 (Remove some #[linkage] options) - #163498 (Revert "Rollup merge of #120589 - devnexen:cpuaff_fbsd_upd, r=clarfonthey")
rust-bors Bot
pushed a commit
that referenced
this pull request
Sep 29, 2026
Rollup of 8 pull requests Successful merges: - #158102 (When compiling without a specified `--edition`, emit a message) - #158936 (Add `std::fs::{Home|Media}Dirs`) - #163414 (Add support for span context to `derive(Diagnostic)` and use it) - #163481 (triagebot: use r-l/r zulip linkifier for r-l/r rustfmt backport nominations) - #163502 (arch::breakpoint: update docs) - #163505 (Sync cg_gcc subtree 2026-09-29) - #163405 (Remove some #[linkage] options) - #163498 (Revert "Rollup merge of #120589 - devnexen:cpuaff_fbsd_upd, r=clarfonthey")
tgross35
added a commit
to tgross35/rust
that referenced
this pull request
Sep 30, 2026
…, r=petrochenkov
Add support for span context to `derive(Diagnostic)` and use it
Allow writing
```rust
struct Diagnostic {
#[primary_span]
#[label("label text")]
span: Span,
#[context]
context: Span,
}
```
So that the code pointed at by `context` is shown in the rendered diagnostic without an underline.
Point at enclosing item(s) on unexpected item type:
```
error: expected type, found constant
--> $DIR/consts.rs:2:19
|
LL | pub fn accept(
LL | _: impl Trait<K: Copy>
| ^------ bounds are not allowed on associated constants
| |
| unexpected constant
|
note: the associated constant is defined here
--> $DIR/consts.rs:7:5
|
LL | const K: i32;
| ^^^^^^^^^^
```
Show trait/impl for duplicate impl associated items
```
error[E0201]: duplicate definitions with name `Bar`:
--> $DIR/associated-item-duplicate-names-3.rs:14:5
|
LL | trait Foo {
LL | type Bar;
| --------- item in trait
...
LL | impl Foo for Baz {
LL | type Bar = i16;
| --------------- previous definition here
LL | type Bar = u16;
| ^^^^^^^^^^^^^^^ duplicate definition
```
Point at trait/impl on associated item mismatch
```
error[E0324]: item `RESOLVED_WRONG` is an associated method, which doesn't match its trait `MyTrait`
--> $DIR/unresolved-trait-impl-item.rs:12:5
|
LL | trait MyTrait {
LL | async fn resolved(&self);
LL | const RESOLVED_WRONG: u8 = 0;
| ----------------------------- item in trait
...
LL | impl MyTrait for i32 {
...
LL | async fn RESOLVED_WRONG() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ does not match trait
```
On `impl` item signature mismatch, point at impl and trait
```
error: `impl` item signature doesn't match `trait` item signature
--> $DIR/self-without-lifetime-constraint.rs:46:5
|
LL | pub trait FromSql: Sized {
LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self, &Self>;
| -------------------------------------------------------------------- expected `fn(ValueRef<'1>) -> Result<(&'2 str, &'1 &'2 str), _>`
...
LL | impl FromSql for &str {
LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult<&str, &&str> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(ValueRef<'1>) -> Result<(&'1 str, &'1 &'1 str), _>`
|
= note: expected signature `fn(ValueRef<'1>) -> Result<(&'2 str, &'1 &'2 str), FromSqlError>`
found signature `fn(ValueRef<'1>) -> Result<(&'1 str, &'1 &'1 str), FromSqlError>`
help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
--> $DIR/self-without-lifetime-constraint.rs:42:60
|
LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self, &Self>;
| ^^^^ consider borrowing this type parameter in the trait
```
rust-bors Bot
pushed a commit
that referenced
this pull request
Sep 30, 2026
Rollup of 7 pull requests Successful merges: - #158936 (Add `std::fs::{Home|Media}Dirs`) - #163414 (Add support for span context to `derive(Diagnostic)` and use it) - #163481 (triagebot: use r-l/r zulip linkifier for r-l/r rustfmt backport nominations) - #163502 (arch::breakpoint: update docs) - #163505 (Sync cg_gcc subtree 2026-09-29) - #163405 (Remove some #[linkage] options) - #163498 (Revert "Rollup merge of #120589 - devnexen:cpuaff_fbsd_upd, r=clarfonthey")
rust-bors Bot
pushed a commit
that referenced
this pull request
Sep 30, 2026
Rollup of 6 pull requests Successful merges: - #158936 (Add `std::fs::{Home|Media}Dirs`) - #163414 (Add support for span context to `derive(Diagnostic)` and use it) - #163481 (triagebot: use r-l/r zulip linkifier for r-l/r rustfmt backport nominations) - #163502 (arch::breakpoint: update docs) - #163505 (Sync cg_gcc subtree 2026-09-29) - #163498 (Revert "Rollup merge of #120589 - devnexen:cpuaff_fbsd_upd, r=clarfonthey")
rust-bors Bot
pushed a commit
that referenced
this pull request
Sep 30, 2026
Rollup merge of #163414 - estebank:derive-diagnostic-context, r=petrochenkov Add support for span context to `derive(Diagnostic)` and use it Allow writing ```rust struct Diagnostic { #[primary_span] #[label("label text")] span: Span, #[context] context: Span, } ``` So that the code pointed at by `context` is shown in the rendered diagnostic without an underline. Point at enclosing item(s) on unexpected item type: ``` error: expected type, found constant --> $DIR/consts.rs:2:19 | LL | pub fn accept( LL | _: impl Trait<K: Copy> | ^------ bounds are not allowed on associated constants | | | unexpected constant | note: the associated constant is defined here --> $DIR/consts.rs:7:5 | LL | const K: i32; | ^^^^^^^^^^ ``` Show trait/impl for duplicate impl associated items ``` error[E0201]: duplicate definitions with name `Bar`: --> $DIR/associated-item-duplicate-names-3.rs:14:5 | LL | trait Foo { LL | type Bar; | --------- item in trait ... LL | impl Foo for Baz { LL | type Bar = i16; | --------------- previous definition here LL | type Bar = u16; | ^^^^^^^^^^^^^^^ duplicate definition ``` Point at trait/impl on associated item mismatch ``` error[E0324]: item `RESOLVED_WRONG` is an associated method, which doesn't match its trait `MyTrait` --> $DIR/unresolved-trait-impl-item.rs:12:5 | LL | trait MyTrait { LL | async fn resolved(&self); LL | const RESOLVED_WRONG: u8 = 0; | ----------------------------- item in trait ... LL | impl MyTrait for i32 { ... LL | async fn RESOLVED_WRONG() {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ does not match trait ``` On `impl` item signature mismatch, point at impl and trait ``` error: `impl` item signature doesn't match `trait` item signature --> $DIR/self-without-lifetime-constraint.rs:46:5 | LL | pub trait FromSql: Sized { LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self, &Self>; | -------------------------------------------------------------------- expected `fn(ValueRef<'1>) -> Result<(&'2 str, &'1 &'2 str), _>` ... LL | impl FromSql for &str { LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult<&str, &&str> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(ValueRef<'1>) -> Result<(&'1 str, &'1 &'1 str), _>` | = note: expected signature `fn(ValueRef<'1>) -> Result<(&'2 str, &'1 &'2 str), FromSqlError>` found signature `fn(ValueRef<'1>) -> Result<(&'1 str, &'1 &'1 str), FromSqlError>` help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait` --> $DIR/self-without-lifetime-constraint.rs:42:60 | LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self, &Self>; | ^^^^ consider borrowing this type parameter in the trait ```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Allow writing
So that the code pointed at by
contextis shown in the rendered diagnostic without an underline.Point at enclosing item(s) on unexpected item type:
Show trait/impl for duplicate impl associated items
Point at trait/impl on associated item mismatch
On
implitem signature mismatch, point at impl and trait