Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

fix Issue 6652 - 3. Change modifying non ref variable allowed and not affect to loop #1010

Merged
merged 1 commit into from

3 participants

@9rnsr
Collaborator

This pull is a part of phases for deprecating/changing code behavior. See my comment in bugzilla.

@9rnsr
Collaborator

Also contains a fix for the key in foreach(i, e; array).

@9rnsr 9rnsr referenced this pull request
Closed

Bug6652 #377

@MartinNowak
Collaborator

You mentioned in #377 that you'd wanted to copy it's regression tests.
Please feel free to take over anything that you find useful.

@9rnsr
Collaborator

@dawgfoto Thanks. I updated test cases based on that in #377.

@WalterBright WalterBright merged commit ad74ad3 into D-Programming-Language:master
@ghost Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@ghost Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@ghost Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@ghost Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 31, 2013
  1. @9rnsr
This page is out of date. Refresh to see the latest.
View
4 src/declaration.h
@@ -93,10 +93,6 @@ enum PURE;
STCmanifest | STCimmutable | STCshared | STCnothrow | STCpure | STCref | STCtls | \
STCgshared | STCproperty | STCsafe | STCtrusted | STCsystem | STCdisable)
-#ifdef BUG6652
-#define STCbug6652 0x800000000000LL //
-#endif
-
struct Match
{
int count; // number of matches found
View
10 src/expression.c
@@ -5201,16 +5201,6 @@ Expression *VarExp::modifiableLvalue(Scope *sc, Expression *e)
//if (type && type->toBasetype()->ty == Tsarray)
//error("cannot change reference to static array '%s'", var->toChars());
-#if (BUG6652 == 1)
- VarDeclaration *v = var->isVarDeclaration();
- if (v && (v->storage_class & STCbug6652) && v->type->isMutable())
- warning("variable modified in foreach body requires ref storage class");
-#elif (BUG6652 == 2)
- VarDeclaration *v = var->isVarDeclaration();
- if (v && (v->storage_class & STCbug6652) && v->type->isMutable())
- deprecation("variable modified in foreach body requires ref storage class");
-#endif
-
// See if this expression is a modifiable lvalue (i.e. not const)
return Expression::modifiableLvalue(sc, e);
}
View
3  src/mars.h
@@ -90,9 +90,6 @@ void unittests();
#define SNAN_DEFAULT_INIT DMDV2 // if floats are default initialized to signalling NaN
#define SARRAYVALUE DMDV2 // static arrays are value types
#define MODULEINFO_IS_STRUCT DMDV2 // if ModuleInfo is a struct rather than a class
-#define BUG6652 2 // Making foreach range statement parameter non-ref in default
- // 1: Modifying iteratee in body is warned with -w switch
- // 2: Modifying iteratee in body is error without -d switch
// Set if C++ mangling is done by the front end
#define CPP_MANGLE (DMDV2 && (TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS))
View
8 src/statement.c
@@ -1873,11 +1873,7 @@ Statement *ForeachStatement::semantic(Scope *sc)
{
ExpInitializer *ie = new ExpInitializer(loc, new IdentifierExp(loc, key->ident));
VarDeclaration *v = new VarDeclaration(loc, arg->type, arg->ident, ie);
-#if (BUG6652 == 1 || BUG6652 == 2)
- v->storage_class |= STCforeach | STCref | (arg->storageClass & STCref ? 0 : STCbug6652);
-#else
v->storage_class |= STCforeach | (arg->storageClass & STCref);
-#endif
body = new CompoundStatement(loc, new ExpStatement(loc, v), body);
}
}
@@ -2569,11 +2565,7 @@ Statement *ForeachRangeStatement::semantic(Scope *sc)
{
ie = new ExpInitializer(loc, new IdentifierExp(loc, key->ident));
VarDeclaration *v = new VarDeclaration(loc, arg->type, arg->ident, ie);
-#if (BUG6652 == 1 || BUG6652 == 2)
- v->storage_class |= STCforeach | STCref | (arg->storageClass & STCref ? 0 : STCbug6652);
-#else
v->storage_class |= STCforeach | (arg->storageClass & STCref);
-#endif
body = new CompoundStatement(loc, new ExpStatement(loc, v), body);
}
if (arg->storageClass & STCref)
View
15 test/fail_compilation/fail6652a.d
@@ -1,25 +1,24 @@
-// REQUIRED_ARGS: -de
-// PERMUTE_ARGS: -w
+// PERMUTE_ARGS: -w -dw -de -d
+
/******************************************/
// 6652
/*
TEST_OUTPUT:
---
-fail_compilation/fail6652a.d(19): Deprecation: variable modified in foreach body requires ref storage class
-fail_compilation/fail6652a.d(24): Error: cannot modify const expression i
+fail_compilation/fail6652a.d(18): Error: cannot modify const expression i
+fail_compilation/fail6652a.d(23): Error: cannot modify const expression i
---
*/
void main()
{
- size_t[] res;
- foreach (i; 0..2)
+ foreach (const i; 0..2)
{
- res ~= ++i;
+ ++i;
}
- foreach (const i; 0..2)
+ foreach (ref const i; 0..2)
{
++i;
}
View
15 test/fail_compilation/fail6652b.d
@@ -1,25 +1,24 @@
-// REQUIRED_ARGS: -de
-// PERMUTE_ARGS: -w
+// PERMUTE_ARGS: -w -dw -de -d
+
/******************************************/
// 6652
/*
TEST_OUTPUT:
---
-fail_compilation/fail6652b.d(19): Deprecation: variable modified in foreach body requires ref storage class
-fail_compilation/fail6652b.d(24): Error: cannot modify const expression i
+fail_compilation/fail6652b.d(18): Error: cannot modify const expression i
+fail_compilation/fail6652b.d(23): Error: cannot modify const expression i
---
*/
void main()
{
- size_t[] res;
- foreach (i, e; [1,2,3,4,5])
+ foreach (const i, e; [1,2,3,4,5])
{
- res ~= ++i;
+ ++i;
}
- foreach (const i, e; [1,2,3,4,5])
+ foreach (ref const i, e; [1,2,3,4,5])
{
++i;
}
View
21 test/runnable/deprecate1.d
@@ -1253,26 +1253,6 @@ void test18()
}
/******************************************/
-// 6652
-
-void test6652()
-{
- size_t[] res1;
- foreach (i; 0..2)
- {
- res1 ~= ++i;
- }
- assert(res1 == [1]);
-
- size_t[] res2;
- foreach (i, e; [0,1])
- {
- res2 ~= ++i;
- }
- assert(res2 == [1]);
-}
-
-/******************************************/
int main()
{
@@ -1332,7 +1312,6 @@ int main()
test6289();
test4237();
test18();
- test6652();
return 0;
}
View
102 test/runnable/foreach5.d
@@ -431,6 +431,107 @@ void test7814()
}
}
+/******************************************/
+// 6652
+
+void test6652()
+{
+ size_t sum;
+ foreach (i; 0 .. 10)
+ sum += i++; // 0123456789
+ assert(sum == 45);
+
+ sum = 0;
+ foreach (ref i; 0 .. 10)
+ sum += i++; // 02468
+ assert(sum == 20);
+
+ sum = 0;
+ foreach_reverse (i; 0 .. 10)
+ sum += i--; // 9876543210
+ assert(sum == 45);
+
+ sum = 0;
+ foreach_reverse (ref i; 0 .. 10)
+ sum += i--; // 97531
+ assert(sum == 25);
+
+ enum ary = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
+ sum = 0;
+ foreach (i, v; ary)
+ {
+ assert(i == v);
+ sum += i++; // 0123456789
+ }
+ assert(sum == 45);
+
+ sum = 0;
+ foreach (ref i, v; ary)
+ {
+ assert(i == v);
+ sum += i++; // 02468
+ }
+ assert(sum == 20);
+
+ sum = 0;
+ foreach_reverse (i, v; ary)
+ {
+ assert(i == v);
+ sum += i--; // 9876543210
+ }
+ assert(sum == 45);
+
+ sum = 0;
+ foreach_reverse (ref i, v; ary)
+ {
+ assert(i == v);
+ sum += i--; // 97531
+ }
+ assert(sum == 25);
+
+ static struct Iter
+ {
+ ~this()
+ {
+ ++_dtorCount;
+ }
+
+ bool opCmp(ref const Iter rhs)
+ {
+ return _pos == rhs._pos;
+ }
+
+ void opUnary(string op)() if(op == "++" || op == "--")
+ {
+ mixin(op ~ q{_pos;});
+ }
+
+ size_t _pos;
+ static size_t _dtorCount;
+ }
+
+ Iter._dtorCount = sum = 0;
+ foreach (v; Iter(0) .. Iter(10))
+ sum += v._pos++; // 0123456789
+ assert(sum == 45 && Iter._dtorCount == 12);
+
+ Iter._dtorCount = sum = 0;
+ foreach (ref v; Iter(0) .. Iter(10))
+ sum += v._pos++; // 02468
+ assert(sum == 20 && Iter._dtorCount == 2);
+
+ // additional dtor calls due to unnecessary postdecrements
+ Iter._dtorCount = sum = 0;
+ foreach_reverse (v; Iter(0) .. Iter(10))
+ sum += v._pos--; // 9876543210
+ assert(sum == 45 && Iter._dtorCount >= 12);
+
+ Iter._dtorCount = sum = 0;
+ foreach_reverse (ref v; Iter(0) .. Iter(10))
+ sum += v._pos--; // 97531
+ assert(sum == 25 && Iter._dtorCount >= 2);
+}
+
/***************************************/
// 8595
@@ -470,6 +571,7 @@ int main()
test6659b();
test6659c();
test7814();
+ test6652();
printf("Success\n");
return 0;
Something went wrong with that request. Please try again.