The patch fixes a bug in the detection of the usage of static variables
inside generic lambdas. The comment in finish_id_expression_1 already
mentions this case, but the code didn't actually handle it.
PR c++/114450
gcc/cp/ChangeLog:
* lambda.cc (generic_lambda_fn_p): Handle null argument.
* semantics.cc (finish_id_expression_1): Check for generic lambda.
gcc/testsuite/ChangeLog:
* g++.dg/warn/Wunused-var-42.C: New test.
Co-authored-by: Jason Merrill <jason@redhat.com>
bool
generic_lambda_fn_p (tree callop)
{
- return (LAMBDA_FUNCTION_P (callop)
+ return (callop && LAMBDA_FUNCTION_P (callop)
&& DECL_TEMPLATE_INFO (callop)
&& PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (callop)));
}
/* A use in unevaluated operand might not be instantiated appropriately
if tsubst_copy builds a dummy parm, or if we never instantiate a
generic lambda, so mark it now. */
- if (processing_template_decl && cp_unevaluated_operand)
+ if (processing_template_decl
+ && (cp_unevaluated_operand
+ || generic_lambda_fn_p (current_function_decl)))
mark_type_use (decl);
/* Disallow uses of local variables from containing functions, except
--- /dev/null
+// { dg-do compile { target c++14 } }
+// { dg-options "-Wunused" }
+
+template <typename F>
+void f (F &&d)
+{
+ static unsigned context;
+ d(context);
+}
+
+void g ()
+{
+ static int b;
+ f([](auto c) { return c <= b; });
+}
+
+void h ()
+{
+ static int b = 0; // { dg-warning "unused variable" }
+ f([](auto c) { return c <= 0; });
+}
+
+void i ()
+{
+ static int b = 0;
+ [](auto c) { return c <= b; };
+}
+
+void j ()
+{
+ static int b = 0; // { dg-warning "unused variable" }
+ [](auto c) { return c <= 0; };
+}