Skip to content

[clang] Use the materialized temporary's type while creating the APValue #73355

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
Dec 1, 2023

Conversation

zyn0217
Copy link
Contributor

@zyn0217 zyn0217 commented Nov 24, 2023

See #72025 for the bug and its diagnosis.

@zyn0217 zyn0217 added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Nov 24, 2023
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Nov 24, 2023
@llvmbot
Copy link
Member

llvmbot commented Nov 24, 2023

@llvm/pr-subscribers-clang

Author: Younan Zhang (zyn0217)

Changes

See #72025 for the bug and its diagnosis.


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+3)
  • (modified) clang/lib/AST/ExprConstant.cpp (+1-1)
  • (added) clang/test/SemaCXX/pr72025.cpp (+9)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 74358219ba9fb22..d434d016907f815 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -691,6 +691,9 @@ Bug Fixes to C++ Support
   Fixes:
   (`#68769 <https://github.com/llvm/llvm-project/issues/68769>`_)
 
+- Fixed a crash for C++98/03 while checking an ill-formed ``_Static_assert`` expression.
+  Fixes: (`#72025 <https://github.com/llvm/llvm-project/issues/72025>`_)
+
 - Clang now defers the instantiation of explicit specifier until constraint checking
   completes (except deduction guides). Fixes:
   (`#59827 <https://github.com/llvm/llvm-project/issues/59827>`_)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e16fec6109e744e..6c6ad12119d13c3 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -8607,7 +8607,7 @@ bool LValueExprEvaluator::VisitMaterializeTemporaryExpr(
     Result.set(E);
   } else {
     Value = &Info.CurrentCall->createTemporary(
-        E, E->getType(),
+        E, Inner->getType(),
         E->getStorageDuration() == SD_FullExpression ? ScopeKind::FullExpression
                                                      : ScopeKind::Block,
         Result);
diff --git a/clang/test/SemaCXX/pr72025.cpp b/clang/test/SemaCXX/pr72025.cpp
new file mode 100644
index 000000000000000..9f0a4b0f43630c5
--- /dev/null
+++ b/clang/test/SemaCXX/pr72025.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -verify -std=c++03 -fsyntax-only %s
+struct V {
+  char c[2];
+  banana V() : c("i") {} // expected-error {{unknown type name}}
+                         // expected-error@-1 {{constructor cannot have a return type}}
+};
+
+_Static_assert(V().c[0], ""); // expected-error {{is not an integral constant expression}}
+

Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff d896b1f5a614daef1c3aa65cb3521ec82bc728d5 3ff1b189cf55d3705b2823dc39eaaf710fa26541 -- clang/test/SemaCXX/pr72025.cpp clang/lib/AST/ExprConstant.cpp
View the diff from clang-format here.
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 6c6ad12119..0650c91a8f 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -3185,8 +3185,8 @@ static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E,
 }
 
 /// Get the size of the given type in char units.
-static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc,
-                         QualType Type, CharUnits &Size) {
+static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, QualType Type,
+                         CharUnits &Size) {
   // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
   // extension.
   if (Type->isVoidType() || Type->isFunctionType()) {
@@ -11516,8 +11516,8 @@ enum class GCCTypeClass {
 
 /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
 /// as GCC.
-static GCCTypeClass
-EvaluateBuiltinClassifyType(QualType T, const LangOptions &LangOpts) {
+static GCCTypeClass EvaluateBuiltinClassifyType(QualType T,
+                                                const LangOptions &LangOpts) {
   assert(!T->isDependentType() && "unexpected dependent type");
 
   QualType CanTy = T.getCanonicalType();

@zyn0217
Copy link
Contributor Author

zyn0217 commented Nov 30, 2023

Ping. And invited @cor3ntin to kindly take a look at this.

@zyn0217 zyn0217 requested a review from cor3ntin November 30, 2023 12:09
@zyn0217
Copy link
Contributor Author

zyn0217 commented Nov 30, 2023

The code-formatter failed on other unrelated lines, so I think I'd better not to touch them.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

LGTM -- thank you for the detailed analysis in the issue and fix!

@zyn0217 zyn0217 merged commit c1ad363 into llvm:main Dec 1, 2023
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.

3 participants