From be5c538763ed5ee37fec4135319feab9b33c2753 Mon Sep 17 00:00:00 2001 From: Luke Boswell Date: Sun, 28 Jan 2024 10:57:43 +1100 Subject: [PATCH] fix glue TypeId --- crates/glue/platform/InternalTypeId.roc | 11 ----------- crates/glue/platform/TypeId.roc | 12 +++++++++--- crates/glue/platform/Types.roc | 16 ++++++++-------- crates/glue/src/RustGlue.roc | 10 ++++++++-- 4 files changed, 25 insertions(+), 24 deletions(-) delete mode 100644 crates/glue/platform/InternalTypeId.roc diff --git a/crates/glue/platform/InternalTypeId.roc b/crates/glue/platform/InternalTypeId.roc deleted file mode 100644 index 3b23a32ff7..0000000000 --- a/crates/glue/platform/InternalTypeId.roc +++ /dev/null @@ -1,11 +0,0 @@ -interface InternalTypeId - exposes [InternalTypeId, fromU64, toU64] - imports [] - -InternalTypeId := U64 - -toU64 : InternalTypeId -> U64 -toU64 = \@InternalTypeId x -> x - -fromU64 : U64 -> InternalTypeId -fromU64 = @InternalTypeId diff --git a/crates/glue/platform/TypeId.roc b/crates/glue/platform/TypeId.roc index 18e1b88776..e5e4f1f93a 100644 --- a/crates/glue/platform/TypeId.roc +++ b/crates/glue/platform/TypeId.roc @@ -1,5 +1,11 @@ interface TypeId - exposes [TypeId] - imports [InternalTypeId.{ InternalTypeId }] + exposes [TypeId, fromU64, toU64] + imports [] -TypeId : InternalTypeId +TypeId := U64 implements [Eq, Hash] + +toU64 : TypeId -> U64 +toU64 = \@TypeId x -> x + +fromU64 : U64 -> TypeId +fromU64 = @TypeId diff --git a/crates/glue/platform/Types.roc b/crates/glue/platform/Types.roc index f62c7814fb..ae9e2751d4 100644 --- a/crates/glue/platform/Types.roc +++ b/crates/glue/platform/Types.roc @@ -1,6 +1,6 @@ interface Types exposes [Types, shape, size, alignment, target, walkShapes, entryPoints] - imports [Shape.{ Shape }, TypeId.{ TypeId }, Target.{ Target }, InternalTypeId] + imports [Shape.{ Shape }, TypeId.{ TypeId }, Target.{ Target }, TypeId] # TODO: switch AssocList uses to Dict once roc_std is updated. Tuple1 : [T Str TypeId] @@ -34,33 +34,33 @@ entryPoints = \@Types { entrypoints } -> entrypoints walkShapes : Types, state, (state, Shape, TypeId -> state) -> state walkShapes = \@Types { types: shapes }, originalState, update -> List.walkWithIndex shapes originalState \state, elem, index -> - id = InternalTypeId.fromU64 index + id = TypeId.fromU64 index update state elem id shape : Types, TypeId -> Shape shape = \@Types types, id -> - when List.get types.types (InternalTypeId.toU64 id) is + when List.get types.types (TypeId.toU64 id) is Ok answer -> answer Err OutOfBounds -> - idStr = Num.toStr (InternalTypeId.toU64 id) + idStr = Num.toStr (TypeId.toU64 id) crash "TypeId #\(idStr) was not found in Types. This should never happen, and means there was a bug in `roc glue`. If you have time, please open an issue at " alignment : Types, TypeId -> U32 alignment = \@Types types, id -> - when List.get types.aligns (InternalTypeId.toU64 id) is + when List.get types.aligns (TypeId.toU64 id) is Ok answer -> answer Err OutOfBounds -> - idStr = Num.toStr (InternalTypeId.toU64 id) + idStr = Num.toStr (TypeId.toU64 id) crash "TypeId #\(idStr) was not found in Types. This should never happen, and means there was a bug in `roc glue`. If you have time, please open an issue at " size : Types, TypeId -> U32 size = \@Types types, id -> - when List.get types.sizes (InternalTypeId.toU64 id) is + when List.get types.sizes (TypeId.toU64 id) is Ok answer -> answer Err OutOfBounds -> - idStr = Num.toStr (InternalTypeId.toU64 id) + idStr = Num.toStr (TypeId.toU64 id) crash "TypeId #\(idStr) was not found in Types. This should never happen, and means there was a bug in `roc glue`. If you have time, please open an issue at " diff --git a/crates/glue/src/RustGlue.roc b/crates/glue/src/RustGlue.roc index 286ec34556..fc39ce7088 100644 --- a/crates/glue/src/RustGlue.roc +++ b/crates/glue/src/RustGlue.roc @@ -1360,12 +1360,18 @@ generateUnionField = \types -> commaSeparated : Str, List a, (a, U64 -> Str) -> Str commaSeparated = \buf, items, step -> - length = List.len items - List.walk items { buf, count: 0 } \accum, item -> + + length = List.len items |> Num.intCast + + help : { buf : Str, count : U64 }, a -> { buf : Str, count : U64 } + help = \accum, item -> if accum.count + 1 == length then { buf: Str.concat accum.buf (step item accum.count), count: length } else { buf: Str.concat accum.buf (step item accum.count) |> Str.concat ", ", count: accum.count + 1 } + + items + |> List.walk { buf, count: 0 } help |> .buf generateNullableUnwrapped : Str, Types, TypeId, Str, Str, Str, TypeId, [FirstTagIsNull, SecondTagIsNull] -> Str