From c17a818b18f914f08e645d7be47c1848dd64e437 Mon Sep 17 00:00:00 2001 From: b1yd <2156864690@qq.com> Date: Tue, 16 Jun 2026 13:12:27 +0800 Subject: [PATCH] fix --- compiler/rustc_hir_analysis/src/check/wfcheck.rs | 12 ++++++++++-- compiler/rustc_hir_analysis/src/diagnostics.rs | 9 +++++++++ compiler/rustc_resolve/src/ident.rs | 5 ++++- tests/ui/resolve/allow-self-in-const-generics.rs | 16 ++++++++++++++++ .../resolve/allow-self-in-const-generics.stderr | 9 +++++++++ 5 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 tests/ui/resolve/allow-self-in-const-generics.rs create mode 100644 tests/ui/resolve/allow-self-in-const-generics.stderr diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index bf8593eb61882..cdfec8d20b91a 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -45,7 +45,7 @@ use super::compare_eii::{compare_eii_function_types, compare_eii_statics}; use crate::autoderef::Autoderef; use crate::constrained_generic_params::{Parameter, identify_constrained_generic_params}; use crate::diagnostics; -use crate::diagnostics::InvalidReceiverTyHint; +use crate::diagnostics::{InvalidReceiverTyHint, ParamInTyOfConstParam}; pub(super) struct WfCheckingCtxt<'a, 'tcx> { pub(super) ocx: ObligationCtxt<'a, 'tcx, FulfillmentError<'tcx>>, @@ -855,7 +855,15 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &ty::GenericParamDef) -> Result<(), Er let ty = tcx.type_of(param.def_id).instantiate_identity().skip_norm_wip(); let span = tcx.def_span(param.def_id); let def_id = param.def_id.expect_local(); - + if !tcx.features().generic_const_parameter_types() && ty.has_param() { + let hir::GenericParamKind::Const { ty: &hir::Ty { span, .. }, .. } = + tcx.hir_node_by_def_id(def_id).expect_generic_param().kind + else { + bug!() + }; + let err = tcx.dcx().create_err(ParamInTyOfConstParam { span, ty }).emit(); + return Err(err); + } if tcx.features().const_param_ty_unchecked() { enter_wf_checking_ctxt(tcx, tcx.local_parent(def_id), |wfcx| { wfcx.register_wf_obligation(span, None, ty.into()); diff --git a/compiler/rustc_hir_analysis/src/diagnostics.rs b/compiler/rustc_hir_analysis/src/diagnostics.rs index 1bfc495071284..ed16241dd418f 100644 --- a/compiler/rustc_hir_analysis/src/diagnostics.rs +++ b/compiler/rustc_hir_analysis/src/diagnostics.rs @@ -2000,3 +2000,12 @@ impl Diagnostic<'_, G> for UncoveredTyParam<'_> { diag } } + +#[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/ident.rs b/compiler/rustc_resolve/src/ident.rs index 9d08e8b98cb31..9bcc929462542 100644 --- a/compiler/rustc_resolve/src/ident.rs +++ b/compiler/rustc_resolve/src/ident.rs @@ -1584,7 +1584,10 @@ 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() + && !matches!(res, Res::SelfTyAlias { .. }) + { if let Some(span) = finalize { self.report_error( span, diff --git a/tests/ui/resolve/allow-self-in-const-generics.rs b/tests/ui/resolve/allow-self-in-const-generics.rs new file mode 100644 index 0000000000000..8dcde2a66fff7 --- /dev/null +++ b/tests/ui/resolve/allow-self-in-const-generics.rs @@ -0,0 +1,16 @@ +// Allow Self in const generics when Self doesn't depends on generics +trait MyTrait { + fn foo(); +} + +impl MyTrait for i32 { + fn foo() {} +} +impl Wrap { + fn f() {} + //~^ ERROR the type of const parameters must not depend on other generic parameters + +} + +struct Wrap(T); +fn main(){} diff --git a/tests/ui/resolve/allow-self-in-const-generics.stderr b/tests/ui/resolve/allow-self-in-const-generics.stderr new file mode 100644 index 0000000000000..52a70f739bc8c --- /dev/null +++ b/tests/ui/resolve/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:10: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`.