Skip to content

Commit a156bd7

Browse files
Make spans a bit better
1 parent 2b9279f commit a156bd7

File tree

7 files changed

+48
-46
lines changed

7 files changed

+48
-46
lines changed

compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::diagnostics::utils::{
99
FieldInnerTy, FieldMap, HasFieldMap, SetOnce, SpannedOption, SubdiagnosticKind,
1010
};
1111
use proc_macro2::{Ident, Span, TokenStream};
12-
use quote::{format_ident, quote};
12+
use quote::{format_ident, quote, quote_spanned};
1313
use syn::Token;
1414
use syn::{parse_quote, spanned::Spanned, Attribute, Meta, Path, Type};
1515
use synstructure::{BindingInfo, Structure, VariantInfo};
@@ -251,7 +251,8 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> {
251251
let diag = &self.parent.diag;
252252

253253
let field = binding_info.ast();
254-
let field_binding = &binding_info.binding;
254+
let mut field_binding = binding_info.binding.clone();
255+
field_binding.set_span(field.ty.span());
255256

256257
let ident = field.ident.as_ref().unwrap();
257258
let ident = format_ident!("{}", ident); // strip `r#` prefix, if present
@@ -284,9 +285,9 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> {
284285
name == "primary_span" && matches!(inner_ty, FieldInnerTy::Vec(_));
285286
let (binding, needs_destructure) = if needs_clone {
286287
// `primary_span` can accept a `Vec<Span>` so don't destructure that.
287-
(quote! { #field_binding.clone() }, false)
288+
(quote_spanned! {inner_ty.span()=> #field_binding.clone() }, false)
288289
} else {
289-
(quote! { #field_binding }, true)
290+
(quote_spanned! {inner_ty.span()=> #field_binding }, true)
290291
};
291292

292293
let generated_code = self

compiler/rustc_macros/src/diagnostics/subdiagnostic.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -209,18 +209,20 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
209209
}
210210

211211
/// Generates the code for a field with no attributes.
212-
fn generate_field_set_arg(&mut self, binding: &BindingInfo<'_>) -> TokenStream {
213-
let ast = binding.ast();
214-
212+
fn generate_field_set_arg(&mut self, binding_info: &BindingInfo<'_>) -> TokenStream {
215213
let diag = &self.parent.diag;
216-
let ident = ast.ident.as_ref().unwrap();
217-
// strip `r#` prefix, if present
218-
let ident = format_ident!("{}", ident);
214+
215+
let field = binding_info.ast();
216+
let mut field_binding = binding_info.binding.clone();
217+
field_binding.set_span(field.ty.span());
218+
219+
let ident = field.ident.as_ref().unwrap();
220+
let ident = format_ident!("{}", ident); // strip `r#` prefix, if present
219221

220222
quote! {
221223
#diag.set_arg(
222224
stringify!(#ident),
223-
#binding
225+
#field_binding
224226
);
225227
}
226228
}
@@ -397,7 +399,8 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
397399
clone_suggestion_code: bool,
398400
) -> Result<TokenStream, DiagnosticDeriveError> {
399401
let span = attr.span().unwrap();
400-
let ident = &list.path.segments.last().unwrap().ident;
402+
let mut ident = list.path.segments.last().unwrap().ident.clone();
403+
ident.set_span(info.ty.span());
401404
let name = ident.to_string();
402405
let name = name.as_str();
403406

@@ -496,7 +499,7 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
496499
.variant
497500
.bindings()
498501
.iter()
499-
.filter(|binding| !binding.ast().attrs.is_empty())
502+
.filter(|binding| !should_generate_set_arg(binding.ast()))
500503
.map(|binding| self.generate_field_attr_code(binding, kind_stats))
501504
.collect();
502505

compiler/rustc_macros/src/diagnostics/utils.rs

+6
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ impl<'ty> FieldInnerTy<'ty> {
207207
FieldInnerTy::Plain(..) => quote! { #inner },
208208
}
209209
}
210+
211+
pub fn span(&self) -> proc_macro2::Span {
212+
match self {
213+
FieldInnerTy::Option(ty) | FieldInnerTy::Vec(ty) | FieldInnerTy::Plain(ty) => ty.span(),
214+
}
215+
}
210216
}
211217

212218
/// Field information passed to the builder. Deliberately omits attrs to discourage the

tests/ui-fulldeps/session-diagnostic/diagnostic-derive-doc-comment-field.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// check-fail
22
// Tests that a doc comment will not preclude a field from being considered a diagnostic argument
3+
// normalize-stderr-test "the following other types implement trait `IntoDiagnosticArg`:(?:.*\n){0,9}\s+and \d+ others" -> "normalized in stderr"
4+
// normalize-stderr-test "diagnostic_builder\.rs:[0-9]+:[0-9]+" -> "diagnostic_builder.rs:LL:CC"
35

46
// The proc_macro2 crate handles spans differently when on beta/stable release rather than nightly,
57
// changing the output of this test. Since Subdiagnostic is strictly internal to the compiler
@@ -27,21 +29,21 @@ fluent_messages! { "./example.ftl" }
2729
struct NotIntoDiagnosticArg;
2830

2931
#[derive(Diagnostic)]
30-
//~^ ERROR the trait bound `NotIntoDiagnosticArg: IntoDiagnosticArg` is not satisfied
3132
#[diag(no_crate_example)]
3233
struct Test {
3334
#[primary_span]
3435
span: Span,
3536
/// A doc comment
3637
arg: NotIntoDiagnosticArg,
38+
//~^ ERROR the trait bound `NotIntoDiagnosticArg: IntoDiagnosticArg` is not satisfied
3739
}
3840

3941
#[derive(Subdiagnostic)]
40-
//~^ ERROR the trait bound `NotIntoDiagnosticArg: IntoDiagnosticArg` is not satisfied
4142
#[label(no_crate_example)]
4243
struct SubTest {
4344
#[primary_span]
4445
span: Span,
4546
/// A doc comment
4647
arg: NotIntoDiagnosticArg,
48+
//~^ ERROR the trait bound `NotIntoDiagnosticArg: IntoDiagnosticArg` is not satisfied
4749
}

tests/ui-fulldeps/session-diagnostic/diagnostic-derive-doc-comment-field.stderr

+14-27
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,29 @@
11
error[E0277]: the trait bound `NotIntoDiagnosticArg: IntoDiagnosticArg` is not satisfied
2-
--> $DIR/diagnostic-derive-doc-comment-field.rs:29:10
2+
--> $DIR/diagnostic-derive-doc-comment-field.rs:37:10
33
|
44
LL | #[derive(Diagnostic)]
5-
| ^^^^^^^^^^ the trait `IntoDiagnosticArg` is not implemented for `NotIntoDiagnosticArg`
5+
| ---------- required by a bound introduced by this call
6+
...
7+
LL | arg: NotIntoDiagnosticArg,
8+
| ^^^^^^^^^^^^^^^^^^^^ the trait `IntoDiagnosticArg` is not implemented for `NotIntoDiagnosticArg`
69
|
7-
= help: the following other types implement trait `IntoDiagnosticArg`:
8-
&'a T
9-
&'a std::path::Path
10-
&'a str
11-
&rustc_target::spec::TargetTriple
12-
Box<(dyn std::error::Error + 'static)>
13-
CString
14-
CguReuse
15-
Cow<'a, str>
16-
and 42 others
10+
= help: normalized in stderr
1711
note: required by a bound in `DiagnosticBuilder::<'a, G>::set_arg`
18-
--> $COMPILER_DIR/rustc_errors/src/diagnostic_builder.rs:747:5
19-
= note: this error originates in the derive macro `Diagnostic` which comes from the expansion of the macro `forward` (in Nightly builds, run with -Z macro-backtrace for more info)
12+
--> $COMPILER_DIR/rustc_errors/src/diagnostic_builder.rs:LL:CC
13+
= note: this error originates in the macro `forward` (in Nightly builds, run with -Z macro-backtrace for more info)
2014

2115
error[E0277]: the trait bound `NotIntoDiagnosticArg: IntoDiagnosticArg` is not satisfied
22-
--> $DIR/diagnostic-derive-doc-comment-field.rs:39:10
16+
--> $DIR/diagnostic-derive-doc-comment-field.rs:47:10
2317
|
2418
LL | #[derive(Subdiagnostic)]
25-
| ^^^^^^^^^^^^^ the trait `IntoDiagnosticArg` is not implemented for `NotIntoDiagnosticArg`
19+
| ------------- required by a bound introduced by this call
20+
...
21+
LL | arg: NotIntoDiagnosticArg,
22+
| ^^^^^^^^^^^^^^^^^^^^ the trait `IntoDiagnosticArg` is not implemented for `NotIntoDiagnosticArg`
2623
|
27-
= help: the following other types implement trait `IntoDiagnosticArg`:
28-
&'a T
29-
&'a std::path::Path
30-
&'a str
31-
&rustc_target::spec::TargetTriple
32-
Box<(dyn std::error::Error + 'static)>
33-
CString
34-
CguReuse
35-
Cow<'a, str>
36-
and 42 others
24+
= help: normalized in stderr
3725
note: required by a bound in `Diagnostic::set_arg`
3826
--> $COMPILER_DIR/rustc_errors/src/diagnostic.rs:964:5
39-
= note: this error originates in the derive macro `Subdiagnostic` (in Nightly builds, run with -Z macro-backtrace for more info)
4027

4128
error: aborting due to 2 previous errors
4229

tests/ui-fulldeps/session-diagnostic/diagnostic-derive.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,12 @@ struct ErrorWithDefaultLabelAttr<'a> {
339339
}
340340

341341
#[derive(Diagnostic)]
342-
//~^ ERROR the trait bound `Hello: IntoDiagnosticArg` is not satisfied
343342
#[diag(no_crate_example, code = "E0123")]
344343
struct ArgFieldWithoutSkip {
345344
#[primary_span]
346345
span: Span,
347346
other: Hello,
347+
//~^ ERROR the trait bound `Hello: IntoDiagnosticArg` is not satisfied
348348
}
349349

350350
#[derive(Diagnostic)]

tests/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr

+6-3
Original file line numberDiff line numberDiff line change
@@ -641,15 +641,18 @@ LL | #[derive(Diagnostic)]
641641
= note: this error originates in the derive macro `Diagnostic` (in Nightly builds, run with -Z macro-backtrace for more info)
642642

643643
error[E0277]: the trait bound `Hello: IntoDiagnosticArg` is not satisfied
644-
--> $DIR/diagnostic-derive.rs:341:10
644+
--> $DIR/diagnostic-derive.rs:346:12
645645
|
646646
LL | #[derive(Diagnostic)]
647-
| ^^^^^^^^^^ the trait `IntoDiagnosticArg` is not implemented for `Hello`
647+
| ---------- required by a bound introduced by this call
648+
...
649+
LL | other: Hello,
650+
| ^^^^^ the trait `IntoDiagnosticArg` is not implemented for `Hello`
648651
|
649652
= help: normalized in stderr
650653
note: required by a bound in `DiagnosticBuilder::<'a, G>::set_arg`
651654
--> $COMPILER_DIR/rustc_errors/src/diagnostic_builder.rs:LL:CC
652-
= note: this error originates in the derive macro `Diagnostic` which comes from the expansion of the macro `forward` (in Nightly builds, run with -Z macro-backtrace for more info)
655+
= note: this error originates in the macro `forward` (in Nightly builds, run with -Z macro-backtrace for more info)
653656

654657
error: aborting due to 84 previous errors
655658

0 commit comments

Comments
 (0)