[PATCH] D119609: [Clang][Sema] Don't act on ReturnStmt when parsing the lambda declarator.
Jun Zhang via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 4 22:09:57 PST 2022
junaire updated this revision to Diff 413195.
junaire added a comment.
This update proibit any use of ({}) in the default argument.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D119609/new/
https://reviews.llvm.org/D119609
Files:
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Parse/ParseDecl.cpp
clang/test/Sema/err-expr-stmt-in-default-arg.cpp
clang/test/SemaTemplate/dependent-expr.cpp
Index: clang/test/SemaTemplate/dependent-expr.cpp
===================================================================
--- clang/test/SemaTemplate/dependent-expr.cpp
+++ clang/test/SemaTemplate/dependent-expr.cpp
@@ -143,13 +143,6 @@
void h(auto a, decltype(g<char>())*) {} // expected-note {{previous}}
void h(auto a, void*) {} // expected-error {{redefinition}}
- void i(auto a) {
- [](auto a, int = ({decltype(a) i; i * 2;})){}(a); // expected-error {{invalid operands to binary expression ('decltype(a)' (aka 'void *') and 'int')}} expected-note {{in instantiation of}}
- }
- void use_i() {
- i(0);
- i((void*)0); // expected-note {{instantiation of}}
- }
}
namespace BindingInStmtExpr {
Index: clang/test/Sema/err-expr-stmt-in-default-arg.cpp
===================================================================
--- /dev/null
+++ clang/test/Sema/err-expr-stmt-in-default-arg.cpp
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 %s -fsyntax-only -verify -std=c++20
+
+void foo() {
+ void fn(int i, int = ({ 1; })); // expected-error {{can't use expression statement in default argument}}
+
+ auto a = [](int = ({ 1; })) {}; // expected-error {{can't use expression statement in default argument}}
+}
+
+template <typename Callable>
+int bar(Callable &&Call) {
+ return Call();
+}
+
+int baz() {
+ auto l = [](int a = ({ int x = 12; x; })) { // expected-error {{can't use expression statement in default argument}}
+ return 1;
+ };
+ return bar(l);
+}
Index: clang/lib/Parse/ParseDecl.cpp
===================================================================
--- clang/lib/Parse/ParseDecl.cpp
+++ clang/lib/Parse/ParseDecl.cpp
@@ -7085,8 +7085,15 @@
if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
DefArgResult = ParseBraceInitializer();
- } else
+ } else {
+ if (Tok.is(tok::l_paren) && NextToken().is(tok::l_brace)) {
+ Diag(Tok, diag::err_expr_statement_in_default_arg);
+ Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
+ SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
+ return;
+ }
DefArgResult = ParseAssignmentExpression();
+ }
DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
if (DefArgResult.isInvalid()) {
Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -4362,6 +4362,8 @@
def err_default_arg_makes_ctor_special : Error<
"addition of default argument on redeclaration makes this constructor a "
"%select{default|copy|move}0 constructor">;
+def err_expr_statement_in_default_arg : Error<
+ "can't use expression statement in default argument">;
def err_use_of_default_argument_to_function_declared_later : Error<
"use of default argument to function %0 that is declared later in class %1">;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119609.413195.patch
Type: text/x-patch
Size: 3182 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220305/d04af567/attachment.bin>
More information about the cfe-commits
mailing list