Skip to content

Commit

Permalink
Tuple literal type covariance (#1175)
Browse files Browse the repository at this point in the history
Enhancements:
- Tuple literal type covariance (closes #1166)
- Improved type inference for list literals and if expressions (see #1173)
  • Loading branch information
degory committed Apr 17, 2024
1 parent 2fff7e2 commit fd0e159
Show file tree
Hide file tree
Showing 56 changed files with 1,311 additions and 617 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.42",
"version": "0.8.43",
"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.43-alpha.2</Version>
<Version>0.8.44-alpha.80</Version>
<NoWarn>$(NoWarn);NU1507</NoWarn>
</PropertyGroup>
<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/parse/function-nesting-error-1/err.expected
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
test.ghul: 13,13..13,28: error: expected function literal formal arguments
test.ghul: 15,24..15,29: error: cannot return value of type Ghul.int from function of type Test.IncompleteFunction.Test
test.ghul: 12,9..12,24: error: expected function literal formal arguments
test.ghul: 14,20..14,25: error: cannot return value of type Ghul.int where Test expected
5 changes: 1 addition & 4 deletions integration-tests/parse/function-nesting-error-1/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"
}
60 changes: 29 additions & 31 deletions integration-tests/parse/function-nesting-error-1/test.ghul
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
namespace Test.IncompleteFunction is
class Test is
count: int;
class Test is
count: int;

f(i: int) -> int is
return 0
si

g(i:int) -> int is
count = count - 1;
return 0;
f(i: int) -> int is
return 0
si

"asdf" (i: int) -> Test is
count = count - 1;
return count;
si
g(i:int) -> int is
count = count - 1;
return 0;

"asdf" (i: int) -> Test is
count = count - 1;
return count;
si

i(i: int) -> Test is
let x = if count > 0 then
"count is {count}"
else
"count is 0"
fi;

if count > 0 then
return null
else
return null
fi
si
si

i(i: int) -> Test is
let x = if count > 0 then
"count is {count}"
else
"count is 0"
fi;

j(i: int) -> int is
return count
si
if count > 0 then
return null
else
return null
fi
si

j(i: int) -> int is
return count
si
si
si

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test.ghul: 22,20..22,27: error: cannot return value of type Ghul.string from function of type Ghul.int
test.ghul: 29,32..29,39: error: cannot return value of type Ghul.string from function of type Ghul.int
test.ghul: 32,20..32,24: error: cannot return value from function of void type
test.ghul: 37,26..37,31: error: cannot return value from function of void type
test.ghul: 11,16..11,23: error: cannot return value of type Ghul.string where Ghul.int expected
test.ghul: 18,28..18,35: error: cannot return value of type Ghul.string where Ghul.int expected
test.ghul: 21,16..21,20: error: cannot return value from function of void type
test.ghul: 26,22..26,27: error: cannot return value from function of void type
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"
}
54 changes: 21 additions & 33 deletions integration-tests/semantic/function-return-type-check/test.ghul
Original file line number Diff line number Diff line change
@@ -1,45 +1,33 @@
namespace Test is
use Std = IO.Std;

class Main is
entry() static is
return;
si

test_int_1() -> int static is
return 1234; // OK - correct type

return "Hello"; // Error: string not assignable to return type int

return; // Warning: no return expression means default value for int will be returned
si

test_int_2() -> int => 1234; // OK - correct type

test_int_3() -> int => "Hello"; // Error: string not assignable to int

test_void_1() is
return 1234; // Error: any value returned from void function is incorrect

use Std = IO.Std;

class Main is
entry() static is


return;
si

test_int_1() -> int static is
return 1234; // OK - correct type

return "Hello"; // Error: string not assignable to return type int

return; // Warning: no return expression means default value for int will be returned
si

test_int_2() -> int => 1234; // OK - correct type

test_int_3() -> int => "Hello"; // Error: string not assignable to int

test_void_1() is
return 1234; // Error: any value returned from void function is incorrect

return; // OK - no value expected
si
return; // OK - no value expected
si

test_void_2() => 12345; // Eventually would like an omitted return type to be inferred, but it's void for now
test_void_2() => 12345; // Eventually would like an omitted return type to be inferred, but it's void for now

test_object_1() -> object is
return 1234; // OK - int 1234 is assignable to object
test_object_1() -> object is
return 1234; // OK - int 1234 is assignable to object

return "Hello"; // OK - string "Hello" is assignable to object
si
return "Hello"; // OK - string "Hello" is assignable to object
si
si
si
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test.ghul: 24,13..24,19: warn: return without value from non void function returns default value of type Ghul.int
test.ghul: 13,9..13,15: warn: return without value from non void function returns default value of type Ghul.int
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
test.ghul: 11,12..11,13: error: cannot return value of type U from function of type T
test.ghul: 11,12..11,13: error: cannot return value of type U where T expected
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: 31,16..31,17: error: cannot return value of type U where T expected
test.ghul: 33,16..33,20: error: cannot return value of type null where T expected
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: 49,16..49,17: error: cannot return value of type U where T expected
test.ghul: 51,16..51,20: error: cannot return value of type null where T expected
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: U is not assignable to T
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
test.ghul: 26,16..26,21: error: cannot return value of type T from function of type BLAH
test.ghul: 26,16..26,21: error: cannot return value of type T where BLAH expected
test.ghul: 31,15..31,19: error: member blah not found in T
test.ghul: 36,9..36,21: error: BLAH is not assignable to T
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
test.ghul: 10,16..10,20: error: cannot return value of type null from function of type U
test.ghul: 15,9..15,21: error: null is not assignable to T
test.ghul: 18,9..18,21: error: null is not assignable to U
test.ghul: 14,5..14,17: error: null is not assignable to T
test.ghul: 17,5..17,17: error: null is not assignable to U
test.ghul: 9,12..9,16: error: cannot return value of type null where U expected
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
namespace Test.GenericFunctionTypeArgumentsAreAssignableFromNull is
use Collections;
use Collections;

class BLAH is
blah();
si
class BLAH is
blah();
si

to_t[T,U](value: T) -> U is
// expect error: T is not compatible with null
return null;
si
to_t[T,U](value: T) -> U is
// expect error: T is not compatible with null
return null;
si

set_from_t[T,U](value: T, other: U) is
// expect error: T is not compatible with null
value = null;
set_from_t[T,U](value: T, other: U) is
// expect error: T is not compatible with null
value = null;

// expect error: U is not compatible with null
other = null;
si
// expect error: U is not compatible with null
other = null;
si

entry() is
si
si
entry() is
si
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
test.ghul: 11,16..11,21: error: cannot return value of type T from function of type U
test.ghul: 16,9..16,22: error: U is not assignable to T
test.ghul: 14,5..14,18: error: U is not assignable to T
test.ghul: 9,12..9,17: error: cannot return value of type T where U expected
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
namespace Test.GenericFunctionTypeArgumentsAreObjects is
use Collections;
use Collections;

class BLAH is
blah();
si
class BLAH is
blah();
si

to_t[T,U](value: T) -> U is
// expect error - T should not be assignable to U
return value;
si

to_t[T,U](value: T) -> U is
// expect error - T should not be assignable to U
return value;
si
set_from_t[T,U](value: T, other: U) is
// expect error - U should not be assignable to T
value = other;
si

set_from_t[T,U](value: T, other: U) is
// expect error - U should not be assignable to T
value = other;
si

entry() is
si
si
entry() is
si
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test.ghul: 22,13..22,28: error: T is not assignable to Test.GenericFunctionTypeArgumentsAreObjects.BLAH
test.ghul: 25,16..25,21: error: cannot return value of type T from function of type Test.GenericFunctionTypeArgumentsAreObjects.BLAH
test.ghul: 30,15..30,19: error: member blah not found in T
test.ghul: 35,9..35,21: error: Test.GenericFunctionTypeArgumentsAreObjects.BLAH is not assignable to T
test.ghul: 21,9..21,24: error: T is not assignable to BLAH
test.ghul: 24,12..24,17: error: cannot return value of type T where BLAH expected
test.ghul: 29,11..29,15: error: member blah not found in T
test.ghul: 34,5..34,17: error: BLAH is not assignable to T
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
namespace Test.GenericFunctionTypeArgumentsAreObjects is
use Collections;

class BLAH is
blah();
si


to_string[T](value: T) -> string is
// expect OK - to_string() is a method on all objects
return value.to_string();
si

to_object[T](value: T) -> object is
// expect OK - T should be assignable to object
return value;
si

// FIXME: this should give an error
to_blah[T](value: T) -> BLAH is
// expect error - T is not assignable to BLAH
let b: BLAH = value;

// expect error - T is not assignable to BLAH
return value;
si

do_blah[T](value: T) is
// expect error - T does not have a method called blah
value.blah();
si

set_from_blah[T](value: T, blah: BLAH) is
// expect error - BLAH is not assignable to T
value = blah;
si

entry() is
si
use Collections;

class BLAH is
blah();
si


to_string[T](value: T) -> string is
// expect OK - to_string() is a method on all objects
return value.to_string();
si

to_object[T](value: T) -> object is
// expect OK - T should be assignable to object
return value;
si

// FIXME: this should give an error
to_blah[T](value: T) -> BLAH is
// expect error - T is not assignable to BLAH
let b: BLAH = value;

// expect error - T is not assignable to BLAH
return value;
si

do_blah[T](value: T) is
// expect error - T does not have a method called blah
value.blah();
si

set_from_blah[T](value: T, blah: BLAH) is
// expect error - BLAH is not assignable to T
value = blah;
si

entry() is
si
4 changes: 2 additions & 2 deletions integration-tests/semantic/generic-inheritance/err.expected
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
test.ghul: 503,19..503,23: error: cannot return value of type null from function of type T
test.ghul: 510,22..510,26: error: cannot return value of type null from function of type T
test.ghul: 503,19..503,23: error: cannot return value of type null where T expected
test.ghul: 510,22..510,26: error: cannot return value of type null where T expected
4 changes: 2 additions & 2 deletions integration-tests/semantic/generic-overrides/err.expected
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
test.ghul: 15,22..15,26: error: cannot return value of type null from function of type T
test.ghul: 6,19..6,23: error: cannot return value of type null from function of type T
test.ghul: 15,22..15,26: error: cannot return value of type null where T expected
test.ghul: 6,19..6,23: error: cannot return value of type null where T expected
Loading

0 comments on commit fd0e159

Please sign in to comment.