commit a3370f6e8c39a98f9c1dffa72d27e92f9afc8271 Author: Jason Merrill Date: Tue Oct 10 12:40:32 2017 -0400 More delayed lambda capture fixes. * call.c (add_function_candidate): Use build_address. (build_op_call_1): Call mark_lvalue_use early. (build_over_call): Handle error from build_this. * constexpr.c (cxx_bind_parameters_in_call): Use build_address. (cxx_eval_increment_expression): Don't use rvalue(). * cvt.c (convert_to_void): Use mark_discarded_use. * expr.c (mark_use): Handle PARM_DECL, NON_DEPENDENT_EXPR. Fix reference handling. Don't copy the expression. (mark_discarded_use): New. * lambda.c (insert_capture_proxy): Add some sanity checking. (maybe_add_lambda_conv_op): Set cp_unevaluated_operand. * pt.c (register_local_specialization): Add sanity check. * semantics.c (process_outer_var_ref): Fix check for existing proxy. * typeck.c (cp_build_addr_expr_1): Handle error from mark_lvalue_use. (cp_build_modify_expr): Call mark_lvalue_use_nonread, handle error from rvalue. diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 9d747be9d79..13269024547 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -2160,7 +2160,10 @@ add_function_candidate (struct z_candidate **candidates, else { parmtype = build_pointer_type (parmtype); - arg = build_this (arg); + /* We don't use build_this here because we don't want to + capture the object argument until we've chosen a + non-static member function. */ + arg = build_address (arg); argtype = lvalue_type (arg); } } @@ -4446,14 +4449,17 @@ build_op_call_1 (tree obj, vec **args, tsubst_flags_t complain) { struct z_candidate *candidates = 0, *cand; tree fns, convs, first_mem_arg = NULL_TREE; - tree type = TREE_TYPE (obj); bool any_viable_p; tree result = NULL_TREE; void *p; + obj = mark_lvalue_use (obj); + if (error_operand_p (obj)) return error_mark_node; + tree type = TREE_TYPE (obj); + obj = prep_operand (obj); if (TYPE_PTRMEMFUNC_P (type)) @@ -7772,6 +7778,9 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain) tree converted_arg; tree base_binfo; + if (arg == error_mark_node) + return error_mark_node; + if (convs[i]->bad_p) { if (complain & tf_error) diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index f279f707676..59192829d71 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -1268,7 +1268,10 @@ cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, && is_dummy_object (x)) { x = ctx->object; - x = cp_build_addr_expr (x, tf_warning_or_error); + /* We don't use cp_build_addr_expr here because we don't want to + capture the object argument until we've chosen a non-static member + function. */ + x = build_address (x); } bool lval = false; arg = cxx_eval_constant_expression (ctx, x, lval, @@ -3642,9 +3645,9 @@ cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t, non_constant_p, overflow_p); /* The operand as an rvalue. */ - tree val = rvalue (op); - val = cxx_eval_constant_expression (ctx, val, false, - non_constant_p, overflow_p); + tree val + = cxx_eval_constant_expression (ctx, op, false, + non_constant_p, overflow_p); /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to a local array in a constexpr function. */ bool ptr = POINTER_TYPE_P (TREE_TYPE (val)); diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 482c977f6b4..1c008dbb3b3 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -6270,6 +6270,7 @@ extern tree mark_rvalue_use (tree, extern tree mark_lvalue_use (tree); extern tree mark_lvalue_use_nonread (tree); extern tree mark_type_use (tree); +extern tree mark_discarded_use (tree); extern void mark_exp_read (tree); /* friend.c */ diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c index a3bd4a137d8..57cde632cc8 100644 --- a/gcc/cp/cvt.c +++ b/gcc/cp/cvt.c @@ -1055,24 +1055,10 @@ convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain) || TREE_TYPE (expr) == error_mark_node) return error_mark_node; + expr = mark_discarded_use (expr); if (implicit == ICV_CAST) + /* An explicit cast to void avoids all -Wunused-but-set* warnings. */ mark_exp_read (expr); - else - { - tree exprv = expr; - - while (TREE_CODE (exprv) == COMPOUND_EXPR) - exprv = TREE_OPERAND (exprv, 1); - if (DECL_P (exprv) - || handled_component_p (exprv) - || INDIRECT_REF_P (exprv)) - /* Expr is not being 'used' here, otherwise we whould have - called mark_{rl}value_use use here, which would have in turn - called mark_exp_read. Rather, we call mark_exp_read directly - to avoid some warnings when - -Wunused-but-set-{variable,parameter} is in effect. */ - mark_exp_read (exprv); - } if (!TREE_TYPE (expr)) return expr; diff --git a/gcc/cp/expr.c b/gcc/cp/expr.c index f5c8e801918..23e30cf789c 100644 --- a/gcc/cp/expr.c +++ b/gcc/cp/expr.c @@ -96,16 +96,21 @@ mark_use (tree expr, bool rvalue_p, bool read_p, { #define RECUR(t) mark_use ((t), rvalue_p, read_p, loc, reject_builtin) + if (expr == NULL_TREE || expr == error_mark_node) + return expr; + if (reject_builtin && reject_gcc_builtin (expr, loc)) return error_mark_node; if (read_p) mark_exp_read (expr); + tree oexpr = expr; bool recurse_op[3] = { false, false, false }; switch (TREE_CODE (expr)) { case VAR_DECL: + case PARM_DECL: if (outer_automatic_var_p (expr) && decl_constant_var_p (expr)) { @@ -119,10 +124,13 @@ mark_use (tree expr, bool rvalue_p, bool read_p, } } expr = process_outer_var_ref (expr, tf_warning_or_error, true); - expr = convert_from_reference (expr); + if (!(TREE_TYPE (oexpr) + && TREE_CODE (TREE_TYPE (oexpr)) == REFERENCE_TYPE)) + expr = convert_from_reference (expr); } break; case COMPONENT_REF: + case NON_DEPENDENT_EXPR: recurse_op[0] = true; break; case COMPOUND_EXPR: @@ -140,35 +148,23 @@ mark_use (tree expr, bool rvalue_p, bool read_p, tree ref = TREE_OPERAND (expr, 0); tree r = mark_rvalue_use (ref, loc, reject_builtin); if (r != ref) - { - expr = copy_node (expr); - TREE_OPERAND (expr, 0) = r; - } + expr = convert_from_reference (r); } break; default: break; } - bool changed = false; - tree ops[3]; for (int i = 0; i < 3; ++i) if (recurse_op[i]) { tree op = TREE_OPERAND (expr, i); - ops[i] = RECUR (op); - if (ops[i] != op) - changed = true; + op = RECUR (op); + if (op == error_mark_node) + return error_mark_node; + TREE_OPERAND (expr, i) = op; } - if (changed) - { - expr = copy_node (expr); - for (int i = 0; i < 3; ++i) - if (recurse_op[i]) - TREE_OPERAND (expr, i) = ops[i]; - } - return expr; #undef RECUR } @@ -187,6 +183,52 @@ mark_rvalue_use (tree e, return mark_use (e, true, true, loc, reject_builtin); } +/* Called when expr appears as a discarded-value expression. */ + +tree +mark_discarded_use (tree expr) +{ + /* The lvalue-to-rvalue conversion (7.1) is applied if and only if the + expression is a glvalue of volatile-qualified type and it is one of the + following: + * ( expression ), where expression is one of these expressions, + * id-expression (8.1.4), + * subscripting (8.2.1), + * class member access (8.2.5), + * indirection (8.3.1), + * pointer-to-member operation (8.5), + * conditional expression (8.16) where both the second and the third + operands are one of these expressions, or + * comma expression (8.19) where the right operand is one of these + expressions. */ + if (expr == NULL_TREE) + return expr; + + switch (TREE_CODE (expr)) + { + case COND_EXPR: + TREE_OPERAND (expr, 2) = mark_discarded_use (TREE_OPERAND (expr, 2)); + gcc_fallthrough (); + case COMPOUND_EXPR: + TREE_OPERAND (expr, 1) = mark_discarded_use (TREE_OPERAND (expr, 1)); + return expr; + + case COMPONENT_REF: + case ARRAY_REF: + case INDIRECT_REF: + case MEMBER_REF: + break; + default: + if (DECL_P (expr)) + break; + else + return expr; + } + + /* Like mark_rvalue_use, but don't reject built-ins. */ + return mark_use (expr, true, true, input_location, false); +} + /* Called whenever an expression is used in an lvalue context. */ tree diff --git a/gcc/cp/lambda.c b/gcc/cp/lambda.c index 6f611487ddb..76f2f29578f 100644 --- a/gcc/cp/lambda.c +++ b/gcc/cp/lambda.c @@ -297,7 +297,17 @@ void insert_capture_proxy (tree var) { if (is_normal_capture_proxy (var)) - register_local_specialization (var, DECL_CAPTURED_VARIABLE (var)); + { + tree cap = DECL_CAPTURED_VARIABLE (var); + if (CHECKING_P) + { + gcc_assert (!is_normal_capture_proxy (cap)); + tree old = retrieve_local_specialization (cap); + if (old) + gcc_assert (DECL_CONTEXT (old) != DECL_CONTEXT (var)); + } + register_local_specialization (var, cap); + } /* Put the capture proxy in the extra body block so that it won't clash with a later local variable. */ @@ -1188,7 +1198,10 @@ maybe_add_lambda_conv_op (tree type) if (generic_lambda_p) { + /* Avoid capturing variables in this context. */ + ++cp_unevaluated_operand; tree a = forward_parm (tgt); + --cp_unevaluated_operand; CALL_EXPR_ARG (call, ix) = a; if (decltype_call) diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 7883c64f33f..142a090cbab 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -11873,6 +11873,8 @@ cp_convert_range_for (tree statement, tree range_decl, tree range_expr, tree iter_type, begin_expr, end_expr; tree condition, expression; + range_expr = mark_lvalue_use (range_expr); + if (range_decl == error_mark_node || range_expr == error_mark_node) /* If an error happened previously do nothing or else a lot of unhelpful errors would be issued. */ diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 78748a91a07..ba52f3b57a6 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -1895,6 +1895,7 @@ reregister_specialization (tree spec, tree tinfo, tree new_spec) void register_local_specialization (tree spec, tree tmpl) { + gcc_assert (tmpl != spec); local_specializations->put (tmpl, spec); } diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index eb36b3029f1..f182006dfc2 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -3320,8 +3320,12 @@ process_outer_var_ref (tree decl, tsubst_flags_t complain, bool force_use) if (containing_function && LAMBDA_FUNCTION_P (containing_function)) { /* Check whether we've already built a proxy. */ - tree d = retrieve_local_specialization (decl); - if (d && is_capture_proxy (d)) + tree var = decl; + while (is_normal_capture_proxy (var)) + var = DECL_CAPTURED_VARIABLE (var); + tree d = retrieve_local_specialization (var); + + if (d && d != decl && is_capture_proxy (d)) { if (DECL_CONTEXT (d) == containing_function) /* We already have an inner proxy. */ diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index fc114b8f592..08b2ae555e6 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -5654,6 +5654,9 @@ cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain) return error_mark_node; arg = mark_lvalue_use (arg); + if (error_operand_p (arg)) + return error_mark_node; + argtype = lvalue_type (arg); gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg))); @@ -7698,6 +7701,8 @@ tree cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode, tree rhs, tsubst_flags_t complain) { + lhs = mark_lvalue_use_nonread (lhs); + tree result = NULL_TREE; tree newrhs = rhs; tree lhstype = TREE_TYPE (lhs); @@ -7920,6 +7925,8 @@ cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode, operator. -- end note ] */ lhs = cp_stabilize_reference (lhs); rhs = rvalue (rhs); + if (rhs == error_mark_node) + return error_mark_node; rhs = stabilize_expr (rhs, &init); newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain); if (newrhs == error_mark_node) diff --git a/gcc/testsuite/g++.dg/cpp0x/error1.C b/gcc/testsuite/g++.dg/cpp0x/error1.C index 33557f2f80b..115d800bb35 100644 --- a/gcc/testsuite/g++.dg/cpp0x/error1.C +++ b/gcc/testsuite/g++.dg/cpp0x/error1.C @@ -1,10 +1,17 @@ // PR c++/34395 // { dg-do compile { target c++11 } } -template void foo (int... x[N]) // { dg-message "int \\\[N\\\]\\.\\.\\. x" } +void f(...); +template void foo (int... x[N]) // { dg-message "declared here" } { struct A { - A () { x; } // { dg-error "use of parameter from containing function" } + A () { f(x...); } // { dg-error "use of parameter from containing function" } }; } + +int main() +{ + int ar[4]; + foo<4>(ar); +} diff --git a/gcc/testsuite/g++.dg/cpp1y/lambda-generic-ice5.C b/gcc/testsuite/g++.dg/cpp1y/lambda-generic-ice5.C index 473e412cb9d..88b7d1a05a1 100644 --- a/gcc/testsuite/g++.dg/cpp1y/lambda-generic-ice5.C +++ b/gcc/testsuite/g++.dg/cpp1y/lambda-generic-ice5.C @@ -12,7 +12,7 @@ using Void = void; template auto -bar(F f, A a) -> decltype( ( f(a) , 0 ) ) // { dg-error "no match" } +bar(F f, A a) -> decltype( ( f(a) , 0 ) ) // { dg-message "" } { return {}; } diff --git a/gcc/testsuite/g++.dg/template/crash108.C b/gcc/testsuite/g++.dg/template/crash108.C index 221d80ee5f1..9bcabc6009b 100644 --- a/gcc/testsuite/g++.dg/template/crash108.C +++ b/gcc/testsuite/g++.dg/template/crash108.C @@ -1,5 +1,5 @@ // PR c++/50861 -template struct A {A(int b=k(0));}; // { dg-error "parameter|arguments" } +template struct A {A(int b=k(0));}; // { dg-error "parameter|argument" } void f(int k){A a;} // // { dg-message "declared" } // { dg-message "note" "note" { target *-*-* } 3 }