Skip to content

Commit 0fd0acd

Browse files
authored
fix for wrong location for incompatible argument error (#22370)
1 parent 6b45edc commit 0fd0acd

File tree

6 files changed

+41
-12
lines changed

6 files changed

+41
-12
lines changed

compiler/src/dmd/expressionsem.d

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8251,7 +8251,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
82518251
return setError();
82528252
}
82538253

8254-
void errorHelper(const(char)* failMessage) scope
8254+
void errorHelper(const(char)* failMessage, Loc argloc) scope
82558255
{
82568256
OutBuffer buf;
82578257
buf.writeByte('(');
@@ -8264,7 +8264,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
82648264
.error(exp.loc, "%s `%s` is not callable using argument types `%s`",
82658265
p, exp.e1.toErrMsg(), buf.peekChars());
82668266
if (failMessage)
8267-
errorSupplemental(exp.loc, "%s", failMessage);
8267+
errorSupplemental((argloc !is Loc.initial) ? argloc : exp.loc, "%s", failMessage);
82688268
}
82698269

82708270
if (callMatch(exp.f, tf, null, exp.argumentList, 0, &errorHelper, sc) == MATCH.nomatch)
@@ -8327,7 +8327,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
83278327
exp.f = exp.f.toAliasFunc();
83288328
TypeFunction tf = cast(TypeFunction)exp.f.type;
83298329

8330-
void errorHelper2(const(char)* failMessage) scope
8330+
void errorHelper2(const(char)* failMessage, Loc argloc) scope
83318331
{
83328332
OutBuffer buf;
83338333
buf.writeByte('(');
@@ -8345,7 +8345,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
83458345
.error(exp.loc, "%s `%s` is not callable using argument types `%s`",
83468346
exp.f.kind(), exp.f.toErrMsg(), buf.peekChars());
83478347
if (failMessage)
8348-
errorSupplemental(exp.loc, "%s", failMessage);
8348+
errorSupplemental((argloc !is Loc.initial) ? argloc : exp.loc, "%s", failMessage);
83498349
.errorSupplemental(exp.f.loc, "`%s%s` declared here", exp.f.toPrettyChars(), parametersTypeToChars(tf.parameterList));
83508350
exp.f = null;
83518351
}

compiler/src/dmd/funcsem.d

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,12 +2096,12 @@ FuncDeclaration resolveFuncCall(Loc loc, Scope* sc, Dsymbol s,
20962096
}
20972097

20982098
bool calledHelper;
2099-
void errorHelper(const(char)* failMessage) scope
2099+
void errorHelper(const(char)* failMessage, Loc argloc = Loc.initial) scope
21002100
{
21012101
.error(loc, "%s `%s%s%s` is not callable using argument types `%s`",
21022102
fd.kind(), fd.toPrettyChars(), parametersTypeToChars(tf.parameterList),
21032103
tf.modToChars(), fargsBuf.peekChars());
2104-
errorSupplemental(loc, failMessage);
2104+
errorSupplemental((argloc !is Loc.initial) ? argloc : loc, failMessage);
21052105
calledHelper = true;
21062106
}
21072107

@@ -2169,9 +2169,9 @@ FuncDeclaration resolveFuncCall(Loc loc, Scope* sc, Dsymbol s,
21692169
}
21702170
}
21712171

2172-
void errorHelper2(const(char)* failMessage) scope
2172+
void errorHelper2(const(char)* failMessage, Loc argloc = Loc.initial) scope
21732173
{
2174-
errorSupplemental(loc, failMessage);
2174+
errorSupplemental((argloc !is Loc.initial) ? argloc : loc, failMessage);
21752175
}
21762176

21772177
functionResolve(m, orig_s, loc, sc, tiargs, tthis, argumentList, &errorHelper2);

compiler/src/dmd/templatesem.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5569,7 +5569,7 @@ bool TemplateInstance_semanticTiargs(Loc loc, Scope* sc, Objects* tiargs, int fl
55695569
* errorHelper = delegate to send error message to if not null
55705570
*/
55715571
void functionResolve(ref MatchAccumulator m, Dsymbol dstart, Loc loc, Scope* sc, Objects* tiargs,
5572-
Type tthis, ArgumentList argumentList, void delegate(const(char)*) scope errorHelper = null)
5572+
Type tthis, ArgumentList argumentList, void delegate(const(char)*, Loc argloc = Loc.initial) scope errorHelper = null)
55735573
{
55745574
version (none)
55755575
{

compiler/src/dmd/typesem.d

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,7 +2165,7 @@ extern(D) Expressions* resolveNamedArgs(TypeFunction tf, ArgumentList argumentLi
21652165
* MATCHxxxx
21662166
*/
21672167
extern (D) MATCH callMatch(FuncDeclaration fd, TypeFunction tf, Type tthis, ArgumentList argumentList,
2168-
int flag = 0, void delegate(const(char)*) scope errorHelper = null, Scope* sc = null)
2168+
int flag = 0, void delegate(const(char)*, Loc argloc = Loc.initial) scope errorHelper = null, Scope* sc = null)
21692169
{
21702170
//printf("callMatch() fd: %s, tf: %s\n", fd ? fd.ident.toChars() : "null", toChars(tf));
21712171
MATCH match = MATCH.exact; // assume exact match
@@ -2330,12 +2330,20 @@ extern (D) MATCH callMatch(FuncDeclaration fd, TypeFunction tf, Type tthis, Argu
23302330
if (errorHelper)
23312331
{
23322332
if (u >= args.length)
2333+
{
23332334
getMatchError(buf, "missing argument for parameter #%d: `%s`",
23342335
u + 1, parameterToChars(p, tf, false));
2336+
}
23352337
// If an error happened previously, `pMessage` was already filled
23362338
else if (buf.length == 0)
2339+
{
23372340
buf.writestring(tf.getParamError(args[u], p));
2338-
2341+
if(args[u].loc !is Loc.initial)
2342+
{
2343+
errorHelper(buf.peekChars(),args[u].loc);
2344+
return MATCH.nomatch;
2345+
}
2346+
}
23392347
errorHelper(buf.peekChars());
23402348
}
23412349
return MATCH.nomatch;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
TEST_OUTPUT:
3+
---
4+
fail_compilation\diag21167.d(15): Error: function `f` is not callable using argument types `(int, string, int)`
5+
fail_compilation\diag21167.d(17): cannot pass argument `"foo"` of type `string` to parameter `int __param_1`
6+
fail_compilation\diag21167.d(11): `diag21167.f(int __param_0, int __param_1, int __param_2)` declared here
7+
---
8+
*/
9+
// https://github.com/dlang/dmd/issues/21167
10+
11+
void f(int, int, int){}
12+
13+
void main()
14+
{
15+
f(
16+
1,
17+
"foo",
18+
3
19+
);
20+
21+
}

compiler/test/fail_compilation/test21634.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fail_compilation/test21634.d(22): Error: function literal `(int x) { }` is not c
77
^
88
fail_compilation/test21634.d(22): cannot pass argument `"%s"` of type `string` to parameter `int x`
99
(int x) {} ("%s");
10-
^
10+
^
1111
fail_compilation/test21634.d(24): Error: declaration `test21634.main.foo` is already defined
1212
int foo;
1313
^

0 commit comments

Comments
 (0)