Skip to content

Commit e70105f

Browse files
committed
Keep spans for generics in #[derive(_)] desugaring
Keep the spans for generics coming from a `derive`d Item, so that errors and suggestions have better detail. Fix #84003.
1 parent e5038e2 commit e70105f

File tree

10 files changed

+312
-53
lines changed

10 files changed

+312
-53
lines changed

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,10 @@ impl<'a> TraitDef<'a> {
567567
})
568568
});
569569

570-
let Generics { mut params, mut where_clause, span } =
570+
let Generics { mut params, mut where_clause, .. } =
571571
self.generics.to_generics(cx, self.span, type_ident, generics);
572+
where_clause.span = generics.where_clause.span;
573+
let span = generics.span;
572574

573575
// Create the generic parameters
574576
params.extend(generics.params.iter().map(|param| match &param.kind {
@@ -589,7 +591,7 @@ impl<'a> TraitDef<'a> {
589591
param.bounds.iter().cloned()
590592
).collect();
591593

592-
cx.typaram(self.span, param.ident, vec![], bounds, None)
594+
cx.typaram(param.ident.span, param.ident, vec![], bounds, None)
593595
}
594596
GenericParamKind::Const { ty, kw_span, .. } => {
595597
let const_nodefault_kind = GenericParamKind::Const {
@@ -610,23 +612,23 @@ impl<'a> TraitDef<'a> {
610612
match *clause {
611613
ast::WherePredicate::BoundPredicate(ref wb) => {
612614
ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
613-
span: self.span,
615+
span: wb.span,
614616
bound_generic_params: wb.bound_generic_params.clone(),
615617
bounded_ty: wb.bounded_ty.clone(),
616618
bounds: wb.bounds.to_vec(),
617619
})
618620
}
619621
ast::WherePredicate::RegionPredicate(ref rb) => {
620622
ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
621-
span: self.span,
623+
span: rb.span,
622624
lifetime: rb.lifetime,
623625
bounds: rb.bounds.to_vec(),
624626
})
625627
}
626628
ast::WherePredicate::EqPredicate(ref we) => {
627629
ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
628630
id: ast::DUMMY_NODE_ID,
629-
span: self.span,
631+
span: we.span,
630632
lhs_ty: we.lhs_ty.clone(),
631633
rhs_ty: we.rhs_ty.clone(),
632634
})
@@ -691,13 +693,13 @@ impl<'a> TraitDef<'a> {
691693
.iter()
692694
.map(|param| match param.kind {
693695
GenericParamKind::Lifetime { .. } => {
694-
GenericArg::Lifetime(cx.lifetime(self.span, param.ident))
696+
GenericArg::Lifetime(cx.lifetime(param.ident.span, param.ident))
695697
}
696698
GenericParamKind::Type { .. } => {
697-
GenericArg::Type(cx.ty_ident(self.span, param.ident))
699+
GenericArg::Type(cx.ty_ident(param.ident.span, param.ident))
698700
}
699701
GenericParamKind::Const { .. } => {
700-
GenericArg::Const(cx.const_ident(self.span, param.ident))
702+
GenericArg::Const(cx.const_ident(param.ident.span, param.ident))
701703
}
702704
})
703705
.collect();
@@ -1556,11 +1558,9 @@ impl<'a> TraitDef<'a> {
15561558

15571559
let is_tuple = matches!(struct_def, ast::VariantData::Tuple(..));
15581560
match (just_spans.is_empty(), named_idents.is_empty()) {
1559-
(false, false) => cx.span_bug(
1560-
self.span,
1561-
"a struct with named and unnamed \
1562-
fields in generic `derive`",
1563-
),
1561+
(false, false) => {
1562+
cx.span_bug(self.span, "a struct with named and unnamed fields in generic `derive`")
1563+
}
15641564
// named fields
15651565
(_, false) => Named(named_idents),
15661566
// unnamed fields

compiler/rustc_builtin_macros/src/deriving/generic/ty.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,6 @@ fn mk_ty_param(
211211
cx.typaram(span, Ident::new(name, span), attrs.to_owned(), bounds, None)
212212
}
213213

214-
fn mk_generics(params: Vec<ast::GenericParam>, span: Span) -> Generics {
215-
Generics {
216-
params,
217-
where_clause: ast::WhereClause { has_where_token: false, predicates: Vec::new(), span },
218-
span,
219-
}
220-
}
221-
222214
/// Bounds on type parameters.
223215
#[derive(Clone)]
224216
pub struct Bounds {
@@ -236,7 +228,7 @@ impl Bounds {
236228
self_ty: Ident,
237229
self_generics: &Generics,
238230
) -> Generics {
239-
let generic_params = self
231+
let params = self
240232
.bounds
241233
.iter()
242234
.map(|t| {
@@ -245,7 +237,11 @@ impl Bounds {
245237
})
246238
.collect();
247239

248-
mk_generics(generic_params, span)
240+
Generics {
241+
params,
242+
where_clause: ast::WhereClause { has_where_token: false, predicates: Vec::new(), span },
243+
span,
244+
}
249245
}
250246
}
251247

compiler/rustc_hir/src/hir.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ use rustc_index::vec::IndexVec;
1717
use rustc_macros::HashStable_Generic;
1818
use rustc_span::source_map::Spanned;
1919
use rustc_span::symbol::{kw, sym, Ident, Symbol};
20-
use rustc_span::{def_id::LocalDefId, BytePos};
21-
use rustc_span::{MultiSpan, Span, DUMMY_SP};
20+
use rustc_span::{def_id::LocalDefId, BytePos, MultiSpan, Span, DUMMY_SP};
2221
use rustc_target::asm::InlineAsmRegOrRegClass;
2322
use rustc_target::spec::abi::Abi;
2423

@@ -529,7 +528,6 @@ impl GenericParam<'hir> {
529528
pub fn bounds_span(&self) -> Option<Span> {
530529
self.bounds.iter().fold(None, |span, bound| {
531530
let span = span.map(|s| s.to(bound.span())).unwrap_or_else(|| bound.span());
532-
533531
Some(span)
534532
})
535533
}

compiler/rustc_resolve/src/late/diagnostics.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -1803,7 +1803,7 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
18031803
);
18041804
err.span_label(lifetime_ref.span, "undeclared lifetime");
18051805
let mut suggests_in_band = false;
1806-
let mut suggest_note = true;
1806+
let mut suggested_spans = vec![];
18071807
for missing in &self.missing_named_lifetime_spots {
18081808
match missing {
18091809
MissingLifetimeSpot::Generics(generics) => {
@@ -1821,23 +1821,17 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
18211821
suggests_in_band = true;
18221822
(generics.span, format!("<{}>", lifetime_ref))
18231823
};
1824+
if suggested_spans.contains(&span) {
1825+
continue;
1826+
}
1827+
suggested_spans.push(span);
18241828
if !span.from_expansion() {
18251829
err.span_suggestion(
18261830
span,
18271831
&format!("consider introducing lifetime `{}` here", lifetime_ref),
18281832
sugg,
18291833
Applicability::MaybeIncorrect,
18301834
);
1831-
} else if suggest_note {
1832-
suggest_note = false; // Avoid displaying the same help multiple times.
1833-
err.span_label(
1834-
span,
1835-
&format!(
1836-
"lifetime `{}` is missing in item created through this procedural \
1837-
macro",
1838-
lifetime_ref,
1839-
),
1840-
);
18411835
}
18421836
}
18431837
MissingLifetimeSpot::HigherRanked { span, span_type } => {

src/test/ui/issues/issue-38821.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ note: required because of the requirements on the impl of `IntoNullable` for `<C
1010
LL | impl<T: NotNull> IntoNullable for T {
1111
| ^^^^^^^^^^^^ ^
1212
= note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)
13+
help: consider further restricting the associated type
14+
|
15+
LL | Expr: Expression<SqlType=<Col::SqlType as IntoNullable>::Nullable>, <Col as Expression>::SqlType: NotNull
16+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1317

1418
error: aborting due to previous error
1519

src/test/ui/issues/issue-50480.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
#[derive(Clone, Copy)]
22
//~^ ERROR the trait `Copy` may not be implemented for this type
3-
struct Foo(NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
3+
struct Foo(N, NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
44
//~^ ERROR cannot find type `NotDefined` in this scope
55
//~| ERROR cannot find type `NotDefined` in this scope
6+
//~| ERROR cannot find type `N` in this scope
7+
//~| ERROR cannot find type `N` in this scope
8+
//~| ERROR `i32` is not an iterator
9+
10+
#[derive(Clone, Copy)]
11+
//~^ ERROR the trait `Copy` may not be implemented for this type
12+
struct Bar<T>(T, N, NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
13+
//~^ ERROR cannot find type `NotDefined` in this scope
14+
//~| ERROR cannot find type `N` in this scope
615
//~| ERROR `i32` is not an iterator
716

817
fn main() {}

src/test/ui/issues/issue-50480.stderr

+76-13
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,61 @@
1-
error[E0412]: cannot find type `NotDefined` in this scope
1+
error[E0412]: cannot find type `N` in this scope
22
--> $DIR/issue-50480.rs:3:12
33
|
4-
LL | struct Foo(NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
5-
| ^^^^^^^^^^ not found in this scope
4+
LL | struct Foo(N, NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
5+
| -^ not found in this scope
6+
| |
7+
| help: you might be missing a type parameter: `<N>`
68

79
error[E0412]: cannot find type `NotDefined` in this scope
10+
--> $DIR/issue-50480.rs:3:15
11+
|
12+
LL | struct Foo(N, NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
13+
| ^^^^^^^^^^ not found in this scope
14+
15+
error[E0412]: cannot find type `N` in this scope
816
--> $DIR/issue-50480.rs:3:12
917
|
10-
LL | struct Foo(NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
11-
| ^^^^^^^^^^ not found in this scope
18+
LL | struct Foo(N, NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
19+
| -^ not found in this scope
20+
| |
21+
| help: you might be missing a type parameter: `<N>`
22+
23+
error[E0412]: cannot find type `NotDefined` in this scope
24+
--> $DIR/issue-50480.rs:3:15
25+
|
26+
LL | struct Foo(N, NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
27+
| - ^^^^^^^^^^ not found in this scope
28+
| |
29+
| help: you might be missing a type parameter: `<NotDefined>`
30+
31+
error[E0412]: cannot find type `N` in this scope
32+
--> $DIR/issue-50480.rs:12:18
33+
|
34+
LL | struct Bar<T>(T, N, NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
35+
| - ^
36+
| |
37+
| similarly named type parameter `T` defined here
38+
|
39+
help: a type parameter with a similar name exists
40+
|
41+
LL | struct Bar<T>(T, T, NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
42+
| ~
43+
help: you might be missing a type parameter
44+
|
45+
LL | struct Bar<T, N>(T, N, NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
46+
| +++
47+
48+
error[E0412]: cannot find type `NotDefined` in this scope
49+
--> $DIR/issue-50480.rs:12:21
50+
|
51+
LL | struct Bar<T>(T, N, NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
52+
| ^^^^^^^^^^ not found in this scope
1253

1354
error[E0277]: `i32` is not an iterator
14-
--> $DIR/issue-50480.rs:3:24
55+
--> $DIR/issue-50480.rs:3:27
1556
|
16-
LL | struct Foo(NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
17-
| ^^^^^^^^^^^^^^^^^^^^^^^ `i32` is not an iterator
57+
LL | struct Foo(N, NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
58+
| ^^^^^^^^^^^^^^^^^^^^^^^ `i32` is not an iterator
1859
|
1960
= help: the trait `Iterator` is not implemented for `i32`
2061
= note: if you want to iterate between `start` until a value `end`, use the exclusive range syntax `start..end` or the inclusive range syntax `start..=end`
@@ -25,14 +66,36 @@ error[E0204]: the trait `Copy` may not be implemented for this type
2566
LL | #[derive(Clone, Copy)]
2667
| ^^^^
2768
LL |
28-
LL | struct Foo(NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
29-
| -------- ------ this field does not implement `Copy`
30-
| |
31-
| this field does not implement `Copy`
69+
LL | struct Foo(N, NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
70+
| -------- ------ this field does not implement `Copy`
71+
| |
72+
| this field does not implement `Copy`
73+
|
74+
= note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)
75+
76+
error[E0277]: `i32` is not an iterator
77+
--> $DIR/issue-50480.rs:12:33
78+
|
79+
LL | struct Bar<T>(T, N, NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
80+
| ^^^^^^^^^^^^^^^^^^^^^^^ `i32` is not an iterator
81+
|
82+
= help: the trait `Iterator` is not implemented for `i32`
83+
= note: if you want to iterate between `start` until a value `end`, use the exclusive range syntax `start..end` or the inclusive range syntax `start..=end`
84+
85+
error[E0204]: the trait `Copy` may not be implemented for this type
86+
--> $DIR/issue-50480.rs:10:17
87+
|
88+
LL | #[derive(Clone, Copy)]
89+
| ^^^^
90+
LL |
91+
LL | struct Bar<T>(T, N, NotDefined, <i32 as Iterator>::Item, Vec<i32>, String);
92+
| -------- ------ this field does not implement `Copy`
93+
| |
94+
| this field does not implement `Copy`
3295
|
3396
= note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)
3497

35-
error: aborting due to 4 previous errors
98+
error: aborting due to 10 previous errors
3699

37100
Some errors have detailed explanations: E0204, E0277, E0412.
38101
For more information about an error, try `rustc --explain E0204`.

src/test/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ LL | a: &'b str,
1111
error[E0261]: use of undeclared lifetime name `'b`
1212
--> $DIR/undeclared-lifetime-used-in-debug-macro-issue-70152.rs:3:9
1313
|
14-
LL | #[derive(Eq, PartialEq)]
15-
| -- lifetime `'b` is missing in item created through this procedural macro
1614
LL | struct Test {
15+
| - help: consider introducing lifetime `'b` here: `<'b>`
1716
LL | a: &'b str,
1817
| ^^ undeclared lifetime
1918
|

0 commit comments

Comments
 (0)