From c9f06a917209a38cb4fc63562aeac7087db00a1c Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Fri, 13 May 2022 13:06:33 -0400 Subject: [PATCH] Ad some docs to tag union layouts --- compiler/mono/src/layout.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/compiler/mono/src/layout.rs b/compiler/mono/src/layout.rs index a7c9111e7c..a38b0b1fa6 100644 --- a/compiler/mono/src/layout.rs +++ b/compiler/mono/src/layout.rs @@ -276,6 +276,12 @@ pub enum UnionLayout<'a> { /// It has more than one other variant, so they need tag IDs (payloads are "wrapped") /// e.g. `FingerTree a : [ Empty, Single a, More (Some a) (FingerTree (Tuple a)) (Some a) ]` /// see also: https://youtu.be/ip92VMpf_-A?t=164 + /// + /// nullable_id refers to the index of the tag that is represented at runtime as NULL. + /// For example, in `FingerTree a : [ Empty, Single a, More (Some a) (FingerTree (Tuple a)) (Some a) ]`, + /// the ids would be Empty = 0, More = 1, Single = 2, because that's how those tags are + /// ordered alphabetically. Since the Empty tag will be represented at runtime as NULL, + /// and since Empty's tag id is 0, here nullable_id would be 0. NullableWrapped { nullable_id: u16, other_tags: &'a [&'a [Layout<'a>]], @@ -283,6 +289,14 @@ pub enum UnionLayout<'a> { /// A recursive tag union with only two variants, where one is empty. /// Optimizations: Use null for the empty variant AND don't store a tag ID for the other variant. /// e.g. `ConsList a : [ Nil, Cons a (ConsList a) ]` + /// + /// nullable_id is a bool because it's only ever 0 or 1, but (as with the NullableWrapped + /// variant), it reprsents the index of the tag that will be represented at runtime as NULL. + /// + /// So for example, in `ConsList a : [ Nil, Cons a (ConsList a) ]`, Nil is tag id 1 and + /// Cons is tag id 0 because Nil comes alphabetically after Cons. Here, Nil will be + /// represented as NULL at runtime, so nullable_id is 1 - which is to say, `true`, because + /// `(1 as bool)` is `true`. NullableUnwrapped { nullable_id: bool, other_fields: &'a [Layout<'a>],