Skip to content

Commit 96e351f

Browse files
authored
Fix #22983 - Named argument allowed in int(x: 3) (#22984)
1 parent 8ef36a3 commit 96e351f

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

compiler/src/dmd/expressionsem.d

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7117,7 +7117,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
71177117
{
71187118
if (exp.names && (*exp.names)[0].name)
71197119
{
7120-
error(exp.loc, "no named argument `%s` allowed for scalar", (*exp.names)[0].name.toErrMsg());
7120+
error(exp.loc, "no named argument `%s` allowed for basic type", (*exp.names)[0].name.toErrMsg());
71217121
return setError();
71227122
}
71237123
Expression e = (*exp.arguments)[0];
@@ -7965,6 +7965,11 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
79657965
}
79667966
else if (exp.arguments.length == 1)
79677967
{
7968+
if (exp.names && (*exp.names)[0].name)
7969+
{
7970+
error(exp.loc, "no named argument `%s` allowed for basic type", (*exp.names)[0].name.toErrMsg());
7971+
return setError();
7972+
}
79687973
e = (*exp.arguments)[0];
79697974
e = e.implicitCastTo(sc, t1);
79707975
e = new CastExp(exp.loc, e, t1);

compiler/src/dmd/parse.d

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8492,7 +8492,10 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
84928492
if (token.value == TOK.leftParenthesis)
84938493
{
84948494
e = new AST.TypeExp(loc, t);
8495-
e = new AST.CallExp(loc, e, parseArguments());
8495+
auto args = new AST.Expressions();
8496+
auto names = new AST.ArgumentLabels();
8497+
parseNamedArguments(args, names);
8498+
e = new AST.CallExp(loc, e, args, names);
84968499
break;
84978500
}
84988501
check(TOK.dot);

compiler/test/fail_compilation/named_arguments_error.d

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ fail_compilation/named_arguments_error.d(42): Error: function `g` is not callabl
1717
fail_compilation/named_arguments_error.d(42): missing argument for parameter #1: `int x`
1818
fail_compilation/named_arguments_error.d(34): `named_arguments_error.g(int x, int y, int z = 3)` declared here
1919
fail_compilation/named_arguments_error.d(44): Error: no named argument `element` allowed for array dimension
20-
fail_compilation/named_arguments_error.d(45): Error: no named argument `number` allowed for scalar
20+
fail_compilation/named_arguments_error.d(45): Error: no named argument `number` allowed for basic type
2121
fail_compilation/named_arguments_error.d(46): Error: cannot implicitly convert expression `g(3, 4, 5)` of type `int` to `string`
2222
fail_compilation/named_arguments_error.d(47): Error: template `tempfun` is not callable using argument types `!()(int, int)`
2323
fail_compilation/named_arguments_error.d(47): argument `1` goes past end of parameter list
2424
fail_compilation/named_arguments_error.d(50): Candidate is: `tempfun(T, U)(T t, U u)`
25+
fail_compilation/named_arguments_error.d(59): Error: no named argument `a` allowed for basic type
26+
fail_compilation/named_arguments_error.d(60): Error: no named argument `bar` allowed for basic type
2527
---
2628
*/
2729

2830

2931

30-
31-
3232
void f(int x, int y, int z);
3333

3434
int g(int x, int y, int z = 3);
@@ -51,3 +51,11 @@ int tempfun(T, U)(T t, U u)
5151
{
5252
return 3;
5353
}
54+
55+
// https://github.com/dlang/dmd/issues/22980
56+
void test22980()
57+
{
58+
alias T = int;
59+
auto x = T(a: 3);
60+
auto y = int(bar: 2);
61+
}

0 commit comments

Comments
 (0)