[clang] [clang] Diagnose default arguments defined in different scopes (PR #124844)

via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 29 01:35:53 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;
+
----------------
cor3ntin wrote:

do we have tests for that?

```cpp

struct S {
    void f(int a = 0);
};

void S::f(int a = 2) {}
```

https://github.com/llvm/llvm-project/pull/124844


More information about the cfe-commits mailing list