diff mbox series

C++ PATCH for c++/68810, wrong diagnostic location with reinterpret_cast

Message ID CADzB+2kBVv=W+Um+0Lb7JrGuy-SbMQM+R+_dM3CXEmou2rTtQg@mail.gmail.com
State New
Headers show
Series C++ PATCH for c++/68810, wrong diagnostic location with reinterpret_cast | expand

Commit Message

Jason Merrill Jan. 29, 2018, 8:55 p.m. UTC
Here, the problem was that convert_to_pointer_maybe_fold doesn't
entirely respect the dofold argument, and folds anyway if the argument
is constant.  In this case we really don't want folding.

Tested x86_64-pc-linux-gnu, applying to trunk.
commit ea38396630703879e5416265cd66db87845f3684
Author: Jason Merrill <jason@redhat.com>
Date:   Mon Jan 29 15:52:09 2018 -0500

            PR c++/68810 - wrong location for reinterpret_cast error.
    
            * cvt.c (cp_convert_to_pointer): Always build a CONVERT_EXPR when
            !dofold.
diff mbox series

Patch

diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
index 3ab3e2e2b40..f5da08bbee2 100644
--- a/gcc/cp/cvt.c
+++ b/gcc/cp/cvt.c
@@ -239,6 +239,11 @@  cp_convert_to_pointer (tree type, tree expr, bool dofold,
       gcc_assert (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (expr)))
 		  == GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)));
 
+      /* FIXME needed because convert_to_pointer_maybe_fold still folds
+	 conversion of constants.  */
+      if (!dofold)
+	return build1 (CONVERT_EXPR, type, expr);
+
       return convert_to_pointer_maybe_fold (type, expr, dofold);
     }
 
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-reinterpret1.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-reinterpret1.C
index d2ee2bac223..d7d244f752d 100644
--- a/gcc/testsuite/g++.dg/cpp0x/constexpr-reinterpret1.C
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-reinterpret1.C
@@ -16,7 +16,9 @@  public:
 
   constexpr static Inner & getInner()
   /* I am surprised this is considered a constexpr */
-  { return *((Inner *)4); } // { dg-error "reinterpret_cast" }
+  {
+    return *((Inner *)4); // { dg-error "reinterpret_cast" }
+  }
 };
 
 B B::instance;