Change semantics of widening.

This commit is contained in:
Christoffer Lerno
2021-12-05 23:41:45 +01:00
parent d6d4c0a912
commit 4153cbe16d
3 changed files with 13 additions and 1 deletions

View File

@@ -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;
}

View File

@@ -0,0 +1,7 @@
fn int main()
{
ushort x;
uint y;
ushort z = x + (ushort)(0x12);
return 0;
}

View File

@@ -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;