mirror of
https://github.com/dlang/dmd.git
synced 2025-04-26 05:00:16 +03:00
revert Issue 22198 - Compile time bounds checking for static arrays
This commit is contained in:
parent
77935bf494
commit
37533528ef
3 changed files with 11 additions and 27 deletions
|
@ -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)
|
||||
{
|
||||
|
|
10
compiler/test/compilable/issue20618.d
Normal file
10
compiler/test/compilable/issue20618.d
Normal 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];
|
||||
}
|
|
@ -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];
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue