Hi! As the testcase shows, BLOCK_VARS of the outermost scope can contain decls other than VAR_DECL that have DECL_NAME identical to what we are looking for, in this case a LABEL_DECL. The code is looking for the VAR_DECL that has been NRV optimized, and while e.g. DECL_HAS_VALUE_EXPR_P is allowed on all of VAR_DECL, PARM_DECL and RESULT_DECL, BLOCK_VARS should not contain the latter two.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk and release branches? 2017-06-07 Jakub Jelinek <ja...@redhat.com> PR c++/80984 * cp-gimplify.c (cp_genericize): Only look for VAR_DECLs in BLOCK_VARS (outer) chain. (cxx_omp_const_qual_no_mutable): Likewise. * g++.dg/opt/nrv18.C: New test. --- gcc/cp/cp-gimplify.c.jj 2017-05-22 20:49:04.000000000 +0200 +++ gcc/cp/cp-gimplify.c 2017-06-06 10:11:22.780850949 +0200 @@ -1590,7 +1590,8 @@ cp_genericize (tree fndecl) if (outer) for (var = BLOCK_VARS (outer); var; var = DECL_CHAIN (var)) - if (DECL_NAME (t) == DECL_NAME (var) + if (VAR_P (var) + && DECL_NAME (t) == DECL_NAME (var) && DECL_HAS_VALUE_EXPR_P (var) && DECL_VALUE_EXPR (var) == t) { @@ -1837,7 +1838,8 @@ cxx_omp_const_qual_no_mutable (tree decl if (outer) for (var = BLOCK_VARS (outer); var; var = DECL_CHAIN (var)) - if (DECL_NAME (decl) == DECL_NAME (var) + if (VAR_P (var) + && DECL_NAME (decl) == DECL_NAME (var) && (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (var)))) { --- gcc/testsuite/g++.dg/opt/nrv18.C.jj 2017-06-06 10:13:00.925650648 +0200 +++ gcc/testsuite/g++.dg/opt/nrv18.C 2017-06-06 10:12:10.670265267 +0200 @@ -0,0 +1,12 @@ +// PR c++/80984 +// { dg-do compile } + +struct A { ~A (); }; + +A +foo () +{ + A a; +a: + return a; +} Jakub