Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #885 from 9rnsr/fix7922

Issue 7922 - alias this causes weird formatting issues for strings
  • Loading branch information...
commit 3ecc1f3fbfd1d821bd647bde2e9d4adbcf9f2b9d 2 parents ad84e0d + a235ba4
Walter Bright authored May 21, 2012
12  src/declaration.c
@@ -1386,11 +1386,17 @@ void VarDeclaration::semantic(Scope *sc)
1386 1386
                                         e->op = TOKblit;
1387 1387
                                     }
1388 1388
                                     e->type = t;
1389  
-                                    (*pinit) = new CommaExp(loc, e, (*pinit));
1390 1389
 
1391  
-                                    /* Replace __ctmp being constructed with e1
  1390
+                                    /* Replace __ctmp being constructed with e1.
  1391
+                                     * We need to copy constructor call expression,
  1392
+                                     * because it may be used in other place.
1392 1393
                                      */
1393  
-                                    dve->e1 = e1;
  1394
+                                    DotVarExp *dvx = (DotVarExp *)dve->copy();
  1395
+                                    dvx->e1 = e1;
  1396
+                                    CallExp *cx = (CallExp *)ce->copy();
  1397
+                                    cx->e1 = dvx;
  1398
+
  1399
+                                    (*pinit) = new CommaExp(loc, e, cx);
1394 1400
                                     (*pinit) = (*pinit)->semantic(sc);
1395 1401
                                     goto Ldtor;
1396 1402
                                 }
72  src/opover.c
@@ -355,9 +355,11 @@ Expression *UnaExp::op_overload(Scope *sc)
355 355
             /* Rewrite op(e1) as:
356 356
              *  op(e1.aliasthis)
357 357
              */
358  
-            UnaExp *e = (UnaExp *)syntaxCopy();
359  
-            e->e1 = new DotIdExp(loc, e->e1, ad->aliasthis->ident);
360  
-            return e->trySemantic(sc);
  358
+            Expression *e1 = new DotIdExp(loc, this->e1, ad->aliasthis->ident);
  359
+            Expression *e = copy();
  360
+            ((UnaExp *)e)->e1 = e1;
  361
+            e = e->trySemantic(sc);
  362
+            return e;
361 363
         }
362 364
 #endif
363 365
     }
@@ -412,9 +414,11 @@ Expression *ArrayExp::op_overload(Scope *sc)
412 414
             /* Rewrite op(e1) as:
413 415
              *  op(e1.aliasthis)
414 416
              */
415  
-            UnaExp *e = (UnaExp *)syntaxCopy();
416  
-            e->e1 = new DotIdExp(loc, e->e1, ad->aliasthis->ident);
417  
-            return e->trySemantic(sc);
  417
+            Expression *e1 = new DotIdExp(loc, this->e1, ad->aliasthis->ident);
  418
+            Expression *e = copy();
  419
+            ((UnaExp *)e)->e1 = e1;
  420
+            e = e->trySemantic(sc);
  421
+            return e;
418 422
         }
419 423
     }
420 424
     return NULL;
@@ -457,9 +461,11 @@ Expression *CastExp::op_overload(Scope *sc)
457 461
             /* Rewrite op(e1) as:
458 462
              *  op(e1.aliasthis)
459 463
              */
460  
-            UnaExp *e = (UnaExp *)syntaxCopy();
461  
-            e->e1 = new DotIdExp(loc, e->e1, ad->aliasthis->ident);
462  
-            return e->trySemantic(sc);
  464
+            Expression *e1 = new DotIdExp(loc, this->e1, ad->aliasthis->ident);
  465
+            Expression *e = copy();
  466
+            ((UnaExp *)e)->e1 = e1;
  467
+            e = e->trySemantic(sc);
  468
+            return e;
463 469
         }
464 470
     }
465 471
     return NULL;
@@ -715,9 +721,11 @@ Expression *BinExp::op_overload(Scope *sc)
715 721
         /* Rewrite (e1 op e2) as:
716 722
          *      (e1.aliasthis op e2)
717 723
          */
718  
-        BinExp *e = (BinExp *)syntaxCopy();
719  
-        e->e1 = new DotIdExp(loc, e->e1, ad1->aliasthis->ident);
720  
-        return e->trySemantic(sc);
  724
+        Expression *e1 = new DotIdExp(loc, this->e1, ad1->aliasthis->ident);
  725
+        Expression *e = copy();
  726
+        ((BinExp *)e)->e1 = e1;
  727
+        e = e->trySemantic(sc);
  728
+        return e;
721 729
     }
722 730
 
723 731
     // Try alias this on second operand
@@ -730,9 +738,11 @@ Expression *BinExp::op_overload(Scope *sc)
730 738
         /* Rewrite (e1 op e2) as:
731 739
          *      (e1 op e2.aliasthis)
732 740
          */
733  
-        BinExp *e = (BinExp *)syntaxCopy();
734  
-        e->e2 = new DotIdExp(loc, e->e2, ad2->aliasthis->ident);
735  
-        return e->trySemantic(sc);
  741
+        Expression *e2 = new DotIdExp(loc, this->e2, ad2->aliasthis->ident);
  742
+        Expression *e = copy();
  743
+        ((BinExp *)e)->e2 = e2;
  744
+        e = e->trySemantic(sc);
  745
+        return e;
736 746
     }
737 747
 #endif
738 748
     return NULL;
@@ -884,9 +894,11 @@ Expression *BinExp::compare_overload(Scope *sc, Identifier *id)
884 894
         /* Rewrite (e1 op e2) as:
885 895
          *      (e1.aliasthis op e2)
886 896
          */
887  
-        BinExp *e = (BinExp *)syntaxCopy();
888  
-        e->e1 = new DotIdExp(loc, e->e1, ad1->aliasthis->ident);
889  
-        return e->trySemantic(sc);
  897
+        Expression *e1 = new DotIdExp(loc, this->e1, ad1->aliasthis->ident);
  898
+        Expression *e = copy();
  899
+        ((BinExp *)e)->e1 = e1;
  900
+        e = e->trySemantic(sc);
  901
+        return e;
890 902
     }
891 903
 
892 904
     // Try alias this on second operand
@@ -895,9 +907,11 @@ Expression *BinExp::compare_overload(Scope *sc, Identifier *id)
895 907
         /* Rewrite (e1 op e2) as:
896 908
          *      (e1 op e2.aliasthis)
897 909
          */
898  
-        BinExp *e = (BinExp *)syntaxCopy();
899  
-        e->e2 = new DotIdExp(loc, e->e2, ad2->aliasthis->ident);
900  
-        return e->trySemantic(sc);
  910
+        Expression *e2 = new DotIdExp(loc, this->e2, ad2->aliasthis->ident);
  911
+        Expression *e = copy();
  912
+        ((BinExp *)e)->e2 = e2;
  913
+        e = e->trySemantic(sc);
  914
+        return e;
901 915
     }
902 916
 
903 917
     return NULL;
@@ -1132,9 +1146,11 @@ Expression *BinAssignExp::op_overload(Scope *sc)
1132 1146
         /* Rewrite (e1 op e2) as:
1133 1147
          *      (e1.aliasthis op e2)
1134 1148
          */
1135  
-        BinExp *e = (BinExp *)syntaxCopy();
1136  
-        e->e1 = new DotIdExp(loc, e->e1, ad1->aliasthis->ident);
1137  
-        return e->trySemantic(sc);
  1149
+        Expression *e1 = new DotIdExp(loc, this->e1, ad1->aliasthis->ident);
  1150
+        Expression *e = copy();
  1151
+        ((BinExp *)e)->e1 = e1;
  1152
+        e = e->trySemantic(sc);
  1153
+        return e;
1138 1154
     }
1139 1155
 
1140 1156
     // Try alias this on second operand
@@ -1144,9 +1160,11 @@ Expression *BinAssignExp::op_overload(Scope *sc)
1144 1160
         /* Rewrite (e1 op e2) as:
1145 1161
          *      (e1 op e2.aliasthis)
1146 1162
          */
1147  
-        BinExp *e = (BinExp *)syntaxCopy();
1148  
-        e->e2 = new DotIdExp(loc, e->e2, ad2->aliasthis->ident);
1149  
-        return e->trySemantic(sc);
  1163
+        Expression *e2 = new DotIdExp(loc, this->e2, ad2->aliasthis->ident);
  1164
+        Expression *e = copy();
  1165
+        ((BinExp *)e)->e2 = e2;
  1166
+        e = e->trySemantic(sc);
  1167
+        return e;
1150 1168
     }
1151 1169
 #endif
1152 1170
     return NULL;
25  test/runnable/aliasthis.d
... ...
@@ -1,5 +1,6 @@
1 1
 
2 2
 extern (C) int printf(const(char*) fmt, ...);
  3
+import core.vararg;
3 4
 
4 5
 struct Tup(T...)
5 6
 {
@@ -810,6 +811,29 @@ void test7945()
810 811
 }
811 812
 
812 813
 /***************************************************/
  814
+// 7992
  815
+
  816
+struct S7992
  817
+{
  818
+    int[] arr;
  819
+    alias arr this;
  820
+}
  821
+S7992 func7992(...)
  822
+{
  823
+    S7992 ret;
  824
+    ret.arr.length = _arguments.length;
  825
+    return ret;
  826
+}
  827
+void test7992()
  828
+{
  829
+    int[] arr;
  830
+    assert(arr.length == 0);
  831
+    arr ~= func7992(1, 2);  //NG
  832
+    //arr = func7992(1, 2); //OK
  833
+    assert(arr.length == 2);
  834
+}
  835
+
  836
+/***************************************************/
813 837
 
814 838
 int main()
815 839
 {
@@ -841,6 +865,7 @@ int main()
841 865
     test7731();
842 866
     test7808();
843 867
     test7945();
  868
+    test7992();
844 869
 
845 870
     printf("Success\n");
846 871
     return 0;

0 notes on commit 3ecc1f3

Please sign in to comment.
Something went wrong with that request. Please try again.