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
19 changes: 13 additions & 6 deletions src/arr/compiler/type-check.arr
Original file line number Diff line number Diff line change
Expand Up @@ -1858,8 +1858,8 @@ end

fun synthesis-extend(update-loc :: Loc, obj :: Expr, obj-type :: Type, fields :: List<A.Member>, context :: Context) -> TypingResult:
collect-members(fields, false, context).typing-bind(lam(new-members, shadow context):
instantiate-object-type(obj-type, context).typing-bind(lam(shadow obj-type, shadow context):
cases(Type) obj-type:
instantiate-object-type(obj-type, context).typing-bind(lam(instantiated-obj-type, shadow context):
cases(Type) instantiated-obj-type:
| t-record(t-fields, _, inferred) =>
final-fields = new-members.fold-keys(lam(key, final-fields):
final-fields.set(key, new-members.get-value(key))
Expand All @@ -1868,21 +1868,24 @@ fun synthesis-extend(update-loc :: Loc, obj :: Expr, obj-type :: Type, fields ::
| t-existential(_, l, _) =>
typing-error([list: C.unable-to-infer(l)])
| else =>
typing-error([list: C.incorrect-type-expression(tostring(obj-type), obj-type.l, "an object type", update-loc, obj)])
shadow context = new-members.fold-keys(lam(key, shadow context):
context.add-field-constraint(instantiated-obj-type, key, new-members.get-value(key))
end, context)
typing-result(A.s-extend(update-loc, obj, fields), obj-type, context)
end
end)
end)
end

fun synthesis-update(update-loc :: Loc, obj :: Expr, obj-type :: Type, fields :: List<A.Member>, context :: Context) -> TypingResult:
collect-members(fields, false, context).typing-bind(lam(new-members, shadow context):
instantiate-object-type(obj-type, context).typing-bind(lam(shadow obj-type, shadow context):
instantiate-object-type(obj-type, context).typing-bind(lam(instantiated-obj-type, shadow context):
cases(Type) obj-type:
| t-record(t-fields, _, inferred) =>
foldr-fold-result(lam(key, shadow context, final-fields):
cases(Option<Type>) t-fields.get(key):
| none =>
fold-errors([list: C.object-missing-field(key, tostring(obj-type), obj-type.l, update-loc)])
fold-errors([list: C.object-missing-field(key, tostring(instantiated-obj-type), instantiated-obj-type.l, update-loc)])
| some(old-type) =>
cases(Type) old-type:
| t-ref(onto, l, ref-inferred) =>
Expand All @@ -1898,7 +1901,11 @@ fun synthesis-update(update-loc :: Loc, obj :: Expr, obj-type :: Type, fields ::
| t-existential(_, l, _) =>
typing-error([list: C.unable-to-infer(l)])
| else =>
typing-error([list: C.incorrect-type-expression(tostring(obj-type), obj-type.l, "an object type", update-loc, obj)])
shadow context = new-members.fold-keys(lam(key, shadow context):
member-type = new-members.get-value(key)
context.add-field-constraint(instantiated-obj-type, key, t-ref(member-type, member-type.l, false))
end, context)
typing-result(A.s-update(update-loc, obj, fields), obj-type, context)
end
end)
end)
Expand Down
6 changes: 6 additions & 0 deletions tests/type-check/bad/data-as-object-extend-missing.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
data Foo:
| bar(a :: Number, ref b :: String)
end

# bar(1, "a") does not have a field c
bar(1, "a").{c: "a"}
6 changes: 6 additions & 0 deletions tests/type-check/bad/data-as-object-extend-ref.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
data Foo:
| bar(a :: Number, ref b :: String)
end

# bar(1, "a").b is a ref String while "a" is a String
bar(1, "a").{b: "a"}
6 changes: 6 additions & 0 deletions tests/type-check/bad/data-as-object-extend-wrong-type.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
data Foo:
| bar(a :: Number, ref b :: String)
end

# bar(1, "a").a has type Number not String
bar(1, "a").{a: "b"}
6 changes: 6 additions & 0 deletions tests/type-check/bad/data-as-object-update-missing.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
data Foo:
| bar(a :: Number, ref b :: String)
end

# bar(1, "a") does not have a field c
bar(1, "a")!{c: 3}
6 changes: 6 additions & 0 deletions tests/type-check/bad/data-as-object-update-non-ref.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
data Foo:
| bar(a :: Number, ref b :: String)
end

# bar(1, "a").a is a String but update is for refs
bar(1, "a")!{a: 3}
6 changes: 6 additions & 0 deletions tests/type-check/bad/data-as-object-update-wrong.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
data Foo:
| bar(a :: Number, ref b :: String)
end

# bar(1, "a").b has type String not Number
bar(1, "a")!{b: 3}
6 changes: 6 additions & 0 deletions tests/type-check/good/data-as-object.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
data Foo:
| bar(a :: Number, ref b :: String)
end

bar(1, "a").{a: 3}
bar(1, "b")!{b: "a"}