From: Marek Polacek <polacek@redhat.com>
To: GCC Patches <gcc-patches@gcc.gnu.org>, Jason Merrill <jason@redhat.com>
Subject: C++ PATCH for c++/78244 - narrowing conversion in template not detected, part 2
Date: Thu, 17 Jan 2019 19:09:00 -0000 [thread overview]
Message-ID: <20190117190921.GM19569@redhat.com> (raw)
This patch ought to fix the rest of 78244, a missing narrowing warning in
decltype.
As I explained in Bugzilla, there can be three scenarios:
1) decltype is in a template and it has no dependent expressions, which
is the problematical case. finish_compound_literal just returns the
compound literal without checking narrowing if processing_template_decl.
But finish_decltype_type sees an expr that is not instantiation-dependent,
so it just takes its unlowered_expr_type and returns it, without calling
check_narrowing.
2) decltype is in a template and has dependent expressions
This works, because while finish_compound_literal just returns the compound
literal, finish_decltype_type sees an expr that is instantiation-dependent,
so it creates a DECLTYPE_TYPE.
tsubst_copy_and_build then calls
RETURN (finish_compound_literal (type, r, complain, cl));
while substituting the CONSTRUCTOR. Now processing_template_decl is 0, so
finish_compound_literal calls check_narrowing, so we detect it.
3) decltype is not in a template
finish_compound_literal calls check_narrowing and the narrowing is detected.
I think it's better not to blithely instantiate such decltypes just for the
benefit of check_narrowing, but we can still try to check narrowing even in a
template. check_narrowing calls instantiation_dependent_expression_p, so it
does nothing when there are dependent expressions.
Bootstrapped/regtested on x86_64-linux, ok for trunk?
2019-01-17 Marek Polacek <polacek@redhat.com>
PR c++/78244 - narrowing conversion in template not detected.
* semantics.c (finish_compound_literal): When processing a template,
try to check narrowing.
* g++.dg/cpp0x/Wnarrowing15.C: New test.
* g++.dg/cpp1y/Wnarrowing1.C: New test.
diff --git gcc/cp/semantics.c gcc/cp/semantics.c
index bc9d53800f7..828f1578697 100644
--- gcc/cp/semantics.c
+++ gcc/cp/semantics.c
@@ -2797,6 +2797,14 @@ finish_compound_literal (tree type, tree compound_literal,
if (processing_template_decl)
{
+ /* If there are no dependent expressions, we can detect narrowing
+ conversions. */
+ if (SCALAR_TYPE_P (type)
+ && CONSTRUCTOR_NELTS (compound_literal) == 1
+ && !check_narrowing (type,
+ CONSTRUCTOR_ELT (compound_literal, 0)->value,
+ complain))
+ return error_mark_node;
TREE_TYPE (compound_literal) = type;
/* Mark the expression as a compound literal. */
TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
diff --git gcc/testsuite/g++.dg/cpp0x/Wnarrowing15.C gcc/testsuite/g++.dg/cpp0x/Wnarrowing15.C
new file mode 100644
index 00000000000..4e7c17dcfca
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp0x/Wnarrowing15.C
@@ -0,0 +1,14 @@
+// PR c++/78244
+// { dg-do compile { target c++11 } }
+
+template <typename T>
+auto f1() -> decltype(int{2.0}, void()) { } // { dg-error "narrowing conversion" }
+
+template <typename T>
+auto f2() -> decltype(int{2.0}) { return 1; } // { dg-error "narrowing conversion" }
+
+template <typename T>
+auto f3() -> decltype(void(), int{2.0}) { return 1; } // { dg-error "narrowing conversion" }
+
+template <typename T>
+auto f4() -> decltype((int{2.0})) { return 1; } // { dg-error "narrowing conversion" }
diff --git gcc/testsuite/g++.dg/cpp1y/Wnarrowing1.C gcc/testsuite/g++.dg/cpp1y/Wnarrowing1.C
new file mode 100644
index 00000000000..e1e499542f0
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp1y/Wnarrowing1.C
@@ -0,0 +1,5 @@
+// PR c++/78244
+// { dg-do compile { target c++14 } }
+
+template<typename>
+decltype(int{1.1}) v; // { dg-error "narrowing conversion" }
next reply other threads:[~2019-01-17 19:09 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-17 19:09 Marek Polacek [this message]
2019-01-17 21:17 ` Jason Merrill
2019-01-18 14:12 ` Marek Polacek
2019-01-21 20:15 ` Jason Merrill
2019-01-22 21:12 ` Marek Polacek
2019-01-23 14:01 ` Jason Merrill
2019-01-23 18:29 ` Marek Polacek
2019-01-23 20:51 ` Jason Merrill
2019-01-25 0:51 ` Marek Polacek
2019-01-25 15:06 ` Jason Merrill
2019-01-25 21:55 ` Marek Polacek
2019-01-27 0:25 ` Jason Merrill
2019-01-27 20:18 ` Marek Polacek
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=20190117190921.GM19569@redhat.com \
--to=polacek@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).