Skip to content

Commit 50afcc4

Browse files
committed
add flag
1 parent 9cf43a4 commit 50afcc4

File tree

9 files changed

+133
-85
lines changed

9 files changed

+133
-85
lines changed

shared/src/main/scala/mlscript/ConstraintSolver.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ class ConstraintSolver extends NormalForms { self: Typer =>
627627
recLb(ar.inner, b.inner)
628628
rec(b.inner.ub, ar.inner.ub, false)
629629
case (LhsRefined(S(b: ArrayBase), ts, r, _), _) => reportError()
630-
case (LhsRefined(S(ov: Overload), ts, r, trs), RhsBases(_, S(L(f: FunctionType)), _)) =>
630+
case (LhsRefined(S(ov: Overload), ts, r, trs), RhsBases(_, S(L(f: FunctionType)), _)) if noApproximateOverload =>
631631
TupleSetConstraints.mk(ov, f) match {
632632
case S(tsc) => if (!tsc.tvs.isEmpty && tsc.constraints.isEmpty) reportError()
633633
case N => reportError()

shared/src/main/scala/mlscript/Typer.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Typer(var dbg: Boolean, var verbose: Bool, var explainErrors: Bool, val ne
3434
var constrainedTypes: Boolean = false
3535

3636
var recordProvenances: Boolean = true
37+
var noApproximateOverload: Boolean = false
3738

3839
type Binding = Str -> SimpleType
3940
type Bindings = Map[Str, SimpleType]

shared/src/test/diff/basics/Intersections.fun

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,68 +26,84 @@ succ / foo(1)
2626
let foo = (Int => Int) & (Bool => Bool)
2727
//│ foo: int -> int & bool -> bool
2828

29-
// used to return int & bool, equivalent to nothing
30-
// or approximated as int | bool in MLscript
31-
// overloaded function foo can now be used as either int -> int or bool -> bool
32-
foo(1)
29+
:e
30+
foo(1) // returns int & bool, equivalent to nothing
3331
succ / foo(1)
3432
foo(true)
3533
not / foo(true)
36-
//│ res: int
37-
//│ res: int
38-
//│ res: bool
39-
//│ res: bool
34+
//│ res: bool | int
35+
//│ ╔══[ERROR] Type mismatch in application:
36+
//│ ║ l.31: succ / foo(1)
37+
//│ ║ ^^^^^^^^^^^^^
38+
//│ ╟── reference of type `bool` is not an instance of type `int`
39+
//│ ║ l.26: let foo = (Int => Int) & (Bool => Bool)
40+
//│ ║ ^^^^
41+
//│ ╟── but it flows into application with expected type `int`
42+
//│ ║ l.31: succ / foo(1)
43+
//│ ╙── ^^^^^^
44+
//│ res: error | int
45+
//│ res: bool | int
46+
//│ ╔══[ERROR] Type mismatch in application:
47+
//│ ║ l.33: not / foo(true)
48+
//│ ║ ^^^^^^^^^^^^^^^
49+
//│ ╟── reference of type `int` is not an instance of type `bool`
50+
//│ ║ l.26: let foo = (Int => Int) & (Bool => Bool)
51+
//│ ║ ^^^
52+
//│ ╟── but it flows into application with expected type `bool`
53+
//│ ║ l.33: not / foo(true)
54+
//│ ╙── ^^^^^^^^^
55+
//│ res: bool | error
4056

4157
:e
4258
not / foo(1)
4359
foo(1) as Nothing
4460
//│ ╔══[ERROR] Type mismatch in application:
45-
//│ ║ l.42: not / foo(1)
61+
//│ ║ l.58: not / foo(1)
4662
//│ ║ ^^^^^^^^^^^^
4763
//│ ╟── reference of type `int` is not an instance of type `bool`
4864
//│ ║ l.26: let foo = (Int => Int) & (Bool => Bool)
4965
//│ ║ ^^^
5066
//│ ╟── but it flows into application with expected type `bool`
51-
//│ ║ l.42: not / foo(1)
67+
//│ ║ l.58: not / foo(1)
5268
//│ ╙── ^^^^^^
5369
//│ res: bool | error
5470
//│ ╔══[ERROR] Type mismatch in 'as' binding:
55-
//│ ║ l.43: foo(1) as Nothing
71+
//│ ║ l.59: foo(1) as Nothing
5672
//│ ║ ^^^^^^^^^^^^^^^^^
5773
//│ ╟── reference of type `int` does not match type `nothing`
5874
//│ ║ l.26: let foo = (Int => Int) & (Bool => Bool)
5975
//│ ║ ^^^
6076
//│ ╟── but it flows into application with expected type `nothing`
61-
//│ ║ l.43: foo(1) as Nothing
77+
//│ ║ l.59: foo(1) as Nothing
6278
//│ ║ ^^^^^^
6379
//│ ╟── Note: constraint arises from reference:
64-
//│ ║ l.43: foo(1) as Nothing
80+
//│ ║ l.59: foo(1) as Nothing
6581
//│ ╙── ^^^^^^^
6682
//│ res: nothing
6783

6884
:e
6985
foo as Nothing
7086
//│ ╔══[ERROR] Type mismatch in 'as' binding:
71-
//│ ║ l.69: foo as Nothing
87+
//│ ║ l.85: foo as Nothing
7288
//│ ║ ^^^^^^^^^^^^^^
7389
//│ ╟── type intersection of type `int -> int & bool -> bool` does not match type `nothing`
7490
//│ ║ l.26: let foo = (Int => Int) & (Bool => Bool)
7591
//│ ║ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7692
//│ ╟── but it flows into reference with expected type `nothing`
77-
//│ ║ l.69: foo as Nothing
93+
//│ ║ l.85: foo as Nothing
7894
//│ ║ ^^^
7995
//│ ╟── Note: constraint arises from reference:
80-
//│ ║ l.69: foo as Nothing
96+
//│ ║ l.85: foo as Nothing
8197
//│ ╙── ^^^^^^^
8298
//│ res: nothing
8399

84100
:e
85101
let oops = (&)
86102
//│ ╔══[ERROR] Illegal use of reserved operator: &
87-
//│ ║ l.85: let oops = (&)
88-
//│ ╙── ^^^
103+
//│ ║ l.101: let oops = (&)
104+
//│ ╙── ^^^
89105
//│ ╔══[ERROR] identifier not found: &
90-
//│ ║ l.85: let oops = (&)
91-
//│ ╙── ^^^
106+
//│ ║ l.101: let oops = (&)
107+
//│ ╙── ^^^
92108
//│ oops: error
93109

shared/src/test/diff/fcp/Overloads.mls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
:NoJS
3-
3+
:NoApproximateOverload
44

55
type IISS = int -> int & string -> string
66
type BBNN = bool -> bool & number -> number

shared/src/test/diff/mlscript/BadPolym.mls

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ fooImpl id
4040

4141

4242
fooImpl2 (f: int -> int & string -> string) = f 1
43-
//│ fooImpl2: (int -> int & string -> string) -> int
43+
//│ fooImpl2: (int -> int & string -> string) -> (int | string)
4444
//│ = [Function: fooImpl2]
4545

4646
fooImpl2 id
47-
//│ res: int
47+
//│ res: int | string
4848
//│ = 1
4949

5050
:e

shared/src/test/diff/nu/ArrayProg.mls

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ module A {
155155
//│ }
156156

157157
A.g(0)
158-
//│ Int
158+
//│ Int | Str
159159
//│ res
160160
//│ = 0
161161

@@ -174,13 +174,13 @@ s([Numbr(0),Numbr(0)])
174174
//│ ╔══[ERROR] Type mismatch in application:
175175
//│ ║ l.+1: s([Numbr(0),Numbr(0)])
176176
//│ ║ ^^^^^^^^^^^^^^^^^^^^^^
177-
//│ ╟── type `(Numbr | Vectr, Numbr | Vectr) -> Int` is not a function
178-
//│ ║ l.136: fun s: ([Numbr,Numbr] -> Int) & ([Vectr,Vectr] -> Int)
179-
//│ ║ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
180-
//│ ╟── but it flows into reference with expected type `([?a, ?b]) -> ?c`
177+
//│ ╟── argument of type `[[?a, ?b]]` does not match type `[Numbr | Vectr, Numbr | Vectr]`
181178
//│ ║ l.+1: s([Numbr(0),Numbr(0)])
182-
//│ ╙── ^
183-
//│ error
179+
//│ ║ ^^^^^^^^^^^^^^^^^^^^^
180+
//│ ╟── Note: constraint arises from tuple type:
181+
//│ ║ l.136: fun s: ([Numbr,Numbr] -> Int) & ([Vectr,Vectr] -> Int)
182+
//│ ╙── ^^^^^^^^^^^^^
183+
//│ Int | error
184184
// g <: 0 -> 'a
185185

186186
:e

0 commit comments

Comments
 (0)