Skip to content

[Clang] [Sema] Fix a crash when a friend function is redefined as deleted #135679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 16, 2025

Conversation

Sirraide
Copy link
Member

NB: This only fixes the crash introduced in Clang 19; we still accept this code even though we shouldn’t:

struct S {
    friend int f() { return 3; }
    friend int f() = delete;
};

I tried figuring out a way to diagnose this redeclaration, but it seems tricky because I kept running into issues around defaulted comparison operators. From my testing, however, this fix here would still be required even once we do start diagnosing this.

Fixes #135506.

@Sirraide Sirraide added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Apr 14, 2025
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Apr 14, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 14, 2025

@llvm/pr-subscribers-clang

Author: None (Sirraide)

Changes

NB: This only fixes the crash introduced in Clang 19; we still accept this code even though we shouldn’t:

struct S {
    friend int f() { return 3; }
    friend int f() = delete;
};

I tried figuring out a way to diagnose this redeclaration, but it seems tricky because I kept running into issues around defaulted comparison operators. From my testing, however, this fix here would still be required even once we do start diagnosing this.

Fixes #135506.


Full diff: https://github.com/llvm/llvm-project/pull/135679.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1-1)
  • (modified) clang/lib/Sema/SemaDecl.cpp (+5-10)
  • (modified) clang/test/SemaCXX/cxx2c-delete-with-message.cpp (+30)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 11f62bc881b03..037f9f6fe79e0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -389,7 +389,7 @@ Bug Fixes in This Version
 
     #if 1 ? 1 : 999999999999999999999
     #endif
-
+- Fixed a crash when a ``friend`` function is redefined as deleted. (#GH135506)
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index e9805c345b6af..8fd857d347895 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -16178,16 +16178,11 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body,
     // This is meant to pop the context added in ActOnStartOfFunctionDef().
     ExitFunctionBodyRAII ExitRAII(*this, isLambdaCallOperator(FD));
     if (FD) {
-      // If this is called by Parser::ParseFunctionDefinition() after marking
-      // the declaration as deleted, and if the deleted-function-body contains
-      // a message (C++26), then a DefaultedOrDeletedInfo will have already been
-      // added to store that message; do not overwrite it in that case.
-      //
-      // Since this would always set the body to 'nullptr' in that case anyway,
-      // which is already done when the function decl is initially created,
-      // always skipping this irrespective of whether there is a delete message
-      // should not be a problem.
-      if (!FD->isDeletedAsWritten())
+      // The function body and the DefaultedOrDeletedInfo, if present, use
+      // the same storage; don't overwrite the latter if the former is null
+      // (the body is initialised to null anyway, so even if the latter isn't
+      // present, this would still be a no-op).
+      if (Body)
         FD->setBody(Body);
       FD->setWillHaveBody(false);
 
diff --git a/clang/test/SemaCXX/cxx2c-delete-with-message.cpp b/clang/test/SemaCXX/cxx2c-delete-with-message.cpp
index 22e65d902ecd4..5609da18c05aa 100644
--- a/clang/test/SemaCXX/cxx2c-delete-with-message.cpp
+++ b/clang/test/SemaCXX/cxx2c-delete-with-message.cpp
@@ -271,3 +271,33 @@ void operators() {
   if (to_int_int) {} // expected-error {{attempt to use a deleted function: deleted (TO<int, int>, operator bool)}}
   static_cast<bool>(to_int_int); // expected-error {{static_cast from 'TO<int, int>' to 'bool' uses deleted function: deleted (TO<int, int>, operator bool)}}
 };
+
+namespace gh135506 {
+struct a {
+  // FIXME: We currently don't diagnose these invalid redeclarations if the
+  // second declaration is defaulted or deleted. This probably needs to be
+  // handled in ParseCXXInlineMethodDef() after parsing the defaulted/deleted
+  // body.
+  friend consteval int f() { return 3; }
+  friend consteval int f() = delete("foo");
+
+  friend consteval int g() { return 3; }
+  friend consteval int g() = delete;
+
+  friend int h() { return 3; }
+  friend int h() = delete;
+
+  friend consteval int i() = delete; // expected-note {{previous definition is here}}
+  friend consteval int i() { return 3; } // expected-error {{redefinition of 'i'}}
+};
+
+struct b {
+  friend consteval bool operator==(b, b) { return true; } // expected-note {{previous declaration is here}}
+  friend consteval bool operator==(b, b) = default; // expected-error {{defaulting this equality comparison operator is not allowed because it was already declared outside the class}}
+};
+
+struct c {
+  friend consteval bool operator==(c, c) = default; // expected-note {{previous definition is here}}
+  friend consteval bool operator==(c, c) { return true; } // expected-error {{redefinition of 'operator=='}}
+};
+}


struct b {
friend consteval bool operator==(b, b) { return true; } // expected-note {{previous declaration is here}}
friend consteval bool operator==(b, b) = default; // expected-error {{defaulting this equality comparison operator is not allowed because it was already declared outside the class}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This diagnostic is a bit off, the previous declaration is not outside the class.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that’s not ideal either; I’ll make a separate issue for that (this pr doesn’t change anything about how that is handled; I just put it there to check that we don’t crash on it).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually... the wording in the standard around this is a bit unclear (obviously this is ill-formed but I’m talking about the case where the first declaration is not a definition). I’ll investigate this some more.

@Sirraide Sirraide merged commit 616613c into llvm:main Apr 16, 2025
11 of 12 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Apr 16, 2025

LLVM Buildbot has detected a new failure on builder llvm-clang-aarch64-darwin running on doug-worker-5 while building clang at step 6 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/190/builds/18406

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-all) failure: test (failure)
******************** TEST 'lit :: shtest-external-shell-kill.py' FAILED ********************
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 23
env -u FILECHECK_OPTS "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/bin/python3.9" /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/llvm/utils/lit/lit.py -j1 --order=lexical -a Inputs/shtest-external-shell-kill | grep -v 'bash.exe: warning: could not find /tmp, please create!' | FileCheck /Users/buildbot/buildbot-root/aarch64-darwin/build/utils/lit/tests/shtest-external-shell-kill.py
# executed command: env -u FILECHECK_OPTS /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/bin/python3.9 /Users/buildbot/buildbot-root/aarch64-darwin/llvm-project/llvm/utils/lit/lit.py -j1 --order=lexical -a Inputs/shtest-external-shell-kill
# note: command had no output on stdout or stderr
# error: command failed with exit status: 1
# executed command: grep -v 'bash.exe: warning: could not find /tmp, please create!'
# executed command: FileCheck /Users/buildbot/buildbot-root/aarch64-darwin/build/utils/lit/tests/shtest-external-shell-kill.py
# .---command stderr------------
# | �[1m/Users/buildbot/buildbot-root/aarch64-darwin/build/utils/lit/tests/shtest-external-shell-kill.py:29:15: �[0m�[0;1;31merror: �[0m�[1mCHECK-NEXT: is not on the line after the previous match
�[0m# | �[1m�[0m# CHECK-NEXT: end
# | �[0;1;32m              ^
�[0m# | �[0;1;32m�[0m�[1m<stdin>:22:6: �[0m�[0;1;30mnote: �[0m�[1m'next' match was here
�[0m# | �[1m�[0mecho end # RUN: at line 5
# | �[0;1;32m     ^
�[0m# | �[0;1;32m�[0m�[1m<stdin>:8:6: �[0m�[0;1;30mnote: �[0m�[1mprevious match ended here
�[0m# | �[1m�[0mstart
# | �[0;1;32m     ^
�[0m# | �[0;1;32m�[0m�[1m<stdin>:9:1: �[0m�[0;1;30mnote: �[0m�[1mnon-matching line after previous match is here
�[0m# | �[1m�[0m
# | �[0;1;32m^
�[0m# | �[0;1;32m�[0m
# | Input file: <stdin>
# | Check file: /Users/buildbot/buildbot-root/aarch64-darwin/build/utils/lit/tests/shtest-external-shell-kill.py
# | 
# | -dump-input=help explains the following input dump.
# | 
# | Input was:
# | <<<<<<
# | �[1m�[0m�[0;1;30m          1: �[0m�[1m�[0;1;46m-- Testing: 1 tests, 1 workers -- �[0m
# | �[0;1;30m          2: �[0m�[1m�[0;1;46mFAIL: shtest-external-shell-kill :: test.txt (1 of 1) �[0m
# | �[0;1;30m          3: �[0m�[1m�[0;1;46m******************** TEST 'shtest-external-shell-kill :: test.txt' FAILED ******************** �[0m
# | �[0;1;30m          4: �[0m�[1m�[0;1;46mExit Code: 1 �[0m
# | �[0;1;30m          5: �[0m�[1m�[0;1;46m �[0m
# | �[0;1;30m          6: �[0m�[1m�[0;1;46m�[0mCommand Output (stdout):�[0;1;46m �[0m
# | �[0;1;32mcheck:26     ^~~~~~~~~~~~~~~~~~~~~~~~
�[0m# | �[0;1;32m�[0m�[0;1;30m          7: �[0m�[1m�[0;1;46m�[0m--�[0;1;46m �[0m
# | �[0;1;32mnext:27      ^~
�[0m# | �[0;1;32m�[0m�[0;1;30m          8: �[0m�[1m�[0;1;46m�[0mstart�[0;1;46m �[0m
# | �[0;1;32mnext:28      ^~~~~
�[0m# | �[0;1;32m�[0m�[0;1;30m          9: �[0m�[1m�[0;1;46m �[0m
# | �[0;1;30m         10: �[0m�[1m�[0;1;46m-- �[0m
# | �[0;1;30m         11: �[0m�[1m�[0;1;46mCommand Output (stderr): �[0m
# | �[0;1;30m         12: �[0m�[1m�[0;1;46m-- �[0m
# | �[0;1;30m         13: �[0m�[1m�[0;1;46mecho start # RUN: at line 1 �[0m
# | �[0;1;30m         14: �[0m�[1m�[0;1;46m+ echo start �[0m
...

var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
…eleted (llvm#135679)

NB: This only fixes the crash introduced in Clang 19; we still accept
this code even though we shouldn’t:
```c++
struct S {
    friend int f() { return 3; }
    friend int f() = delete;
};
```
I tried figuring out a way to diagnose this redeclaration, but it seems
tricky because I kept running into issues around defaulted comparison
operators. From my testing, however, this fix here would still be
required even once we do start diagnosing this.

Fixes llvm#135506.
friend consteval int f() { return 3; }
friend consteval int f() = delete("foo");

friend consteval int g() { return 3; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also test constexpr as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clang++ 21 crash in clang::CXXRecordDecl::defaultedDestructorIsConstexpr
5 participants