-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[clang][Sema] Reject negative tuple sizes #159579
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
Conversation
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesNegative sizes don't make sense and trip up the code using Fixes #159563 Full diff: https://github.com/llvm/llvm-project/pull/159579.diff 2 Files Affected:
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index fb57b43882911..2acd07cc5a6f1 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -1219,7 +1219,7 @@ static IsTupleLike isTupleLike(Sema &S, SourceLocation Loc, QualType T,
return IsTupleLike::Error;
E = S.VerifyIntegerConstantExpression(E.get(), &Size, Diagnoser);
- if (E.isInvalid())
+ if (E.isInvalid() || Size.isNegative())
return IsTupleLike::Error;
return IsTupleLike::TupleLike;
diff --git a/clang/test/SemaCXX/builtin-structured-binding-size.cpp b/clang/test/SemaCXX/builtin-structured-binding-size.cpp
index 53576048754ab..de881a539310c 100644
--- a/clang/test/SemaCXX/builtin-structured-binding-size.cpp
+++ b/clang/test/SemaCXX/builtin-structured-binding-size.cpp
@@ -229,3 +229,12 @@ static_assert(__is_same_as(tag_of_t<S1>, int));
static_assert(__is_same_as(tag_of_t<int>, int)); // error
// expected-error@-1 {{constraints not satisfied for alias template 'tag_of_t' [with T = int]}}
// expected-note@#tag-of-constr {{because substituted constraint expression is ill-formed: type 'int' cannot be decomposed}}
+
+struct Neg {
+ int a;
+};
+template <> struct std::tuple_size<Neg> {
+ static constexpr int value = -1;
+};
+
+int e = __builtin_structured_binding_size(Neg); // expected-error {{type 'Neg' cannot be decomposed}}
|
We should backport that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mention UnsignedOrNone
in the summary but that is not directly involved in the code you modify. It looks like it is used in GetDecompositionElementCount
which uses isTupleLike
but the connection is not obvious. It lines up w/ the git bisect but a more detailed explaination would be helpful especially since the PR was very large and likely warrants further review and understanding the issue better would help there.
|
Just for clarification |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't realize someone else was working on it, but I propose #159677 instead, as the diagnostics produced are more complete.
Negative sizes don't make sense and trip up the code using
UnsignedOrNone
.Fixes #159563