Skip to content

Commit

Permalink
Type inference improvements
Browse files Browse the repository at this point in the history
Enhancements:
- Generic argument type inference for constructors (see #681)
- Generic argument type inference for static method calls
  • Loading branch information
degory committed Apr 15, 2024
1 parent c0b184a commit 487177b
Show file tree
Hide file tree
Showing 100 changed files with 1,637 additions and 1,165 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
]
},
"ghul.compiler": {
"version": "0.8.40",
"version": "0.8.41",
"commands": [
"ghul-compiler"
]
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>0.8.41-alpha.2</Version>
<Version>0.8.42-alpha.124</Version>
<NoWarn>$(NoWarn);NU1507</NoWarn>
</PropertyGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Run test",
"command": "dotnet ghul-test \"${workspaceFolder}\"",
"type": "shell",
"group": {
"kind": "test",
"isDefault": true
}
},
{
"label": "Capture test expectation",
"command": "../../../tasks/capture.sh \"${workspaceFolder}\"",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"compiler": "dotnet ../../../publish/ghul.dll"
}
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
construct generic_constructor_type_inference_1__test.THING[System.Int32,System.Int32] from System.Int32[] and System.Func`2[System.Int32,System.Int32]
construct generic_constructor_type_inference_1__test.THING[System.Int32,System.String] from System.Int32[] and System.Func`2[System.Int32,System.String]
construct generic_constructor_type_inference_1__test.THING[System.Int32[],System.String[]] from System.Int32[][] and System.Func`2[System.Int32[],System.String[]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use Collections;

use IO.Std.write_line;

entry() is
let t_int_int: THING[int,int] = THING([1, 2, 3, 4], x => x + 1);

let t_string_int: THING[int, string] = THING([1, 2, 3, 4], x => "{x}");

let t_iint_istring: THING[int[], string[]] = THING([[1], [2], [3], [4]], x => ["a", "b", "c"]);
si

class THING[T,U] is
init(input: Iterable[T], mapper: T -> U) is
write_line("construct {self} from {input} and {mapper}");
si
si

Empty file.
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
{
"compiler": "dotnet ../../../publish/ghul.dll",
"other_flags": "--v3",
"source": [
"."
]
"compiler": "dotnet ../../../publish/ghul.dll"
}
5 changes: 1 addition & 4 deletions integration-tests/execution/overload-resolution-2/ghul.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"compiler": "dotnet ../../../publish/ghul.dll",
"source": [
"."
]
"compiler": "dotnet ../../../publish/ghul.dll"
}
15 changes: 0 additions & 15 deletions integration-tests/execution/overload-resolution-2/test.ghulproj

This file was deleted.

5 changes: 1 addition & 4 deletions integration-tests/execution/overload-resolution-3/ghul.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"compiler": "dotnet ../../../publish/ghul.dll",
"source": [
"."
]
"compiler": "dotnet ../../../publish/ghul.dll"
}
15 changes: 0 additions & 15 deletions integration-tests/execution/overload-resolution-3/test.ghulproj

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"compiler": "dotnet ../../../publish/ghul.dll",
"source": [
"."
]
"compiler": "dotnet ../../../publish/ghul.dll"
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
test.ghul: 37,13..37,18: error: T is not assignable to Ghul.int
test.ghul: 38,13..38,18: error: U is not assignable to Ghul.string
test.ghul: 43,13..43,18: error: Main.X is not assignable to Ghul.int
test.ghul: 44,13..44,18: error: Main.Y is not assignable to Ghul.string
test.ghul: 46,13..46,25: error: Ghul.object is not assignable to T
test.ghul: 47,13..47,25: error: Ghul.object is not assignable to U
test.ghul: 49,13..49,25: error: Ghul.object is not assignable to Main.X
test.ghul: 50,13..50,25: error: Ghul.object is not assignable to Main.Y
test.ghul: 52,13..52,25: error: Ghul.object is not assignable to Ghul.int
test.ghul: 53,13..53,25: error: Ghul.object is not assignable to Ghul.string
test.ghul: 55,13..55,19: error: Ghul.int is not assignable to T
test.ghul: 56,13..56,24: error: Ghul.string is not assignable to Main.Y
test.ghul: 67,17..67,25: error: Ghul.object is not assignable to T
test.ghul: 68,17..68,25: error: Ghul.object is not assignable to U
test.ghul: 17,9..17,14: error: T is not assignable to Ghul.int
test.ghul: 18,9..18,14: error: U is not assignable to Ghul.string
test.ghul: 23,9..23,14: error: X is not assignable to Ghul.int
test.ghul: 24,9..24,14: error: Y is not assignable to Ghul.string
test.ghul: 26,9..26,21: error: Ghul.object is not assignable to T
test.ghul: 27,9..27,21: error: Ghul.object is not assignable to U
test.ghul: 29,9..29,21: error: Ghul.object is not assignable to X
test.ghul: 30,9..30,21: error: Ghul.object is not assignable to Y
test.ghul: 32,9..32,21: error: Ghul.object is not assignable to Ghul.int
test.ghul: 33,9..33,21: error: Ghul.object is not assignable to Ghul.string
test.ghul: 35,9..35,15: error: Ghul.int is not assignable to T
test.ghul: 36,9..36,20: error: Ghul.string is not assignable to Y
test.ghul: 47,13..47,21: error: Ghul.object is not assignable to T
test.ghul: 48,13..48,21: error: Ghul.object is not assignable to U
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"compiler": "dotnet ../../../publish/ghul.dll",
"source": [
"."
]
"compiler": "dotnet ../../../publish/ghul.dll"
}
Original file line number Diff line number Diff line change
@@ -1,70 +1,50 @@
/*
use Std = IO.Std;

https://github.com/degory/ghul/issues/803
class Main[X,Y] is
entry() static is
let t = function(123, "hello");

Function generic argument types should only be wildcards when binding

Function generic argument types should only be wildcards for purposes of
function overload resolution, but they're currently wildcards for all type
comparisons, including for code within the method.

the following should result in compile time errors, as the assignments of t and u to i and s involve incompatible types:

function[T,U](t: T, u: U) -> (T,U) is
let i: int = t;
let s: string = u;
si

*/


use Std = IO.Std;

class Main[X,Y] is
entry() static is
let t = function(123, "hello");

let u = function([123,456], ["aaa", "bbb"]);
si
let u = function([123,456], ["aaa", "bbb"]);
si

function[T,U](t: T, u: U) -> (t: T,u: U) static is
let i: int;
let j: string;
function[T,U](t: T, u: U) -> (t: T,u: U) static is
let i: int;
let j: string;

let x: X;
let y: Y;
let x: X;
let y: Y;

i = t; // expect error T is not an int
j = u; // expect error U is not a string
i = t; // expect error T is not an int
j = u; // expect error U is not a string

t.to_string();
x.to_string();
t.to_string();
x.to_string();

i = x; // expect error X is not an int
j = y; // expect error Y is not a string
i = x; // expect error X is not an int
j = y; // expect error Y is not a string

t = object(); // expect error object is not a T
u = object(); // expect error object is not a U
t = object(); // expect error object is not a T
u = object(); // expect error object is not a U

x = object(); // expect error object is not a X
y = object(); // expect error object is not a Y
x = object(); // expect error object is not a X
y = object(); // expect error object is not a Y

i = object(); // expect error object is not an int
j = object(); // expect error object is not a string
i = object(); // expect error object is not an int
j = object(); // expect error object is not a string

t = 10; // expect error int is not a T
y = "hello"; // expect error string is not a Y
t = 10; // expect error int is not a T
y = "hello"; // expect error string is not a Y

let o: object;
let p: object;
let o: object;
let p: object;

o = x; // OK X is an object
p = y; // OK Y is an object
o = x; // OK X is an object
p = y; // OK Y is an object

o = t; // OK T is an object
p = u; // OK U is an object

let w: T = o; // expect error object is not a T
let z: U = p; // expect error object is not a U
si
o = t; // OK T is an object
p = u; // OK U is an object

let w: T = o; // expect error object is not a T
let z: U = p; // expect error object is not a U
si
si
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"compiler": "dotnet ../../../publish/ghul.dll",
"source": [
"."
]
"compiler": "dotnet ../../../publish/ghul.dll"
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
test.ghul: 11,12..11,13: error: cannot return value of type U from function of type T
test.ghul: 26,9..26,17: error: null is not assignable to THING.T
test.ghul: 28,9..28,14: error: THING.U is not assignable to THING.T
test.ghul: 31,16..31,17: error: cannot return value of type THING.U from function of type THING.T
test.ghul: 33,16..33,20: error: cannot return value of type null from function of type THING.T
test.ghul: 44,9..44,17: error: null is not assignable to THING.T
test.ghul: 46,9..46,14: error: THING.U is not assignable to THING.T
test.ghul: 49,16..49,17: error: cannot return value of type THING.U from function of type THING.T
test.ghul: 51,16..51,20: error: cannot return value of type null from function of type THING.T
test.ghul: 58,9..58,15: error: THING.T is not assignable to THING.U
test.ghul: 26,9..26,17: error: null is not assignable to T
test.ghul: 28,9..28,14: error: U is not assignable to T
test.ghul: 31,16..31,17: error: cannot return value of type U from function of type T
test.ghul: 33,16..33,20: error: cannot return value of type null from function of type T
test.ghul: 44,9..44,17: error: null is not assignable to T
test.ghul: 46,9..46,14: error: U is not assignable to T
test.ghul: 49,16..49,17: error: cannot return value of type U from function of type T
test.ghul: 51,16..51,20: error: cannot return value of type null from function of type T
test.ghul: 58,9..58,15: error: T is not assignable to U
test.ghul: 6,5..6,13: error: null is not assignable to T
test.ghul: 60,9..60,15: error: THING.U is not assignable to THING.T
test.ghul: 64,9..64,18: error: null is not assignable to THING.T
test.ghul: 65,9..65,18: error: null is not assignable to THING.U
test.ghul: 60,9..60,15: error: U is not assignable to T
test.ghul: 64,9..64,18: error: null is not assignable to T
test.ghul: 65,9..65,18: error: null is not assignable to U
test.ghul: 70,14..70,24: error: no static overload found for function(null, null), tried function[T,U](t: T, u: U) -> T
test.ghul: 71,24..71,34: error: no static overload found for function(null, null), tried function[Ghul.int,Ghul.int](t: int, u: int) -> Ghul.int
test.ghul: 72,27..72,37: error: no static overload found for function(null, null), tried function[Ghul.int,Ghul.string](t: int, u: string) -> Ghul.int
test.ghul: 73,27..73,40: error: no static overload found for function(Ghul.string, null), tried function[Ghul.string,Ghul.int](t: string, u: int) -> Ghul.string
test.ghul: 71,23..71,33: error: no static overload found for function(null, null), tried function[Ghul.int,Ghul.int](t: int, u: int) -> Ghul.int
test.ghul: 72,26..72,36: error: no static overload found for function(null, null), tried function[Ghul.int,Ghul.string](t: int, u: string) -> Ghul.int
test.ghul: 73,26..73,39: error: no static overload found for function(Ghul.string, null), tried function[Ghul.string,Ghul.int](t: string, u: int) -> Ghul.string
test.ghul: 8,5..8,10: error: U is not assignable to T
test.ghul: 85,30..85,40: error: no static overload found for function(null, null), tried THING[Ghul.int,Ghul.int].function(t: int, u: int) -> Ghul.int
test.ghul: 86,33..86,43: error: no static overload found for function(null, null), tried THING[Ghul.int,Ghul.string].function(t: int, u: string) -> Ghul.int
test.ghul: 87,33..87,43: error: no static overload found for function(null, null), tried THING[Ghul.string,Ghul.int].function(t: string, u: int) -> Ghul.string
test.ghul: 89,30..89,43: error: no static overload found for function(Ghul.int, Ghul.string), tried THING[Ghul.int,Ghul.int].function(t: int, u: int) -> Ghul.int
test.ghul: 90,33..90,43: error: no static overload found for function(Ghul.int, Ghul.int), tried THING[Ghul.int,Ghul.string].function(t: int, u: string) -> Ghul.int
test.ghul: 91,33..91,49: error: no static overload found for function(Ghul.string, Ghul.string), tried THING[Ghul.string,Ghul.int].function(t: string, u: int) -> Ghul.string
test.ghul: 92,36..92,49: error: no static overload found for function(Ghul.string, Ghul.int), tried THING[Ghul.string,Ghul.string].function(t: string, u: string) -> Ghul.string
test.ghul: 85,29..85,39: error: no static overload found for function(null, null), tried THING[Ghul.int,Ghul.int].function(t: int, u: int) -> Ghul.int
test.ghul: 86,32..86,42: error: no static overload found for function(null, null), tried THING[Ghul.int,Ghul.string].function(t: int, u: string) -> Ghul.int
test.ghul: 87,32..87,42: error: no static overload found for function(null, null), tried THING[Ghul.string,Ghul.int].function(t: string, u: int) -> Ghul.string
test.ghul: 89,29..89,42: error: no static overload found for function(Ghul.int, Ghul.string), tried THING[Ghul.int,Ghul.int].function(t: int, u: int) -> Ghul.int
test.ghul: 90,32..90,42: error: no static overload found for function(Ghul.int, Ghul.int), tried THING[Ghul.int,Ghul.string].function(t: int, u: string) -> Ghul.int
test.ghul: 91,32..91,48: error: no static overload found for function(Ghul.string, Ghul.string), tried THING[Ghul.string,Ghul.int].function(t: string, u: int) -> Ghul.string
test.ghul: 92,35..92,48: error: no static overload found for function(Ghul.string, Ghul.int), tried THING[Ghul.string,Ghul.string].function(t: string, u: string) -> Ghul.string
Loading

0 comments on commit 487177b

Please sign in to comment.