public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Patrick Palka <ppalka@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-6123] c++: hard error w/ ptr+N and incomplete type [PR103700]
Date: Mon, 27 Dec 2021 14:05:39 +0000 (GMT)	[thread overview]
Message-ID: <20211227140539.1112C3858D39@sourceware.org> (raw)

https://gcc.gnu.org/g:88cdcb5c18d73bfc9960d774c678f0e8103b8031

commit r12-6123-g88cdcb5c18d73bfc9960d774c678f0e8103b8031
Author: Patrick Palka <ppalka@redhat.com>
Date:   Mon Dec 27 09:05:17 2021 -0500

    c++: hard error w/ ptr+N and incomplete type [PR103700]
    
    In pointer_int_sum when called from a SFINAE context, we need to avoid
    calling size_in_bytes_loc on an incomplete pointed-to type since this
    latter function isn't SFINAE-enabled and always emits an error on such
    input.
    
            PR c++/103700
    
    gcc/c-family/ChangeLog:
    
            * c-common.c (pointer_int_sum): When quiet, return
            error_mark_node for an incomplete pointed-to type and don't
            call size_in_bytes_loc.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/template/sfinae32.C: New test.

Diff:
---
 gcc/c-family/c-common.c                  |  2 ++
 gcc/testsuite/g++.dg/template/sfinae32.C | 24 ++++++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index a25d59fa77b..f3e3e9ba0a5 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -3308,6 +3308,8 @@ pointer_int_sum (location_t loc, enum tree_code resultcode,
     size_exp = integer_one_node;
   else
     {
+      if (!complain && !COMPLETE_TYPE_P (TREE_TYPE (result_type)))
+	return error_mark_node;
       size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
       /* Wrap the pointer expression in a SAVE_EXPR to make sure it
 	 is evaluated first when the size expression may depend
diff --git a/gcc/testsuite/g++.dg/template/sfinae32.C b/gcc/testsuite/g++.dg/template/sfinae32.C
new file mode 100644
index 00000000000..ae1dadaacc4
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/sfinae32.C
@@ -0,0 +1,24 @@
+// PR c++/103700
+// { dg-do compile { target c++11 } }
+
+template<class T, int N> auto f(T* p) -> decltype(p + N);
+template<class T, int N> auto f(T* p) -> decltype(p - N);
+template<class T, int N> auto f(T* p) -> decltype(N + p);
+template<class T, int N> void f(T* p);
+
+template<class T> auto g(T* p, int n) -> decltype(p + n);
+template<class T> auto g(T* p, int n) -> decltype(p - n);
+template<class T> auto g(T* p, int n) -> decltype(n + p);
+template<class T> void g(T* p, int n);
+
+struct Incomplete;
+
+int main() {
+  f<Incomplete,  0>(nullptr);
+  f<Incomplete,  1>(nullptr);
+  f<Incomplete, -1>(nullptr);
+  f<Incomplete,  7>(nullptr);
+  f<Incomplete, -7>(nullptr);
+
+  g<Incomplete>(nullptr, 0);
+}


                 reply	other threads:[~2021-12-27 14:05 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20211227140539.1112C3858D39@sourceware.org \
    --to=ppalka@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /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).