Skip to content
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

[Clang][Sema] Fix templated array size calculation. #96464

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

awson
Copy link

@awson awson commented Jun 24, 2024

The last attempt to fix #41441 has been reverted immediately.

Here I'm trying the simplest idea I've been able to come with: skip handling dependent case in BuildCXXNew.

The original test (borrowed form #89036) passes.

Also I've created and added to the tests a minimal repro of the code #89036 fails on. This (obviously) also passes.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 24, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jun 24, 2024

@llvm/pr-subscribers-clang

Author: None (awson)

Changes

The last attempt to fix #41441 has been reverted immediately.

Here I'm trying the simplest idea I've been able to come with: skip handling dependent case in BuildCXXNew.

The original test (borrowed form #89036) passes.

Also I've created and added to the tests a minimal repro of the code #89036 fails on. This (obviously) also passes.


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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaExprCXX.cpp (+2-1)
  • (added) clang/test/SemaCXX/GH41441.cpp (+46)
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index f3af8dee6b090..2f79540faea00 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -2174,7 +2174,8 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal,
 
   // Per C++0x [expr.new]p5, the type being constructed may be a
   // typedef of an array type.
-  if (!ArraySize) {
+  // Dependent case will be handled separately.
+  if (!ArraySize && !AllocType->isDependentType()) {
     if (const ConstantArrayType *Array
                               = Context.getAsConstantArrayType(AllocType)) {
       ArraySize = IntegerLiteral::Create(Context, Array->getSize(),
diff --git a/clang/test/SemaCXX/GH41441.cpp b/clang/test/SemaCXX/GH41441.cpp
new file mode 100644
index 0000000000000..7a6260fef91b5
--- /dev/null
+++ b/clang/test/SemaCXX/GH41441.cpp
@@ -0,0 +1,46 @@
+// RUN: %clang --target=x86_64-pc-linux -S -fno-discard-value-names -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 %s -fsyntax-only -verify
+
+namespace std {
+  using size_t = decltype(sizeof(int));
+};
+void* operator new[](std::size_t, void*) noexcept;
+
+// CHECK: call void @llvm.memset.p0.i64(ptr align 1 %x, i8 0, i64 8, i1 false)
+// CHECK: call void @llvm.memset.p0.i64(ptr align 16 %x, i8 0, i64 32, i1 false)
+template <typename TYPE>
+void f()
+{
+    typedef TYPE TArray[8];
+
+    TArray x;
+    new(&x) TArray();
+}
+
+template <typename T>
+void f1() {
+  int (*x)[1] = new int[1][1];
+}
+template void f1<char>();
+void f2() {
+  int (*x)[1] = new int[1][1];
+}
+
+int main()
+{
+    f<char>();
+    f<int>();
+}
+
+// expected-no-diagnostics
+template <typename T> struct unique_ptr {unique_ptr(T* p){}};
+
+template <typename T>
+unique_ptr<T> make_unique(unsigned long long n) {
+  return unique_ptr<T>(new T[n]());
+}
+
+auto boro(int n){
+	typedef double HistoryBuffer[4];
+	return make_unique<HistoryBuffer>(n);
+}

@awson awson changed the title Try to fix https://github.com/llvm/llvm-project/issues/41441 Try to fix llvm/llvm-project#41441 Jun 24, 2024
I've borrowed size-calculation test from PR89036 and added another test, which PR89036 fails on.
@awson
Copy link
Author

awson commented Jun 27, 2024

@erichkeane, @cor3ntin

I see you actively reviewed (related, failed) #89036.

Could you look into this?

The idea of the fix is extremely simple: if we want to do something once, but mistakenly do it twice, just don't do it the first time.

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.

Thank you for catching this! Can you please add a release note to clang/docs/ReleaseNotes.rst so users know about the fix?

I think these changes otherwise look good.

@erichkeane
Copy link
Collaborator

It seems reasonable to me, but I want @cor3ntin to review this, he's the one who got his foot stuck in array bounds for a while :D

However, this patch needs a few changes:

1- A better patch title, perhaps improved commit message
2- Release note

@awson awson changed the title Try to fix llvm/llvm-project#41441 [Clang][Sema] Fix templated array size calculation. Sep 17, 2024
@awson
Copy link
Author

awson commented Sep 17, 2024

It seems reasonable to me, but I want @cor3ntin to review this, he's the one who got his foot stuck in array bounds for a while :D

However, this patch needs a few changes:

1- A better patch title, perhaps improved commit message 2- Release note

  1. Partially (patch title) done. After llvm:main merge (I've done it to sync release notes) "improved commit message" is a bit problematic, I fear.
  2. Done.

@erichkeane
Copy link
Collaborator

It seems reasonable to me, but I want @cor3ntin to review this, he's the one who got his foot stuck in array bounds for a while :D
However, this patch needs a few changes:
1- A better patch title, perhaps improved commit message 2- Release note

1. Partially (patch title) done. After `llvm:main` merge (I've done it to sync release notes) "improved commit message" is a bit problematic, I fear.

2. Done.

Our 'merge' process is a squash & Merge, so it really means "edit the github title and description to be more descriptive, and that is what will be picked up". So no need to modify the actual commits.

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

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

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

LGTM, feel free to merge.

@awson
Copy link
Author

awson commented Sep 25, 2024

@shafik ?

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.

Inplace new-ing an array overwrites the square of the memory
5 participants