A Rewrite System for Type Checking Scoped Conformances #1585
dabrahams
started this conversation in
Language design
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm posting this to capture results from this Slack thread, which will become inaccessible eventually.
The compiler can ensure soundness when there is no global coherence by applying a set of rewrite rules, and then using typechecking rules that would work when there is global coherence.
The crucial soundness problem solved is that a generic type can change dramatically (even in size or storage layout) based on conformances, due to the ability to store instances of associated types. As a result, a single spelling for a generic type in two different conformance contexts can represent a different type in each context. The additional type information injected by the rewrite rules allows the compiler distinguish these types. You can think of the rewrite process as a desugaring.
Crucial desirable properties of this scheme are:
Int
can pass freely between conformance contexts.For each trait X we synthesize a second trait X' with same-shaped associated types.
and so on.
Every generic component is augmented with an additional type parameter for each of its type parameters, with a parallel set of constraints:
becomes
and so on.
The extra parameters to generic types ensure that after the rewrite, pre-rewrite spellings are only the same type when they depend on the same set of conformances.
For every conformance declaration we generate a type declaration, which is passed to rewritten uses of generic components:
Plain lexical scoping rules make scoped conformance work:
The error above is that
f
requiresA.P == B.P
, but in this contextA.P
isInt
andB.P
isBool
.Beta Was this translation helpful? Give feedback.
All reactions