Fix : Improved error message on override mismatch (#21023)

This commit is contained in:
Abul Hossain Khan 2025-03-18 05:58:57 +05:30 committed by GitHub
parent 49198f2983
commit 8663b6dcdc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 57 additions and 0 deletions

View file

@ -983,6 +983,43 @@ void funcDeclarationSemantic(Scope* sc, FuncDeclaration funcdecl)
error(funcdecl.loc, "function `%s` does not override any function, did you mean to override `%s`?",
funcdeclToChars, buf1.peekChars());
// Supplemental error for parameter scope differences
auto tf1 = cast(TypeFunction)funcdecl.type;
auto tf2 = cast(TypeFunction)fd.type;
if (tf1 && tf2)
{
auto params1 = tf1.parameterList;
auto params2 = tf2.parameterList;
if (params1.length == params2.length)
{
bool hasScopeDifference = false;
for (size_t i = 0; i < params1.length; i++)
{
auto p1 = params1[i];
auto p2 = params2[i];
if ((p1.storageClass & STC.scope_) == (p2.storageClass & STC.scope_))
continue;
if (!(p2.storageClass & STC.scope_))
continue;
if (!hasScopeDifference)
{
// Intended signature
errorSupplemental(funcdecl.loc, "Did you intend to override:");
errorSupplemental(funcdecl.loc, "`%s`", buf1.peekChars());
hasScopeDifference = true;
}
errorSupplemental(funcdecl.loc, "Parameter %d is missing `scope`",
cast(int)(i + 1));
}
}
}
}
}
else

View file

@ -0,0 +1,20 @@
/*
TEST_OUTPUT:
---
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)`?
fail_compilation/test20489.d(19): Did you intend to override:
fail_compilation/test20489.d(19): `pure nothrow @nogc @safe int test20489.B.f(scope int delegate(int) pure nothrow @nogc @safe)`
fail_compilation/test20489.d(19): Parameter 1 is missing `scope`
---
*/
// Test case for https://github.com/dlang/dmd/issues/20489
// Improved error message on override mismatches
class B {
pure nothrow @nogc @safe int f(scope int delegate(int) pure nothrow @nogc @safe) { return 0; }
}
class D : B {
override pure nothrow @nogc @safe int f(int delegate(int) pure nothrow @nogc @safe body) { return 0; }
}