Skip to content

Commit

Permalink
Add some tests and remove obsolete comment
Browse files Browse the repository at this point in the history
  • Loading branch information
LPTK committed Jan 20, 2022
1 parent 1007506 commit b15b776
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 94 deletions.
1 change: 0 additions & 1 deletion shared/src/main/scala/mlscript/ConstraintSolver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ class ConstraintSolver extends NormalForms { self: Typer =>
case (NegType(lhs), NegType(rhs)) => rec(rhs, lhs, true)
case (FunctionType(l0, r0), FunctionType(l1, r1)) =>
rec(l1, l0, false)
// ^ disregard error context: keep it from reversing polarity (or the messages become redundant)
rec(r0, r1, false)
case (prim: ClassTag, ot: ObjectTag)
if (ot.id match { case v: Var => prim.parents.contains(v); case _ => false }) => ()
Expand Down
36 changes: 27 additions & 9 deletions shared/src/test/diff/codegen/Terms.mls
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,9 @@ case A { a = 0 } of

// With
:js
n = 42 with { x = 1 }
n + n.x
rcd1 = { x = "a"; y = "b" }
rcd2 = rcd1 with { x = 1; z = 2 }
(rcd1.x, rcd1.y, rcd2.x, rcd2.y, rcd2.z)
//│ // Prelude
//│ function withConstruct(target, fields) {
//│ if (typeof target === "string" || typeof target === "number" || typeof target === "boolean" || typeof target === "bigint" || typeof target === "symbol") {
Expand All @@ -339,13 +340,30 @@ n + n.x
//│ return copy;
//│ }
//│ // Query 1
//│ globalThis.n = withConstruct(42, { x: 1 });
//│ res = n;
//│ globalThis.rcd1 = {
//│ x: "a",
//│ y: "b"
//│ };
//│ res = rcd1;
//│ // Query 2
//│ res = n + n.x;
//│ globalThis.rcd2 = withConstruct(rcd1, {
//│ x: 1,
//│ z: 2
//│ });
//│ res = rcd2;
//│ // Query 3
//│ res = [
//│ rcd1.x,
//│ rcd1.y,
//│ rcd2.x,
//│ rcd2.y,
//│ rcd2.z
//│ ];
//│ // End of generated code
//│ n: 42 & {x: 1}
//│ = [Number: 42] { x: 1 }
//│ res: int
//│ = 43
//│ rcd1: {x: "a", y: "b"}
//│ = { x: 'a', y: 'b' }
//│ rcd2: {x: 1, y: "b", z: 2}
//│ = { x: 1, y: 'b', z: 2 }
//│ res: ("a", "b", 1, "b", 2,)
//│ = [ 'a', 'b', 1, 'b', 2 ]

45 changes: 45 additions & 0 deletions shared/src/test/diff/codegen/TrickyWiths.mls
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

() with {}
//│ res: ()
//│ = Array {}

:js
n = 42 with { x = 1 }
n + n.x
//│ // Query 1
//│ globalThis.n = withConstruct(42, { x: 1 });
//│ res = n;
//│ // Query 2
//│ res = n + n.x;
//│ // End of generated code
//│ n: 42 & {x: 1}
//│ = [Number: 42] { x: 1 }
//│ res: int
//│ = 43


// TODO: make `with` actually work on arrays:

:js
a = (1,2,3) with {}
def a: nothing // unsound escape hatch
//│ // Query 1
//│ globalThis.a = withConstruct([
//│ 1,
//│ 2,
//│ 3
//│ ], {});
//│ res = a;
//│ // End of generated code
//│ a: (1, 2, 3,)
//│ = Array { '0': 1, '1': 2, '2': 3 }
//│ a: nothing

a.length
a.map(fun x -> x + 1)
//│ res: nothing
//│ = 0
//│ res: nothing
//│ = []


41 changes: 41 additions & 0 deletions shared/src/test/diff/mlscript/BadTraits.mls
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,44 @@ t.x
//│ res: int
//│ = 1


trait Lol
method Foo = 1
class Hey
method Foo = "oops"
//│ Defined trait Lol
//│ Defined Lol.Foo: lol -> 1
//│ Defined class Hey
//│ Defined Hey.Foo: Hey -> "oops"

:e
class H: Lol & Hey
//│ ╔══[ERROR] An overriding method definition must be given when inheriting from multiple method definitions
//│ ║ l.177: class H: Lol & Hey
//│ ║ ^
//│ ╟── Definitions of method Foo inherited from:
//│ ╟── • Lol
//│ ║ l.168: method Foo = 1
//│ ║ ^^^^^^^
//│ ╟── • Hey
//│ ║ l.170: method Foo = "oops"
//│ ╙── ^^^^^^^^^^^^
//│ Defined class H

// FIXME: should probably forbid trait constructors when the trait contains method defs as well:

h = Hey{}
l = Lol h
//│ h: Hey
//│ = Hey {}
//│ l: Hey & lol
//│ = Hey {}

l.(Lol.Foo)
//│ res: 1
//│ = undefined

l.(Hey.Foo)
//│ res: "oops"
//│ = undefined

Loading

0 comments on commit b15b776

Please sign in to comment.