Skip to content
Open
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
50 changes: 43 additions & 7 deletions compiler/src/dmd/templatesem.d
Original file line number Diff line number Diff line change
Expand Up @@ -7598,7 +7598,39 @@ MATCH deduceType(scope RootObject o, scope Scope* sc, scope Type tparam,
//printf("\ttf = %s\n", tf.toChars());
const dim = tf.parameterList.length;

if (tof.parameterList.length != dim || tof.parameterList.varargs != tf.parameterList.varargs)
if (tof.parameterList.varargs != tf.parameterList.varargs)
return;

// https://github.com/dlang/dmd/issues/19905
// Resolve and expand TypeTuples (e.g. from AliasSeq) in tof's parameters
// before comparing counts. We can't call TypeFunction.typeSemantic because
// the return type may contain unresolved template parameters.
Types* expandedTypes;
foreach (pto; *tof.parameterList.parameters)
{
Type pt = pto.type;
if (!reliesOnTemplateParameters(pt, parameters[inferStart .. parameters.length]))
{
pt = pt.syntaxCopy().typeSemantic(e.loc, sc);
Copy link
Contributor

Choose a reason for hiding this comment

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

Something about that syntaxCopy call doesn't seem necessary.

Needs further review.

Copy link
Member Author

Choose a reason for hiding this comment

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

Claude says:

syntaxCopy is needed because typeSemantic may modify the type in-place
(setting deco), and we don't want to contaminate the template's
parameter list for future instantiations.

if (pt.ty == Terror)
return;
}
if (auto tt = pt.isTypeTuple())
{
if (!expandedTypes)
expandedTypes = new Types();
foreach (arg; *tt.arguments)
expandedTypes.push(arg.type);
}
else
{
if (!expandedTypes)
expandedTypes = new Types();
expandedTypes.push(pt);
}
}

if (!expandedTypes || expandedTypes.length != dim)
return;

auto tiargs = new Objects();
Expand All @@ -7614,15 +7646,19 @@ MATCH deduceType(scope RootObject o, scope Scope* sc, scope Type tparam,
++u;
}
assert(u < dim);
Parameter pto = tof.parameterList[u];
if (!pto)
Type t = (*expandedTypes)[u];
if (!t)
break;
Type t = pto.type.syntaxCopy(); // https://issues.dlang.org/show_bug.cgi?id=11774
if (reliesOnTemplateParameters(t, parameters[inferStart .. parameters.length]))
return;
t = t.typeSemantic(e.loc, sc);
if (t.ty == Terror)
return;
// https://issues.dlang.org/show_bug.cgi?id=11774
t = t.syntaxCopy();
if (!t.deco)
{
t = t.typeSemantic(e.loc, sc);
if (t.ty == Terror)
return;
}
tiargs.push(t);
}

Expand Down
29 changes: 29 additions & 0 deletions compiler/test/compilable/test19905.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// https://github.com/dlang/dmd/issues/19905
// Type list (tuple) not expanded in delegate during IFTI

alias AliasSeq(TList...) = TList;

struct S
{
void m() {}
}

void fun1(R)(R delegate(AliasSeq!S) dg) {}
void fun2(R)(R delegate(AliasSeq!(S, S)) dg) {}
void fun3(R)(R delegate(AliasSeq!(S, S, S)) dg) {}
void fun0(R)(R delegate(AliasSeq!()) dg) {}

void main()
{
// Explicit template argument always worked
fun1!void((a) { a.m(); });
fun2!void((a, b) { a.m(); b.m(); });
fun3!void((a, b, c) { a.m(); b.m(); c.m(); });
fun0!void(() {});

// IFTI with tuple expansion - was broken, now fixed
fun1((a) { a.m(); });
fun2((a, b) { a.m(); b.m(); });
fun3((a, b, c) { a.m(); b.m(); c.m(); });
fun0(() {});
}
Loading