diff --git a/src/dtemplate.d b/src/dtemplate.d index 63be2ec6a9ee..f2a6030a64f4 100644 --- a/src/dtemplate.d +++ b/src/dtemplate.d @@ -6267,19 +6267,22 @@ public: /* Template functions may have different instantiations based on * "auto ref" parameters. */ - if (fargs) + if (auto fd = ti.toAlias().isFuncDeclaration()) { - FuncDeclaration fd = ti.toAlias().isFuncDeclaration(); - if (fd && !fd.errors) + if (!fd.errors) { - Parameters* fparameters = fd.getParameters(null); - size_t nfparams = Parameter.dim(fparameters); // Num function parameters - for (size_t j = 0; j < nfparams && j < fargs.dim; j++) + auto fparameters = fd.getParameters(null); + size_t nfparams = Parameter.dim(fparameters); // Num function parameters + for (size_t j = 0; j < nfparams; j++) { Parameter fparam = Parameter.getNth(fparameters, j); - Expression farg = (*fargs)[j]; - if (fparam.storageClass & STCautoref) // if "auto ref" + if (fparam.storageClass & STCautoref) // if "auto ref" { + if (!fargs) + goto Lnotequals; + if (fargs.dim <= j) + break; + Expression farg = (*fargs)[j]; if (farg.isLvalue()) { if (!(fparam.storageClass & STCref)) diff --git a/test/fail_compilation/fail14669.d b/test/fail_compilation/fail14669.d index 1580ed645ddb..4cbfca6d5693 100644 --- a/test/fail_compilation/fail14669.d +++ b/test/fail_compilation/fail14669.d @@ -16,3 +16,28 @@ void test1() alias f1 = foo1!(); foo2(1); } + +/* +TEST_OUTPUT: +--- +fail_compilation/fail14669.d(29): Error: 'auto' can only be used as the part of 'auto ref' for template function parameters +fail_compilation/fail14669.d(38): Error: template instance fail14669.bar1!int error instantiating +fail_compilation/fail14669.d(30): Error: 'auto' can only be used as the part of 'auto ref' for template function parameters +fail_compilation/fail14669.d(40): Error: template instance fail14669.bar2!int error instantiating +--- +*/ +void bar1(T)(auto ref T x) {} +void bar2(T)(auto ref T x) {} + +void test2() +{ + int n; + + bar1(1); + bar1(n); + alias b1 = bar1!(int); + + alias b2 = bar2!(int); + bar2(n); + bar2(1); +}