Skip to content

Commit aa0ed84

Browse files
dynamic_cast in constant evaluation requires public bases [GH #398]
1 parent 468c292 commit aa0ed84

18 files changed

Lines changed: 415 additions & 243 deletions

File tree

‎src/interpret.c‎

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26107,10 +26107,12 @@ represented by an entry of type a_constant (ck_address or ck_integer).
2610726107
new_con->variant.address.offset -=
2610826108
(a_targ_ptrdiff_t)prev_bcp->offset;
2610926109
if (type_is(tp, tk_void) ||
26110-
same_entities(tp, prev_bcp->derived_class)) {
26110+
(same_entities(tp, prev_bcp->derived_class) &&
26111+
prev_bcp->has_public_derivation)) {
2611126112
/* A cast to the most-derived class (either by casting to void*
2611226113
or because the destination type happens to match the most-
26113-
derived class type). */
26114+
derived class type, of which the operand is a public base
26115+
class subobject). */
2611426116
remove_trailing_subobject_path_entry(new_con);
2611526117
clear_runtime_constant_address(result_storage, new_con);
2611626118
goto done;
@@ -26123,7 +26125,28 @@ represented by an entry of type a_constant (ck_address or ck_integer).
2612326125
bcdp = bcdp->next;
2612426126
for (; dsp != tail->next; dsp = dsp->next) {
2612526127
if (same_entities(dsp->base_class->type, tp)) {
26126-
if (new_bcp == NULL) {
26128+
a_derivation_step_ptr step;
26129+
a_type_ptr step_class = dsp->base_class->type;
26130+
a_boolean is_public_base = TRUE;
26131+
/* The operand must be a public base class subobject of the
26132+
destination subobject (but not necessarily of the
26133+
most-derived object). The base classes of the derivation
26134+
steps are relative to the most-derived class, so check the
26135+
corresponding direct base classes instead. */
26136+
for (step = dsp->next; step != tail->next;
26137+
step = step->next) {
26138+
a_base_class_ptr direct_bcp =
26139+
find_direct_base_class_of(
26140+
step_class, step->base_class->type);
26141+
if (!direct_bcp->has_public_derivation) {
26142+
is_public_base = FALSE;
26143+
} /* if */
26144+
step_class = step->base_class->type;
26145+
} /* for */
26146+
if (!is_public_base) {
26147+
/* Ignore this derivation path. Another path may reach
26148+
the same virtual base class subobject publicly. */
26149+
} else if (new_bcp == NULL) {
2612726150
new_bcp = dsp->base_class;
2612826151
} else if (dsp->base_class != new_bcp) {
2612926152
/* The subobject on the derivation path is not unambiguous.
@@ -26149,7 +26172,11 @@ represented by an entry of type a_constant (ck_address or ck_integer).
2614926172
&expr->position, new_bcp->type, ips);
2615026173
} /* if */
2615126174
goto done;
26152-
} else if (!is_accessible_base_class(new_bcp)) {
26175+
} else if (!prev_bcp->has_public_derivation ||
26176+
!new_bcp->has_public_derivation) {
26177+
/* The operand must be a public base class subobject of the
26178+
most-derived object, and the destination a public base
26179+
class of the most-derived type. */
2615326180
if (pointer_case) {
2615426181
clear_address(result_storage, NULL);
2615526182
} else {
@@ -26199,6 +26226,7 @@ represented by an entry of type a_constant (ck_address or ck_integer).
2619926226
a_base_class_ptr bcp, baseward_bcp;
2620026227
a_type_ptr dtp;
2620126228
a_byte *subobj = opnd_addr->address;
26229+
a_boolean is_public_base = TRUE;
2620226230
/* Check for the null address case first. */
2620326231
if (subobj == NULL) {
2620426232
/* Casting a null pointer value results in a null pointer value. */
@@ -26215,8 +26243,8 @@ represented by an entry of type a_constant (ck_address or ck_integer).
2621526243
do_constexpr_fail(result);
2621626244
goto done;
2621726245
} /* if */
26218-
if (type_is(tp, tk_void) &&
26219-
(baseward_bcp = find_base_in_type(opnd_type, tp))!= NULL) {
26246+
if (!type_is(tp, tk_void) &&
26247+
(baseward_bcp = find_base_in_type(opnd_type, tp)) != NULL) {
2622026248
/* The dynamic cast is actually a (static) derived-to-base cast. */
2622126249
if (baseward_bcp->ambiguous) {
2622226250
do_constexpr_fail(result);
@@ -26240,13 +26268,17 @@ represented by an entry of type a_constant (ck_address or ck_integer).
2624026268
a_byte_count offset;
2624126269
get_mapped_byte_count(&persistent_map, bcp, offset);
2624226270
result_addr->address -= offset;
26271+
if (!bcp->has_public_derivation) is_public_base = FALSE;
2624326272
dtp = bcp->derived_class;
2624426273
if (same_entities(dtp, tp)) {
2624526274
/* We found a derived subobject of the right type. Since types with
2624626275
virtual bases cannot be literal types (and thus cannot be stored
2624726276
in interpreter memory), there is no concern about this being
26248-
ambiguous. */
26249-
goto done;
26277+
ambiguous. The cast succeeds only if the operand is a public base
26278+
class subobject of it; otherwise, it also is not a public base
26279+
class subobject of the most-derived object, and so the cast fails
26280+
below. */
26281+
if (is_public_base) goto done;
2625026282
} /* if */
2625126283
if (is_initialized(result_addr)) {
2625226284
bcp = *(a_base_class_ptr*)result_addr->address;
@@ -26263,11 +26295,13 @@ represented by an entry of type a_constant (ck_address or ck_integer).
2626326295
goto done;
2626426296
} /* if */
2626526297
/* We've reached the most derived type (dtp) without encountering the
26266-
destination type. This might still be a "sideways" cast. */
26298+
destination type as a class of which the operand is a public base
26299+
class subobject. This might still be a "sideways" cast. */
2626726300
baseward_bcp = find_base_in_type(dtp, tp);
2626826301
if (baseward_bcp != NULL) {
2626926302
/* There is a subobject of the right kind: Verify that it is
26270-
unambiguous and accessible. */
26303+
unambiguous and that it and the operand are public base class
26304+
subobjects of the most-derived object. */
2627126305
if (baseward_bcp->ambiguous) {
2627226306
if (pointer_case) {
2627326307
clear_address(result_storage, NULL);
@@ -26277,7 +26311,7 @@ represented by an entry of type a_constant (ck_address or ck_integer).
2627726311
&expr->position, tp, ips);
2627826312
} /* if */
2627926313
goto done;
26280-
} else if (!is_accessible_base_class(baseward_bcp)) {
26314+
} else if (!is_public_base || !baseward_bcp->has_public_derivation) {
2628126315
if (pointer_case) {
2628226316
clear_address(result_storage, NULL);
2628326317
} else {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[GH #398]
2+
dynamic_cast in constant evaluation requires public base class subobjects
3+
4+
During constant evaluation, the run-time checks of a dynamic_cast did not
5+
require the operand to point to (or refer to) a public base class subobject
6+
of the destination object (for a cast to a derived class) or of the most
7+
derived object (for a cast to a sibling base class, which also requires the
8+
destination type to be a public base class of the most derived type).
9+
Instead, the accessibility of the base classes in the context of the cast
10+
was checked. For example, with --c++20:
11+
12+
struct B { virtual void b() {} };
13+
struct D : private B {};
14+
struct F : D {};
15+
constexpr F f;
16+
static_assert(dynamic_cast<const D*>((const B*)&f) == nullptr);
17+
// Previously failed.
18+
19+
This is now fixed.

‎tests/expectations/edg_x86_64/imported.log‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7069,7 +7069,6 @@ gnu/cpp/cpp2a/consteval13.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL:
70697069
gnu/cpp/cpp2a/consteval25.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL:
70707070
gnu/cpp/cpp2a/consteval32.sft.cpp:fn:-DTEST_NUMBER=1 --c++20:FAIL:
70717071
gnu/cpp/cpp2a/constexpr-99287.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL:
7072-
gnu/cpp/cpp2a/constexpr-dynamic13.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL:
70737072
gnu/cpp/cpp2a/constexpr-init18.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL:
70747073
gnu/cpp/cpp2a/constexpr-inst1.sft.cpp:fn:-DTEST_NUMBER=1 --c++14:FAIL:
70757074
gnu/cpp/cpp2a/constexpr-inst2.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:FAIL:
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
fe_only -DTEST_NUMBER=1 --c++26 Test_name.c
2+
"Test_name.c", line 20: error: initialization is not constant
3+
constexpr D d;
4+
^
5+
"Test_name.c", line 20: note: cannot call non-constexpr function "D::D() noexcept" (declared implicitly)
6+
constexpr D d;
7+
^
8+
9+
"Test_name.c", line 20: error: a constexpr variable must have a literal type or a reference type
10+
constexpr D d;
11+
^
12+
13+
"Test_name.c", line 21: error: expression must have a constant value
14+
static_assert(dynamic_cast<const Good*>((const B*)&d) == (const Good*)&d);
15+
^
16+
"Test_name.c", line 21: note: cannot fold operation involving virtual base class ("B")
17+
static_assert(dynamic_cast<const Good*>((const B*)&d) == (const Good*)&d);
18+
^
19+
20+
"Test_name.c", line 22: error: expression must have a constant value
21+
static_assert(dynamic_cast<const P*>((const B*)&d) ==
22+
^
23+
"Test_name.c", line 22: note: cannot fold operation involving virtual base class ("B")
24+
static_assert(dynamic_cast<const P*>((const B*)&d) ==
25+
^
26+
27+
"Test_name.c", line 24: error: expression must have a constant value
28+
static_assert(dynamic_cast<const D*>((const B*)&d) == nullptr);
29+
^
30+
"Test_name.c", line 24: note: cannot fold operation involving virtual base class ("B")
31+
static_assert(dynamic_cast<const D*>((const B*)&d) == nullptr);
32+
^
33+
34+
"Test_name.c", line 25: error: initialization is not constant
35+
constexpr D2 d2;
36+
^
37+
"Test_name.c", line 25: note: cannot call non-constexpr function "D2::D2() noexcept" (declared implicitly)
38+
constexpr D2 d2;
39+
^
40+
41+
"Test_name.c", line 25: error: a constexpr variable must have a literal type or a reference type
42+
constexpr D2 d2;
43+
^
44+
45+
"Test_name.c", line 26: error: expression must have a constant value
46+
static_assert(dynamic_cast<const Good2*>((const B*)&d2) == (const Good2*)&d2);
47+
^
48+
"Test_name.c", line 26: note: cannot fold operation involving virtual base class ("B")
49+
static_assert(dynamic_cast<const Good2*>((const B*)&d2) == (const Good2*)&d2);
50+
^
51+
52+
"Test_name.c", line 35: error: expression must have a constant value
53+
static_assert(local());
54+
^
55+
"Test_name.c", line 29: note: cannot call non-constexpr function "D::D() noexcept" (declared implicitly)
56+
D x;
57+
^
58+
59+
9 errors detected in the compilation of "Test_name.c".
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=1 --c++20 Test_name.c
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//type:fn
2+
//options:--c++26
3+
//remark:dynamic_cast in constant evaluation with virtual base classes
4+
5+
// These cases cannot be constant-evaluated yet: constexpr objects of classes
6+
// with virtual base classes are not supported (that requires P3533 "constexpr
7+
// virtual inheritance"). When that support is added, the errors below go
8+
// away and these assertions must pass. In particular, a downcast must
9+
// succeed when the operand is a public base class subobject of the
10+
// destination object through at least one derivation path, even if another
11+
// path to the same virtual base class subobject is not public.
12+
struct B { virtual void f() {} };
13+
struct Q : private virtual B {};
14+
struct P : public virtual B {};
15+
struct Good : public Q, public P {};
16+
struct D : private Good {};
17+
struct Good2 : public P, public Q {};
18+
struct D2 : private Good2 {};
19+
20+
constexpr D d;
21+
static_assert(dynamic_cast<const Good*>((const B*)&d) == (const Good*)&d);
22+
static_assert(dynamic_cast<const P*>((const B*)&d) ==
23+
(const P*)(const Good*)&d);
24+
static_assert(dynamic_cast<const D*>((const B*)&d) == nullptr);
25+
constexpr D2 d2;
26+
static_assert(dynamic_cast<const Good2*>((const B*)&d2) == (const Good2*)&d2);
27+
28+
constexpr bool local() {
29+
D x;
30+
const B* pb = (const B*)&x;
31+
return dynamic_cast<const Good*>(pb) == (const Good*)&x &&
32+
dynamic_cast<const P*>(pb) == (const P*)(const Good*)&x &&
33+
dynamic_cast<const D*>(pb) == nullptr;
34+
}
35+
static_assert(local());

‎tests/tests/changes/gh-398.sft.cpp‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//type:fp
2+
//options:--c++20
3+
//remark:dynamic_cast in constant evaluation requires public base subobjects
4+
5+
// The run-time checks require public base class subobjects, independent of
6+
// access in the context of the cast.
7+
struct B { virtual void b() {} };
8+
struct C { virtual void c() {} };
9+
struct D : private B {
10+
virtual void d() {}
11+
constexpr const B* pb() const { return this; }
12+
};
13+
struct E { virtual void e() {} };
14+
struct F : D, E, private C {
15+
virtual void f() {}
16+
constexpr bool m() const {
17+
return dynamic_cast<const F*>((const C*)this) == nullptr &&
18+
dynamic_cast<const E*>((const C*)this) == nullptr;
19+
}
20+
};
21+
22+
constexpr F gf;
23+
// Each of the following three assertions previously failed.
24+
static_assert(dynamic_cast<const D*>((const B*)&gf) == nullptr);
25+
static_assert(dynamic_cast<const E*>((const B*)&gf) == nullptr);
26+
static_assert(dynamic_cast<const F*>((const C*)&gf) == nullptr);
27+
static_assert(dynamic_cast<const F*>((const D*)&gf) == &gf);
28+
static_assert(dynamic_cast<const F*>((const E*)&gf) == &gf);
29+
static_assert(dynamic_cast<const E*>((const D*)&gf) == &gf);
30+
static_assert(dynamic_cast<const void*>((const B*)&gf) == &gf);
31+
static_assert(gf.m()); // Previously failed.
32+
33+
constexpr bool local() {
34+
F f;
35+
const B* pb = (const B*)&f;
36+
const C* pc = (const C*)&f;
37+
return dynamic_cast<const D*>(pb) == nullptr &&
38+
dynamic_cast<const E*>(pb) == nullptr &&
39+
dynamic_cast<const F*>(pc) == nullptr &&
40+
dynamic_cast<const E*>(pc) == nullptr &&
41+
dynamic_cast<const D*>(f.pb()) == nullptr &&
42+
dynamic_cast<const F*>((const D*)&f) == &f &&
43+
dynamic_cast<const E*>((const D*)&f) == &f &&
44+
dynamic_cast<const D*>((const E*)&f) == &f &&
45+
dynamic_cast<const void*>(pb) == &f &&
46+
f.m();
47+
}
48+
static_assert(local()); // Previously failed.
49+
50+
struct G : B {};
51+
struct H : G, E {};
52+
constexpr bool public_bases() {
53+
H h;
54+
const B* pb = &h;
55+
return dynamic_cast<const G*>(pb) == &h &&
56+
dynamic_cast<const H*>(pb) == &h &&
57+
dynamic_cast<const E*>(pb) == &h &&
58+
dynamic_cast<const B*>((const E*)&h) == pb;
59+
}
60+
static_assert(public_bases());
61+
62+
// A downcast only requires the operand to be a public base class subobject
63+
// of the destination object, not of the most-derived object.
64+
struct K : B {};
65+
struct L : K {};
66+
struct M : private L, E {};
67+
constexpr M gm;
68+
static_assert(dynamic_cast<const K*>((const B*)&gm) == (const K*)&gm);
69+
static_assert(dynamic_cast<const L*>((const B*)&gm) == (const L*)&gm);
70+
static_assert(dynamic_cast<const M*>((const B*)&gm) == nullptr);
71+
static_assert(dynamic_cast<const E*>((const B*)&gm) == nullptr);
72+
constexpr bool private_most_derived() {
73+
M m;
74+
const B* pb = (const B*)&m;
75+
return dynamic_cast<const K*>((const B*)&m) == (const K*)&m &&
76+
dynamic_cast<const L*>((const B*)&m) == (const L*)&m &&
77+
dynamic_cast<const L*>(pb) == (const L*)&m &&
78+
dynamic_cast<const M*>(pb) == nullptr &&
79+
dynamic_cast<const E*>(pb) == nullptr;
80+
}
81+
static_assert(private_most_derived());

‎tests/tests/imported/clang/cpp/SemaCXX/.constant-expression-cxx2a.rto/default.1.1.txt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
8282
static_assert(&dynamic_cast<A&>((D&)g) == &(A&)(B&)g);
8383
^
8484

85+
"SemaCXX/constant-expression-cxx2a.cpp", line 274: error: expression must have a constant value
86+
constexpr int e_f = (dynamic_cast<F&>((E&)g), 0);
87+
^
88+
"SemaCXX/constant-expression-cxx2a.cpp", line 274: note: conversion to inaccessible base class "DynamicCast::F" is not allowed
89+
constexpr int e_f = (dynamic_cast<F&>((E&)g), 0);
90+
^
91+
8592
"SemaCXX/constant-expression-cxx2a.cpp", line 278: error: expression must have a constant value
8693
constexpr int b_e = (dynamic_cast<E&>((B&)g), 0);
8794
^

‎tests/tests/imported/clang/cpp/SemaCXX/.constant-expression-cxx2a.rto/edg_x86_32.1.1.txt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
8282
static_assert(&dynamic_cast<A&>((D&)g) == &(A&)(B&)g);
8383
^
8484

85+
"SemaCXX/constant-expression-cxx2a.cpp", line 274: error: expression must have a constant value
86+
constexpr int e_f = (dynamic_cast<F&>((E&)g), 0);
87+
^
88+
"SemaCXX/constant-expression-cxx2a.cpp", line 274: note: conversion to inaccessible base class "DynamicCast::F" is not allowed
89+
constexpr int e_f = (dynamic_cast<F&>((E&)g), 0);
90+
^
91+
8592
"SemaCXX/constant-expression-cxx2a.cpp", line 278: error: expression must have a constant value
8693
constexpr int b_e = (dynamic_cast<E&>((B&)g), 0);
8794
^

‎tests/tests/imported/clang/cpp/SemaCXX/.constant-expression-cxx2a.rto/edg_x86_32_cp.1.1.txt‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,20 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
8282
constexpr int d_a = (dynamic_cast<const A&>(static_cast<const D&>(g)), 0);
8383
^
8484

85-
"SemaCXX/constant-expression-cxx2a.cpp", line 264: error: expression must have a constant value
86-
static_assert(&dynamic_cast<A&>((B&)g) == &(A&)(B&)g);
87-
^
88-
"SemaCXX/constant-expression-cxx2a.cpp", line 264: note: base class "DynamicCast::A" is ambiguous
89-
static_assert(&dynamic_cast<A&>((B&)g) == &(A&)(B&)g);
90-
^
91-
9285
"SemaCXX/constant-expression-cxx2a.cpp", line 267: error: expression must have a constant value
9386
static_assert(&dynamic_cast<A&>((D&)g) == &(A&)(B&)g);
9487
^
9588
"SemaCXX/constant-expression-cxx2a.cpp", line 267: note: base class "DynamicCast::A" is ambiguous
9689
static_assert(&dynamic_cast<A&>((D&)g) == &(A&)(B&)g);
9790
^
9891

92+
"SemaCXX/constant-expression-cxx2a.cpp", line 274: error: expression must have a constant value
93+
constexpr int e_f = (dynamic_cast<F&>((E&)g), 0);
94+
^
95+
"SemaCXX/constant-expression-cxx2a.cpp", line 274: note: conversion to inaccessible base class "DynamicCast::F" is not allowed
96+
constexpr int e_f = (dynamic_cast<F&>((E&)g), 0);
97+
^
98+
9999
"SemaCXX/constant-expression-cxx2a.cpp", line 278: error: expression must have a constant value
100100
constexpr int b_e = (dynamic_cast<E&>((B&)g), 0);
101101
^

0 commit comments

Comments
 (0)