`.Self` has a type that changes, which causes type equality issues
Discord discussion: https://discord.com/channels/655572317891461132/941071822756143115/1526593756109865141
https://github.com/carbon-language/carbon-lang/pull/7501 adds a number of fail_todo_ tests that demonstrate the issue. One such test is here:
interface L(L1: type) { let W: type; }
interface M(M1: type) {}
final impl forall [U: type, T: M(U)] T as L(U) where .W = () {}
interface Z(Z1: type) {}
class C(C1: type);
fn H(generic T: (L(.Self) where .Self impls M(.Self)) where C(.W) impls Z(.Self)) {
// CHECK:STDERR: dana.carbon:[[@LINE+4]]:3: error: cannot convert type `C(())` into type implementing `Z(T)` [ConversionFailureTypeToFacet]
// CHECK:STDERR: C(()) as Z(T);
// CHECK:STDERR: ^~~~~~~~~~~~~
// CHECK:STDERR:
C(()) as Z(T);
}The impl requires U to be the same type in L(U) and M(U). We deduce L(U) to be L(.Self : type) but we have a witness in the facet type for M(.Self: L(.Self)). These .Self refer to the same type (whatever replaces T), but they themselves contain different constraints, so they do not compare as equal, and the impl is rejected.
For most symbolic types, this is not an issue, as their facet type does not change over time. It is an issue for .Self because we shadow it with a new type across where, as specified in the design (bold emphasis mine):
The
.Selfconstruct follows these rules:
- A checked binding
Xintroduces a checked generic binding.Self: type, where references to.Selfare resolved toX. This allows you to use.Selfas an interface parameter as inX: I(.Self).A whereintroduces a checked generic binding.Self: Aand a.Foodesignator for each memberFooofA.
If you remove the generic parameters of L and M, this code works. And .Self does refer to a single type. Together this seems like it should pass, but the design says it should not since the different .Self are not the same type constant while still referring to the same eventual type in a concrete specific.
Perhaps we can resolve this by:
- Stop changing the type of
.Self. It is alwaystype. - Encode entends constraints into ambient state (the
where_stackas "early impls"). We already encoded non-extend constraints there. Keep track that they were extending. - For name lookup through a facet, allow name lookup to look in the ambient state for extends constraints, instead of only looking into the facet's type.
This sounds like it would require multiple changes to the design to specify this behaviour.
I'd love to propose some alternative strategies but I am not sure what they would be right now. Maybe we could allow .Self to convert during deduce but that seems a bit more risky. Right now the deduction says they are both U so they have the same type, and our model is that that means they have the same constant value.
So, phrasing as a question: a) Should this test pass? b) If yes, what makes it so? Or what should we change to make it so?
Source: carbon-language/carbon-lang