From e896b5c98d20119e034514184afa1bdc0231e7de Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Sun, 23 Mar 2025 21:47:29 +0000 Subject: [PATCH] Fix hidden base constructor supplemental message (#21069) Fixes #21068. Also say base class *constructor* in supplemental message. --- compiler/src/dmd/funcsem.d | 13 +++++++--- compiler/test/fail_compilation/diag23384.d | 4 +-- compiler/test/fail_compilation/hidden_ctor.d | 27 ++++++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 compiler/test/fail_compilation/hidden_ctor.d diff --git a/compiler/src/dmd/funcsem.d b/compiler/src/dmd/funcsem.d index 0a8a574c59..a8b46d84c2 100644 --- a/compiler/src/dmd/funcsem.d +++ b/compiler/src/dmd/funcsem.d @@ -1835,10 +1835,15 @@ FuncDeclaration resolveFuncCall(Loc loc, Scope* sc, Dsymbol s, functionResolve(mErr, baseFunction, loc, sc, tiargs, baseClass.type, argumentList); if (mErr.last > MATCH.nomatch && mErr.lastf) { - errorSupplemental(loc, "%s `%s` hides base class function `%s`", - fd.kind, fd.toPrettyChars(), mErr.lastf.toPrettyChars()); - errorSupplemental(loc, "add `alias %s = %s` to `%s`'s body to merge the overload sets", - fd.toChars(), mErr.lastf.toPrettyChars(), tthis.toChars()); + errorSupplemental(loc, "Note: %s `%s` hides base class %s `%s`", + fd.kind, fd.toPrettyChars(), + mErr.lastf.kind, mErr.lastf.toPrettyChars()); + + if (!fd.isCtorDeclaration) + { + errorSupplemental(loc, "Add `alias %s = %s;` to `%s`'s body to merge the overload sets", + fd.toChars(), mErr.lastf.toPrettyChars(), tthis.toChars()); + } return null; } } diff --git a/compiler/test/fail_compilation/diag23384.d b/compiler/test/fail_compilation/diag23384.d index 1fa4da5ffb..9cb17c00d7 100644 --- a/compiler/test/fail_compilation/diag23384.d +++ b/compiler/test/fail_compilation/diag23384.d @@ -4,8 +4,8 @@ TEST_OUTPUT: --- fail_compilation/diag23384.d(28): Error: function `diag23384.Derived.fun(B b)` is not callable using argument types `(A)` -fail_compilation/diag23384.d(28): function `diag23384.Derived.fun` hides base class function `diag23384.Base.fun` -fail_compilation/diag23384.d(28): add `alias fun = diag23384.Base.fun` to `diag23384.Derived`'s body to merge the overload sets +fail_compilation/diag23384.d(28): Note: function `diag23384.Derived.fun` hides base class function `diag23384.Base.fun` +fail_compilation/diag23384.d(28): Add `alias fun = diag23384.Base.fun;` to `diag23384.Derived`'s body to merge the overload sets --- */ diff --git a/compiler/test/fail_compilation/hidden_ctor.d b/compiler/test/fail_compilation/hidden_ctor.d new file mode 100644 index 0000000000..8d6e6959f2 --- /dev/null +++ b/compiler/test/fail_compilation/hidden_ctor.d @@ -0,0 +1,27 @@ +/* +TEST_OUTPUT: +--- +fail_compilation/hidden_ctor.d(25): Error: constructor `hidden_ctor.B.this(string s)` is not callable using argument types `()` +fail_compilation/hidden_ctor.d(25): Note: constructor `hidden_ctor.B.this` hides base class constructor `hidden_ctor.A.this` +--- +*/ + +class A { + int a; + + this() { + this.a = 1; + } +} +class B : A { + string b; + + this(string s) { + super(); + this.b = s; + } +} +void main() { + auto b = new B(); + b = new B("Hi, Mom!"); +}