Skip to content

[Clang] Fix a crash when diagnosing a non relocatable with no copy ctr #143350

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 1 commit into from
Jun 9, 2025

Conversation

cor3ntin
Copy link
Contributor

@cor3ntin cor3ntin commented Jun 9, 2025

If lookup did not find a copy constructor, it would return nulll, leading to an assert in cast.

Fixes #143325

If lookup did not find a copy constructor, it would return nulll,
leading to an assert in `cast`.

Fixes llvm#143325
@cor3ntin cor3ntin requested a review from ojhunt June 9, 2025 07:37
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 9, 2025

@llvm/pr-subscribers-clang

Author: Corentin Jabot (cor3ntin)

Changes

If lookup did not find a copy constructor, it would return nulll, leading to an assert in cast.

Fixes #143325


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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaTypeTraits.cpp (+1-1)
  • (modified) clang/test/SemaCXX/type-traits-unsatisfied-diags.cpp (+44)
diff --git a/clang/lib/Sema/SemaTypeTraits.cpp b/clang/lib/Sema/SemaTypeTraits.cpp
index 330f2aa750a09..a2cef76cfc673 100644
--- a/clang/lib/Sema/SemaTypeTraits.cpp
+++ b/clang/lib/Sema/SemaTypeTraits.cpp
@@ -2052,7 +2052,7 @@ static void DiagnoseNonTriviallyRelocatableReason(Sema &SemaRef,
   }
 
   if (!D->hasSimpleMoveConstructor() && !D->hasSimpleCopyConstructor()) {
-    const auto *Decl = cast<CXXConstructorDecl>(
+    const auto *Decl = cast_or_null<CXXConstructorDecl>(
         LookupSpecialMemberFromXValue(SemaRef, D, /*Assign=*/false));
     if (Decl && Decl->isUserProvided())
       SemaRef.Diag(Loc, diag::note_unsatisfied_trait_reason)
diff --git a/clang/test/SemaCXX/type-traits-unsatisfied-diags.cpp b/clang/test/SemaCXX/type-traits-unsatisfied-diags.cpp
index 0256569fcca5f..26378ef25ee99 100644
--- a/clang/test/SemaCXX/type-traits-unsatisfied-diags.cpp
+++ b/clang/test/SemaCXX/type-traits-unsatisfied-diags.cpp
@@ -145,6 +145,50 @@ static_assert(__builtin_is_cpp_trivially_relocatable(U2));
 
 }
 
+
+namespace GH143325 {
+struct Foo  { // expected-note {{previous definition is here}}
+  Foo(const Foo&);
+  ~Foo();
+};
+
+struct Foo { // expected-error {{redefinition of 'Foo'}}
+  Foo();
+  int;
+};
+struct Wrapper { // #GH143325-Wrapper
+  union {
+    Foo p;
+  } u;
+};
+
+static_assert(__builtin_is_cpp_trivially_relocatable(Wrapper));
+// expected-error@-1 {{static assertion failed due to requirement '__builtin_is_cpp_trivially_relocatable(GH143325::Wrapper)'}} \
+// expected-note@-1 {{'Wrapper' is not trivially relocatable}} \
+// expected-note@-1 {{because it has a non-trivially-relocatable member 'u' of type 'union}} \
+// expected-note@-1 {{because it has a deleted destructor}}
+// expected-note@#GH143325-Wrapper {{'Wrapper' defined here}}
+
+struct Polymorphic  {
+  virtual ~Polymorphic();
+};
+
+struct UnionOfPolymorphic { // #GH143325-UnionOfPolymorphic
+  union {
+    Polymorphic p;
+    int i;
+  } u;
+};
+
+static_assert(__builtin_is_cpp_trivially_relocatable(UnionOfPolymorphic));
+// expected-error@-1 {{static assertion failed due to requirement '__builtin_is_cpp_trivially_relocatable(GH143325::UnionOfPolymorphic)'}} \
+// expected-note@-1 {{'UnionOfPolymorphic' is not trivially relocatable}} \
+// expected-note@-1 {{because it has a non-trivially-relocatable member 'u' of type 'union}} \
+// expected-note@-1 {{because it has a deleted destructor}} \
+// expected-note@#GH143325-UnionOfPolymorphic {{'UnionOfPolymorphic' defined here}}
+
+}
+
 namespace trivially_copyable {
 struct B {
  virtual ~B();

Copy link
Contributor

@ojhunt ojhunt left a comment

Choose a reason for hiding this comment

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

Looks good

@cor3ntin cor3ntin merged commit 62c9b0c into llvm:main Jun 9, 2025
10 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jun 9, 2025

LLVM Buildbot has detected a new failure on builder lldb-x86_64-debian running on lldb-x86_64-debian while building clang at step 6 "test".

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

Here is the relevant piece of the build log for the reference
Step 6 (test) failure: build (failure)
...
UNSUPPORTED: lldb-shell :: ScriptInterpreter/Python/Crashlog/no_threadState.test (2983 of 2994)
UNSUPPORTED: lldb-shell :: ScriptInterpreter/Lua/nested_sessions.test (2984 of 2994)
UNSUPPORTED: lldb-shell :: ScriptInterpreter/Lua/lua-python.test (2985 of 2994)
UNSUPPORTED: lldb-shell :: Process/Windows/process_load.cpp (2986 of 2994)
UNSUPPORTED: lldb-shell :: ScriptInterpreter/Lua/lua.test (2987 of 2994)
UNSUPPORTED: lldb-shell :: ScriptInterpreter/Lua/command_script_import.test (2988 of 2994)
UNSUPPORTED: lldb-shell :: ScriptInterpreter/Lua/persistent_state.test (2989 of 2994)
UNSUPPORTED: lldb-shell :: ScriptInterpreter/Python/Crashlog/interactive_crashlog_invalid_target.test (2990 of 2994)
PASS: lldb-api :: terminal/TestEditlineCompletions.py (2991 of 2994)
UNRESOLVED: lldb-api :: tools/lldb-dap/launch/TestDAP_launch.py (2992 of 2994)
******************** TEST 'lldb-api :: tools/lldb-dap/launch/TestDAP_launch.py' FAILED ********************
Script:
--
/usr/bin/python3 /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/./lib --env LLVM_INCLUDE_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/include --env LLVM_TOOLS_DIR=/home/worker/2.0.1/lldb-x86_64-debian/build/./bin --arch x86_64 --build-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex --lldb-module-cache-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex/module-cache-lldb/lldb-api --clang-module-cache-dir /home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex/module-cache-clang/lldb-api --executable /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/lldb --compiler /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/clang --dsymutil /home/worker/2.0.1/lldb-x86_64-debian/build/./bin/dsymutil --make /usr/bin/gmake --llvm-tools-dir /home/worker/2.0.1/lldb-x86_64-debian/build/./bin --lldb-obj-root /home/worker/2.0.1/lldb-x86_64-debian/build/tools/lldb --lldb-libs-dir /home/worker/2.0.1/lldb-x86_64-debian/build/./lib --cmake-build-type Release -t /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/tools/lldb-dap/launch -p TestDAP_launch.py
--
Exit Code: 1

Command Output (stdout):
--
lldb version 21.0.0git (https://github.com/llvm/llvm-project.git revision 62c9b0cab614f2d408929f9ee4280fc3c0084e08)
  clang revision 62c9b0cab614f2d408929f9ee4280fc3c0084e08
  llvm revision 62c9b0cab614f2d408929f9ee4280fc3c0084e08
Skipping the following test categories: ['libc++', 'dsym', 'gmodules', 'debugserver', 'objc']

--
Command Output (stderr):
--
Change dir to: /home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/tools/lldb-dap/launch
runCmd: settings clear --all

output: 

runCmd: settings set symbols.enable-external-lookup false

output: 

runCmd: settings set target.inherit-tcc true

output: 

runCmd: settings set target.disable-aslr false

output: 

runCmd: settings set target.detach-on-error false

output: 

runCmd: settings set target.auto-apply-fixits false

rorth pushed a commit to rorth/llvm-project that referenced this pull request Jun 11, 2025
llvm#143350)

If lookup did not find a copy constructor, it would return nulll,
leading to an assert in `cast`.

Fixes llvm#143325
DhruvSrivastavaX pushed a commit to DhruvSrivastavaX/lldb-for-aix that referenced this pull request Jun 12, 2025
llvm#143350)

If lookup did not find a copy constructor, it would return nulll,
leading to an assert in `cast`.

Fixes llvm#143325
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
llvm#143350)

If lookup did not find a copy constructor, it would return nulll,
leading to an assert in `cast`.

Fixes llvm#143325
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.

relocation diagnostics asserts in response to code that is broken for unrelated reasons #awesome
4 participants