From 16c2d215dff3bd0a3f3d500596b566f194433d92 Mon Sep 17 00:00:00 2001 From: b1yd <2156864690@qq.com> Date: Tue, 16 Jun 2026 13:12:27 +0800 Subject: [PATCH] fix --- .../rustc_hir_analysis/src/collect/type_of.rs | 16 +++++++-- .../rustc_hir_analysis/src/diagnostics.rs | 9 +++++ compiler/rustc_resolve/src/diagnostics.rs | 4 +++ compiler/rustc_resolve/src/error_helper.rs | 12 +++++-- compiler/rustc_resolve/src/ident.rs | 27 +++++++++----- compiler/rustc_resolve/src/lib.rs | 2 ++ .../allow-self-in-const-generics.rs | 36 +++++++++++++++++++ .../allow-self-in-const-generics.stderr | 9 +++++ .../ban-self-when-feature-not-enabled.rs | 13 +++++++ .../ban-self-when-feature-not-enabled.stderr | 24 +++++++++++++ 10 files changed, 138 insertions(+), 14 deletions(-) create mode 100644 tests/ui/const-generics/allow-self-in-const-generics.rs create mode 100644 tests/ui/const-generics/allow-self-in-const-generics.stderr create mode 100644 tests/ui/const-generics/ban-self-when-feature-not-enabled.rs create mode 100644 tests/ui/const-generics/ban-self-when-feature-not-enabled.stderr diff --git a/compiler/rustc_hir_analysis/src/collect/type_of.rs b/compiler/rustc_hir_analysis/src/collect/type_of.rs index 9c8e61d89da97..e4da6660031f6 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of.rs @@ -13,6 +13,7 @@ use tracing::instrument; use super::{HirPlaceholderCollector, ItemCtxt, bad_placeholder}; use crate::check::wfcheck::check_static_item; +use crate::diagnostics::ParamInTyOfConstParam; use crate::hir_ty_lowering::HirTyLowerer; mod opaque; @@ -235,8 +236,19 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_ } Node::GenericParam(param) => match ¶m.kind { - GenericParamKind::Type { default: Some(ty), .. } - | GenericParamKind::Const { ty, .. } => icx.lower_ty(ty), + GenericParamKind::Type { default: Some(ty), .. } => icx.lower_ty(ty), + GenericParamKind::Const { ty, .. } => { + let lowered_ty = icx.lower_ty(ty); + if !tcx.features().generic_const_parameter_types() && lowered_ty.has_param() { + let guar = tcx + .dcx() + .create_err(ParamInTyOfConstParam { span: ty.span, ty: lowered_ty }) + .emit(); + Ty::new_error(tcx, guar) + } else { + lowered_ty + } + } x => bug!("unexpected non-type Node::GenericParam: {:?}", x), }, diff --git a/compiler/rustc_hir_analysis/src/diagnostics.rs b/compiler/rustc_hir_analysis/src/diagnostics.rs index a671b02b5fa66..c48fbf17ef687 100644 --- a/compiler/rustc_hir_analysis/src/diagnostics.rs +++ b/compiler/rustc_hir_analysis/src/diagnostics.rs @@ -2030,3 +2030,12 @@ pub(crate) struct OnlyStructsCanBeViewedAdt<'tcx> { pub article: &'static str, pub kind: &'static str, } + +#[derive(Diagnostic)] +#[diag("the type of const parameters must not depend on other generic parameters", code = E0770)] +pub(crate) struct ParamInTyOfConstParam<'tcx> { + #[primary_span] + #[label("the type `{$ty}` must not depend on other generic parameter")] + pub(crate) span: Span, + pub(crate) ty: Ty<'tcx>, +} diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 22cc6a581f19a..a0dedfa7d2f53 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -384,6 +384,10 @@ pub(crate) struct SelfInGenericParamDefault { pub(crate) struct SelfInConstGenericTy { #[primary_span] pub(crate) span: Span, + #[help( + "add `#![feature(min_adt_const_params)]` to the crate attributes to enable `Self` as a const parameter type" + )] + pub(crate) enable_feature: bool, } #[derive(Diagnostic)] diff --git a/compiler/rustc_resolve/src/error_helper.rs b/compiler/rustc_resolve/src/error_helper.rs index 8ab193afe0eee..e49d1e3a7e106 100644 --- a/compiler/rustc_resolve/src/error_helper.rs +++ b/compiler/rustc_resolve/src/error_helper.rs @@ -1283,6 +1283,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { ResolutionError::ParamInTyOfConstParam { name } => { self.dcx().create_err(diagnostics::ParamInTyOfConstParam { span, name }) } + ResolutionError::SelfInConstParam => { + self.dcx().create_err(diagnostics::SelfInConstGenericTy { + span, + enable_feature: self.tcx().sess.is_nightly_build(), + }) + } ResolutionError::ParamInNonTrivialAnonConst { is_gca, name, param_kind: is_type } => { self.dcx().create_err(diagnostics::ParamInNonTrivialAnonConst { span, @@ -1304,9 +1310,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { ForwardGenericParamBanReason::Default => { self.dcx().create_err(diagnostics::SelfInGenericParamDefault { span }) } - ForwardGenericParamBanReason::ConstParamTy => { - self.dcx().create_err(diagnostics::SelfInConstGenericTy { span }) - } + ForwardGenericParamBanReason::ConstParamTy => self + .dcx() + .create_err(diagnostics::SelfInConstGenericTy { span, enable_feature: false }), }, ResolutionError::UnreachableLabel { name, definition_span, suggestion } => { let ((sub_suggestion_label, sub_suggestion), sub_unreachable_label) = diff --git a/compiler/rustc_resolve/src/ident.rs b/compiler/rustc_resolve/src/ident.rs index 12dd05bfe6e60..1758730a991d7 100644 --- a/compiler/rustc_resolve/src/ident.rs +++ b/compiler/rustc_resolve/src/ident.rs @@ -1593,18 +1593,27 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { } RibKind::ConstParamTy => { - if !self.features.generic_const_parameter_types() { + // We check whether Self depends on generics parameters during HIR analysis + if self.features.generic_const_parameter_types() + || (self.features.min_adt_const_params() + || self.features.adt_const_params() + && matches!(res, Res::SelfTyAlias { .. })) + { + continue; + } else { if let Some(span) = finalize { - self.report_error( - span, - ResolutionError::ParamInTyOfConstParam { - name: rib_ident.name, - }, - ); + if matches!(res, Res::SelfTyAlias { .. }) { + self.report_error(span, ResolutionError::SelfInConstParam); + } else { + self.report_error( + span, + ResolutionError::ParamInTyOfConstParam { + name: rib_ident.name, + }, + ); + } } return Res::Err; - } else { - continue; } } diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 911350665ed32..397a8d6c44bc2 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -297,6 +297,8 @@ enum ResolutionError<'ra> { // problematic to use *forward declared* parameters when the feature is enabled. /// ERROR E0770: the type of const parameters must not depend on other generic parameters. ParamInTyOfConstParam { name: Symbol }, + /// cannot use self in const param + SelfInConstParam, /// generic parameters must not be used inside const evaluations. /// /// This error is only emitted when using `min_const_generics`. diff --git a/tests/ui/const-generics/allow-self-in-const-generics.rs b/tests/ui/const-generics/allow-self-in-const-generics.rs new file mode 100644 index 0000000000000..d81866c80546d --- /dev/null +++ b/tests/ui/const-generics/allow-self-in-const-generics.rs @@ -0,0 +1,36 @@ +// Allow Self in const generics when Self doesn't depends on generics(#149203) +#![feature(min_adt_const_params)] + +//1 +trait MyTrait { + fn foo(); +} + +impl MyTrait for i32 { + fn foo() {} +} + +//2 +impl Wrap { + fn f() {} + //~^ ERROR the type of const parameters must not depend on other generic parameters + +} +struct Wrap(T); + +//3 +type Foo = Bar; + +#[derive(Eq, PartialEq, core::marker::ConstParamTy)] +struct Bar; + +trait Trait { + fn bar(); +} + +impl Trait for Foo { + fn bar() {} + // FIXME: currently the compiler let this pass + // https://github.com/rust-lang/rust/pull/157949#discussion_r3544858218 +} +fn main(){} diff --git a/tests/ui/const-generics/allow-self-in-const-generics.stderr b/tests/ui/const-generics/allow-self-in-const-generics.stderr new file mode 100644 index 0000000000000..26864e9803306 --- /dev/null +++ b/tests/ui/const-generics/allow-self-in-const-generics.stderr @@ -0,0 +1,9 @@ +error[E0770]: the type of const parameters must not depend on other generic parameters + --> $DIR/allow-self-in-const-generics.rs:15:19 + | +LL | fn f() {} + | ^^^^ the type `Wrap` must not depend on other generic parameter + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0770`. diff --git a/tests/ui/const-generics/ban-self-when-feature-not-enabled.rs b/tests/ui/const-generics/ban-self-when-feature-not-enabled.rs new file mode 100644 index 0000000000000..49241033b7251 --- /dev/null +++ b/tests/ui/const-generics/ban-self-when-feature-not-enabled.rs @@ -0,0 +1,13 @@ +// Ban Self in const generics when min_adt_const_params and adt_const_params are not enabled +// #149203 +trait MyTrait { + fn foo(); +} + +impl MyTrait for i32 { + fn foo() {} + //~^ ERROR cannot use `Self` in const parameter type + //~| ERROR associated function `foo` has an incompatible generic parameter for trait `MyTrait` +} + +fn main(){} diff --git a/tests/ui/const-generics/ban-self-when-feature-not-enabled.stderr b/tests/ui/const-generics/ban-self-when-feature-not-enabled.stderr new file mode 100644 index 0000000000000..4961d79900375 --- /dev/null +++ b/tests/ui/const-generics/ban-self-when-feature-not-enabled.stderr @@ -0,0 +1,24 @@ +error: cannot use `Self` in const parameter type + --> $DIR/ban-self-when-feature-not-enabled.rs:8:21 + | +LL | fn foo() {} + | ^^^^ + | + = help: add `#![feature(min_adt_const_params)]` to the crate attributes to enable `Self` as a const parameter type + +error[E0053]: associated function `foo` has an incompatible generic parameter for trait `MyTrait` + --> $DIR/ban-self-when-feature-not-enabled.rs:8:12 + | +LL | trait MyTrait { + | ------- +LL | fn foo(); + | ------------ expected const parameter of type `i32` +... +LL | impl MyTrait for i32 { + | -------------------- +LL | fn foo() {} + | ^^^^^^^^^^^^^ found const parameter of type `{type error}` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0053`.