Skip to content

Commit ffe8fb4

Browse files
committed
Improved error message on override mismatch
1 parent a4cbc08 commit ffe8fb4

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

compiler/src/dmd/funcsem.d

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,43 @@ void funcDeclarationSemantic(Scope* sc, FuncDeclaration funcdecl)
983983

984984
error(funcdecl.loc, "function `%s` does not override any function, did you mean to override `%s`?",
985985
funcdeclToChars, buf1.peekChars());
986+
987+
// Supplemental error for parameter scope differences
988+
auto tf1 = cast(TypeFunction)funcdecl.type;
989+
auto tf2 = cast(TypeFunction)fd.type;
990+
991+
if (tf1 && tf2)
992+
{
993+
auto params1 = tf1.parameterList;
994+
auto params2 = tf2.parameterList;
995+
996+
if (params1.length == params2.length)
997+
{
998+
bool hasScopeDifference = false;
999+
1000+
for (size_t i = 0; i < params1.length; i++)
1001+
{
1002+
auto p1 = params1[i];
1003+
auto p2 = params2[i];
1004+
1005+
// Check for scope difference
1006+
if ((p1.storageClass & STC.scope_) != (p2.storageClass & STC.scope_))
1007+
{
1008+
if (p2.storageClass & STC.scope_)
1009+
{
1010+
if (!hasScopeDifference)
1011+
{
1012+
// Intended signature
1013+
errorSupplemental(funcdecl.loc, "Did you intend to override:");
1014+
errorSupplemental(funcdecl.loc, "`%s`", buf1.peekChars());
1015+
hasScopeDifference = true;
1016+
}
1017+
errorSupplemental(funcdecl.loc, "Parameter %d is missing `scope`", cast(int)(i + 1));
1018+
}
1019+
}
1020+
}
1021+
}
1022+
}
9861023
}
9871024
}
9881025
else
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
TEST_OUTPUT:
3+
---
4+
fail_compilation/test20489.d(19): Error: function `pure nothrow @nogc @safe int test20489.D.f(int delegate(int) pure nothrow @nogc @safe body)` does not override any function, did you mean to override `pure nothrow @nogc @safe int test20489.B.f(scope int delegate(int) pure nothrow @nogc @safe)`?
5+
fail_compilation/test20489.d(19): Did you intend to override:
6+
fail_compilation/test20489.d(19): `pure nothrow @nogc @safe int test20489.B.f(scope int delegate(int) pure nothrow @nogc @safe)`
7+
fail_compilation/test20489.d(19): Parameter 1 is missing `scope`
8+
---
9+
*/
10+
11+
// Test case for https://github.com/dlang/dmd/issues/20489
12+
// Improved error message on override mismatches
13+
14+
class B {
15+
pure nothrow @nogc @safe int f(scope int delegate(int) pure nothrow @nogc @safe) { return 0; }
16+
}
17+
18+
class D : B {
19+
override pure nothrow @nogc @safe int f(int delegate(int) pure nothrow @nogc @safe body) { return 0; }
20+
}

0 commit comments

Comments
 (0)