TypeChecker

- If we have a `LiteralValue` and a non-`LiteralValue` then coerce the `LiteralValue` towards the non`-LiteralValue` via `typeEnforce()`
- This should allow the correct range checking of literal values within the range of the to-type and not require annoying explicit casts
This commit is contained in:
Tristan B. Velloza Kildaire 2023-07-17 12:37:32 +02:00
parent 7f902b9fcf
commit 155298e9ee
1 changed files with 25 additions and 1 deletions

View File

@ -1575,7 +1575,31 @@ public final class TypeChecker
assert(vLhsTypeIntegral);
Integer vRhsTypeIntegral = cast(Integer)vRhsType;
assert(vRhsTypeIntegral);
if(vLhsTypeIntegral.getSize() < vRhsTypeIntegral.getSize())
// TODO: There is a case to be made for when the instruction
// with the bigger size is an IntegerLiteral - in that case
// range checking would be nice
// We could use isCoercibe on
if(cast(LiteralValue)vLhsInstr || cast(LiteralValue)vRhsInstr)
{
// Type enforce left-hand instruction to right-hand instruction
if(cast(LiteralValue)vLhsInstr && cast(LiteralValue)vRhsInstr is null)
{
typeEnforce(vRhsTypeIntegral, vLhsInstr, vLhsInstr, true);
}
// Type enforce right-hand instruction to left-hand instruction
else if(cast(LiteralValue)vLhsInstr is null && cast(LiteralValue)vRhsInstr)
{
typeEnforce(vLhsTypeIntegral, vRhsInstr, vRhsInstr, true);
}
// Both are literal values
else
{
// Do nothing
}
}
else if(vLhsTypeIntegral.getSize() < vRhsTypeIntegral.getSize())
{
typeEnforce(vRhsTypeIntegral, vLhsInstr, vLhsInstr, true);
}