From 08adb27ead175ed72c9ee1e61b488a7fa3cb70a7 Mon Sep 17 00:00:00 2001 From: Amirhossein Akhlaghpour Date: Tue, 28 Jul 2026 13:19:01 +0330 Subject: [PATCH] fix: avoid relating erased regions in valtree field types Signed-off-by: Amirhossein Akhlaghpour --- .../rustc_trait_selection/src/traits/wf.rs | 4 ++ .../valtree-erased-region-issue-159688.rs | 29 ++++++++++ .../valtree-erased-region-issue-159688.stderr | 54 +++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 tests/ui/const-generics/mgca/valtree-erased-region-issue-159688.rs create mode 100644 tests/ui/const-generics/mgca/valtree-erased-region-issue-159688.stderr diff --git a/compiler/rustc_trait_selection/src/traits/wf.rs b/compiler/rustc_trait_selection/src/traits/wf.rs index 95043385cf17a..5052e732a9975 100644 --- a/compiler/rustc_trait_selection/src/traits/wf.rs +++ b/compiler/rustc_trait_selection/src/traits/wf.rs @@ -1151,6 +1151,10 @@ impl<'a, 'tcx> TypeVisitor> for WfPredicates<'a, 'tcx> { .type_of(field_def.did) .instantiate(tcx, args) .skip_norm_wip(); + // Lifetimes stored in valtree constants are erased, so avoid + // relating those erased regions with the field's original + // regions during well-formedness checking. + let field_ty = tcx.erase_and_anonymize_regions(field_ty); let predicate = ty::PredicateKind::Clause( ty::ClauseKind::ConstArgHasType(field_val, field_ty), ); diff --git a/tests/ui/const-generics/mgca/valtree-erased-region-issue-159688.rs b/tests/ui/const-generics/mgca/valtree-erased-region-issue-159688.rs new file mode 100644 index 0000000000000..3aebb48af1fd3 --- /dev/null +++ b/tests/ui/const-generics/mgca/valtree-erased-region-issue-159688.rs @@ -0,0 +1,29 @@ +//@ check-fail + +#![feature(min_generic_const_args)] +#![feature(min_adt_const_params)] +#![expect(incomplete_features)] + +pub struct Foo { + slice: &'static [u8], +} + +#[derive(PartialEq, Eq)] +pub struct Foo_; +//~^ ERROR `Foo` must implement `ConstParamTy` +//~| ERROR `Foo` must implement `ConstParamTy` +//~| ERROR `Foo` must implement `ConstParamTy` +//~| ERROR `Foo` must implement `ConstParamTy` + +impl + Foo_< + { + Foo { + slice: &[1, 2, 3], + } + }, + > +{ +} + +fn main() {} diff --git a/tests/ui/const-generics/mgca/valtree-erased-region-issue-159688.stderr b/tests/ui/const-generics/mgca/valtree-erased-region-issue-159688.stderr new file mode 100644 index 0000000000000..1e9a39dafa26d --- /dev/null +++ b/tests/ui/const-generics/mgca/valtree-erased-region-issue-159688.stderr @@ -0,0 +1,54 @@ +error[E0741]: `Foo` must implement `ConstParamTy` to be used as the type of a const generic parameter + --> $DIR/valtree-erased-region-issue-159688.rs:12:26 + | +LL | pub struct Foo_; + | ^^^ + | +help: add `#[derive(ConstParamTy, PartialEq, Eq)]` to the struct + | +LL + #[derive(ConstParamTy, PartialEq, Eq)] +LL | pub struct Foo { + | + +error[E0741]: `Foo` must implement `ConstParamTy` to be used as the type of a const generic parameter + --> $DIR/valtree-erased-region-issue-159688.rs:12:26 + | +LL | pub struct Foo_; + | ^^^ + | + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` +help: add `#[derive(ConstParamTy, PartialEq, Eq)]` to the struct + | +LL + #[derive(ConstParamTy, PartialEq, Eq)] +LL | pub struct Foo { + | + +error[E0741]: `Foo` must implement `ConstParamTy` to be used as the type of a const generic parameter + --> $DIR/valtree-erased-region-issue-159688.rs:12:26 + | +LL | pub struct Foo_; + | ^^^ + | + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` +help: add `#[derive(ConstParamTy, PartialEq, Eq)]` to the struct + | +LL + #[derive(ConstParamTy, PartialEq, Eq)] +LL | pub struct Foo { + | + +error[E0741]: `Foo` must implement `ConstParamTy` to be used as the type of a const generic parameter + --> $DIR/valtree-erased-region-issue-159688.rs:12:26 + | +LL | pub struct Foo_; + | ^^^ + | + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` +help: add `#[derive(ConstParamTy, PartialEq, Eq)]` to the struct + | +LL + #[derive(ConstParamTy, PartialEq, Eq)] +LL | pub struct Foo { + | + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0741`.