From 643d4cd6a10f6d202011eb3005c65994219932f3 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Tue, 9 Feb 2010 15:05:51 -0500 Subject: [PATCH] re PR c++/42370 ([C++0x][lambda] in a void function: "Warning: control reaches end of non-void function") PR c++/42370 * decl2.c (change_return_type): New fn. * semantics.c (apply_lambda_return_type): Use it. * cp-tree.h: Declare it. From-SVN: r156634 --- gcc/cp/ChangeLog | 7 +++++ gcc/cp/cp-tree.h | 1 + gcc/cp/decl2.c | 27 +++++++++++++++++++ gcc/cp/semantics.c | 2 +- gcc/testsuite/ChangeLog | 5 ++++ .../g++.dg/cpp0x/lambda/lambda-warn2.C | 7 +++++ 6 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/lambda/lambda-warn2.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 26926ce0b16..42c0e9f44aa 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,10 @@ +2010-02-09 Jason Merrill + + PR c++/42370 + * decl2.c (change_return_type): New fn. + * semantics.c (apply_lambda_return_type): Use it. + * cp-tree.h: Declare it. + 2010-02-05 Richard Guenther * Make-lang.in (cp/cp-lang.o): Depend on gt-cp-cp-lang.h. diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 2f925e15e8a..27c820b8ed1 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -4732,6 +4732,7 @@ extern tree cxx_maybe_build_cleanup (tree); /* in decl2.c */ extern bool check_java_method (tree); extern tree build_memfn_type (tree, tree, cp_cv_quals); +extern tree change_return_type (tree, tree); extern void maybe_retrofit_in_chrg (tree); extern void maybe_make_one_only (tree); extern bool vague_linkage_fn_p (tree); diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c index 2b284fb1262..c5b6e874ed2 100644 --- a/gcc/cp/decl2.c +++ b/gcc/cp/decl2.c @@ -139,6 +139,33 @@ build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals) return fntype; } +/* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its + return type changed to NEW_RET. */ + +tree +change_return_type (tree new_ret, tree fntype) +{ + tree newtype; + tree args = TYPE_ARG_TYPES (fntype); + tree raises = TYPE_RAISES_EXCEPTIONS (fntype); + tree attrs = TYPE_ATTRIBUTES (fntype); + + if (same_type_p (new_ret, TREE_TYPE (fntype))) + return fntype; + + if (TREE_CODE (fntype) == FUNCTION_TYPE) + newtype = build_function_type (new_ret, args); + else + newtype = build_method_type_directly (TYPE_METHOD_BASETYPE (fntype), + new_ret, TREE_CHAIN (args)); + if (raises) + newtype = build_exception_variant (newtype, raises); + if (attrs) + newtype = cp_build_type_attribute_variant (newtype, attrs); + + return newtype; +} + /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE appropriately. */ diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 441081c3d96..39085be19d4 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -5584,7 +5584,7 @@ apply_lambda_return_type (tree lambda, tree return_type) /* TREE_TYPE (FUNCTION_DECL) == METHOD_TYPE TREE_TYPE (METHOD_TYPE) == return-type */ - TREE_TYPE (TREE_TYPE (fco)) = return_type; + TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco)); result = DECL_RESULT (fco); if (result == NULL_TREE) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 4dd6762a758..b91829c5b9c 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2010-02-09 Jason Merrill + + PR c++/42370 + * g++.dg/cpp0x/lambda/lambda-warn2.C: New. + 2010-02-09 Tobias Burnus PR fortran/41869 diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-warn2.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-warn2.C new file mode 100644 index 00000000000..ce5e7c4503a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-warn2.C @@ -0,0 +1,7 @@ +// PR c++/42370 +// { dg-options "-std=c++0x -Wall" } + +void foo() +{ + []{ return 0; }(); +} // { dg-bogus "no return statement" }