https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/159579
Negative sizes don't make sense and trip up the code using `UnsignedOrNone`. Fixes #159563 >From 5fcc0e20cd64f27c284782c5e60c54f68ca657da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]> Date: Thu, 18 Sep 2025 16:22:45 +0200 Subject: [PATCH] [clang][Sema] Reject negative tuple sizes Fixes #159563 --- clang/lib/Sema/SemaDeclCXX.cpp | 2 +- clang/test/SemaCXX/builtin-structured-binding-size.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) 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}} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
