Patchwork [C++] PR 53158

login
register
mail settings
Submitter Paolo Carlini
Date May 8, 2012, 3:28 a.m.
Message ID <4FA892D5.9030700@oracle.com>
Download mbox | patch
Permalink /patch/157550/
State New
Headers show

Comments

Paolo Carlini - May 8, 2012, 3:28 a.m.
On 05/08/2012 04:10 AM, Jason Merrill wrote:
> How do we even get to calling cp_truthvalue_conversion?
ocp_convert wants to do that, around line 750... And I have a brand new 
proposal ;)

The patchlet below (which passes testing) leads to very consistent error 
messages for the testcase in the PR, and:

void foo();

void bar(int a, int b)
{
if (foo())
;
}

and

void foo();

void bar(int a, int b)
{
if (foo() && a < b)
;
}

exactly the same actually. But maybe we want to differentiate somehow 
what we emit for lambdas, but I don't think we want the status quo for 
%qE for a lambda here, because it would be:

error: could not convert ‘b.main()::<lambda()>()’ from ‘void’ to ‘bool’

What do you think?

Thanks!
Paolo.

///////////////////////////
Jason Merrill - May 8, 2012, 1 p.m.
On 05/07/2012 11:28 PM, Paolo Carlini wrote:
> error: could not convert ‘b.main()::<lambda()>()’ from ‘void’ to ‘bool’

It wouldn't say "operator()"?

I think I'd leave that alone; it is somewhat more informative (about 
what b() expands to) and we're moving toward replacing %qE with caret 
anyway.

The patch to ocp_convert is OK.

Jason
Paolo Carlini - May 8, 2012, 1:28 p.m.
On 05/08/2012 03:00 PM, Jason Merrill wrote:
> On 05/07/2012 11:28 PM, Paolo Carlini wrote:
>> error: could not convert ‘b.main()::<lambda()>()’ from ‘void’ to ‘bool’
>
> It wouldn't say "operator()"?
Nope, it says exactly the above. If you tell me what would be more 
sensible which remaining informative, I can see if I can quickly prepare 
something... like b.operator()? Would it be better? That would be very 
quick to implement: b is for free, and the arguments too, if there is 
something better we want to print in the middle, just let me know.
> I think I'd leave that alone; it is somewhat more informative (about 
> what b() expands to) and we're moving toward replacing %qE with caret 
> anyway.
... or anyway.
> The patch to ocp_convert is OK.
Great, thanks.

Paolo.

Patch

Index: testsuite/g++.dg/cpp0x/lambda/lambda-err2.C
===================================================================
--- testsuite/g++.dg/cpp0x/lambda/lambda-err2.C	(revision 0)
+++ testsuite/g++.dg/cpp0x/lambda/lambda-err2.C	(revision 0)
@@ -0,0 +1,12 @@ 
+// PR c++/53158
+// { dg-do compile { target c++11 } }
+
+int main()
+{
+  auto a = []() { return true; };
+  auto b = []() { return a(); };  // { dg-error "'a' is not captured" }
+  int c, d;
+  while (b() && c < d) // { dg-error "could not convert 'b\\(\\)' from 'void' to 'bool'" }
+    {
+    }
+}
Index: cp/error.c
===================================================================
--- cp/error.c	(revision 187249)
+++ cp/error.c	(working copy)
@@ -1897,6 +1897,13 @@  dump_expr (tree t, int flags)
 	    if (TREE_CODE (ob) == ADDR_EXPR)
 	      {
 		dump_expr (TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
+		/* Handle lambdas specially.  */
+		if (TREE_CODE (fn) == FUNCTION_DECL
+		    && DECL_NAME (fn) && LAMBDA_FUNCTION_P (fn))
+		  {
+		    dump_call_expr_args (t, flags, true);
+		    return;
+		  }
 		pp_cxx_dot (cxx_pp);
 	      }
 	    else if (TREE_CODE (ob) != PARM_DECL
Index: cp/cvt.c
===================================================================
--- cp/cvt.c	(revision 187249)
+++ cp/cvt.c	(working copy)
@@ -743,6 +743,12 @@  ocp_convert (tree type, tree expr, int convtype, i
 	}
       if (code == BOOLEAN_TYPE)
 	{
+	  if (TREE_CODE (intype) == VOID_TYPE)
+	    {
+	      error ("could not convert %qE from %<void%> to %<bool%>", expr);
+	      return error_mark_node;
+	    }
+
 	  /* We can't implicitly convert a scoped enum to bool, so convert
 	     to the underlying type first.  */
 	  if (SCOPED_ENUM_P (intype) && (convtype & CONV_STATIC))