revert Issue 22198 - Compile time bounds checking for static arrays

This commit is contained in:
Iain Buclaw 2022-12-20 14:25:21 +01:00 committed by Mathias LANG
parent 77935bf494
commit 37533528ef
3 changed files with 11 additions and 27 deletions

View file

@ -8058,22 +8058,12 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
Expression el = new ArrayLengthExp(exp.loc, exp.e1);
el = el.expressionSemantic(sc);
el = el.optimize(WANTvalue);
if (el.op == EXP.int64 && t1b.ty == Tsarray)
if (el.op == EXP.int64)
{
// Array length is known at compile-time. Upper is in bounds if it fits length.
dinteger_t length = el.toInteger();
auto bounds = IntRange(SignExtendedNumber(0), SignExtendedNumber(length));
exp.upperIsInBounds = bounds.contains(uprRange);
if (exp.lwr.op == EXP.int64 && exp.upr.op == EXP.int64 && exp.lwr.toInteger() > exp.upr.toInteger())
{
exp.error("in slice `%s[%llu .. %llu]`, lower bound is greater than upper bound", exp.e1.toChars, exp.lwr.toInteger(), exp.upr.toInteger());
return setError();
}
if (exp.upr.op == EXP.int64 && exp.upr.toInteger() > length)
{
exp.error("in slice `%s[%llu .. %llu]`, upper bound is greater than array length `%llu`", exp.e1.toChars, exp.lwr.toInteger(), exp.upr.toInteger(), length);
return setError();
}
}
else if (exp.upr.op == EXP.int64 && exp.upr.toInteger() == 0)
{

View file

@ -0,0 +1,10 @@
// https://issues.dlang.org/show_bug.cgi?id=22198
// This test was in fail_compilation, however the change in the compiler has
// been reverted to make this code compilable again.
void main()
{
int[10] a;
auto b = a[1..12];
auto c = a[4..3];
auto d = a[0..$ + 1];
}

View file

@ -1,16 +0,0 @@
/*
TEST_OUTPUT:
---
fail_compilation/fail20618.d(13): Error: in slice `a[1 .. 12]`, upper bound is greater than array length `10`
fail_compilation/fail20618.d(14): Error: in slice `a[4 .. 3]`, lower bound is greater than upper bound
fail_compilation/fail20618.d(15): Error: in slice `a[0 .. 11]`, upper bound is greater than array length `10`
---
*/
void main()
{
int[10] a;
auto b = a[1..12];
auto c = a[4..3];
auto d = a[0..$ + 1];
}