From 37533528ef4984dab2b70fbca891a00c8e97e73b Mon Sep 17 00:00:00 2001 From: Iain Buclaw Date: Tue, 20 Dec 2022 14:25:21 +0100 Subject: [PATCH] =?UTF-8?q?revert=20Issue=C2=A022198=20-=20Compile=20time?= =?UTF-8?q?=20bounds=20checking=20for=20static=20arrays?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compiler/src/dmd/expressionsem.d | 12 +----------- compiler/test/compilable/issue20618.d | 10 ++++++++++ compiler/test/fail_compilation/fail20618.d | 16 ---------------- 3 files changed, 11 insertions(+), 27 deletions(-) create mode 100644 compiler/test/compilable/issue20618.d delete mode 100644 compiler/test/fail_compilation/fail20618.d diff --git a/compiler/src/dmd/expressionsem.d b/compiler/src/dmd/expressionsem.d index e8405a1b20..ea56b6a332 100644 --- a/compiler/src/dmd/expressionsem.d +++ b/compiler/src/dmd/expressionsem.d @@ -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) { diff --git a/compiler/test/compilable/issue20618.d b/compiler/test/compilable/issue20618.d new file mode 100644 index 0000000000..0cc90cff66 --- /dev/null +++ b/compiler/test/compilable/issue20618.d @@ -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]; +} diff --git a/compiler/test/fail_compilation/fail20618.d b/compiler/test/fail_compilation/fail20618.d deleted file mode 100644 index ac6b33a3a9..0000000000 --- a/compiler/test/fail_compilation/fail20618.d +++ /dev/null @@ -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]; -}