From a607b96e80f5790c88b80a8ac0b0c0dd88fe413b Mon Sep 17 00:00:00 2001 From: Mark Mitchell Date: Mon, 9 Apr 2007 19:45:53 +0000 Subject: [PATCH] re PR c++/31449 ([4.1] static_cast can remove const-ness) PR c++/31449 * class.c (build_base_path): Ensure that the converted pointer has the same cv-qualification as the input. PR c++/31449 * g++.dg/init/const5.C: New test. From-SVN: r123679 --- gcc/cp/ChangeLog | 6 ++++++ gcc/cp/class.c | 13 ++++++++++++- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/g++.dg/init/const5.C | 11 +++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/init/const5.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 9f21ddc..b9823db 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2007-04-09 Mark Mitchell + + PR c++/31449 + * class.c (build_base_path): Ensure that the converted pointer has + the same cv-qualification as the input. + 2007-04-09 Paolo Carlini * tree.c (cp_tree_equal): Deal with TRAIT_EXPR. diff --git a/gcc/cp/class.c b/gcc/cp/class.c index 0bb441e..e01fbe1 100644 --- a/gcc/cp/class.c +++ b/gcc/cp/class.c @@ -303,7 +303,18 @@ build_base_path (enum tree_code code, field, because other parts of the compiler know that such expressions are always non-NULL. */ if (!virtual_access && integer_zerop (offset)) - return build_nop (build_pointer_type (target_type), expr); + { + tree class_type; + /* TARGET_TYPE has been extracted from BINFO, and, is + therefore always cv-unqualified. Extract the + cv-qualifiers from EXPR so that the expression returned + matches the input. */ + class_type = TREE_TYPE (TREE_TYPE (expr)); + target_type + = cp_build_qualified_type (target_type, + cp_type_quals (class_type)); + return build_nop (build_pointer_type (target_type), expr); + } null_test = error_mark_node; } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 90550f2..037e7a2 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2007-04-09 Mark Mitchell + + PR c++/31449 + * g++.dg/init/const5.C: New test. + 2007-04-09 Jan Hubicka * gcc.dg/tree-ssa/foldaddr-3.c: New file. diff --git a/gcc/testsuite/g++.dg/init/const5.C b/gcc/testsuite/g++.dg/init/const5.C new file mode 100644 index 0000000..fa8d8fc --- /dev/null +++ b/gcc/testsuite/g++.dg/init/const5.C @@ -0,0 +1,11 @@ +// PR c++/31449 + +class Foo {}; +class Bar : public Foo {}; +static const Foo *foo = 0; + +static Bar *bar = static_cast(foo); // { dg-error "conversion" } + +void func(const Foo *foo) { + Bar *bar = static_cast(foo); // { dg-error "conversion" } +} -- 2.7.4