Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions compiler/rustc_infer/src/infer/relate/generalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,24 +752,40 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
//
// FIXME: replace the StructurallyRelateAliases::Yes branch with
// `structurally_relate_consts` once it is fully structural.
ty::ConstKind::Unevaluated(uv) => match self.structurally_relate_aliases {
// Hack: Fall back to old behavior if GCE is enabled (it used to just be the Yes
// path), as doing this new No path breaks some GCE things. I expect GCE to be
// ripped out soon so this shouldn't matter soon.
StructurallyRelateAliases::No if !tcx.features().generic_const_exprs() => {
self.generalize_alias_term(uv.into()).map(|v| v.expect_const())
}
_ => {
let ty::UnevaluatedConst { kind, args, .. } = uv;
let args = self.relate_with_variance(
ty::Invariant,
ty::VarianceDiagInfo::default(),
args,
args,
)?;
Ok(ty::Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(tcx, kind, args)))
ty::ConstKind::Unevaluated(uv) => {
// a normalizable const in coherence mode misses overlaps; see #157937
// assoc const is now anon const, so unwrap with `expand_abstract_consts`
let defer_in_coherence = self.infcx.typing_mode_raw().is_coherence()
&& matches!(
tcx.expand_abstract_consts(c).kind(),
ty::ConstKind::Unevaluated(e)
if !matches!(e.kind, ty::UnevaluatedConstKind::Anon { .. })
);

match self.structurally_relate_aliases {
// Hack: Fall back to old behavior if GCE is enabled (it used to just be the
// Yes path), as doing this new No path breaks some GCE things. I expect GCE
// to be ripped out soon so this shouldn't matter soon.
StructurallyRelateAliases::No
if !tcx.features().generic_const_exprs() || defer_in_coherence =>
{
self.generalize_alias_term(uv.into()).map(|v| v.expect_const())
}
_ => {
let ty::UnevaluatedConst { kind, args, .. } = uv;
let args = self.relate_with_variance(
ty::Invariant,
ty::VarianceDiagInfo::default(),
args,
args,
)?;
Ok(ty::Const::new_unevaluated(
tcx,
ty::UnevaluatedConst::new(tcx, kind, args),
))
}
}
},
}
ty::ConstKind::Placeholder(placeholder) => {
if self.for_universe.can_name(placeholder.universe) {
Ok(c)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Cited from [#157937](https://github.com/rust-lang/rust/issues/157937)

#![feature(generic_const_exprs)]
trait Trait {
const CONST: usize;
}
struct A<T> {
_marker: T,
}
impl<const N: usize> Trait for [i8; N] {
const CONST: usize = N;
}
impl<const N: usize> From<usize> for A<[i8; N]> {
fn from(_: usize) -> Self {
todo!()
}
}
impl<T: Trait> From<A<[i8; T::CONST]>> for A<T> {
//~^ ERROR: conflicting implementations of trait `From<A<[i8; _]>>` for type `A<[i8; _]>` [E0119]
fn from(_: A<[i8; T::CONST]>) -> Self {
todo!()
}
}
fn f<T: Trait>() -> A<T>
where
[(); T::CONST]:,
{
let a = A::from(0);
A::from(a)
}
fn main() {
f::<[i8; 1]>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0119]: conflicting implementations of trait `From<A<[i8; _]>>` for type `A<[i8; _]>`
--> $DIR/coherence-overlap-const-projection-157937.rs:18:1
|
LL | impl<T: Trait> From<A<[i8; T::CONST]>> for A<T> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: conflicting implementation in crate `core`:
- impl<T> From<T> for T;

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0119`.
Loading