[clang] [clang] Diagnose default arguments defined in different scopes (PR #124844)
Vlad Serebrennikov via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 29 15:03:47 PST 2025
================
@@ -488,6 +490,20 @@ bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old,
continue;
}
+ if (PrevForDefaultArgs->getLexicalDeclContext()->getPrimaryContext() !=
+ ScopeDC->getPrimaryContext() &&
+ !New->isCXXClassMember())
+ // If previous declaration is lexically in a different scope,
+ // we don't inherit its default arguments, except for out-of-line
+ // declarations of member functions.
+ //
+ // extern "C" and local functions can have default arguments across
+ // different scopes, but diagnosing that early would reject well-formed
+ // code (_N5001_.[over.match.best]/4.) Instead, they are checked
+ // in ConvertArgumentsForCall, after the best viable function has been
+ // selected.
+ continue;
+
----------------
Endilll wrote:
> Move the comment immediately above the if
I'm keeping this consistent with the surrounding code.
> we usually quote C++2c rather than a specific draft
Hmm, fine.
> but i think the wording we want to quote here is actually
https://eel.is/c++draft/dcl.fct.default#4.sentence-2
No, the wording that you point at (together with the ODR) is what allows `MergeCXXFunctionDecl` to diagnose redeclarations of default arguments in the same scope early, during declaration matching.
The wording I'm pointing at explains why we need to defer check for default arguments defined in different scopes.
> do we have tests for that?
Yes, `l3` in `default-arguments-different-scopes.cpp`.
https://github.com/llvm/llvm-project/pull/124844
More information about the cfe-commits
mailing list