From 4085e94ece1ca462a89aa28e3d38a5c8742fd2e9 Mon Sep 17 00:00:00 2001 From: Boxy Date: Thu, 24 Nov 2022 08:36:28 +0000 Subject: [PATCH] `super_relate_consts` do not spurriously fail on assoc consts --- compiler/rustc_middle/src/ty/relate.rs | 25 +++++++------------ .../dropck_unifies_assoc_consts.rs | 20 +++++++++++++++ 2 files changed, 29 insertions(+), 16 deletions(-) create mode 100644 src/test/ui/const-generics/generic_const_exprs/assoc_const_unification/dropck_unifies_assoc_consts.rs diff --git a/compiler/rustc_middle/src/ty/relate.rs b/compiler/rustc_middle/src/ty/relate.rs index 8b460bfe044..a5580fa62ac 100644 --- a/compiler/rustc_middle/src/ty/relate.rs +++ b/compiler/rustc_middle/src/ty/relate.rs @@ -631,6 +631,15 @@ pub fn super_relate_consts<'tcx, R: TypeRelation<'tcx>>( b = b.eval(tcx, relation.param_env()); } + if tcx.features().generic_const_exprs { + if let Ok(Some(a2)) = tcx.expand_abstract_consts(a) { + a = a2; + } + if let Ok(Some(b2)) = tcx.expand_abstract_consts(b) { + b = b2 + } + } + // Currently, the values that can be unified are primitive types, // and those that derive both `PartialEq` and `Eq`, corresponding // to structural-match types. @@ -647,22 +656,6 @@ pub fn super_relate_consts<'tcx, R: TypeRelation<'tcx>>( (ty::ConstKind::Placeholder(p1), ty::ConstKind::Placeholder(p2)) => p1 == p2, (ty::ConstKind::Value(a_val), ty::ConstKind::Value(b_val)) => a_val == b_val, - (ty::ConstKind::Unevaluated(_), ty::ConstKind::Unevaluated(_)) - if tcx.features().generic_const_exprs => - { - // FIXME(generic_const_exprs): this spurriously fails when relating two assoc consts - // i.e. `::ASSOC eq ::ASSOC` would return `false`. Wheras if - // both were behind an anon const that gets normalized away here it would succeed. - if let (Ok(Some(a)), Ok(Some(b))) = ( - tcx.expand_abstract_consts(a), - tcx.expand_abstract_consts(b), - ) && a.ty() == b.ty() { - return relation.consts(a, b); - } else { - false - } - } - // While this is slightly incorrect, it shouldn't matter for `min_const_generics` // and is the better alternative to waiting until `generic_const_exprs` can // be stabilized. diff --git a/src/test/ui/const-generics/generic_const_exprs/assoc_const_unification/dropck_unifies_assoc_consts.rs b/src/test/ui/const-generics/generic_const_exprs/assoc_const_unification/dropck_unifies_assoc_consts.rs new file mode 100644 index 00000000000..274caa1e993 --- /dev/null +++ b/src/test/ui/const-generics/generic_const_exprs/assoc_const_unification/dropck_unifies_assoc_consts.rs @@ -0,0 +1,20 @@ +// check-pass +#![feature(generic_const_exprs)] +#![allow(incomplete_features)] + +trait Trait { + const ASSOC: usize; +} + +struct Foo(T) +where + [(); T::ASSOC]:; + +impl Drop for Foo +where + [(); T::ASSOC]:, +{ + fn drop(&mut self) {} +} + +fn main() {}