Skip to content

Commit f71ad2f

Browse files
committed
Suggest 'static when in static/const items.
1 parent af8739b commit f71ad2f

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs

+24
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,30 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
317317
let hir::ParamName::Plain(name) = p.name else { return None };
318318
Some(name.to_string())
319319
}));
320+
// Suggest `'static` when in const/static item-like.
321+
if let hir::Node::Item(hir::Item {
322+
kind: hir::ItemKind::Static { .. } | hir::ItemKind::Const { .. },
323+
..
324+
})
325+
| hir::Node::TraitItem(hir::TraitItem {
326+
kind: hir::TraitItemKind::Const { .. },
327+
..
328+
})
329+
| hir::Node::ImplItem(hir::ImplItem {
330+
kind: hir::ImplItemKind::Const { .. },
331+
..
332+
})
333+
| hir::Node::ForeignItem(hir::ForeignItem {
334+
kind: hir::ForeignItemKind::Static { .. },
335+
..
336+
})
337+
| hir::Node::AnonConst(..) = node
338+
{
339+
ret.extend(
340+
std::iter::repeat("'static".to_owned())
341+
.take(num_params_to_take.saturating_sub(ret.len())),
342+
);
343+
}
320344
if ret.len() >= num_params_to_take {
321345
return ret[..num_params_to_take].join(", ");
322346
}

src/test/ui/generic-associated-types/elided-in-expr-position.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | type Assoc<'a> where Self: 'a;
1111
| ^^^^^ --
1212
help: add missing lifetime argument
1313
|
14-
LL | fn g(&self) -> Self::Assoc<'_>;
14+
LL | fn g(&self) -> Self::Assoc<'a>;
1515
| ~~~~~~~~~
1616

1717
error[E0107]: missing generics for associated type `Trait::Assoc`
@@ -27,7 +27,7 @@ LL | type Assoc<'a> where Self: 'a;
2727
| ^^^^^ --
2828
help: add missing lifetime argument
2929
|
30-
LL | fn g(&self) -> Self::Assoc<'_> {
30+
LL | fn g(&self) -> Self::Assoc<'a> {
3131
| ~~~~~~~~~
3232

3333
error: aborting due to 2 previous errors

src/test/ui/generic-associated-types/issue-81862.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | type Item<'a>;
1111
| ^^^^ --
1212
help: add missing lifetime argument
1313
|
14-
LL | fn next(&mut self) -> Option<Self::Item<'_>>;
14+
LL | fn next(&mut self) -> Option<Self::Item<'a>>;
1515
| ~~~~~~~~
1616

1717
error: aborting due to previous error

src/test/ui/suggestions/missing-lifetime-specifier.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ LL | pub union Qux<'t, 'k, I> {
171171
| ^^^ -- --
172172
help: add missing lifetime argument
173173
|
174-
LL | static e: RefCell<HashMap<i32, Vec<Vec<Qux<'static, '_, i32>>>>> = RefCell::new(HashMap::new());
175-
| ++++
174+
LL | static e: RefCell<HashMap<i32, Vec<Vec<Qux<'static, 'static, i32>>>>> = RefCell::new(HashMap::new());
175+
| +++++++++
176176

177177
error[E0107]: this union takes 2 lifetime arguments but 1 lifetime argument was supplied
178178
--> $DIR/missing-lifetime-specifier.rs:43:44
@@ -243,8 +243,8 @@ LL | pub union Qux<'t, 'k, I> {
243243
| ^^^ -- --
244244
help: add missing lifetime argument
245245
|
246-
LL | static e: RefCell<HashMap<i32, Vec<Vec<Qux<'static, '_, i32>>>>> = RefCell::new(HashMap::new());
247-
| ++++
246+
LL | static e: RefCell<HashMap<i32, Vec<Vec<Qux<'static, 'static, i32>>>>> = RefCell::new(HashMap::new());
247+
| +++++++++
248248

249249
error[E0107]: this trait takes 2 lifetime arguments but 1 lifetime argument was supplied
250250
--> $DIR/missing-lifetime-specifier.rs:51:45
@@ -261,8 +261,8 @@ LL | trait Tar<'t, 'k, I> {}
261261
| ^^^ -- --
262262
help: add missing lifetime argument
263263
|
264-
LL | static f: RefCell<HashMap<i32, Vec<Vec<&Tar<'static, '_, i32>>>>> = RefCell::new(HashMap::new());
265-
| ++++
264+
LL | static f: RefCell<HashMap<i32, Vec<Vec<&Tar<'static, 'static, i32>>>>> = RefCell::new(HashMap::new());
265+
| +++++++++
266266

267267
error[E0106]: missing lifetime specifier
268268
--> $DIR/missing-lifetime-specifier.rs:51:44
@@ -360,8 +360,8 @@ LL | trait Tar<'t, 'k, I> {}
360360
| ^^^ -- --
361361
help: add missing lifetime argument
362362
|
363-
LL | static f: RefCell<HashMap<i32, Vec<Vec<&Tar<'static, '_, i32>>>>> = RefCell::new(HashMap::new());
364-
| ++++
363+
LL | static f: RefCell<HashMap<i32, Vec<Vec<&Tar<'static, 'static, i32>>>>> = RefCell::new(HashMap::new());
364+
| +++++++++
365365

366366
error: aborting due to 24 previous errors
367367

0 commit comments

Comments
 (0)