From f71ad2f9920817b24a0253566f528bda845d9374 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 5 Jun 2022 09:59:26 +0200 Subject: [PATCH] Suggest 'static when in static/const items. --- .../wrong_number_of_generic_args.rs | 24 +++++++++++++++++++ .../elided-in-expr-position.stderr | 4 ++-- .../issue-81862.stderr | 2 +- .../missing-lifetime-specifier.stderr | 16 ++++++------- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs b/compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs index edd2aedfe87..c440e93fe0a 100644 --- a/compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs +++ b/compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs @@ -317,6 +317,30 @@ fn get_lifetime_args_suggestions_from_param_names( let hir::ParamName::Plain(name) = p.name else { return None }; Some(name.to_string()) })); + // Suggest `'static` when in const/static item-like. + if let hir::Node::Item(hir::Item { + kind: hir::ItemKind::Static { .. } | hir::ItemKind::Const { .. }, + .. + }) + | hir::Node::TraitItem(hir::TraitItem { + kind: hir::TraitItemKind::Const { .. }, + .. + }) + | hir::Node::ImplItem(hir::ImplItem { + kind: hir::ImplItemKind::Const { .. }, + .. + }) + | hir::Node::ForeignItem(hir::ForeignItem { + kind: hir::ForeignItemKind::Static { .. }, + .. + }) + | hir::Node::AnonConst(..) = node + { + ret.extend( + std::iter::repeat("'static".to_owned()) + .take(num_params_to_take.saturating_sub(ret.len())), + ); + } if ret.len() >= num_params_to_take { return ret[..num_params_to_take].join(", "); } diff --git a/src/test/ui/generic-associated-types/elided-in-expr-position.stderr b/src/test/ui/generic-associated-types/elided-in-expr-position.stderr index 9263f3d67e3..b395a1cfd8a 100644 --- a/src/test/ui/generic-associated-types/elided-in-expr-position.stderr +++ b/src/test/ui/generic-associated-types/elided-in-expr-position.stderr @@ -11,7 +11,7 @@ LL | type Assoc<'a> where Self: 'a; | ^^^^^ -- help: add missing lifetime argument | -LL | fn g(&self) -> Self::Assoc<'_>; +LL | fn g(&self) -> Self::Assoc<'a>; | ~~~~~~~~~ error[E0107]: missing generics for associated type `Trait::Assoc` @@ -27,7 +27,7 @@ LL | type Assoc<'a> where Self: 'a; | ^^^^^ -- help: add missing lifetime argument | -LL | fn g(&self) -> Self::Assoc<'_> { +LL | fn g(&self) -> Self::Assoc<'a> { | ~~~~~~~~~ error: aborting due to 2 previous errors diff --git a/src/test/ui/generic-associated-types/issue-81862.stderr b/src/test/ui/generic-associated-types/issue-81862.stderr index 024f8ad89f7..c664b3ee668 100644 --- a/src/test/ui/generic-associated-types/issue-81862.stderr +++ b/src/test/ui/generic-associated-types/issue-81862.stderr @@ -11,7 +11,7 @@ LL | type Item<'a>; | ^^^^ -- help: add missing lifetime argument | -LL | fn next(&mut self) -> Option>; +LL | fn next(&mut self) -> Option>; | ~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/suggestions/missing-lifetime-specifier.stderr b/src/test/ui/suggestions/missing-lifetime-specifier.stderr index b04ea1c9158..1498337549d 100644 --- a/src/test/ui/suggestions/missing-lifetime-specifier.stderr +++ b/src/test/ui/suggestions/missing-lifetime-specifier.stderr @@ -171,8 +171,8 @@ LL | pub union Qux<'t, 'k, I> { | ^^^ -- -- help: add missing lifetime argument | -LL | static e: RefCell>>>> = RefCell::new(HashMap::new()); - | ++++ +LL | static e: RefCell>>>> = RefCell::new(HashMap::new()); + | +++++++++ error[E0107]: this union takes 2 lifetime arguments but 1 lifetime argument was supplied --> $DIR/missing-lifetime-specifier.rs:43:44 @@ -243,8 +243,8 @@ LL | pub union Qux<'t, 'k, I> { | ^^^ -- -- help: add missing lifetime argument | -LL | static e: RefCell>>>> = RefCell::new(HashMap::new()); - | ++++ +LL | static e: RefCell>>>> = RefCell::new(HashMap::new()); + | +++++++++ error[E0107]: this trait takes 2 lifetime arguments but 1 lifetime argument was supplied --> $DIR/missing-lifetime-specifier.rs:51:45 @@ -261,8 +261,8 @@ LL | trait Tar<'t, 'k, I> {} | ^^^ -- -- help: add missing lifetime argument | -LL | static f: RefCell>>>> = RefCell::new(HashMap::new()); - | ++++ +LL | static f: RefCell>>>> = RefCell::new(HashMap::new()); + | +++++++++ error[E0106]: missing lifetime specifier --> $DIR/missing-lifetime-specifier.rs:51:44 @@ -360,8 +360,8 @@ LL | trait Tar<'t, 'k, I> {} | ^^^ -- -- help: add missing lifetime argument | -LL | static f: RefCell>>>> = RefCell::new(HashMap::new()); - | ++++ +LL | static f: RefCell>>>> = RefCell::new(HashMap::new()); + | +++++++++ error: aborting due to 24 previous errors