From 02ea38cfff9b07be2366fb76bcd2f24ddb589df2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Wed, 6 Aug 2025 01:26:26 +0200 Subject: [PATCH] Fortify generic param default checks --- .../src/collect/generics_of.rs | 200 +++++++++--------- .../defaults/default-on-impl.rs | 2 +- .../defaults/default-on-impl.stderr | 2 +- .../generic_const_exprs/issue-105257.rs | 6 +- .../generic_const_exprs/issue-105257.stderr | 4 +- .../default_function_param.rs | 2 +- .../default_function_param.stderr | 2 +- .../type-param-defaults.rs | 6 +- .../type-param-defaults.stderr | 6 +- .../generic-const-items/parameter-defaults.rs | 16 +- .../parameter-defaults.stderr | 31 ++- tests/ui/generics/generic-extern.rs | 10 +- tests/ui/generics/generic-extern.stderr | 23 +- .../ui/generics/invalid-type-param-default.rs | 22 ++ .../invalid-type-param-default.stderr | 70 ++++++ .../overlapping-errors-span-issue-123861.rs | 2 +- ...verlapping-errors-span-issue-123861.stderr | 4 +- tests/ui/impl-trait/where-allowed.rs | 12 +- tests/ui/impl-trait/where-allowed.stderr | 55 ++--- tests/ui/issues/issue-26812.rs | 6 - tests/ui/issues/issue-26812.stderr | 30 --- .../ui/lifetimes/unusual-rib-combinations.rs | 2 +- .../lifetimes/unusual-rib-combinations.stderr | 4 +- .../missing-items/missing-type-parameter2.rs | 2 +- .../missing-type-parameter2.stderr | 2 +- ...ed-type-param-in-fn-with-assoc-type.stderr | 2 +- .../default_type_parameter_in_fn_or_impl.rs | 12 -- ...efault_type_parameter_in_fn_or_impl.stderr | 43 ---- ...ce-hir-wf-check-anon-const-issue-122199.rs | 2 +- ...ir-wf-check-anon-const-issue-122199.stderr | 2 +- 30 files changed, 297 insertions(+), 285 deletions(-) create mode 100644 tests/ui/generics/invalid-type-param-default.rs create mode 100644 tests/ui/generics/invalid-type-param-default.stderr delete mode 100644 tests/ui/issues/issue-26812.rs delete mode 100644 tests/ui/issues/issue-26812.stderr delete mode 100644 tests/ui/type/default_type_parameter_in_fn_or_impl.rs delete mode 100644 tests/ui/type/default_type_parameter_in_fn_or_impl.stderr diff --git a/compiler/rustc_hir_analysis/src/collect/generics_of.rs b/compiler/rustc_hir_analysis/src/collect/generics_of.rs index e2462c2d46590..ce0e51f106f59 100644 --- a/compiler/rustc_hir_analysis/src/collect/generics_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/generics_of.rs @@ -223,60 +223,27 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics { "synthetic HIR should have its `generics_of` explicitly fed" ), - _ => span_bug!(tcx.def_span(def_id), "unhandled node {node:?}"), + _ => span_bug!(tcx.def_span(def_id), "generics_of: unexpected node kind {node:?}"), }; - enum Defaults { - Allowed, - // See #36887 - FutureCompatDisallowed, - Deny, - } - - let hir_generics = node.generics().unwrap_or(hir::Generics::empty()); - let (opt_self, allow_defaults) = match node { - Node::Item(item) => { - match item.kind { - ItemKind::Trait(..) | ItemKind::TraitAlias(..) => { - // Add in the self type parameter. - // - // Something of a hack: use the node id for the trait, also as - // the node id for the Self type parameter. - let opt_self = Some(ty::GenericParamDef { - index: 0, - name: kw::SelfUpper, - def_id: def_id.to_def_id(), - pure_wrt_drop: false, - kind: ty::GenericParamDefKind::Type { - has_default: false, - synthetic: false, - }, - }); - - (opt_self, Defaults::Allowed) - } - ItemKind::TyAlias(..) - | ItemKind::Enum(..) - | ItemKind::Struct(..) - | ItemKind::Union(..) => (None, Defaults::Allowed), - ItemKind::Const(..) => (None, Defaults::Deny), - _ => (None, Defaults::FutureCompatDisallowed), - } - } - - Node::OpaqueTy(..) => (None, Defaults::Allowed), - - // GATs - Node::TraitItem(item) if matches!(item.kind, TraitItemKind::Type(..)) => { - (None, Defaults::Deny) - } - Node::ImplItem(item) if matches!(item.kind, ImplItemKind::Type(..)) => { - (None, Defaults::Deny) - } - - _ => (None, Defaults::FutureCompatDisallowed), + // Add in the self type parameter. + let opt_self = if let Node::Item(item) = node + && let ItemKind::Trait(..) | ItemKind::TraitAlias(..) = item.kind + { + // Something of a hack: We reuse the node ID of the trait for the self type parameter. + Some(ty::GenericParamDef { + index: 0, + name: kw::SelfUpper, + def_id: def_id.to_def_id(), + pure_wrt_drop: false, + kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false }, + }) + } else { + None }; + let param_default_policy = param_default_policy(node); + let hir_generics = node.generics().unwrap_or(hir::Generics::empty()); let has_self = opt_self.is_some(); let mut parent_has_self = false; let mut own_start = has_self as u32; @@ -312,60 +279,53 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics { prev + type_start }; - const TYPE_DEFAULT_NOT_ALLOWED: &'static str = "defaults for type parameters are only allowed in \ - `struct`, `enum`, `type`, or `trait` definitions"; - - own_params.extend(hir_generics.params.iter().filter_map(|param| match param.kind { - GenericParamKind::Lifetime { .. } => None, - GenericParamKind::Type { default, synthetic, .. } => { - if default.is_some() { - match allow_defaults { - Defaults::Allowed => {} - Defaults::FutureCompatDisallowed => { - tcx.node_span_lint( - lint::builtin::INVALID_TYPE_PARAM_DEFAULT, - param.hir_id, - param.span, - |lint| { - lint.primary_message(TYPE_DEFAULT_NOT_ALLOWED); - }, - ); - } - Defaults::Deny => { - tcx.dcx().span_err(param.span, TYPE_DEFAULT_NOT_ALLOWED); + own_params.extend(hir_generics.params.iter().filter_map(|param| { + const MESSAGE: &str = "defaults for generic parameters are not allowed here"; + let kind = match param.kind { + GenericParamKind::Lifetime { .. } => return None, + GenericParamKind::Type { default, synthetic } => { + if default.is_some() { + match param_default_policy.expect("no policy for generic param default") { + ParamDefaultPolicy::Allowed => {} + ParamDefaultPolicy::FutureCompatForbidden => { + tcx.node_span_lint( + lint::builtin::INVALID_TYPE_PARAM_DEFAULT, + param.hir_id, + param.span, + |lint| { + lint.primary_message(MESSAGE); + }, + ); + } + ParamDefaultPolicy::Forbidden => { + tcx.dcx().span_err(param.span, MESSAGE); + } } } - } - - let kind = ty::GenericParamDefKind::Type { has_default: default.is_some(), synthetic }; - Some(ty::GenericParamDef { - index: next_index(), - name: param.name.ident().name, - def_id: param.def_id.to_def_id(), - pure_wrt_drop: param.pure_wrt_drop, - kind, - }) - } - GenericParamKind::Const { ty: _, default, synthetic } => { - if !matches!(allow_defaults, Defaults::Allowed) && default.is_some() { - tcx.dcx().span_err( - param.span, - "defaults for const parameters are only allowed in \ - `struct`, `enum`, `type`, or `trait` definitions", - ); + ty::GenericParamDefKind::Type { has_default: default.is_some(), synthetic } } + GenericParamKind::Const { ty: _, default, synthetic } => { + if default.is_some() { + match param_default_policy.expect("no policy for generic param default") { + ParamDefaultPolicy::Allowed => {} + ParamDefaultPolicy::FutureCompatForbidden + | ParamDefaultPolicy::Forbidden => { + tcx.dcx().span_err(param.span, MESSAGE); + } + } + } - let index = next_index(); - - Some(ty::GenericParamDef { - index, - name: param.name.ident().name, - def_id: param.def_id.to_def_id(), - pure_wrt_drop: param.pure_wrt_drop, - kind: ty::GenericParamDefKind::Const { has_default: default.is_some(), synthetic }, - }) - } + ty::GenericParamDefKind::Const { has_default: default.is_some(), synthetic } + } + }; + Some(ty::GenericParamDef { + index: next_index(), + name: param.name.ident().name, + def_id: param.def_id.to_def_id(), + pure_wrt_drop: param.pure_wrt_drop, + kind, + }) })); // provide junk type parameter defs - the only place that @@ -438,6 +398,48 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics { } } +#[derive(Clone, Copy)] +enum ParamDefaultPolicy { + Allowed, + /// Tracked in . + FutureCompatForbidden, + Forbidden, +} + +fn param_default_policy(node: Node<'_>) -> Option { + use rustc_hir::*; + + Some(match node { + Node::Item(item) => match item.kind { + ItemKind::Trait(..) + | ItemKind::TraitAlias(..) + | ItemKind::TyAlias(..) + | ItemKind::Enum(..) + | ItemKind::Struct(..) + | ItemKind::Union(..) => ParamDefaultPolicy::Allowed, + ItemKind::Fn { .. } | ItemKind::Impl(_) => ParamDefaultPolicy::FutureCompatForbidden, + // Re. GCI, we're not bound by backward compatibility. + ItemKind::Const(..) => ParamDefaultPolicy::Forbidden, + _ => return None, + }, + Node::TraitItem(item) => match item.kind { + // Re. GATs and GACs (generic_const_items), we're not bound by backward compatibility. + TraitItemKind::Const(..) | TraitItemKind::Type(..) => ParamDefaultPolicy::Forbidden, + TraitItemKind::Fn(..) => ParamDefaultPolicy::FutureCompatForbidden, + }, + Node::ImplItem(item) => match item.kind { + // Re. GATs and GACs (generic_const_items), we're not bound by backward compatibility. + ImplItemKind::Const(..) | ImplItemKind::Type(..) => ParamDefaultPolicy::Forbidden, + ImplItemKind::Fn(..) => ParamDefaultPolicy::FutureCompatForbidden, + }, + // Generic params are (semantically) invalid on foreign items. Still, for maximum forward + // compatibility, let's hard-reject defaults on them. + Node::ForeignItem(_) => ParamDefaultPolicy::Forbidden, + Node::OpaqueTy(..) => ParamDefaultPolicy::Allowed, + _ => return None, + }) +} + fn has_late_bound_regions<'tcx>(tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Option { struct LateBoundRegionsDetector<'tcx> { tcx: TyCtxt<'tcx>, diff --git a/tests/ui/const-generics/defaults/default-on-impl.rs b/tests/ui/const-generics/defaults/default-on-impl.rs index 9ce46aa09dee6..85d0c58396592 100644 --- a/tests/ui/const-generics/defaults/default-on-impl.rs +++ b/tests/ui/const-generics/defaults/default-on-impl.rs @@ -1,6 +1,6 @@ struct Foo; impl Foo {} -//~^ ERROR defaults for const parameters are only allowed +//~^ ERROR defaults for generic parameters are not allowed here fn main() {} diff --git a/tests/ui/const-generics/defaults/default-on-impl.stderr b/tests/ui/const-generics/defaults/default-on-impl.stderr index 691e0354eddc2..eb5d57e14bf89 100644 --- a/tests/ui/const-generics/defaults/default-on-impl.stderr +++ b/tests/ui/const-generics/defaults/default-on-impl.stderr @@ -1,4 +1,4 @@ -error: defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions +error: defaults for generic parameters are not allowed here --> $DIR/default-on-impl.rs:3:6 | LL | impl Foo {} diff --git a/tests/ui/const-generics/generic_const_exprs/issue-105257.rs b/tests/ui/const-generics/generic_const_exprs/issue-105257.rs index a107556fd79dd..85a28f2b3303b 100644 --- a/tests/ui/const-generics/generic_const_exprs/issue-105257.rs +++ b/tests/ui/const-generics/generic_const_exprs/issue-105257.rs @@ -1,10 +1,10 @@ #![feature(generic_const_exprs)] -#![allow(incomplete_features)] +#![expect(incomplete_features)] trait Trait { - fn fnc(&self) {} //~ERROR defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions + fn fnc(&self) {} //~ERROR defaults for generic parameters are not allowed here //~^ ERROR: mismatched types - fn foo() }>(&self) {} //~ERROR defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions + fn foo() }>(&self) {} //~ERROR defaults for generic parameters are not allowed here } fn main() {} diff --git a/tests/ui/const-generics/generic_const_exprs/issue-105257.stderr b/tests/ui/const-generics/generic_const_exprs/issue-105257.stderr index d7ded0f1f748b..1d0ab56519cfb 100644 --- a/tests/ui/const-generics/generic_const_exprs/issue-105257.stderr +++ b/tests/ui/const-generics/generic_const_exprs/issue-105257.stderr @@ -1,10 +1,10 @@ -error: defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions +error: defaults for generic parameters are not allowed here --> $DIR/issue-105257.rs:5:12 | LL | fn fnc(&self) {} | ^^^^^^^^^^^^^^^^^^^ -error: defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions +error: defaults for generic parameters are not allowed here --> $DIR/issue-105257.rs:7:12 | LL | fn foo() }>(&self) {} diff --git a/tests/ui/const-generics/min_const_generics/default_function_param.rs b/tests/ui/const-generics/min_const_generics/default_function_param.rs index 92d495ef6653b..153cd94849eb5 100644 --- a/tests/ui/const-generics/min_const_generics/default_function_param.rs +++ b/tests/ui/const-generics/min_const_generics/default_function_param.rs @@ -1,4 +1,4 @@ #![crate_type = "lib"] fn foo() {} -//~^ ERROR defaults for const parameters are +//~^ ERROR defaults for generic parameters are not allowed here diff --git a/tests/ui/const-generics/min_const_generics/default_function_param.stderr b/tests/ui/const-generics/min_const_generics/default_function_param.stderr index 247eea3d98995..261298a1c6340 100644 --- a/tests/ui/const-generics/min_const_generics/default_function_param.stderr +++ b/tests/ui/const-generics/min_const_generics/default_function_param.stderr @@ -1,4 +1,4 @@ -error: defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions +error: defaults for generic parameters are not allowed here --> $DIR/default_function_param.rs:3:8 | LL | fn foo() {} diff --git a/tests/ui/generic-associated-types/type-param-defaults.rs b/tests/ui/generic-associated-types/type-param-defaults.rs index eea54c460734c..6e9a62b96c4bc 100644 --- a/tests/ui/generic-associated-types/type-param-defaults.rs +++ b/tests/ui/generic-associated-types/type-param-defaults.rs @@ -4,17 +4,17 @@ trait Trait { type Assoc; - //~^ ERROR defaults for type parameters are only allowed + //~^ ERROR defaults for generic parameters are not allowed here } impl Trait for () { type Assoc = u64; - //~^ ERROR defaults for type parameters are only allowed + //~^ ERROR defaults for generic parameters are not allowed here } impl Trait for u32 { type Assoc = T; - //~^ ERROR defaults for type parameters are only allowed + //~^ ERROR defaults for generic parameters are not allowed here } trait Other {} diff --git a/tests/ui/generic-associated-types/type-param-defaults.stderr b/tests/ui/generic-associated-types/type-param-defaults.stderr index 3c094d45fffcc..d9872dadbdb2e 100644 --- a/tests/ui/generic-associated-types/type-param-defaults.stderr +++ b/tests/ui/generic-associated-types/type-param-defaults.stderr @@ -1,16 +1,16 @@ -error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions +error: defaults for generic parameters are not allowed here --> $DIR/type-param-defaults.rs:6:16 | LL | type Assoc; | ^^^^^^^ -error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions +error: defaults for generic parameters are not allowed here --> $DIR/type-param-defaults.rs:11:16 | LL | type Assoc = u64; | ^^^^^^^ -error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions +error: defaults for generic parameters are not allowed here --> $DIR/type-param-defaults.rs:16:16 | LL | type Assoc = T; diff --git a/tests/ui/generic-const-items/parameter-defaults.rs b/tests/ui/generic-const-items/parameter-defaults.rs index c933db17fa209..b52cb0fd0028b 100644 --- a/tests/ui/generic-const-items/parameter-defaults.rs +++ b/tests/ui/generic-const-items/parameter-defaults.rs @@ -7,9 +7,17 @@ // FIXME(default_type_parameter_fallback): Consider reallowing them once they work properly. -const NONE: Option = None::; //~ ERROR defaults for type parameters are only allowed +const NONE: Option = None::; +//~^ ERROR defaults for generic parameters are not allowed here -fn main() { - let _ = NONE; - //~^ ERROR type annotations needed +impl Host { + const NADA: Option = None::; + //~^ ERROR defaults for generic parameters are not allowed here } + +enum Host {} + +fn body0() { let _ = NONE; } //~ ERROR type annotations needed +fn body1() { let _ = Host::NADA; } //~ ERROR type annotations needed + +fn main() {} diff --git a/tests/ui/generic-const-items/parameter-defaults.stderr b/tests/ui/generic-const-items/parameter-defaults.stderr index 13562c98f6d96..9bf1f6412f59b 100644 --- a/tests/ui/generic-const-items/parameter-defaults.stderr +++ b/tests/ui/generic-const-items/parameter-defaults.stderr @@ -1,20 +1,37 @@ -error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions +error: defaults for generic parameters are not allowed here --> $DIR/parameter-defaults.rs:10:12 | LL | const NONE: Option = None::; | ^^^^^^ +error: defaults for generic parameters are not allowed here + --> $DIR/parameter-defaults.rs:14:16 + | +LL | const NADA: Option = None::; + | ^^^^^^ + +error[E0282]: type annotations needed for `Option<_>` + --> $DIR/parameter-defaults.rs:20:18 + | +LL | fn body0() { let _ = NONE; } + | ^ ---- type must be known at this point + | +help: consider giving this pattern a type, where the type for type parameter `T` is specified + | +LL | fn body0() { let _: Option = NONE; } + | +++++++++++ + error[E0282]: type annotations needed for `Option<_>` - --> $DIR/parameter-defaults.rs:13:9 + --> $DIR/parameter-defaults.rs:21:18 | -LL | let _ = NONE; - | ^ ---- type must be known at this point +LL | fn body1() { let _ = Host::NADA; } + | ^ ---------- type must be known at this point | help: consider giving this pattern a type, where the type for type parameter `T` is specified | -LL | let _: Option = NONE; - | +++++++++++ +LL | fn body1() { let _: Option = Host::NADA; } + | +++++++++++ -error: aborting due to 2 previous errors +error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0282`. diff --git a/tests/ui/generics/generic-extern.rs b/tests/ui/generics/generic-extern.rs index 36fa5eaafd672..b4f00fc118767 100644 --- a/tests/ui/generics/generic-extern.rs +++ b/tests/ui/generics/generic-extern.rs @@ -1,7 +1,15 @@ +// Ensure that we reject generic parameters on foreign items. + extern "C" { fn foo(); //~ ERROR foreign items may not have type parameters + + // Furthermore, check that type parameter defaults lead to a *hard* error, + // not just a lint error, for maximum forward compatibility. + #[allow(invalid_type_param_default)] // Should have no effect here. + fn bar(); //~ ERROR foreign items may not have type parameters + //~^ ERROR defaults for generic parameters are not allowed here } fn main() { - foo::(); //~ ERROR requires unsafe + unsafe { foo::() }; } diff --git a/tests/ui/generics/generic-extern.stderr b/tests/ui/generics/generic-extern.stderr index a3f2882531638..6e83715681264 100644 --- a/tests/ui/generics/generic-extern.stderr +++ b/tests/ui/generics/generic-extern.stderr @@ -1,20 +1,25 @@ error[E0044]: foreign items may not have type parameters - --> $DIR/generic-extern.rs:2:5 + --> $DIR/generic-extern.rs:4:5 | LL | fn foo(); | ^^^^^^^^^^^^ can't have type parameters | = help: replace the type parameters with concrete types like `u32` -error[E0133]: call to unsafe function `foo` is unsafe and requires unsafe function or block - --> $DIR/generic-extern.rs:6:5 +error: defaults for generic parameters are not allowed here + --> $DIR/generic-extern.rs:9:12 | -LL | foo::(); - | ^^^^^^^^^^^^ call to unsafe function +LL | fn bar(); + | ^^^^^^ + +error[E0044]: foreign items may not have type parameters + --> $DIR/generic-extern.rs:9:5 | - = note: consult the function's documentation for information on how to avoid undefined behavior +LL | fn bar(); + | ^^^^^^^^^^^^^^^^^ can't have type parameters + | + = help: replace the type parameters with concrete types like `u32` -error: aborting due to 2 previous errors +error: aborting due to 3 previous errors -Some errors have detailed explanations: E0044, E0133. -For more information about an error, try `rustc --explain E0044`. +For more information about this error, try `rustc --explain E0044`. diff --git a/tests/ui/generics/invalid-type-param-default.rs b/tests/ui/generics/invalid-type-param-default.rs new file mode 100644 index 0000000000000..b47e142605c37 --- /dev/null +++ b/tests/ui/generics/invalid-type-param-default.rs @@ -0,0 +1,22 @@ +// Ensure that we emit the deny-by-default lint `invalid_type_param_default` in locations where +// type parameter defaults were accidentally allowed but don't have any effect whatsoever. +// +// Tracked in . +// FIXME(default_type_parameter_fallback): Consider reallowing them once they work properly. + +fn avg(_: T) {} +//~^ ERROR defaults for generic parameters are not allowed here [invalid_type_param_default] +//~| WARN this was previously accepted + +// issue: +fn mdn(_: T) {} +//~^ ERROR generic parameter defaults cannot reference parameters before they are declared +//~| ERROR defaults for generic parameters are not allowed here [invalid_type_param_default] +//~| WARN this was previously accepted + +struct S(T); +impl S {} +//~^ ERROR defaults for generic parameters are not allowed here [invalid_type_param_default] +//~| WARN this was previously accepted + +fn main() {} diff --git a/tests/ui/generics/invalid-type-param-default.stderr b/tests/ui/generics/invalid-type-param-default.stderr new file mode 100644 index 0000000000000..1c8fdd8ab5ca8 --- /dev/null +++ b/tests/ui/generics/invalid-type-param-default.stderr @@ -0,0 +1,70 @@ +error[E0128]: generic parameter defaults cannot reference parameters before they are declared + --> $DIR/invalid-type-param-default.rs:12:12 + | +LL | fn mdn(_: T) {} + | ^^^^^^^ cannot reference `T` before it is declared + +error: defaults for generic parameters are not allowed here + --> $DIR/invalid-type-param-default.rs:7:8 + | +LL | fn avg(_: T) {} + | ^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #36887 + = note: `#[deny(invalid_type_param_default)]` on by default + +error: defaults for generic parameters are not allowed here + --> $DIR/invalid-type-param-default.rs:12:8 + | +LL | fn mdn(_: T) {} + | ^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #36887 + +error: defaults for generic parameters are not allowed here + --> $DIR/invalid-type-param-default.rs:18:6 + | +LL | impl S {} + | ^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #36887 + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0128`. +Future incompatibility report: Future breakage diagnostic: +error: defaults for generic parameters are not allowed here + --> $DIR/invalid-type-param-default.rs:7:8 + | +LL | fn avg(_: T) {} + | ^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #36887 + = note: `#[deny(invalid_type_param_default)]` on by default + +Future breakage diagnostic: +error: defaults for generic parameters are not allowed here + --> $DIR/invalid-type-param-default.rs:12:8 + | +LL | fn mdn(_: T) {} + | ^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #36887 + = note: `#[deny(invalid_type_param_default)]` on by default + +Future breakage diagnostic: +error: defaults for generic parameters are not allowed here + --> $DIR/invalid-type-param-default.rs:18:6 + | +LL | impl S {} + | ^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #36887 + = note: `#[deny(invalid_type_param_default)]` on by default + diff --git a/tests/ui/generics/overlapping-errors-span-issue-123861.rs b/tests/ui/generics/overlapping-errors-span-issue-123861.rs index e0a27f6874874..2549f4b371465 100644 --- a/tests/ui/generics/overlapping-errors-span-issue-123861.rs +++ b/tests/ui/generics/overlapping-errors-span-issue-123861.rs @@ -1,7 +1,7 @@ fn mainIterator<_ = _> {} //~^ ERROR expected identifier, found reserved identifier `_` //~| ERROR missing parameters for function definition -//~| ERROR defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions [invalid_type_param_default] +//~| ERROR defaults for generic parameters are not allowed here [invalid_type_param_default] //~| WARNING this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! //~| ERROR the placeholder `_` is not allowed within types on item signatures for functions [E0121] diff --git a/tests/ui/generics/overlapping-errors-span-issue-123861.stderr b/tests/ui/generics/overlapping-errors-span-issue-123861.stderr index 7d08d8fed9f92..44e8b4a01e7fd 100644 --- a/tests/ui/generics/overlapping-errors-span-issue-123861.stderr +++ b/tests/ui/generics/overlapping-errors-span-issue-123861.stderr @@ -15,7 +15,7 @@ help: add a parameter list LL | fn mainIterator<_ = _>() {} | ++ -error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions +error: defaults for generic parameters are not allowed here --> $DIR/overlapping-errors-span-issue-123861.rs:1:17 | LL | fn mainIterator<_ = _> {} @@ -35,7 +35,7 @@ error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0121`. Future incompatibility report: Future breakage diagnostic: -error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions +error: defaults for generic parameters are not allowed here --> $DIR/overlapping-errors-span-issue-123861.rs:1:17 | LL | fn mainIterator<_ = _> {} diff --git a/tests/ui/impl-trait/where-allowed.rs b/tests/ui/impl-trait/where-allowed.rs index 1c3c66c537ff9..04a95f7f6f013 100644 --- a/tests/ui/impl-trait/where-allowed.rs +++ b/tests/ui/impl-trait/where-allowed.rs @@ -236,17 +236,15 @@ type InTypeAliasGenericParamDefault = T; //~^ ERROR `impl Trait` is not allowed in generic parameter defaults // Disallowed -impl T {} -//~^ ERROR defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions -//~| WARNING this was previously accepted by the compiler but is being phased out -//~| ERROR `impl Trait` is not allowed in generic parameter defaults +#[expect(invalid_type_param_default)] +impl T {} +//~^ ERROR `impl Trait` is not allowed in generic parameter defaults //~| ERROR no nominal type found // Disallowed +#[expect(invalid_type_param_default)] fn in_method_generic_param_default(_: T) {} -//~^ ERROR defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions -//~| WARNING this was previously accepted by the compiler but is being phased out -//~| ERROR `impl Trait` is not allowed in generic parameter defaults +//~^ ERROR `impl Trait` is not allowed in generic parameter defaults fn main() { let _in_local_variable: impl Fn() = || {}; diff --git a/tests/ui/impl-trait/where-allowed.stderr b/tests/ui/impl-trait/where-allowed.stderr index 052ae5a99315c..08caff326c470 100644 --- a/tests/ui/impl-trait/where-allowed.stderr +++ b/tests/ui/impl-trait/where-allowed.stderr @@ -311,10 +311,10 @@ LL | type InTypeAliasGenericParamDefault = T; = note: `impl Trait` is only allowed in arguments and return types of functions and methods error[E0562]: `impl Trait` is not allowed in generic parameter defaults - --> $DIR/where-allowed.rs:239:11 + --> $DIR/where-allowed.rs:240:10 | -LL | impl T {} - | ^^^^^^^^^^ +LL | impl T {} + | ^^^^^^^^^^ | = note: `impl Trait` is only allowed in arguments and return types of functions and methods @@ -327,7 +327,7 @@ LL | fn in_method_generic_param_default(_: T) {} = note: `impl Trait` is only allowed in arguments and return types of functions and methods error[E0562]: `impl Trait` is not allowed in the type of variable bindings - --> $DIR/where-allowed.rs:252:29 + --> $DIR/where-allowed.rs:250:29 | LL | let _in_local_variable: impl Fn() = || {}; | ^^^^^^^^^ @@ -338,7 +338,7 @@ LL | let _in_local_variable: impl Fn() = || {}; = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date error[E0562]: `impl Trait` is not allowed in closure return types - --> $DIR/where-allowed.rs:254:46 + --> $DIR/where-allowed.rs:252:46 | LL | let _in_return_in_local_variable = || -> impl Fn() { || {} }; | ^^^^^^^^^ @@ -368,25 +368,6 @@ LL - fn in_trait_impl_return() -> impl Debug { () } LL + fn in_trait_impl_return() -> <() as DummyTrait>::Out { () } | -error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions - --> $DIR/where-allowed.rs:246:36 - | -LL | fn in_method_generic_param_default(_: T) {} - | ^^^^^^^^^^^^^^ - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #36887 - = note: `#[deny(invalid_type_param_default)]` on by default - -error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions - --> $DIR/where-allowed.rs:239:7 - | -LL | impl T {} - | ^^^^^^^^^^^^^^ - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #36887 - error[E0283]: type annotations needed --> $DIR/where-allowed.rs:46:57 | @@ -408,10 +389,10 @@ LL | fn in_impl_Fn_return_in_return() -> &'static impl Fn() -> impl Debug { pani where Args: Tuple, F: Fn, A: Allocator, F: ?Sized; error[E0118]: no nominal type found for inherent implementation - --> $DIR/where-allowed.rs:239:1 + --> $DIR/where-allowed.rs:240:1 | -LL | impl T {} - | ^^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type +LL | impl T {} + | ^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type | = note: either implement a trait on it or create a newtype to wrap it instead @@ -431,29 +412,21 @@ LL | type InTypeAlias = impl Debug; | = note: `InTypeAlias` must be used in combination with a concrete type within the same crate -error: aborting due to 50 previous errors +error: aborting due to 48 previous errors Some errors have detailed explanations: E0053, E0118, E0283, E0562, E0658, E0666. For more information about an error, try `rustc --explain E0053`. Future incompatibility report: Future breakage diagnostic: -error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions +warning: defaults for generic parameters are not allowed here --> $DIR/where-allowed.rs:246:36 | LL | fn in_method_generic_param_default(_: T) {} | ^^^^^^^^^^^^^^ - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #36887 - = note: `#[deny(invalid_type_param_default)]` on by default Future breakage diagnostic: -error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions - --> $DIR/where-allowed.rs:239:7 - | -LL | impl T {} - | ^^^^^^^^^^^^^^ +warning: defaults for generic parameters are not allowed here + --> $DIR/where-allowed.rs:240:6 | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #36887 - = note: `#[deny(invalid_type_param_default)]` on by default +LL | impl T {} + | ^^^^^^^^^^^^^^ diff --git a/tests/ui/issues/issue-26812.rs b/tests/ui/issues/issue-26812.rs deleted file mode 100644 index 8eb030a8ec94f..0000000000000 --- a/tests/ui/issues/issue-26812.rs +++ /dev/null @@ -1,6 +0,0 @@ -fn avg(_: T) {} -//~^ ERROR generic parameter defaults cannot reference parameters before they are declared -//~| ERROR defaults for type parameters -//~| WARN previously accepted - -fn main() {} diff --git a/tests/ui/issues/issue-26812.stderr b/tests/ui/issues/issue-26812.stderr deleted file mode 100644 index bb60d67e2876e..0000000000000 --- a/tests/ui/issues/issue-26812.stderr +++ /dev/null @@ -1,30 +0,0 @@ -error[E0128]: generic parameter defaults cannot reference parameters before they are declared - --> $DIR/issue-26812.rs:1:10 - | -LL | fn avg(_: T) {} - | ^^^^^^^ cannot reference `T` before it is declared - -error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions - --> $DIR/issue-26812.rs:1:8 - | -LL | fn avg(_: T) {} - | ^^^^^^^^^ - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #36887 - = note: `#[deny(invalid_type_param_default)]` on by default - -error: aborting due to 2 previous errors - -For more information about this error, try `rustc --explain E0128`. -Future incompatibility report: Future breakage diagnostic: -error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions - --> $DIR/issue-26812.rs:1:8 - | -LL | fn avg(_: T) {} - | ^^^^^^^^^ - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #36887 - = note: `#[deny(invalid_type_param_default)]` on by default - diff --git a/tests/ui/lifetimes/unusual-rib-combinations.rs b/tests/ui/lifetimes/unusual-rib-combinations.rs index 0e92b41ae1ee2..b3e9642332b09 100644 --- a/tests/ui/lifetimes/unusual-rib-combinations.rs +++ b/tests/ui/lifetimes/unusual-rib-combinations.rs @@ -14,7 +14,7 @@ fn b() {} // Paren generic args in AnonymousReportError fn c() {} //~^ ERROR parenthesized type parameters may only be used with a `Fn` trait -//~| ERROR defaults for type parameters are only allowed in +//~| ERROR defaults for generic parameters are not allowed here //~| WARN this was previously accepted // Elided lifetime in path in ConstGeneric diff --git a/tests/ui/lifetimes/unusual-rib-combinations.stderr b/tests/ui/lifetimes/unusual-rib-combinations.stderr index 7373ca8cf8434..bd68479c58c54 100644 --- a/tests/ui/lifetimes/unusual-rib-combinations.stderr +++ b/tests/ui/lifetimes/unusual-rib-combinations.stderr @@ -22,7 +22,7 @@ error[E0214]: parenthesized type parameters may only be used with a `Fn` trait LL | fn c() {} | ^^^^ only `Fn` traits may use parentheses -error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions +error: defaults for generic parameters are not allowed here --> $DIR/unusual-rib-combinations.rs:15:6 | LL | fn c() {} @@ -43,7 +43,7 @@ error: aborting due to 6 previous errors Some errors have detailed explanations: E0106, E0214, E0308, E0770. For more information about an error, try `rustc --explain E0106`. Future incompatibility report: Future breakage diagnostic: -error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions +error: defaults for generic parameters are not allowed here --> $DIR/unusual-rib-combinations.rs:15:6 | LL | fn c() {} diff --git a/tests/ui/missing/missing-items/missing-type-parameter2.rs b/tests/ui/missing/missing-items/missing-type-parameter2.rs index e9b32fb7198d4..772e60b1376c3 100644 --- a/tests/ui/missing/missing-items/missing-type-parameter2.rs +++ b/tests/ui/missing/missing-items/missing-type-parameter2.rs @@ -5,7 +5,7 @@ impl X {} //~| ERROR unresolved item provided when a constant was expected impl X {} //~^ ERROR cannot find type `N` in this scope -//~| ERROR defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions +//~| ERROR defaults for generic parameters are not allowed here //~| ERROR unresolved item provided when a constant was expected fn foo(_: T) where T: Send {} diff --git a/tests/ui/missing/missing-items/missing-type-parameter2.stderr b/tests/ui/missing/missing-items/missing-type-parameter2.stderr index f6418de20b6a4..3c132e769eaef 100644 --- a/tests/ui/missing/missing-items/missing-type-parameter2.stderr +++ b/tests/ui/missing/missing-items/missing-type-parameter2.stderr @@ -103,7 +103,7 @@ help: if this generic argument was intended as a const parameter, surround it wi LL | impl X<{ N }> {} | + + -error: defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions +error: defaults for generic parameters are not allowed here --> $DIR/missing-type-parameter2.rs:6:9 | LL | impl X {} diff --git a/tests/ui/type-inference/unbounded-type-param-in-fn-with-assoc-type.stderr b/tests/ui/type-inference/unbounded-type-param-in-fn-with-assoc-type.stderr index bf8829c09257f..e8bbdaaacbf72 100644 --- a/tests/ui/type-inference/unbounded-type-param-in-fn-with-assoc-type.stderr +++ b/tests/ui/type-inference/unbounded-type-param-in-fn-with-assoc-type.stderr @@ -13,7 +13,7 @@ error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0282`. Future incompatibility report: Future breakage diagnostic: -warning: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions +warning: defaults for generic parameters are not allowed here --> $DIR/unbounded-type-param-in-fn-with-assoc-type.rs:3:11 | LL | fn foo() -> (T, U) { diff --git a/tests/ui/type/default_type_parameter_in_fn_or_impl.rs b/tests/ui/type/default_type_parameter_in_fn_or_impl.rs deleted file mode 100644 index 33038e24bc62b..0000000000000 --- a/tests/ui/type/default_type_parameter_in_fn_or_impl.rs +++ /dev/null @@ -1,12 +0,0 @@ -#![allow(unused)] - -fn avg(_: T) {} -//~^ ERROR defaults for type parameters are only allowed -//~| WARN this was previously accepted - -struct S(T); -impl S {} -//~^ ERROR defaults for type parameters are only allowed -//~| WARN this was previously accepted - -fn main() {} diff --git a/tests/ui/type/default_type_parameter_in_fn_or_impl.stderr b/tests/ui/type/default_type_parameter_in_fn_or_impl.stderr deleted file mode 100644 index a3205cd3c29ca..0000000000000 --- a/tests/ui/type/default_type_parameter_in_fn_or_impl.stderr +++ /dev/null @@ -1,43 +0,0 @@ -error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions - --> $DIR/default_type_parameter_in_fn_or_impl.rs:3:8 - | -LL | fn avg(_: T) {} - | ^^^^^ - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #36887 - = note: `#[deny(invalid_type_param_default)]` on by default - -error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions - --> $DIR/default_type_parameter_in_fn_or_impl.rs:8:6 - | -LL | impl S {} - | ^^^^^ - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #36887 - -error: aborting due to 2 previous errors - -Future incompatibility report: Future breakage diagnostic: -error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions - --> $DIR/default_type_parameter_in_fn_or_impl.rs:3:8 - | -LL | fn avg(_: T) {} - | ^^^^^ - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #36887 - = note: `#[deny(invalid_type_param_default)]` on by default - -Future breakage diagnostic: -error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions - --> $DIR/default_type_parameter_in_fn_or_impl.rs:8:6 - | -LL | impl S {} - | ^^^^^ - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #36887 - = note: `#[deny(invalid_type_param_default)]` on by default - diff --git a/tests/ui/wf/ice-hir-wf-check-anon-const-issue-122199.rs b/tests/ui/wf/ice-hir-wf-check-anon-const-issue-122199.rs index ad7d972879fff..072a699a6b5b9 100644 --- a/tests/ui/wf/ice-hir-wf-check-anon-const-issue-122199.rs +++ b/tests/ui/wf/ice-hir-wf-check-anon-const-issue-122199.rs @@ -4,7 +4,7 @@ trait Trait { fn fnc(&self) -> dyn Trait { //~^ ERROR the name `N` is already used for a generic parameter in this item's generic parameters //~| ERROR expected value, found builtin type `u32` - //~| ERROR defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions + //~| ERROR defaults for generic parameters are not allowed here bar //~^ ERROR cannot find value `bar` in this scope } diff --git a/tests/ui/wf/ice-hir-wf-check-anon-const-issue-122199.stderr b/tests/ui/wf/ice-hir-wf-check-anon-const-issue-122199.stderr index dc5a1cf3485ee..47f3e83fae21a 100644 --- a/tests/ui/wf/ice-hir-wf-check-anon-const-issue-122199.stderr +++ b/tests/ui/wf/ice-hir-wf-check-anon-const-issue-122199.stderr @@ -39,7 +39,7 @@ LL | trait Trait { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information -error: defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions +error: defaults for generic parameters are not allowed here --> $DIR/ice-hir-wf-check-anon-const-issue-122199.rs:4:12 | LL | fn fnc(&self) -> dyn Trait {