set_up_extended_ref_temp does an abbreviated version of cp_finish_decl, which didn't include the call to cp_fully_fold that store_init_value does for user-declared variables. Adding that fixes this bug.
Tested x86_64-pc-linux-gnu, applying to trunk.
commit 03b96c95fd7b27fa99964741bd9cb771ad56c882 Author: Jason Merrill <ja...@redhat.com> Date: Wed Apr 4 17:18:28 2018 -0400 PR c++/84938 - ICE with division by ~-1. * call.c (set_up_extended_ref_temp): Call cp_fully_fold. diff --git a/gcc/cp/call.c b/gcc/cp/call.c index f81773157b6..7c99e8ad910 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -10869,6 +10869,8 @@ set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups, /* If the initializer is constant, put it in DECL_INITIAL so we get static initialization and use in constant expressions. */ init = maybe_constant_init (expr); + /* As in store_init_value. */ + init = cp_fully_fold (init); if (TREE_CONSTANT (init)) { if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type)) diff --git a/gcc/testsuite/g++.dg/expr/div-by-zero1.C b/gcc/testsuite/g++.dg/expr/div-by-zero1.C new file mode 100644 index 00000000000..63bef406408 --- /dev/null +++ b/gcc/testsuite/g++.dg/expr/div-by-zero1.C @@ -0,0 +1,3 @@ +// PR c++/84938 + +const int &a = 1 / ~-1; // { dg-warning "division by zero" }