Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion compiler/src/dmd/expression.d
Original file line number Diff line number Diff line change
Expand Up @@ -3369,6 +3369,17 @@ extern (C++) final class StructLiteralExp : Expression
return Expression.toLvalue(sc, e);
}

override Expression resolveLoc(const ref Loc loc, Scope* sc)
{
for (int i = 0; i < (*elements).length; i++)
{
auto elem = (*elements)[i];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foreach (ref elem; (*elements)[])
if (elem)
elem = elem.resolveLoc(loc, sc);

if (elem)
(*elements)[i] = elem.resolveLoc(loc, sc);
}
return this;
}

override void accept(Visitor v)
{
v.visit(this);
Expand Down Expand Up @@ -4282,7 +4293,7 @@ extern (C++) abstract class UnaExp : Expression

}

override final Expression resolveLoc(const ref Loc loc, Scope* sc)
override Expression resolveLoc(const ref Loc loc, Scope* sc)
{
e1 = e1.resolveLoc(loc, sc);
return this;
Expand Down Expand Up @@ -5164,6 +5175,16 @@ extern (C++) final class CallExp : UnaExp
return this;
}

override Expression resolveLoc(ref const Loc loc, Scope* sc)
{
for (int i = 0; i < arguments.length; i++)
{
auto arg = (*arguments)[i];
if (arg)
(*arguments)[i] = arg.resolveLoc(loc, sc);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

return this;
}
override void accept(Visitor v)
{
v.visit(this);
Expand Down
9 changes: 0 additions & 9 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -1789,15 +1789,6 @@ private bool functionParameters(const ref Loc loc, Scope* sc,
arguments.push(arg);
nargs++;
}
else
{
if (isDefaultInitOp(arg.op))
{
arg = arg.resolveLoc(loc, sc);
(*arguments)[i] = arg;
}
}


if (tf.parameterList.varargs == VarArg.typesafe && i + 1 == nparams) // https://dlang.org/spec/function.html#variadic
{
Expand Down
4 changes: 3 additions & 1 deletion compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -6937,6 +6937,7 @@ class StructLiteralExp final : public Expression
int32_t getFieldIndex(Type* type, uint32_t offset);
Expression* addDtorHook(Scope* sc) override;
Expression* toLvalue(Scope* sc, Expression* e) override;
Expression* resolveLoc(const Loc& loc, Scope* sc) override;
void accept(Visitor* v) override;
};

Expand Down Expand Up @@ -7108,7 +7109,7 @@ class UnaExp : public Expression
UnaExp* syntaxCopy() override;
Expression* incompatibleTypes();
void setNoderefOperand();
Expression* resolveLoc(const Loc& loc, Scope* sc) final override;
Expression* resolveLoc(const Loc& loc, Scope* sc) override;
void accept(Visitor* v) override;
};

Expand Down Expand Up @@ -7240,6 +7241,7 @@ class CallExp final : public UnaExp
bool isLvalue() override;
Expression* toLvalue(Scope* sc, Expression* e) override;
Expression* addDtorHook(Scope* sc) override;
Expression* resolveLoc(const Loc& loc, Scope* sc) override;
void accept(Visitor* v) override;
};

Expand Down
2 changes: 1 addition & 1 deletion compiler/test/runnable/test18916.d
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct Line

void foo(Line line1 = __LINE__, int line2 = __LINE__, int line3 = int(__LINE__))
{
assert(line1 == 12);
assert(line1 == 21);
assert(line2 == 21);
assert(line3 == 12);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can also be modified to print 21, however, according to @WalterBright in this comment this sort of construction is used to retain the C++ behavior of LINE

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also have bugs about __FUNCTION__.ptr not resolving correctly so I don't think that's a good argument.

}
Expand Down
27 changes: 27 additions & 0 deletions compiler/test/runnable/testFUNCTION.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// https://issues.dlang.org/show_bug.cgi?id=23403
struct Context
{
string pretty_function;
}

void test23403(Context ctx = Context(__FUNCTION__))
{
assert(ctx.pretty_function == "testFUNCTION.main");
}

// https://issues.dlang.org/show_bug.cgi?id=23408
string foo(string arg)
{
return arg;
}

void test23408(string s = foo(__FUNCTION__))
{
assert(s == "testFUNCTION.main");
}

void main()
{
test23403();
test23408();
}