Skip to content

Commit

Permalink
support casts in initializers
Browse files Browse the repository at this point in the history
rework of #710, reuses the existing type construction

Co-authored-by: ryuukk <[email protected]>
  • Loading branch information
WebFreak001 and ryuukk committed Dec 4, 2023
1 parent fe6ce04 commit dcffd37
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
25 changes: 23 additions & 2 deletions dsymbol/src/dsymbol/conversion/first.d
Original file line number Diff line number Diff line change
Expand Up @@ -1133,9 +1133,10 @@ private:
auto lookup = l ? l : TypeLookupsAllocator.instance.make!TypeLookup(TypeLookupKind.varOrFunType);

lookup.breadcrumbs.insert(TYPEOF_SYMBOL_NAME);
scope (exit)
lookup.breadcrumbs.insert(TYPEOF_END_SYMBOL_NAME);
scope visitor = new InitializerVisitor(lookup, appendForeach, this);
scope (exit)
if (!visitor.isCast)
lookup.breadcrumbs.insert(TYPEOF_END_SYMBOL_NAME);

if (l is null)
lookups.insert(lookup);
Expand Down Expand Up @@ -1586,6 +1587,25 @@ class InitializerVisitor : ASTVisitor
on = false;
}

override void visit(const CastExpression expression)
{
if (expression.type)
{
if (lookup.breadcrumbs.empty || lookup.breadcrumbs.back != TYPEOF_SYMBOL_NAME)
return;

isCast = true;
lookup.breadcrumbs.popBack();
TypeLookups none;
fp.addTypeToLookups(none, expression.type, lookup);
}
else
{
// we don't care about non-type casts (e.g. `cast()` or `cast(const)`) yet
expression.accept(this);
}
}

override void dynamicDispatch(const ExpressionNode expression)
{
on = true;
Expand All @@ -1599,6 +1619,7 @@ class InitializerVisitor : ASTVisitor
bool on = false;
const bool appendForeach;
FirstPass fp;
bool isCast;
}

class ArgumentListVisitor : ASTVisitor
Expand Down
8 changes: 8 additions & 0 deletions tests/tc_casts/expected1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
identifiers
alignof k
init k
inside_c v
mangleof k
sizeof k
stringof k
tupleof k
Empty file added tests/tc_casts/expected2.txt
Empty file.
26 changes: 26 additions & 0 deletions tests/tc_casts/file.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
struct A
{
struct B
{
struct C
{
int inside_c;
}
int inside_b;
}
int inside_a;
}

unittest
{
auto from_cast = cast(A.B.C) nonExist;
from_cast.
}

unittest
{
struct A {}

auto from_cast = cast(A.B.C) nonExist;
from_cast.
}
8 changes: 8 additions & 0 deletions tests/tc_casts/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
set -e
set -u

../../bin/dcd-client $1 file.d -c159 > actual1.txt
diff actual1.txt expected1.txt --strip-trailing-cr

../../bin/dcd-client $1 file.d -c239 > actual2.txt
diff actual2.txt expected2.txt --strip-trailing-cr

0 comments on commit dcffd37

Please sign in to comment.