From: Jakub Jelinek Date: Mon, 25 Jun 2018 17:01:10 +0000 (+0200) Subject: backport: re PR c++/79650 (ICE on invalid c++ code with label arithmetic in convert_n... X-Git-Tag: releases/gcc-6.5.0~238 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c3598e1dc10f7b224e4154833d62c7427831dd1a;p=thirdparty%2Fgcc.git backport: re PR c++/79650 (ICE on invalid c++ code with label arithmetic in convert_nontype_argument (pt.c:6515)) Backported from mainline 2017-12-14 Jakub Jelinek PR c++/79650 * pt.c (convert_nontype_argument): Diagnose reduced_constant_expression_p expressions that aren't INTEGER_CST. * g++.dg/template/pr79650.C: New test. From-SVN: r262045 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 045e5e127a44..4113b0a394d5 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,6 +1,12 @@ 2018-06-25 Jakub Jelinek Backported from mainline + 2017-12-14 Jakub Jelinek + + PR c++/79650 + * pt.c (convert_nontype_argument): Diagnose + reduced_constant_expression_p expressions that aren't INTEGER_CST. + 2017-12-06 Jakub Jelinek PR c++/80259 diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 9dbf17ba4d88..3f57c6afc689 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -6411,7 +6411,20 @@ convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain) return NULL_TREE; /* else cxx_constant_value complained but gave us a real constant, so go ahead. */ - gcc_assert (TREE_CODE (expr) == INTEGER_CST); + if (TREE_CODE (expr) != INTEGER_CST) + { + /* Some assemble time constant expressions like + (intptr_t)&&lab1 - (intptr_t)&&lab2 or + 4 + (intptr_t)&&var satisfy reduced_constant_expression_p + as we can emit them into .rodata initializers of + variables, yet they can't fold into an INTEGER_CST at + compile time. Refuse them here. */ + gcc_checking_assert (reduced_constant_expression_p (expr)); + location_t loc = EXPR_LOC_OR_LOC (expr, input_location); + error_at (loc, "template argument %qE for type %qT not " + "a constant integer", expr, type); + return NULL_TREE; + } } else return NULL_TREE; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index b9b51dce3921..ca38ff3f0af9 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,6 +1,11 @@ 2018-06-25 Jakub Jelinek Backported from mainline + 2017-12-14 Jakub Jelinek + + PR c++/79650 + * g++.dg/template/pr79650.C: New test. + 2017-12-08 Joseph Myers Alexander Monakov Jakub Jelinek diff --git a/gcc/testsuite/g++.dg/template/pr79650.C b/gcc/testsuite/g++.dg/template/pr79650.C new file mode 100644 index 000000000000..72c781409426 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/pr79650.C @@ -0,0 +1,20 @@ +// PR c++/79650 +// { dg-do compile { target c++11 } } +// { dg-options "" } + +typedef __INTPTR_TYPE__ intptr_t; +template struct A {}; + +void +foo () +{ + static int a, b; +lab1: +lab2: + A<(intptr_t)&&lab1 - (__INTPTR_TYPE__)&&lab2> c; // { dg-error "not a constant integer" } + A<(intptr_t)&&lab1 - (__INTPTR_TYPE__)&&lab1> d; + A<(intptr_t)&a - (intptr_t)&b> e; // { dg-error "is not a constant expression" } + A<(intptr_t)&a - (intptr_t)&a> f; + A<(intptr_t)sizeof(a) + (intptr_t)&a> g; // { dg-error "not a constant integer" } + A<(intptr_t)&a> h; // { dg-error "conversion from pointer type" } +}