- Add enum.from_ordinal and fault.from_ordinal

- Deprecate cast-style conversion from integer to enum.
- Make deprecation an error in test mode.
This commit is contained in:
Christoffer Lerno
2024-12-23 15:27:59 +01:00
parent 6078598aff
commit e453e6f9ca
26 changed files with 153 additions and 87 deletions

View File

@@ -1,6 +1,6 @@
fn void test()
{
$assert $or(false, false, true, hello(""));
$assert !$and(false, hello(""));
$assert !$and(true, true, false, hello(""));
$assert false ||| false ||| true ||| hello("");
$assert !(false &&& hello(""));
$assert !(true &&& true &&& false &&& hello(""));
}

View File

@@ -10,7 +10,7 @@ macro elements($Type)
{
int x;
$foreach ($x : $Type.values)
x = (int)$x;
x = $x.ordinal;
$endforeach;
}

View File

@@ -2,23 +2,23 @@ enum Abc : char { ABC }
fn void foo()
{
Abc x = (Abc)10; // #error: exceeds the number of enums
Abc x = Abc.from_ordinal(10); // #error: exceeds the max
}
fn void bar()
{
int a;
Abc x = (Abc)a;
Abc x = Abc.from_ordinal(a);
}
fn void baz()
{
int a;
Abc x = (Abc)0;
Abc x = Abc.from_ordinal(0);
}
fn void abc()
{
int a;
Abc x = (Abc)-1; // #error: negative number
Abc x = Abc.from_ordinal(-1); // #error: negative number
}

View File

@@ -6,13 +6,13 @@ enum Abc : char { ABC }
fn void main()
{
int a;
Abc x = (Abc)a;
Abc x = Abc.from_ordinal(a);
long z;
Abc y = (Abc)z;
Abc y = Abc.from_ordinal(z);
a = 256;
y = (Abc)a;
y = Abc.from_ordinal(a);
a = -1;
y = (Abc)a;
y = Abc.from_ordinal(a);
}
/* #expect: test.ll

View File

@@ -14,7 +14,7 @@ fn void foo(Mouse_Button button)
fn int main() {
uint x = 1;
foo((Mouse_Button)x);
foo(Mouse_Button.from_ordinal(x));
return 0;
}

View File

@@ -6,6 +6,5 @@ macro void test(int a, int $baz)
fn void main()
{
test(.$baz = 1, .a = 4); // #error: Named arguments must always
test($baz: 1, a: 4); // #error: Named arguments must always
}

View File

@@ -27,7 +27,7 @@ macro foo3(...)
{
var $x = 0;
$for (var $i = 0; $i < $vacount; $i++)
$x += $vaconst($i);
$x += $vaconst[$i];
$endfor;
return $x;
}

View File

@@ -28,9 +28,9 @@ fn void test_check_nums()
Foo f = A;
switch (f)
{
case (Foo)(1):
case Foo.from_ordinal(1):
break;
case (Foo)(0):
case Foo.from_ordinal(0):
break;
}
}
@@ -66,7 +66,7 @@ fn void test_duplicate_case2(Foo i)
{
case A:
break;
case (Foo)(1):
case Foo.from_ordinal(1):
break;
case A: // #error: same case value appears
break;
@@ -79,7 +79,7 @@ fn void test_duplicate_case3(Foo i)
{
case A:
break;
case (Foo)(0): // #error: same case value appears
case Foo.from_ordinal(0): // #error: same case value appears
break;
}
}

View File

@@ -2,7 +2,7 @@ module test;
import std::collections::map;
def Foo = HashMap(<String, HashMap(<String, String>)>);
fn void main()
macro test()
{
HashMap(<String, String>) map0;
map0["c3c"] = {};
@@ -16,6 +16,10 @@ fn void main()
map2["c3c"] = { {}, null, 1 ,2 ,2 };
}
fn void main()
{
test();
}
/* #expect: test.ll

View File

@@ -2,8 +2,8 @@
<*
@require Token.kindof == ENUM && $typefrom(Token.inner).kindof == UNSIGNED_INT
@require $defined(((Token)0).token)
@require $defined(((Comment)0).start) && $defined(((Comment)0).end)
@require $defined(Token{}.token)
@require $defined(Comment{}.start) && $defined(Comment{}.end)
*>
module lexer(<Token, Comment>);
import std::io;
@@ -58,7 +58,7 @@ fn void! Lexer.init(&self, InStream reader, Ident ident, Allocator using = alloc
{
String name = tok.token;
assert(name.len > 0 && name.len <= ushort.max);
trie.set(name, (Token)i)!;
trie.set(name, Token.from_ordinal(i))!;
max_token = max(max_token, (ushort)name.len);
}
foreach (tok : Comment.values)

View File

@@ -25,8 +25,8 @@ fn void enumInferenceTest()
bool y = x1 == x1;
Inf2 z = C;
if (z == Inf2.A) return;
if (z == (Inf2)1) return;
z = (Inf2)(2);
if (z == Inf2.from_ordinal(1)) return;
z = Inf2.from_ordinal(2);
switch (z)
{
case Inf2.A:
@@ -34,7 +34,7 @@ fn void enumInferenceTest()
return;
case B:
return;
case (Inf2)(2):
case Inf2.from_ordinal(2):
x1 += 1;
return;
default: