From 9bdae0133df72dab6604c2b9b25f6411e4c41e8b Mon Sep 17 00:00:00 2001 From: Dodji Seketeli Date: Mon, 7 Jan 2013 09:06:46 +0100 Subject: [PATCH] PR c++/55311 - Cannot specialize alias template with arg of type array of char MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Consider this test case: 1 template 2 struct A 3 {}; 4 5 struct B {}; 6 7 extern constexpr char HELLO_WORLD[] = "hello world"; 8 9 A g; // <-- This works fine 10 11 template 12 using PartiallySpecialized = A; // <-- This fails 13 At line 12 G++ fails to instantiate the alias template that has a string variable initialized with a string literal, with the error message: test.cc:12:46: error: ‘"hello world"’ is not a valid template argument of type ‘const char*’ because ‘"hello world"’ is not a variable using PartiallySpecialized = A; // <-- This fails ^ Note that instantiating the template A at line 9 with the same arguments as in the problematic case above works. This happens in the context of lookup_template_class_1, when it handles the alias template instantiation A and thus passes the VAR_DECL for HELLO_WORLD to convert_nontype_argument. Note that from there decay_conversion replaces the the VAR_DECL with its STRING_CST initializer[1]. Latter on, convert_nontype_argument checks that the HELLO_WORLD constant it received as argument was indeed a VAR_DECL: else { tree decl; decl = ((TREE_CODE (expr) == ADDR_EXPR) ? TREE_OPERAND (expr, 0) : expr); if (TREE_CODE (decl) != VAR_DECL) { error ("%qE is not a valid template argument of type %qT " "because %qE is not a variable", expr, type, decl); return NULL_TREE; } But the issue is, that VAR_DECL has been replaced by STRING_CST, so the last 'if' above fails. The fix is to teach decay_conversion to return the address of array, rather than returning its initializer. Bootstrapped and tested on x86_64-unknown-linux-gnu against trunk. gcc/cp/ PR c++/55311 * pt.c (decay_conversion): Do not return the initializer of an array. gcc/testsuite/ PR c++/55311 * g++.dg/cpp0x/alias-decl-30.C: New test. * g++.dg/init/array21.C: New test. From-SVN: r194961 --- gcc/cp/typeck.c | 20 +++++++++++++------- gcc/testsuite/g++.dg/cpp0x/alias-decl-30.C | 15 +++++++++++++++ gcc/testsuite/g++.dg/init/array21.C | 2 +- 3 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-30.C diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index ef76dcd..66fb33a 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -1880,19 +1880,25 @@ decay_conversion (tree exp, tsubst_flags_t complain) return error_mark_node; } - /* FIXME remove? at least need to remember that this isn't really a - constant expression if EXP isn't decl_constant_var_p, like with - C_MAYBE_CONST_EXPR. */ - exp = decl_constant_value_safe (exp); - if (error_operand_p (exp)) - return error_mark_node; + code = TREE_CODE (type); + + /* For an array decl decay_conversion should not try to return its + initializer. */ + if (code != ARRAY_TYPE) + { + /* FIXME remove? at least need to remember that this isn't really a + constant expression if EXP isn't decl_constant_var_p, like with + C_MAYBE_CONST_EXPR. */ + exp = decl_constant_value_safe (exp); + if (error_operand_p (exp)) + return error_mark_node; + } if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp)) return nullptr_node; /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue. Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */ - code = TREE_CODE (type); if (code == VOID_TYPE) { if (complain & tf_error) diff --git a/gcc/testsuite/g++.dg/cpp0x/alias-decl-30.C b/gcc/testsuite/g++.dg/cpp0x/alias-decl-30.C new file mode 100644 index 0000000..7ad5e6d --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/alias-decl-30.C @@ -0,0 +1,15 @@ +// Origin PR c++/55311 +// { dg-do compile { target c++11 } } + +template +struct A +{}; + +struct B {}; + +extern constexpr char HELLO_WORLD[] = "hello world"; + +A g; // <-- This works fine + +template +using PartiallySpecialized = A; // <-- This fails diff --git a/gcc/testsuite/g++.dg/init/array21.C b/gcc/testsuite/g++.dg/init/array21.C index 5438af1..fc6abab 100644 --- a/gcc/testsuite/g++.dg/init/array21.C +++ b/gcc/testsuite/g++.dg/init/array21.C @@ -3,5 +3,5 @@ void foo() { const int x[] = 0; // { dg-error "initializer" } - ++x; + ++x; // { dg-error "read-only|operand" } } -- 2.7.4