Skip to content

Commit

Permalink
Merge pull request #885 from 9rnsr/fix7922
Browse files Browse the repository at this point in the history
Issue 7922 - alias this causes weird formatting issues for strings
  • Loading branch information
WalterBright committed May 21, 2012
2 parents ad84e0d + a235ba4 commit 3ecc1f3
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 30 deletions.
12 changes: 9 additions & 3 deletions src/declaration.c
Expand Up @@ -1386,11 +1386,17 @@ void VarDeclaration::semantic(Scope *sc)
e->op = TOKblit; e->op = TOKblit;
} }
e->type = t; e->type = t;
(*pinit) = new CommaExp(loc, e, (*pinit));


/* Replace __ctmp being constructed with e1 /* Replace __ctmp being constructed with e1.
* We need to copy constructor call expression,
* because it may be used in other place.
*/ */
dve->e1 = e1; DotVarExp *dvx = (DotVarExp *)dve->copy();
dvx->e1 = e1;
CallExp *cx = (CallExp *)ce->copy();
cx->e1 = dvx;

(*pinit) = new CommaExp(loc, e, cx);
(*pinit) = (*pinit)->semantic(sc); (*pinit) = (*pinit)->semantic(sc);
goto Ldtor; goto Ldtor;
} }
Expand Down
72 changes: 45 additions & 27 deletions src/opover.c
Expand Up @@ -355,9 +355,11 @@ Expression *UnaExp::op_overload(Scope *sc)
/* Rewrite op(e1) as: /* Rewrite op(e1) as:
* op(e1.aliasthis) * op(e1.aliasthis)
*/ */
UnaExp *e = (UnaExp *)syntaxCopy(); Expression *e1 = new DotIdExp(loc, this->e1, ad->aliasthis->ident);
e->e1 = new DotIdExp(loc, e->e1, ad->aliasthis->ident); Expression *e = copy();
return e->trySemantic(sc); ((UnaExp *)e)->e1 = e1;
e = e->trySemantic(sc);
return e;
} }
#endif #endif
} }
Expand Down Expand Up @@ -412,9 +414,11 @@ Expression *ArrayExp::op_overload(Scope *sc)
/* Rewrite op(e1) as: /* Rewrite op(e1) as:
* op(e1.aliasthis) * op(e1.aliasthis)
*/ */
UnaExp *e = (UnaExp *)syntaxCopy(); Expression *e1 = new DotIdExp(loc, this->e1, ad->aliasthis->ident);
e->e1 = new DotIdExp(loc, e->e1, ad->aliasthis->ident); Expression *e = copy();
return e->trySemantic(sc); ((UnaExp *)e)->e1 = e1;
e = e->trySemantic(sc);
return e;
} }
} }
return NULL; return NULL;
Expand Down Expand Up @@ -457,9 +461,11 @@ Expression *CastExp::op_overload(Scope *sc)
/* Rewrite op(e1) as: /* Rewrite op(e1) as:
* op(e1.aliasthis) * op(e1.aliasthis)
*/ */
UnaExp *e = (UnaExp *)syntaxCopy(); Expression *e1 = new DotIdExp(loc, this->e1, ad->aliasthis->ident);
e->e1 = new DotIdExp(loc, e->e1, ad->aliasthis->ident); Expression *e = copy();
return e->trySemantic(sc); ((UnaExp *)e)->e1 = e1;
e = e->trySemantic(sc);
return e;
} }
} }
return NULL; return NULL;
Expand Down Expand Up @@ -715,9 +721,11 @@ Expression *BinExp::op_overload(Scope *sc)
/* Rewrite (e1 op e2) as: /* Rewrite (e1 op e2) as:
* (e1.aliasthis op e2) * (e1.aliasthis op e2)
*/ */
BinExp *e = (BinExp *)syntaxCopy(); Expression *e1 = new DotIdExp(loc, this->e1, ad1->aliasthis->ident);
e->e1 = new DotIdExp(loc, e->e1, ad1->aliasthis->ident); Expression *e = copy();
return e->trySemantic(sc); ((BinExp *)e)->e1 = e1;
e = e->trySemantic(sc);
return e;
} }


// Try alias this on second operand // Try alias this on second operand
Expand All @@ -730,9 +738,11 @@ Expression *BinExp::op_overload(Scope *sc)
/* Rewrite (e1 op e2) as: /* Rewrite (e1 op e2) as:
* (e1 op e2.aliasthis) * (e1 op e2.aliasthis)
*/ */
BinExp *e = (BinExp *)syntaxCopy(); Expression *e2 = new DotIdExp(loc, this->e2, ad2->aliasthis->ident);
e->e2 = new DotIdExp(loc, e->e2, ad2->aliasthis->ident); Expression *e = copy();
return e->trySemantic(sc); ((BinExp *)e)->e2 = e2;
e = e->trySemantic(sc);
return e;
} }
#endif #endif
return NULL; return NULL;
Expand Down Expand Up @@ -884,9 +894,11 @@ Expression *BinExp::compare_overload(Scope *sc, Identifier *id)
/* Rewrite (e1 op e2) as: /* Rewrite (e1 op e2) as:
* (e1.aliasthis op e2) * (e1.aliasthis op e2)
*/ */
BinExp *e = (BinExp *)syntaxCopy(); Expression *e1 = new DotIdExp(loc, this->e1, ad1->aliasthis->ident);
e->e1 = new DotIdExp(loc, e->e1, ad1->aliasthis->ident); Expression *e = copy();
return e->trySemantic(sc); ((BinExp *)e)->e1 = e1;
e = e->trySemantic(sc);
return e;
} }


// Try alias this on second operand // Try alias this on second operand
Expand All @@ -895,9 +907,11 @@ Expression *BinExp::compare_overload(Scope *sc, Identifier *id)
/* Rewrite (e1 op e2) as: /* Rewrite (e1 op e2) as:
* (e1 op e2.aliasthis) * (e1 op e2.aliasthis)
*/ */
BinExp *e = (BinExp *)syntaxCopy(); Expression *e2 = new DotIdExp(loc, this->e2, ad2->aliasthis->ident);
e->e2 = new DotIdExp(loc, e->e2, ad2->aliasthis->ident); Expression *e = copy();
return e->trySemantic(sc); ((BinExp *)e)->e2 = e2;
e = e->trySemantic(sc);
return e;
} }


return NULL; return NULL;
Expand Down Expand Up @@ -1132,9 +1146,11 @@ Expression *BinAssignExp::op_overload(Scope *sc)
/* Rewrite (e1 op e2) as: /* Rewrite (e1 op e2) as:
* (e1.aliasthis op e2) * (e1.aliasthis op e2)
*/ */
BinExp *e = (BinExp *)syntaxCopy(); Expression *e1 = new DotIdExp(loc, this->e1, ad1->aliasthis->ident);
e->e1 = new DotIdExp(loc, e->e1, ad1->aliasthis->ident); Expression *e = copy();
return e->trySemantic(sc); ((BinExp *)e)->e1 = e1;
e = e->trySemantic(sc);
return e;
} }


// Try alias this on second operand // Try alias this on second operand
Expand All @@ -1144,9 +1160,11 @@ Expression *BinAssignExp::op_overload(Scope *sc)
/* Rewrite (e1 op e2) as: /* Rewrite (e1 op e2) as:
* (e1 op e2.aliasthis) * (e1 op e2.aliasthis)
*/ */
BinExp *e = (BinExp *)syntaxCopy(); Expression *e2 = new DotIdExp(loc, this->e2, ad2->aliasthis->ident);
e->e2 = new DotIdExp(loc, e->e2, ad2->aliasthis->ident); Expression *e = copy();
return e->trySemantic(sc); ((BinExp *)e)->e2 = e2;
e = e->trySemantic(sc);
return e;
} }
#endif #endif
return NULL; return NULL;
Expand Down
25 changes: 25 additions & 0 deletions test/runnable/aliasthis.d
@@ -1,5 +1,6 @@


extern (C) int printf(const(char*) fmt, ...); extern (C) int printf(const(char*) fmt, ...);
import core.vararg;


struct Tup(T...) struct Tup(T...)
{ {
Expand Down Expand Up @@ -809,6 +810,29 @@ void test7945()
s.v.foo7945(); // 4.OK, ufcs s.v.foo7945(); // 4.OK, ufcs
} }


/***************************************************/
// 7992

struct S7992
{
int[] arr;
alias arr this;
}
S7992 func7992(...)
{
S7992 ret;
ret.arr.length = _arguments.length;
return ret;
}
void test7992()
{
int[] arr;
assert(arr.length == 0);
arr ~= func7992(1, 2); //NG
//arr = func7992(1, 2); //OK
assert(arr.length == 2);
}

/***************************************************/ /***************************************************/


int main() int main()
Expand Down Expand Up @@ -841,6 +865,7 @@ int main()
test7731(); test7731();
test7808(); test7808();
test7945(); test7945();
test7992();


printf("Success\n"); printf("Success\n");
return 0; return 0;
Expand Down

0 comments on commit 3ecc1f3

Please sign in to comment.