support casts in initializers

rework of #710, reuses the existing type construction

Co-authored-by: ryuukk <ryuukk.dev@gmail.com>
This commit is contained in:
WebFreak001 2023-12-04 11:54:11 +01:00 committed by Jan Jurzitza
parent fe6ce04720
commit dcffd378e1
5 changed files with 65 additions and 2 deletions

View File

@ -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);
@ -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;
@ -1599,6 +1619,7 @@ class InitializerVisitor : ASTVisitor
bool on = false;
const bool appendForeach;
FirstPass fp;
bool isCast;
}
class ArgumentListVisitor : ASTVisitor

View File

@ -0,0 +1,8 @@
identifiers
alignof k
init k
inside_c v
mangleof k
sizeof k
stringof k
tupleof k

View File

26
tests/tc_casts/file.d Normal file
View File

@ -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
tests/tc_casts/run.sh Executable file
View File

@ -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