From 88bf66c5a61c85e46a862149c519d13f6b68a194 Mon Sep 17 00:00:00 2001 From: k-hara Date: Tue, 10 Apr 2012 00:30:56 +0900 Subject: [PATCH] fix Issue 7871 - RangeViolation with findSplitBefore This is a regression caused by fixing issue 7583. When you syntaxCopy() a ast sub-tree, contained lengthVar is discarded. But it is already inserted to scope symbol table. So we should share lengthVar between all copy of syntax trees instead of creating new one. --- src/expression.c | 15 +++++++++++++-- src/expression.h | 1 + test/runnable/xtest46.d | 22 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/expression.c b/src/expression.c index 2cb0dc5fb1b4..d7777351783a 100644 --- a/src/expression.c +++ b/src/expression.c @@ -8962,7 +8962,9 @@ Expression *SliceExp::syntaxCopy() if (this->upr) upr = this->upr->syntaxCopy(); - return new SliceExp(loc, e1->syntaxCopy(), lwr, upr); + SliceExp *se = new SliceExp(loc, e1->syntaxCopy(), lwr, upr); + se->lengthVar = this->lengthVar; // bug7871 + return se; } Expression *SliceExp::semantic(Scope *sc) @@ -9307,7 +9309,9 @@ ArrayExp::ArrayExp(Loc loc, Expression *e1, Expressions *args) Expression *ArrayExp::syntaxCopy() { - return new ArrayExp(loc, e1->syntaxCopy(), arraySyntaxCopy(arguments)); + ArrayExp *ae = new ArrayExp(loc, e1->syntaxCopy(), arraySyntaxCopy(arguments)); + ae->lengthVar = this->lengthVar; // bug7871 + return ae; } Expression *ArrayExp::semantic(Scope *sc) @@ -9474,6 +9478,13 @@ IndexExp::IndexExp(Loc loc, Expression *e1, Expression *e2) modifiable = 0; // assume it is an rvalue } +Expression *IndexExp::syntaxCopy() +{ + IndexExp *ie = new IndexExp(loc, e1->syntaxCopy(), e2->syntaxCopy()); + ie->lengthVar = this->lengthVar; // bug7871 + return ie; +} + Expression *IndexExp::semantic(Scope *sc) { Expression *e; Type *t1; diff --git a/src/expression.h b/src/expression.h index 7c91273d7cca..a20d09268f08 100644 --- a/src/expression.h +++ b/src/expression.h @@ -1196,6 +1196,7 @@ struct IndexExp : BinExp int modifiable; IndexExp(Loc loc, Expression *e1, Expression *e2); + Expression *syntaxCopy(); Expression *semantic(Scope *sc); int isLvalue(); Expression *toLvalue(Scope *sc, Expression *e); diff --git a/test/runnable/xtest46.d b/test/runnable/xtest46.d index 973502340704..519cdaefc3d4 100644 --- a/test/runnable/xtest46.d +++ b/test/runnable/xtest46.d @@ -4945,6 +4945,27 @@ struct A7823 { void test7823(A7823 a = A7823.b) { } +/***************************************************/ +// 7871 + +struct Tuple7871 +{ + string field; + alias field this; +} + +//auto findSplitBefore(R1)(R1 haystack) +auto findSplitBefore7871(string haystack) +{ + return Tuple7871(haystack); +} + +void test7871() +{ + string line = ``; + auto a = findSplitBefore7871(line[0 .. $])[0]; +} + /***************************************************/ int main() @@ -5173,6 +5194,7 @@ int main() test7682(); test7735(); test7823(); + test7871(); printf("Success\n"); return 0;