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] Avoid superfluous 'const' qualifiers in composite pointer type #88905

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

offsetof
Copy link
Contributor

Avoid adding const on too many levels to the qualification-combined type when going from "array of N" to "array of unknown bound of".

Fixes #66599

Fix too many levels of 'const' being added to the computed
qualification-combined type when conversions to arrays of unknown
bound are involved, so that (for example) applying the conditional
operator (?:) to operands of types "T (*)[N]" and "T (*)[]" correctly
yields a "T (*)[]" and not "const T (*)[]".
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 Apr 16, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Apr 16, 2024

@llvm/pr-subscribers-clang

Author: None (offsetof)

Changes

Avoid adding const on too many levels to the qualification-combined type when going from "array of N" to "array of unknown bound of".

Fixes #66599


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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaExprCXX.cpp (+2-2)
  • (modified) clang/test/SemaCXX/cxx20-p0388-unbound-ary.cpp (+25-1)
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 74ed3fe7bd5201..e704ca28bed7cf 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -7260,8 +7260,8 @@ QualType Sema::FindCompositePointerType(SourceLocation Loc,
         Composite1 = Arr1->getElementType();
         Composite2 = Arr2->getElementType();
         Steps.emplace_back(Step::Array);
-        if (CAT1 || CAT2)
-          NeedConstBefore = Steps.size();
+        if ((CAT1 || CAT2) && Steps.size() > 2)
+          NeedConstBefore = Steps.size() - 2;
         continue;
       }
     }
diff --git a/clang/test/SemaCXX/cxx20-p0388-unbound-ary.cpp b/clang/test/SemaCXX/cxx20-p0388-unbound-ary.cpp
index f2d5cabad235d0..0e428110d7988c 100644
--- a/clang/test/SemaCXX/cxx20-p0388-unbound-ary.cpp
+++ b/clang/test/SemaCXX/cxx20-p0388-unbound-ary.cpp
@@ -170,4 +170,28 @@ void g3() {
 
 } // namespace Eight
 
-#endif
+namespace Nine {
+
+template<auto, class>
+constexpr bool is_of_type = false;
+
+template<class T, T expr>
+constexpr bool is_of_type<expr, T> = true;
+
+using T1 = int (*)[1];
+using T2 = int (*)[];
+static_assert(is_of_type<0 ? T1() : T2(), T2>);
+
+using U1 = int* (**)[1];
+using U2 = int* (**)[];
+using U3 = int* (* const*)[];
+static_assert(is_of_type<0 ? U1() : U2(), U3>);
+
+using V1 = int* (*(**)[])[1];
+using V2 = int* (*(**)[1])[];
+using V3 = int* (* const(* const*)[])[];
+static_assert(is_of_type<0 ? V1() : V2(), V3>);
+
+} // namespace Nine
+
+#endif // __cplusplus >= 202002

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.

wrong type of ternary expression involving composite pointer type with array of unknown bound
2 participants