Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for downcast to parent. #501

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions include/cpp2util.h
Original file line number Diff line number Diff line change
Expand Up @@ -994,9 +994,15 @@ auto as( X const& x ) -> decltype(auto) {
return x;
}

template< typename C, typename X >
requires std::is_same_v<C, X>
auto as( X& x ) -> decltype(auto) {
return x;
}

template< typename C, typename X >
auto as( X const& x ) -> auto
requires (!std::is_same_v<C, X> && requires { C{x}; })
requires (!std::is_same_v<C, X> && !std::is_base_of_v<C, X> && requires { C{x}; })
{
// Experiment: Recognize the nested `::value_type` pattern for some dynamic library types
// like std::optional, and try to prevent accidental narrowing conversions even when
Expand All @@ -1010,9 +1016,15 @@ auto as( X const& x ) -> auto
}

template< typename C, typename X >
requires std::is_base_of_v<C, X>
auto as( X&& x ) -> C&& {
return CPP2_FORWARD(x);
requires (std::is_base_of_v<C, X> && !std::is_same_v<C, X>)
auto as( X& x ) -> C& {
return x;
}

template< typename C, typename X >
requires (std::is_base_of_v<C, X> && !std::is_same_v<C, X>)
auto as( X const& x ) -> C const& {
return x;
}

template< typename C, typename X >
Expand Down
85 changes: 85 additions & 0 deletions regression-tests/pure2-types-down-upcast.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
A: type = {
public i: int = 0;

const_foo: (virtual this) = { std::cout << "const foo \n"; }
mut_foo: (inout this) = { std::cout << "foo \n"; }
}

B: type = {
this: A = ();
public d: double = 0.0;
}

func_mut: (inout a: A) -> void = { std::cout << "Call A mut: (a.i)$" << std::endl; }
func_mut: (inout b: B) -> void = { std::cout << "Call B mut: (b.d)$" << std::endl; }
func_const: (a: A) -> void = { std::cout << "Call A const: (a.i)$" << std::endl; }
func_const: (b: B) -> void = { std::cout << "Call B const: (b.d)$" << std::endl; }

test_const_foo: () = {
s: A =();
sC: *const A = s&;
s.const_foo();
sC*.const_foo();
(s as A).const_foo();
(sC* as A).const_foo();
_ = s;
_ = sC;
}

test_mut_foo: () = {
s: A =();
s.mut_foo();
(s as A).mut_foo();
_ = s;
}

test_up: () = {
b: B = ();
bC: *const B = b&;

func_const(b);
func_const(b as B);
func_const(b as A);
func_const(bC*);
func_const(bC* as B);
func_const(bC* as A);

func_mut(b);
func_mut(b as B);
func_mut(b as A);

_ = b;
_ = bC;
}

test_down: () = {
b: B = ();
bC: *const B = b&;
a: *A = (b as A)&;
aC: *const A = (b as A)&;

func_const(a*);
func_const(a* as B);
func_const(a* as A);
func_const(aC*);
func_const(aC* as B);
func_const(aC* as A);
func_mut(a*);
func_mut(a* as B);
func_mut(a* as A);

_ = b;
_ = bC;
_ = a;
_ = aC;
}

main: () -> int = {

test_const_foo();
test_mut_foo();
test_up();
test_down();

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const foo
const foo
const foo
const foo
foo
foo
Call B const: 0.000000
Call B const: 0.000000
Call A const: 0
Call B const: 0.000000
Call B const: 0.000000
Call A const: 0
Call B mut: 0.000000
Call B mut: 0.000000
Call A mut: 0
Call A const: 0
Call B const: 0.000000
Call A const: 0
Call A const: 0
Call B const: 0.000000
Call A const: 0
Call A mut: 0
Call B mut: 0.000000
Call A mut: 0
Empty file.
147 changes: 147 additions & 0 deletions regression-tests/test-results/pure2-types-down-upcast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@

#define CPP2_USE_MODULES Yes

//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"

#line 1 "pure2-types-down-upcast.cpp2"
class A;


#line 8 "pure2-types-down-upcast.cpp2"
class B;


//=== Cpp2 type definitions and function declarations ===========================

#line 1 "pure2-types-down-upcast.cpp2"
class A {
public: int i {0};

public: virtual auto const_foo() const -> void;
public: auto mut_foo() -> void;

public: A() = default;
public: A(A const&) = delete; /* No 'that' constructor, suppress copy */
public: auto operator=(A const&) -> void = delete;
#line 6 "pure2-types-down-upcast.cpp2"
};

class B: public A {

public: double d {0.0};
public: B() = default;
public: B(B const&) = delete; /* No 'that' constructor, suppress copy */
public: auto operator=(B const&) -> void = delete;

#line 11 "pure2-types-down-upcast.cpp2"
};

auto func_mut(A& a) -> void;
auto func_mut(B& b) -> void;
auto func_const(cpp2::in<A> a) -> void;
auto func_const(cpp2::in<B> b) -> void;

auto test_const_foo() -> void;


#line 29 "pure2-types-down-upcast.cpp2"
auto test_mut_foo() -> void;


#line 36 "pure2-types-down-upcast.cpp2"
auto test_up() -> void;


#line 55 "pure2-types-down-upcast.cpp2"
auto test_down() -> void;


#line 77 "pure2-types-down-upcast.cpp2"
[[nodiscard]] auto main() -> int;


//=== Cpp2 function definitions =================================================


#line 4 "pure2-types-down-upcast.cpp2"
auto A::const_foo() const -> void{std::cout << "const foo \n"; }
auto A::mut_foo() -> void{std::cout << "foo \n"; }

#line 13 "pure2-types-down-upcast.cpp2"
auto func_mut(A& a) -> void {std::cout << "Call A mut: " + cpp2::to_string(a.i) << std::endl;}
auto func_mut(B& b) -> void {std::cout << "Call B mut: " + cpp2::to_string(b.d) << std::endl;}
auto func_const(cpp2::in<A> a) -> void{std::cout << "Call A const: " + cpp2::to_string(a.i) << std::endl;}
auto func_const(cpp2::in<B> b) -> void{std::cout << "Call B const: " + cpp2::to_string(b.d) << std::endl;}

auto test_const_foo() -> void{
A s {};
A const* sC {&s};
CPP2_UFCS_0(const_foo, s);
CPP2_UFCS_0(const_foo, (*cpp2::assert_not_null(sC)));
CPP2_UFCS_0(const_foo, (cpp2::as_<A>(s)));
CPP2_UFCS_0(const_foo, (cpp2::as_<A>(*cpp2::assert_not_null(sC))));
(void) std::move(s);
(void) std::move(sC);
}

auto test_mut_foo() -> void{
A s {};
CPP2_UFCS_0(mut_foo, s);
CPP2_UFCS_0(mut_foo, (cpp2::as_<A>(s)));
(void) std::move(s);
}

auto test_up() -> void{
B b {};
B const* bC {&b};

func_const(b);
func_const(cpp2::as_<B>(b));
func_const(cpp2::as_<A>(b));
func_const(*cpp2::assert_not_null(bC));
func_const(cpp2::as_<B>(*cpp2::assert_not_null(bC)));
func_const(cpp2::as_<A>(*cpp2::assert_not_null(bC)));

func_mut(b);
func_mut(cpp2::as_<B>(b));
func_mut(cpp2::as_<A>(b));

(void) std::move(b);
(void) std::move(bC);
}

auto test_down() -> void{
B b {};
B const* bC {&b};
A* a {&(cpp2::as_<A>(b))};
A const* aC {&(cpp2::as_<A>(b))};

func_const(*cpp2::assert_not_null(a));
func_const(cpp2::as_<B>(*cpp2::assert_not_null(a)));
func_const(cpp2::as_<A>(*cpp2::assert_not_null(a)));
func_const(*cpp2::assert_not_null(aC));
func_const(cpp2::as_<B>(*cpp2::assert_not_null(aC)));
func_const(cpp2::as_<A>(*cpp2::assert_not_null(aC)));
func_mut(*cpp2::assert_not_null(a));
func_mut(cpp2::as_<B>(*cpp2::assert_not_null(a)));
func_mut(cpp2::as_<A>(*cpp2::assert_not_null(a)));

(void) std::move(b);
(void) std::move(bC);
(void) std::move(a);
(void) std::move(aC);
}

[[nodiscard]] auto main() -> int{

test_const_foo();
test_mut_foo();
test_up();
test_down();

return 0;
}

2 changes: 2 additions & 0 deletions regression-tests/test-results/pure2-types-down-upcast.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pure2-types-down-upcast.cpp2... ok (all Cpp2, passes safety checks)

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pure2-types-down-upcast.cpp2... ok (all Cpp2, passes safety checks)