From 09d5d0766eaaf0dbf1c1773af79d26f73aa65073 Mon Sep 17 00:00:00 2001 From: Kevin Per Date: Tue, 9 Feb 2021 08:35:58 +0000 Subject: [PATCH 1/5] Fixing bad suggestion for `_` in `const` type when a function #81885 --- compiler/rustc_typeck/src/astconv/mod.rs | 1 + compiler/rustc_typeck/src/collect.rs | 22 +++- src/test/ui/did_you_mean/bad-assoc-ty.stderr | 20 --- src/test/ui/issues/issue-74086.stderr | 5 +- src/test/ui/issues/issue-81885.rs | 10 ++ src/test/ui/issues/issue-81885.stderr | 15 +++ src/test/ui/self/self-infer.stderr | 10 -- .../typeck_type_placeholder_item.stderr | 116 ++---------------- .../typeck_type_placeholder_item_help.stderr | 5 +- 9 files changed, 54 insertions(+), 150 deletions(-) create mode 100644 src/test/ui/issues/issue-81885.rs create mode 100644 src/test/ui/issues/issue-81885.stderr diff --git a/compiler/rustc_typeck/src/astconv/mod.rs b/compiler/rustc_typeck/src/astconv/mod.rs index 5659345f0ff..877c7631c9b 100644 --- a/compiler/rustc_typeck/src/astconv/mod.rs +++ b/compiler/rustc_typeck/src/astconv/mod.rs @@ -2327,6 +2327,7 @@ pub fn ty_of_fn( &generics.params[..], visitor.0, true, + true ); } diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index b1d98d75196..3d0220bfd67 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -141,6 +141,7 @@ struct CollectItemTypesVisitor<'tcx> { generics: &[hir::GenericParam<'_>], placeholder_types: Vec, suggest: bool, + is_fn: bool, ) { if placeholder_types.is_empty() { return; @@ -171,7 +172,9 @@ struct CollectItemTypesVisitor<'tcx> { } let mut err = bad_placeholder_type(tcx, placeholder_types); - if suggest { + + // Suggest, but only if it is not a function + if suggest && !is_fn { err.multipart_suggestion( "use type parameters instead", sugg, @@ -198,7 +201,14 @@ fn reject_placeholder_type_signatures_in_item(tcx: TyCtxt<'tcx>, item: &'tcx hir let mut visitor = PlaceholderHirTyCollector::default(); visitor.visit_item(item); - placeholder_type_error(tcx, Some(generics.span), &generics.params[..], visitor.0, suggest); + placeholder_type_error( + tcx, + Some(generics.span), + &generics.params[..], + visitor.0, + suggest, + false + ); } impl Visitor<'tcx> for CollectItemTypesVisitor<'tcx> { @@ -743,7 +753,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::HirId) { // Account for `const C: _;`. let mut visitor = PlaceholderHirTyCollector::default(); visitor.visit_trait_item(trait_item); - placeholder_type_error(tcx, None, &[], visitor.0, false); + placeholder_type_error(tcx, None, &[], visitor.0, false, false); } hir::TraitItemKind::Type(_, Some(_)) => { @@ -752,7 +762,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::HirId) { // Account for `type T = _;`. let mut visitor = PlaceholderHirTyCollector::default(); visitor.visit_trait_item(trait_item); - placeholder_type_error(tcx, None, &[], visitor.0, false); + placeholder_type_error(tcx, None, &[], visitor.0, false, false); } hir::TraitItemKind::Type(_, None) => { @@ -761,7 +771,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::HirId) { // even if there is no concrete type. let mut visitor = PlaceholderHirTyCollector::default(); visitor.visit_trait_item(trait_item); - placeholder_type_error(tcx, None, &[], visitor.0, false); + placeholder_type_error(tcx, None, &[], visitor.0, false, false); } }; @@ -782,7 +792,7 @@ fn convert_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::HirId) { // Account for `type T = _;` let mut visitor = PlaceholderHirTyCollector::default(); visitor.visit_impl_item(impl_item); - placeholder_type_error(tcx, None, &[], visitor.0, false); + placeholder_type_error(tcx, None, &[], visitor.0, false, false); } hir::ImplItemKind::Const(..) => {} } diff --git a/src/test/ui/did_you_mean/bad-assoc-ty.stderr b/src/test/ui/did_you_mean/bad-assoc-ty.stderr index ebc0883370b..fe67e099527 100644 --- a/src/test/ui/did_you_mean/bad-assoc-ty.stderr +++ b/src/test/ui/did_you_mean/bad-assoc-ty.stderr @@ -129,33 +129,18 @@ LL | fn foo>(x: X) {} | ^ ^ not allowed in type signatures | | | not allowed in type signatures - | -help: use type parameters instead - | -LL | fn foo, T>(x: X) {} - | ^ ^ ^^^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/bad-assoc-ty.rs:52:34 | LL | fn bar(_: F) where F: Fn() -> _ {} | ^ not allowed in type signatures - | -help: use type parameters instead - | -LL | fn bar(_: F) where F: Fn() -> T {} - | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/bad-assoc-ty.rs:55:19 | LL | fn baz _>(_: F) {} | ^ not allowed in type signatures - | -help: use type parameters instead - | -LL | fn baz T, T>(_: F) {} - | ^^^^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/bad-assoc-ty.rs:58:33 @@ -217,11 +202,6 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa | LL | fn foo(_: F) where F: Fn() -> _ {} | ^ not allowed in type signatures - | -help: use type parameters instead - | -LL | fn foo(_: F) where F: Fn() -> T {} - | ^^^ ^ error: aborting due to 28 previous errors diff --git a/src/test/ui/issues/issue-74086.stderr b/src/test/ui/issues/issue-74086.stderr index 4127f48a093..e602425059e 100644 --- a/src/test/ui/issues/issue-74086.stderr +++ b/src/test/ui/issues/issue-74086.stderr @@ -2,10 +2,7 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa --> $DIR/issue-74086.rs:2:20 | LL | static BUG: fn(_) -> u8 = |_| 8; - | ^ - | | - | not allowed in type signatures - | help: use type parameters instead: `T` + | ^ not allowed in type signatures error: aborting due to previous error diff --git a/src/test/ui/issues/issue-81885.rs b/src/test/ui/issues/issue-81885.rs new file mode 100644 index 00000000000..86c39d4a48c --- /dev/null +++ b/src/test/ui/issues/issue-81885.rs @@ -0,0 +1,10 @@ +const TEST4: fn() -> _ = 42; + //~^ ERROR the type placeholder `_` is not allowed within types on item + //signatures + +fn main() { + const TEST5: fn() -> _ = 42; + //~^ ERROR the type placeholder `_` is not allowed within types on item + //signatures + +} diff --git a/src/test/ui/issues/issue-81885.stderr b/src/test/ui/issues/issue-81885.stderr new file mode 100644 index 00000000000..955b4283874 --- /dev/null +++ b/src/test/ui/issues/issue-81885.stderr @@ -0,0 +1,15 @@ +error[E0121]: the type placeholder `_` is not allowed within types on item signatures + --> $DIR/issue-81885.rs:1:22 + | +LL | const TEST4: fn() -> _ = 42; + | ^ not allowed in type signatures + +error[E0121]: the type placeholder `_` is not allowed within types on item signatures + --> $DIR/issue-81885.rs:6:26 + | +LL | const TEST5: fn() -> _ = 42; + | ^ not allowed in type signatures + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0121`. diff --git a/src/test/ui/self/self-infer.stderr b/src/test/ui/self/self-infer.stderr index 1475b212b56..f91cfe5eb62 100644 --- a/src/test/ui/self/self-infer.stderr +++ b/src/test/ui/self/self-infer.stderr @@ -3,22 +3,12 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa | LL | fn f(self: _) {} | ^ not allowed in type signatures - | -help: use type parameters instead - | -LL | fn f(self: T) {} - | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/self-infer.rs:5:17 | LL | fn g(self: &_) {} | ^ not allowed in type signatures - | -help: use type parameters instead - | -LL | fn g(self: &T) {} - | ^^^ ^ error: aborting due to 2 previous errors diff --git a/src/test/ui/typeck/typeck_type_placeholder_item.stderr b/src/test/ui/typeck/typeck_type_placeholder_item.stderr index 684f451b7c3..18e06bc2b35 100644 --- a/src/test/ui/typeck/typeck_type_placeholder_item.stderr +++ b/src/test/ui/typeck/typeck_type_placeholder_item.stderr @@ -92,64 +92,36 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa | LL | fn test6(_: _) { } | ^ not allowed in type signatures - | -help: use type parameters instead - | -LL | fn test6(_: T) { } - | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:24:18 | LL | fn test6_b(_: _, _: T) { } | ^ not allowed in type signatures - | -help: use type parameters instead - | -LL | fn test6_b(_: U, _: T) { } - | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:27:30 | LL | fn test6_c(_: _, _: (T, K, L, A, B)) { } | ^ not allowed in type signatures - | -help: use type parameters instead - | -LL | fn test6_c(_: U, _: (T, K, L, A, B)) { } - | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:30:13 | LL | fn test7(x: _) { let _x: usize = x; } | ^ not allowed in type signatures - | -help: use type parameters instead - | -LL | fn test7(x: T) { let _x: usize = x; } - | ^^^ ^ - -error[E0121]: the type placeholder `_` is not allowed within types on item signatures - --> $DIR/typeck_type_placeholder_item.rs:33:22 - | -LL | fn test8(_f: fn() -> _) { } - | ^ - | | - | not allowed in type signatures - | help: use type parameters instead: `T` error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:33:22 | LL | fn test8(_f: fn() -> _) { } | ^ not allowed in type signatures + +error[E0121]: the type placeholder `_` is not allowed within types on item signatures + --> $DIR/typeck_type_placeholder_item.rs:33:22 | -help: use type parameters instead - | -LL | fn test8(_f: fn() -> T) { } - | ^^^ ^ +LL | fn test8(_f: fn() -> _) { } + | ^ not allowed in type signatures error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:47:26 @@ -257,42 +229,24 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa | LL | fn fn_test6(_: _) { } | ^ not allowed in type signatures - | -help: use type parameters instead - | -LL | fn fn_test6(_: T) { } - | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:97:20 | LL | fn fn_test7(x: _) { let _x: usize = x; } | ^ not allowed in type signatures - | -help: use type parameters instead - | -LL | fn fn_test7(x: T) { let _x: usize = x; } - | ^^^ ^ - -error[E0121]: the type placeholder `_` is not allowed within types on item signatures - --> $DIR/typeck_type_placeholder_item.rs:100:29 - | -LL | fn fn_test8(_f: fn() -> _) { } - | ^ - | | - | not allowed in type signatures - | help: use type parameters instead: `T` error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:100:29 | LL | fn fn_test8(_f: fn() -> _) { } | ^ not allowed in type signatures + +error[E0121]: the type placeholder `_` is not allowed within types on item signatures + --> $DIR/typeck_type_placeholder_item.rs:100:29 | -help: use type parameters instead - | -LL | fn fn_test8(_f: fn() -> T) { } - | ^^^ ^ +LL | fn fn_test8(_f: fn() -> _) { } + | ^ not allowed in type signatures error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:123:12 @@ -415,11 +369,6 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa | LL | fn method_test1(&self, x: _); | ^ not allowed in type signatures - | -help: use type parameters instead - | -LL | fn method_test1(&self, x: T); - | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:142:31 @@ -428,33 +377,18 @@ LL | fn method_test2(&self, x: _) -> _; | ^ ^ not allowed in type signatures | | | not allowed in type signatures - | -help: use type parameters instead - | -LL | fn method_test2(&self, x: T) -> T; - | ^^^ ^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:144:31 | LL | fn method_test3(&self) -> _; | ^ not allowed in type signatures - | -help: use type parameters instead - | -LL | fn method_test3(&self) -> T; - | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:146:26 | LL | fn assoc_fn_test1(x: _); | ^ not allowed in type signatures - | -help: use type parameters instead - | -LL | fn assoc_fn_test1(x: T); - | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:148:26 @@ -463,22 +397,12 @@ LL | fn assoc_fn_test2(x: _) -> _; | ^ ^ not allowed in type signatures | | | not allowed in type signatures - | -help: use type parameters instead - | -LL | fn assoc_fn_test2(x: T) -> T; - | ^^^ ^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:150:28 | LL | fn assoc_fn_test3() -> _; | ^ not allowed in type signatures - | -help: use type parameters instead - | -LL | fn assoc_fn_test3() -> T; - | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:190:14 @@ -521,11 +445,6 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa | LL | fn test10(&self, _x : _) { } | ^ not allowed in type signatures - | -help: use type parameters instead - | -LL | fn test10(&self, _x : T) { } - | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:58:24 @@ -541,11 +460,6 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa | LL | fn clone_from(&mut self, other: _) { *self = Test9; } | ^ not allowed in type signatures - | -help: use type parameters instead - | -LL | fn clone_from(&mut self, other: T) { *self = Test9; } - | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:107:31 @@ -561,11 +475,6 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa | LL | fn fn_test10(&self, _x : _) { } | ^ not allowed in type signatures - | -help: use type parameters instead - | -LL | fn fn_test10(&self, _x : T) { } - | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:115:28 @@ -581,11 +490,6 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa | LL | fn clone_from(&mut self, other: _) { *self = FnTest9; } | ^ not allowed in type signatures - | -help: use type parameters instead - | -LL | fn clone_from(&mut self, other: T) { *self = FnTest9; } - | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:201:14 diff --git a/src/test/ui/typeck/typeck_type_placeholder_item_help.stderr b/src/test/ui/typeck/typeck_type_placeholder_item_help.stderr index 0121e186316..f868c8d4834 100644 --- a/src/test/ui/typeck/typeck_type_placeholder_item_help.stderr +++ b/src/test/ui/typeck/typeck_type_placeholder_item_help.stderr @@ -29,10 +29,7 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa --> $DIR/typeck_type_placeholder_item_help.rs:13:22 | LL | const TEST4: fn() -> _ = 42; - | ^ - | | - | not allowed in type signatures - | help: use type parameters instead: `T` + | ^ not allowed in type signatures error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item_help.rs:17:18 From 95be69dabb9ddd5fe32a33942b1cfe4914c79c38 Mon Sep 17 00:00:00 2001 From: Kevin Per Date: Tue, 9 Feb 2021 08:42:08 +0000 Subject: [PATCH 2/5] Fixing codestyle --- compiler/rustc_typeck/src/collect.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index 3d0220bfd67..d26f3242048 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -172,7 +172,7 @@ struct CollectItemTypesVisitor<'tcx> { } let mut err = bad_placeholder_type(tcx, placeholder_types); - + // Suggest, but only if it is not a function if suggest && !is_fn { err.multipart_suggestion( @@ -202,11 +202,11 @@ fn reject_placeholder_type_signatures_in_item(tcx: TyCtxt<'tcx>, item: &'tcx hir visitor.visit_item(item); placeholder_type_error( - tcx, - Some(generics.span), - &generics.params[..], - visitor.0, - suggest, + tcx, + Some(generics.span), + &generics.params[..], + visitor.0, + suggest, false ); } From 49fc41f047dbb961427e1f2421e36a1cacc799dd Mon Sep 17 00:00:00 2001 From: Kevin Per Date: Tue, 9 Feb 2021 10:28:49 +0000 Subject: [PATCH 3/5] Running ./x.py fmt --- compiler/rustc_typeck/src/astconv/mod.rs | 2 +- compiler/rustc_typeck/src/collect.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_typeck/src/astconv/mod.rs b/compiler/rustc_typeck/src/astconv/mod.rs index 877c7631c9b..66bded85f3f 100644 --- a/compiler/rustc_typeck/src/astconv/mod.rs +++ b/compiler/rustc_typeck/src/astconv/mod.rs @@ -2327,7 +2327,7 @@ pub fn ty_of_fn( &generics.params[..], visitor.0, true, - true + true, ); } diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index d26f3242048..58e083f2c24 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -207,7 +207,7 @@ fn reject_placeholder_type_signatures_in_item(tcx: TyCtxt<'tcx>, item: &'tcx hir &generics.params[..], visitor.0, suggest, - false + false, ); } From fba2f883f39332f43d9cd8f69ebc720076e64e0d Mon Sep 17 00:00:00 2001 From: Kevin Per Date: Wed, 10 Feb 2021 15:49:23 +0000 Subject: [PATCH 4/5] Implementing more sophisticated filter for fn in const or static --- compiler/rustc_typeck/src/astconv/mod.rs | 6 ++- compiler/rustc_typeck/src/check/mod.rs | 1 + compiler/rustc_typeck/src/collect.rs | 69 ++++++++++++++++++------ 3 files changed, 59 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_typeck/src/astconv/mod.rs b/compiler/rustc_typeck/src/astconv/mod.rs index 66bded85f3f..120a59aa9d2 100644 --- a/compiler/rustc_typeck/src/astconv/mod.rs +++ b/compiler/rustc_typeck/src/astconv/mod.rs @@ -2145,12 +2145,14 @@ fn ast_ty_to_ty_inner(&self, ast_ty: &hir::Ty<'_>, borrowed: bool) -> Ty<'tcx> { } hir::TyKind::BareFn(ref bf) => { require_c_abi_if_c_variadic(tcx, &bf.decl, bf.abi, ast_ty.span); + tcx.mk_fn_ptr(self.ty_of_fn( bf.unsafety, bf.abi, &bf.decl, &hir::Generics::empty(), None, + Some(ast_ty), )) } hir::TyKind::TraitObject(ref bounds, ref lifetime) => { @@ -2290,6 +2292,7 @@ pub fn ty_of_fn( decl: &hir::FnDecl<'_>, generics: &hir::Generics<'_>, ident_span: Option, + hir_ty: Option<&hir::Ty<'_>>, ) -> ty::PolyFnSig<'tcx> { debug!("ty_of_fn"); @@ -2321,13 +2324,14 @@ pub fn ty_of_fn( // only want to emit an error complaining about them if infer types (`_`) are not // allowed. `allow_ty_infer` gates this behavior. We check for the presence of // `ident_span` to not emit an error twice when we have `fn foo(_: fn() -> _)`. + crate::collect::placeholder_type_error( tcx, ident_span.map(|sp| sp.shrink_to_hi()), &generics.params[..], visitor.0, true, - true, + hir_ty, ); } diff --git a/compiler/rustc_typeck/src/check/mod.rs b/compiler/rustc_typeck/src/check/mod.rs index 229127e95d9..fce7ae8119e 100644 --- a/compiler/rustc_typeck/src/check/mod.rs +++ b/compiler/rustc_typeck/src/check/mod.rs @@ -502,6 +502,7 @@ fn typeck_with_fallback<'tcx>( decl, &hir::Generics::empty(), None, + None, ) } else { tcx.fn_sig(def_id) diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index 58e083f2c24..754fc6a9c7b 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -141,7 +141,7 @@ struct CollectItemTypesVisitor<'tcx> { generics: &[hir::GenericParam<'_>], placeholder_types: Vec, suggest: bool, - is_fn: bool, + hir_ty: Option<&hir::Ty<'_>>, ) { if placeholder_types.is_empty() { return; @@ -173,13 +173,39 @@ struct CollectItemTypesVisitor<'tcx> { let mut err = bad_placeholder_type(tcx, placeholder_types); - // Suggest, but only if it is not a function - if suggest && !is_fn { - err.multipart_suggestion( - "use type parameters instead", - sugg, - Applicability::HasPlaceholders, - ); + // Suggest, but only if it is not a function in const or static + if suggest { + let mut is_fn = false; + let mut is_const = false; + let mut is_static = false; + + if let Some(hir_ty) = hir_ty { + if let hir::TyKind::BareFn(_) = hir_ty.kind { + is_fn = true; + + // Check if parent is const or static + let parent_id = tcx.hir().get_parent_node(hir_ty.hir_id); + let parent_node = tcx.hir().get(parent_id); + + if let hir::Node::Item(item) = parent_node { + if let hir::ItemKind::Const(_, _) = item.kind { + is_const = true; + } else if let hir::ItemKind::Static(_, _, _) = item.kind { + is_static = true; + } + } + } + } + + // if function is wrapped around a const or static, + // then don't show the suggestion + if !(is_fn && (is_const || is_static)) { + err.multipart_suggestion( + "use type parameters instead", + sugg, + Applicability::HasPlaceholders, + ); + } } err.emit(); } @@ -207,7 +233,7 @@ fn reject_placeholder_type_signatures_in_item(tcx: TyCtxt<'tcx>, item: &'tcx hir &generics.params[..], visitor.0, suggest, - false, + None, ); } @@ -648,6 +674,7 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::HirId) { let it = tcx.hir().expect_item(item_id); debug!("convert: item {} with id {}", it.ident, it.hir_id); let def_id = tcx.hir().local_def_id(item_id); + match it.kind { // These don't define types. hir::ItemKind::ExternCrate(_) @@ -753,7 +780,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::HirId) { // Account for `const C: _;`. let mut visitor = PlaceholderHirTyCollector::default(); visitor.visit_trait_item(trait_item); - placeholder_type_error(tcx, None, &[], visitor.0, false, false); + placeholder_type_error(tcx, None, &[], visitor.0, false, None); } hir::TraitItemKind::Type(_, Some(_)) => { @@ -762,7 +789,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::HirId) { // Account for `type T = _;`. let mut visitor = PlaceholderHirTyCollector::default(); visitor.visit_trait_item(trait_item); - placeholder_type_error(tcx, None, &[], visitor.0, false, false); + placeholder_type_error(tcx, None, &[], visitor.0, false, None); } hir::TraitItemKind::Type(_, None) => { @@ -771,7 +798,8 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::HirId) { // even if there is no concrete type. let mut visitor = PlaceholderHirTyCollector::default(); visitor.visit_trait_item(trait_item); - placeholder_type_error(tcx, None, &[], visitor.0, false, false); + + placeholder_type_error(tcx, None, &[], visitor.0, false, None); } }; @@ -792,7 +820,8 @@ fn convert_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::HirId) { // Account for `type T = _;` let mut visitor = PlaceholderHirTyCollector::default(); visitor.visit_impl_item(impl_item); - placeholder_type_error(tcx, None, &[], visitor.0, false, false); + + placeholder_type_error(tcx, None, &[], visitor.0, false, None); } hir::ImplItemKind::Const(..) => {} } @@ -1583,6 +1612,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> { &sig.decl, &generics, Some(ident.span), + None, ), } } @@ -1592,9 +1622,15 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> { ident, generics, .. - }) => { - AstConv::ty_of_fn(&icx, header.unsafety, header.abi, decl, &generics, Some(ident.span)) - } + }) => AstConv::ty_of_fn( + &icx, + header.unsafety, + header.abi, + decl, + &generics, + Some(ident.span), + None, + ), ForeignItem(&hir::ForeignItem { kind: ForeignItemKind::Fn(ref fn_decl, _, _), @@ -2264,6 +2300,7 @@ fn compute_sig_of_foreign_fn_decl<'tcx>( decl, &hir::Generics::empty(), Some(ident.span), + None, ); // Feature gate SIMD types in FFI, since I am not sure that the From 1adc6be23f93c2a348cbf568fb89dc546319e199 Mon Sep 17 00:00:00 2001 From: Kevin Per Date: Fri, 12 Feb 2021 09:19:54 +0000 Subject: [PATCH 5/5] Update .stderr --- src/test/ui/did_you_mean/bad-assoc-ty.stderr | 20 +++ src/test/ui/self/self-infer.stderr | 10 ++ .../typeck_type_placeholder_item.stderr | 116 ++++++++++++++++-- 3 files changed, 136 insertions(+), 10 deletions(-) diff --git a/src/test/ui/did_you_mean/bad-assoc-ty.stderr b/src/test/ui/did_you_mean/bad-assoc-ty.stderr index fe67e099527..ebc0883370b 100644 --- a/src/test/ui/did_you_mean/bad-assoc-ty.stderr +++ b/src/test/ui/did_you_mean/bad-assoc-ty.stderr @@ -129,18 +129,33 @@ LL | fn foo>(x: X) {} | ^ ^ not allowed in type signatures | | | not allowed in type signatures + | +help: use type parameters instead + | +LL | fn foo, T>(x: X) {} + | ^ ^ ^^^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/bad-assoc-ty.rs:52:34 | LL | fn bar(_: F) where F: Fn() -> _ {} | ^ not allowed in type signatures + | +help: use type parameters instead + | +LL | fn bar(_: F) where F: Fn() -> T {} + | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/bad-assoc-ty.rs:55:19 | LL | fn baz _>(_: F) {} | ^ not allowed in type signatures + | +help: use type parameters instead + | +LL | fn baz T, T>(_: F) {} + | ^^^^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/bad-assoc-ty.rs:58:33 @@ -202,6 +217,11 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa | LL | fn foo(_: F) where F: Fn() -> _ {} | ^ not allowed in type signatures + | +help: use type parameters instead + | +LL | fn foo(_: F) where F: Fn() -> T {} + | ^^^ ^ error: aborting due to 28 previous errors diff --git a/src/test/ui/self/self-infer.stderr b/src/test/ui/self/self-infer.stderr index f91cfe5eb62..1475b212b56 100644 --- a/src/test/ui/self/self-infer.stderr +++ b/src/test/ui/self/self-infer.stderr @@ -3,12 +3,22 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa | LL | fn f(self: _) {} | ^ not allowed in type signatures + | +help: use type parameters instead + | +LL | fn f(self: T) {} + | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/self-infer.rs:5:17 | LL | fn g(self: &_) {} | ^ not allowed in type signatures + | +help: use type parameters instead + | +LL | fn g(self: &T) {} + | ^^^ ^ error: aborting due to 2 previous errors diff --git a/src/test/ui/typeck/typeck_type_placeholder_item.stderr b/src/test/ui/typeck/typeck_type_placeholder_item.stderr index 18e06bc2b35..684f451b7c3 100644 --- a/src/test/ui/typeck/typeck_type_placeholder_item.stderr +++ b/src/test/ui/typeck/typeck_type_placeholder_item.stderr @@ -92,36 +92,64 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa | LL | fn test6(_: _) { } | ^ not allowed in type signatures + | +help: use type parameters instead + | +LL | fn test6(_: T) { } + | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:24:18 | LL | fn test6_b(_: _, _: T) { } | ^ not allowed in type signatures + | +help: use type parameters instead + | +LL | fn test6_b(_: U, _: T) { } + | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:27:30 | LL | fn test6_c(_: _, _: (T, K, L, A, B)) { } | ^ not allowed in type signatures + | +help: use type parameters instead + | +LL | fn test6_c(_: U, _: (T, K, L, A, B)) { } + | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:30:13 | LL | fn test7(x: _) { let _x: usize = x; } | ^ not allowed in type signatures + | +help: use type parameters instead + | +LL | fn test7(x: T) { let _x: usize = x; } + | ^^^ ^ + +error[E0121]: the type placeholder `_` is not allowed within types on item signatures + --> $DIR/typeck_type_placeholder_item.rs:33:22 + | +LL | fn test8(_f: fn() -> _) { } + | ^ + | | + | not allowed in type signatures + | help: use type parameters instead: `T` error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:33:22 | LL | fn test8(_f: fn() -> _) { } | ^ not allowed in type signatures - -error[E0121]: the type placeholder `_` is not allowed within types on item signatures - --> $DIR/typeck_type_placeholder_item.rs:33:22 | -LL | fn test8(_f: fn() -> _) { } - | ^ not allowed in type signatures +help: use type parameters instead + | +LL | fn test8(_f: fn() -> T) { } + | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:47:26 @@ -229,24 +257,42 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa | LL | fn fn_test6(_: _) { } | ^ not allowed in type signatures + | +help: use type parameters instead + | +LL | fn fn_test6(_: T) { } + | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:97:20 | LL | fn fn_test7(x: _) { let _x: usize = x; } | ^ not allowed in type signatures + | +help: use type parameters instead + | +LL | fn fn_test7(x: T) { let _x: usize = x; } + | ^^^ ^ + +error[E0121]: the type placeholder `_` is not allowed within types on item signatures + --> $DIR/typeck_type_placeholder_item.rs:100:29 + | +LL | fn fn_test8(_f: fn() -> _) { } + | ^ + | | + | not allowed in type signatures + | help: use type parameters instead: `T` error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:100:29 | LL | fn fn_test8(_f: fn() -> _) { } | ^ not allowed in type signatures - -error[E0121]: the type placeholder `_` is not allowed within types on item signatures - --> $DIR/typeck_type_placeholder_item.rs:100:29 | -LL | fn fn_test8(_f: fn() -> _) { } - | ^ not allowed in type signatures +help: use type parameters instead + | +LL | fn fn_test8(_f: fn() -> T) { } + | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:123:12 @@ -369,6 +415,11 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa | LL | fn method_test1(&self, x: _); | ^ not allowed in type signatures + | +help: use type parameters instead + | +LL | fn method_test1(&self, x: T); + | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:142:31 @@ -377,18 +428,33 @@ LL | fn method_test2(&self, x: _) -> _; | ^ ^ not allowed in type signatures | | | not allowed in type signatures + | +help: use type parameters instead + | +LL | fn method_test2(&self, x: T) -> T; + | ^^^ ^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:144:31 | LL | fn method_test3(&self) -> _; | ^ not allowed in type signatures + | +help: use type parameters instead + | +LL | fn method_test3(&self) -> T; + | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:146:26 | LL | fn assoc_fn_test1(x: _); | ^ not allowed in type signatures + | +help: use type parameters instead + | +LL | fn assoc_fn_test1(x: T); + | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:148:26 @@ -397,12 +463,22 @@ LL | fn assoc_fn_test2(x: _) -> _; | ^ ^ not allowed in type signatures | | | not allowed in type signatures + | +help: use type parameters instead + | +LL | fn assoc_fn_test2(x: T) -> T; + | ^^^ ^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:150:28 | LL | fn assoc_fn_test3() -> _; | ^ not allowed in type signatures + | +help: use type parameters instead + | +LL | fn assoc_fn_test3() -> T; + | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:190:14 @@ -445,6 +521,11 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa | LL | fn test10(&self, _x : _) { } | ^ not allowed in type signatures + | +help: use type parameters instead + | +LL | fn test10(&self, _x : T) { } + | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:58:24 @@ -460,6 +541,11 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa | LL | fn clone_from(&mut self, other: _) { *self = Test9; } | ^ not allowed in type signatures + | +help: use type parameters instead + | +LL | fn clone_from(&mut self, other: T) { *self = Test9; } + | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:107:31 @@ -475,6 +561,11 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa | LL | fn fn_test10(&self, _x : _) { } | ^ not allowed in type signatures + | +help: use type parameters instead + | +LL | fn fn_test10(&self, _x : T) { } + | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:115:28 @@ -490,6 +581,11 @@ error[E0121]: the type placeholder `_` is not allowed within types on item signa | LL | fn clone_from(&mut self, other: _) { *self = FnTest9; } | ^ not allowed in type signatures + | +help: use type parameters instead + | +LL | fn clone_from(&mut self, other: T) { *self = FnTest9; } + | ^^^ ^ error[E0121]: the type placeholder `_` is not allowed within types on item signatures --> $DIR/typeck_type_placeholder_item.rs:201:14