Skip to content

Commit d989950

Browse files
committed
[clang-format] Disallow decltype in the middle of constraints
If a function with a `requires` clause as a constraint has a decltype return type, such as `decltype(auto)`, the decltype was seen to be part of the constraint clause, rather than as part of the function declaration, causing it to be placed on the wrong line. This patch disallows decltype to be a part of these clauses Fixes #59578 Depends on D140339 Reviewed By: HazardyKnusperkeks, owenpan, MyDeveloperDay Differential Revision: https://reviews.llvm.org/D140312
1 parent b1eeec6 commit d989950

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3543,7 +3543,6 @@ void UnwrappedLineParser::parseConstraintExpression() {
35433543
case tok::minus:
35443544
case tok::star:
35453545
case tok::slash:
3546-
case tok::kw_decltype:
35473546
LambdaNextTimeAllowed = true;
35483547
// Just eat them.
35493548
nextToken();

clang/unittests/Format/FormatTest.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24199,6 +24199,10 @@ TEST_F(FormatTest, RequiresClauses) {
2419924199
" }\n"
2420024200
"};");
2420124201

24202+
verifyFormat("template <class T>\n"
24203+
" requires(std::same_as<int, T>)\n"
24204+
"decltype(auto) fun() {}");
24205+
2420224206
auto Style = getLLVMStyle();
2420324207

2420424208
verifyFormat(

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,14 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) {
580580
EXPECT_TRUE(Tokens[9]->ClosesRequiresClause);
581581
EXPECT_TOKEN(Tokens[11], tok::identifier, TT_FunctionDeclarationName);
582582

583+
Tokens = annotate("template <typename T>\n"
584+
"requires Bar<T>\n"
585+
"decltype(auto) foo(T) { return false; }");
586+
ASSERT_EQ(Tokens.size(), 24u) << Tokens;
587+
EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause);
588+
EXPECT_TRUE(Tokens[9]->ClosesRequiresClause);
589+
EXPECT_TOKEN(Tokens[14], tok::identifier, TT_FunctionDeclarationName);
590+
583591
Tokens = annotate("template <typename T>\n"
584592
"struct S {\n"
585593
" void foo() const requires Bar<T>;\n"

0 commit comments

Comments
 (0)