Skip to content

Commit

Permalink
fix Issue 15757 - D main is a nested function and cannot be accessed
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Mar 28, 2016
1 parent 39d6bfa commit 35936ea
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 19 deletions.
16 changes: 8 additions & 8 deletions src/e2ir.c
Expand Up @@ -53,7 +53,7 @@ unsigned totym(Type *tx);
Symbol *toSymbol(Dsymbol *s);
elem *toElem(Expression *e, IRState *irs);
elem *toElemDtor(Expression *e, IRState *irs);
elem *toElemStructLit(StructLiteralExp *sle, IRState *irs, Symbol *sym, bool fillHoles);
elem *toElemStructLit(StructLiteralExp *sle, IRState *irs, TOK op, Symbol *sym, bool fillHoles);
dt_t **Expression_toDt(Expression *e, dt_t **pdt);
Symbol *toStringSymbol(const char *str, size_t len, size_t sz);
Symbol *toStringSymbol(StringExp *se);
Expand Down Expand Up @@ -1640,7 +1640,7 @@ elem *toElem(Expression *e, IRState *irs)
else
{
StructLiteralExp *sle = StructLiteralExp::create(ne->loc, sd, ne->arguments, t);
ez = toElemStructLit(sle, irs, ev->EV.sp.Vsym, false);
ez = toElemStructLit(sle, irs, TOKconstruct, ev->EV.sp.Vsym, false);
}
//elem_print(ex);
//elem_print(ey);
Expand Down Expand Up @@ -2821,10 +2821,10 @@ elem *toElem(Expression *e, IRState *irs)
ex = e1->E1;
if (ae->e2->op == TOKstructliteral &&
ex->Eoper == OPvar && ex->EV.sp.Voffset == 0 &&
ae->op == TOKconstruct)
(ae->op == TOKconstruct || ae->op == TOKblit))
{
StructLiteralExp *sle = (StructLiteralExp *)ae->e2;
e = toElemStructLit(sle, irs, ex->EV.sp.Vsym, true);
e = toElemStructLit(sle, irs, ae->op, ex->EV.sp.Vsym, true);
el_free(e1);
goto Lret;
}
Expand Down Expand Up @@ -5258,7 +5258,7 @@ elem *toElem(Expression *e, IRState *irs)
void visit(StructLiteralExp *sle)
{
//printf("[%s] StructLiteralExp::toElem() %s\n", sle->loc.toChars(), sle->toChars());
result = toElemStructLit(sle, irs, sle->sym, true);
result = toElemStructLit(sle, irs, TOKconstruct, sle->sym, true);
}

/*****************************************************/
Expand Down Expand Up @@ -5326,10 +5326,10 @@ elem *fillHole(Symbol *stmp, size_t *poffset, size_t offset2, size_t maxoff)
return e;
}

elem *toElemStructLit(StructLiteralExp *sle, IRState *irs, Symbol *sym, bool fillHoles)
elem *toElemStructLit(StructLiteralExp *sle, IRState *irs, TOK op, Symbol *sym, bool fillHoles)
{
//printf("[%s] StructLiteralExp::toElem() %s\n", sle->loc.toChars(), sle->toChars());
//printf("\tsym = %p fillHoles = %d\n", sym, fillHoles);
//printf("\tblit = %s, sym = %p fillHoles = %d\n", op == TOKblit, sym, fillHoles);

if (sle->useStaticInit)
{
Expand Down Expand Up @@ -5485,7 +5485,7 @@ elem *toElemStructLit(StructLiteralExp *sle, IRState *irs, Symbol *sym, bool fil
else
{
elem *edim = el_long(TYsize_t, t1b->size() / t2b->size());
e1 = setArray(e1, edim, t2b, ep, irs, TOKconstruct);
e1 = setArray(e1, edim, t2b, ep, irs, op);
}
}
else
Expand Down
34 changes: 24 additions & 10 deletions src/expression.d
Expand Up @@ -9396,20 +9396,23 @@ public:
{
if (t1.ty == Tstruct)
{
StructDeclaration sd = (cast(TypeStruct)t1).sym;
auto sd = (cast(TypeStruct)t1).sym;
sd.size(loc); // Resolve forward references to construct object
if (sd.sizeok != SIZEOKdone)
return new ErrorExp();

// First look for constructor
if (e1.op == TOKtype && sd.ctor)
{
if (!sd.noDefaultCtor && !(arguments && arguments.dim))
goto Lx;

auto sle = new StructLiteralExp(loc, sd, null, e1.type);
if (!sd.fill(loc, sle.elements, true))
return new ErrorExp();
if (checkFrameAccess(loc, sc, sd, sle.elements.dim))
return new ErrorExp();

// Bugzilla 14556: Set concrete type to avoid further redundant semantic().
sle.type = e1.type;

Expand Down Expand Up @@ -12347,9 +12350,9 @@ public:
}
else if (t1.ty == Tstruct)
{
Expression e1x = e1;
Expression e2x = e2;
StructDeclaration sd = (cast(TypeStruct)t1).sym;
auto e1x = e1;
auto e2x = e2;
auto sd = (cast(TypeStruct)t1).sym;

if (op == TOKconstruct)
{
Expand Down Expand Up @@ -12377,26 +12380,37 @@ public:
* variable with a bit copy of the default
* initializer
*/
AssignExp ae = this;
Expression einit;
if (sd.zeroInit == 1 && !sd.isNested())
{
// Bugzilla 14606: Always use BlitExp for the special expression: (struct = 0)
ae = new BlitExp(ae.loc, ae.e1, new IntegerExp(loc, 0, Type.tint32));
einit = new IntegerExp(loc, 0, Type.tint32);
}
else if (sd.isNested())
{
auto sle = new StructLiteralExp(loc, sd, null, t1);
if (!sd.fill(loc, sle.elements, true))
return new ErrorExp();
if (checkFrameAccess(loc, sc, sd, sle.elements.dim))
return new ErrorExp();
sle.type = t1;

einit = sle;
}
else
{
// Keep ae->op == TOKconstruct
ae.e2 = sd.isNested() ? t1.defaultInitLiteral(loc) : t1.defaultInit(loc);
einit = t1.defaultInit(loc);
}
auto ae = new BlitExp(loc, e1, einit);
ae.type = e1x.type;

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

Expression e0;
Expand Down
1 change: 0 additions & 1 deletion src/s2ir.c
Expand Up @@ -44,7 +44,6 @@ elem *exp2_copytotemp(elem *e);
elem *incUsageElem(IRState *irs, Loc loc);
elem *addressElem(elem *e, Type *t, bool alwaysCopy = false);
type *Type_toCtype(Type *t);
elem *toElemStructLit(StructLiteralExp *sle, IRState *irs, Symbol *sym, bool fillHoles);
elem *toElemDtor(Expression *e, IRState *irs);
Symbol *toSymbol(Type *t);
Symbol *toSymbolCpp(ClassDeclaration *cd);
Expand Down
41 changes: 41 additions & 0 deletions test/runnable/nested.d
Expand Up @@ -2663,6 +2663,46 @@ void test15422b()
}
}

/***************************************************/
// 15757

template map15757(fun...)
{
auto map15757(R)(R r)
{
return MapResult15757!(fun, R)(r);
}
}

struct MapResult15757(alias fun, R)
{
R _input;

this(R input)
{
_input = input;
}
}

void wrap15757(R)(R r)
{
struct M(R)
{
this(R r)
{
payload = r;
}
R payload;
}

M!R m = M!R(r);
}

void test15757() @safe
{
[1,2,3].map15757!(x => x*x).wrap15757;
}

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

int main()
Expand Down Expand Up @@ -2758,6 +2798,7 @@ int main()
test14846();
test15422a();
test15422b();
test15757();

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

0 comments on commit 35936ea

Please sign in to comment.