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
13 changes: 12 additions & 1 deletion compiler/rustc_infer/src/infer/relate/generalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,11 +797,22 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
// We only need to be careful with potentially normalizeable
// aliases here. See `generalize_alias_term` for more information.
ty::ConstKind::Alias(ty::IsRigid::No, alias_const) => {
// a normalizable const in coherence mode misses overlaps; see #157937.
// expand anon consts with `expand_abstract_consts`.
let defer_in_coherence = self.infcx.typing_mode_raw().is_coherence()
&& matches!(
tcx.expand_abstract_consts(c).kind(),
ty::ConstKind::Alias(_, e)
if !matches!(e.kind, ty::AliasConstKind::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() => {
StructurallyRelateAliases::No
if !tcx.features().generic_const_exprs() || defer_in_coherence =>
{
self.generalize_alias_term(alias_const.into()).map(|v| v.expect_const())
}
_ => {
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