From: Dodji Seketeli <dodji@redhat.com>
To: GCC Patches <gcc-patches@gcc.gnu.org>
Cc: Jason Merrill <jason@redhat.com>
Subject: [PATCH] PR c++/55311 - Cannot specialize alias template with arg of type array of char
Date: Sat, 22 Dec 2012 16:04:00 -0000 [thread overview]
Message-ID: <87sj6yhy4y.fsf@redhat.com> (raw)
Hello,
Consider this test case:
1 template <const char *const C, typename T>
2 struct A
3 {};
4
5 struct B {};
6
7 extern constexpr char HELLO_WORLD[] = "hello world";
8
9 A<HELLO_WORLD, B> g; // <-- This works fine
10
11 template <typename T>
12 using PartiallySpecialized = A<HELLO_WORLD, T>; // <-- 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<HELLO_WORLD, T>; // <-- 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<HELLO_WORLD, T> 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 idea of the patch I am proposing is to do the test above on the
argument received by convert_nontype_argument rather than on its
modified (decayed?) form.
[1]: The relacement of the VAR_DECL by its initializer is done by
decay_conversion by callig decl_constant_value_safe. That replacement
doesn't happen if processing_template_decl is not set. That's why it
doesn't happen for the class template instantiation at line 9, leading
to no error message there.
Bootstrapped and tested on x86_64-unknown-linux-gnu against trunk.
gcc/cp/
PR c++/55311
* pt.c (convert_nontype_argument<array arguments>): Perform the
sanity tests on the initial expression otherwise decay_conversion
might have made us loose some information.
gcc/testsuite/
PR c++/55311
* g++.dg/cpp0x/alias-decl-30.C: New test.
---
gcc/cp/pt.c | 8 ++++----
gcc/testsuite/g++.dg/cpp0x/alias-decl-30.C | 15 +++++++++++++++
2 files changed, 19 insertions(+), 4 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-30.C
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 1b3f039..4af30cf 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -5454,7 +5454,7 @@ unify_overload_resolution_failure (bool explain_p, tree arg)
static tree
convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
{
- tree expr_type;
+ tree expr_type, initial_expr = expr;
/* Detect immediately string literals as invalid non-type argument.
This special-case is not needed for correctness (we would easily
@@ -5658,10 +5658,10 @@ convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
}
else
{
- tree decl;
+ tree decl = STRIP_NOPS (initial_expr);
- decl = ((TREE_CODE (expr) == ADDR_EXPR)
- ? TREE_OPERAND (expr, 0) : expr);
+ decl = ((TREE_CODE (initial_expr) == ADDR_EXPR)
+ ? TREE_OPERAND (initial_expr, 0) : initial_expr);
if (TREE_CODE (decl) != VAR_DECL)
{
error ("%qE is not a valid template argument of type %qT "
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 <const char *const C, typename T>
+struct A
+{};
+
+struct B {};
+
+extern constexpr char HELLO_WORLD[] = "hello world";
+
+A<HELLO_WORLD, B> g; // <-- This works fine
+
+template <typename T>
+using PartiallySpecialized = A<HELLO_WORLD, T>; // <-- This fails
--
Dodji
next reply other threads:[~2012-12-22 16:04 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-12-22 16:04 Dodji Seketeli [this message]
2012-12-24 5:38 ` Jason Merrill
2012-12-24 14:23 ` Dodji Seketeli
2012-12-26 18:54 ` Jason Merrill
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87sj6yhy4y.fsf@redhat.com \
--to=dodji@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jason@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).