Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Base(child) class object upcast #16426

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
14 changes: 12 additions & 2 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -6133,7 +6133,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
}
// No constructor, look for overload of opCall
if (search_function(sd, Id.call))
goto L1;
goto LopCall;
// overload of opCall, therefore it's a call
if (exp.e1.op != EXP.type)
{
Expand Down Expand Up @@ -6171,7 +6171,17 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
}
else if (t1.ty == Tclass)
{
L1:
// upcast `Base(child)`
if (exp.e1.op == EXP.type && exp.arguments.length == 1 &&
(*exp.arguments)[0].type.implicitConvTo(t1) &&
!t1.isTypeClass().sym.symtab.lookup(Id.call))
{
Expression e = new CastExp(exp.loc, (*exp.arguments)[0], t1);
e = e.expressionSemantic(sc);
result = e;
return;
}
LopCall:
// Rewrite as e1.call(arguments)
Expression e = new DotIdExp(exp.loc, exp.e1, Id.call);
e = new CallExp(exp.loc, e, exp.arguments, exp.names);
Expand Down
3 changes: 0 additions & 3 deletions compiler/test/compilable/b16346.d

This file was deleted.

10 changes: 10 additions & 0 deletions compiler/test/compilable/type_call_expr.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
enum A { B }
static assert(is(typeof(A.B) == A));
static assert(is(typeof(A(A.B)) == A));

void main()
{
Exception ex;
auto o = Object(ex);
static assert(is(typeof(o) == Object));
}
Loading