Patchwork C++ PATCH for PR/39415 (type of static_cast<cv Derived*>(Base*))

login
register
mail settings
Submitter James Dennett
Date Nov. 12, 2010, 4:04 p.m.
Message ID <AANLkTikHRemE9UNTp__fT_2REgCh31rJ9szmsyvo85KX@mail.gmail.com>
Download mbox | patch
Permalink /patch/70983/
State New
Headers show

Comments

James Dennett - Nov. 12, 2010, 4:04 p.m.
This is a (morally 2-line) fix for PR/39415 (duplicated as PR/44916),
where static_cast<cv Derived *> could give the wrong cv-qualification,
with a short regression test.

I've tested this on Linux/x86-64 (only).

My thanks to Jason Merrill for supplying me with clues about how to
prepare and submit this .

Additional clues from others are very welcome.

-- James
Eric Botcazou - Nov. 12, 2010, 6:06 p.m.
> Additional clues from others are very welcome.

A few nits: 1) cp/ has its own ChangeLog so the entry should be put there 
(without the cp/ prefix).  2) You need to put the component in the PR 
reference, otherwise it doesn't serve any useful purpose.  3) Blank line 
after you name.  4) You need to write an entry for testsuite/ as well.
James Dennett - Nov. 17, 2010, 6:37 a.m.
On Fri, Nov 12, 2010 at 6:06 PM, Eric Botcazou <ebotcazou@adacore.com> wrote:
>> Additional clues from others are very welcome.
>
> A few nits: 1) cp/ has its own ChangeLog so the entry should be put there
> (without the cp/ prefix).  2) You need to put the component in the PR
> reference, otherwise it doesn't serve any useful purpose.  3) Blank line
> after you name.  4) You need to write an entry for testsuite/ as well.

Thanks for the review.  I applied the changes you suggested, but not
as quickly as Jason did.  Who knows, next time I might get it right
(or more nearly so) the first time.  On the bright side, this buglet
that's been lurking for years is now fixed at head, and I'm a little
more clued up on how to prepare patches for GCC.

-- James

Patch

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 166644)
+++ ChangeLog	(working copy)
@@ -1,3 +1,8 @@ 
+2010-11-12  James Dennett <jdennett@google.com>
+	PR/39415
+	* cp/typeck.c (build_static_cast_1): Convert to the target type
+	when doing static_cast<cv Derived*>(Base*).
+
 2010-11-11  Nathan Froyd  <froydnj@codesourcery.com>

 	PR c/44782
Index: testsuite/g++.dg/expr/static_cast7.C
===================================================================
--- testsuite/g++.dg/expr/static_cast7.C	(revision 0)
+++ testsuite/g++.dg/expr/static_cast7.C	(revision 0)
@@ -0,0 +1,10 @@ 
+// Regression test for bug 39415 (and its duplicate 44916).
+struct S {};
+struct T : S {};
+int f(const T*) {}
+void f(T*);
+int main() {
+  S* s(0);
+  int a = f(static_cast<const T*>(s));
+  int b = f(static_cast<const T*>(0));
+}
Index: cp/typeck.c
===================================================================
--- cp/typeck.c	(revision 166644)
+++ cp/typeck.c	(working copy)
@@ -5884,7 +5884,8 @@  build_static_cast_1 (tree type, tree exp
       base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
 			  c_cast_p ? ba_unique : ba_check,
 			  NULL);
-      return build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false);
+      expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false);
+      return cp_fold_convert(type, expr);
     }

   if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))