Skip to content

[clang] Fix crash when inheriting from a cv-qualified type #70594

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
Apr 2, 2024

Conversation

Rajveer100
Copy link
Member

Resolves Issue #35603

This change makes the assertion less strict in debug builds by stripping qualifiers from the base class and ignoring them. I hope weakened assertions don't affect other cases where such errors are intended to be caught by the compiler.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 29, 2023
@llvmbot
Copy link
Member

llvmbot commented Oct 29, 2023

@llvm/pr-subscribers-clang

Author: Rajveer Singh Bharadwaj (Rajveer100)

Changes

Resolves Issue #35603

This change makes the assertion less strict in debug builds by stripping qualifiers from the base class and ignoring them. I hope weakened assertions don't affect other cases where such errors are intended to be caught by the compiler.


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

2 Files Affected:

  • (modified) clang/lib/AST/ExprConstant.cpp (+1-1)
  • (added) clang/test/Sema/assertion-crash.cpp (+19)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 5947805f9576ff8..07f0a12385b46e9 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -6431,7 +6431,7 @@ static bool HandleConstructorCall(const Expr *E, const LValue &This,
       // Non-virtual base classes are initialized in the order in the class
       // definition. We have already checked for virtual base classes.
       assert(!BaseIt->isVirtual() && "virtual base for literal type");
-      assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) &&
+      assert(Info.Ctx.hasSameUnqualifiedType(BaseIt->getType(), BaseType) &&
              "base class initializers not in expected order");
       ++BaseIt;
 #endif
diff --git a/clang/test/Sema/assertion-crash.cpp b/clang/test/Sema/assertion-crash.cpp
new file mode 100644
index 000000000000000..853552108ec2b67
--- /dev/null
+++ b/clang/test/Sema/assertion-crash.cpp
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c++11 %s -verify
+// RUN: %clang_cc1 -fsyntax-only -std=c++23 %s -verify
+
+// expected-no-diagnostics
+
+struct A {};
+using CA = const A;
+
+struct S1 : CA {
+  constexpr S1() : CA() {}
+};
+
+struct S2 : A {
+  constexpr S2() : CA() {}
+};
+
+struct S3 : CA {
+  constexpr S3() : A() {}
+};

@Rajveer100 Rajveer100 force-pushed the branch-for-issue-35603 branch from 6c5fda8 to efea75d Compare October 31, 2023 10:52
@github-actions
Copy link

github-actions bot commented Oct 31, 2023

✅ With the latest revision this PR passed the C/C++ code formatter.

@Rajveer100
Copy link
Member Author

Rajveer100 commented Oct 31, 2023

The clang-format issues showing up for changes are not related to this PR.

@cor3ntin
Copy link
Contributor

LGTM too. Please add a release note though

@Rajveer100
Copy link
Member Author

@cor3ntin
Could you describe the format of the release note briefly so I can amend my commit accordingly?

@xgupta
Copy link
Contributor

xgupta commented Dec 6, 2023

@cor3ntin Could you describe the format of the release note briefly so I can amend my commit accordingly?
Like this -
https://github.com/llvm/llvm-project/pull/74553/files#diff-ec770381d76c859f5f572db789175fe44410a72608f58ad5dbb14335ba56eb97

You also need to clang-format this patch with git clang-format HEAD~1.

@Rajveer100 Rajveer100 force-pushed the branch-for-issue-35603 branch from efea75d to 1923c21 Compare December 6, 2023 11:22
@Rajveer100 Rajveer100 requested a review from shafik December 6, 2023 11:24
@Rajveer100
Copy link
Member Author

@cor3ntin
Could you help me in closing this?

@Rajveer100 Rajveer100 force-pushed the branch-for-issue-35603 branch from 1923c21 to 864bd0e Compare January 13, 2024 09:05
@Rajveer100 Rajveer100 changed the title [clang] Fix clang++ crash on assertions when compiling source [clang] Fix crash when inheriting from a cv-qualified type Feb 10, 2024
@Rajveer100 Rajveer100 force-pushed the branch-for-issue-35603 branch from 864bd0e to a27a685 Compare February 22, 2024 17:52
Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

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

LGTM

@Endilll
Copy link
Contributor

Endilll commented Apr 2, 2024

You might want to include a test from #85256 (comment)

@Rajveer100 Rajveer100 force-pushed the branch-for-issue-35603 branch from a27a685 to f2a9a41 Compare April 2, 2024 12:35
Resolves Issue llvm#35603

This bug was caused due to the assertions being too strict,
loosened by stripping qualifiers from the base class but not
from the type of the initializer.

Added Release Notes for the same.
@Rajveer100 Rajveer100 force-pushed the branch-for-issue-35603 branch from f2a9a41 to caabd75 Compare April 2, 2024 12:37
@Rajveer100
Copy link
Member Author

@shafik
Could you land this for me?

@Endilll
Copy link
Contributor

Endilll commented Apr 2, 2024

I can land this for you after CI passes.

@yronglin
Copy link
Contributor

yronglin commented Apr 2, 2024

#85256 can be closed once this PR merged.

@AaronBallman AaronBallman merged commit e12a1f8 into llvm:main Apr 2, 2024
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.

8 participants