mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 03:51:18 +00:00
Change semantics of widening.
This commit is contained in:
@@ -1107,6 +1107,11 @@ bool cast_implicit(Expr *expr, Type *to_type)
|
||||
}
|
||||
}
|
||||
cast(expr, to_type);
|
||||
// Allow narrowing after widening
|
||||
if (type_is_numeric(to_type) && expr->expr_kind == EXPR_CONST && type_size(expr_canonical) < type_size(to_canonical))
|
||||
{
|
||||
expr->const_expr.narrowable = true;
|
||||
}
|
||||
if (expr->expr_kind == EXPR_CAST) expr->cast_expr.implicit = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
7
test/test_suite/expressions/casts/narrowing.c3
Normal file
7
test/test_suite/expressions/casts/narrowing.c3
Normal file
@@ -0,0 +1,7 @@
|
||||
fn int main()
|
||||
{
|
||||
ushort x;
|
||||
uint y;
|
||||
ushort z = x + (ushort)(0x12);
|
||||
return 0;
|
||||
}
|
||||
@@ -117,7 +117,7 @@ fn void test16()
|
||||
int i = 1;
|
||||
|
||||
ichar c = 1 ? 'c' : 'd';
|
||||
ichar d = 1 ? 'c' : i; // #error: 'int' to 'ichar'
|
||||
ichar d = 1 ? 'c' : i;
|
||||
ichar e = 1 ? i : 0; // #error: 'int' to 'ichar'
|
||||
int g = 1 ? i : f; // #error: 'float' to 'int'
|
||||
int a = f ? 1 : 0;
|
||||
|
||||
Reference in New Issue
Block a user