Skip to content

Commit ace8cca

Browse files
committed
fix(to_cpp1): improve recognition of dependent types and deducible parameters
1 parent c5feb42 commit ace8cca

11 files changed

+798
-83
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Dependent, non-deducible parameters are wrapped like non-dependent parameters.
2+
init: <T> (out x: std::integral_constant<i32, T::value>) = { x = (); }
3+
init: <T> (out x: std::integral_constant<i32, T::value>, _: T) = { x = (); }
4+
id: <T> (x: std::integral_constant<i32, T::value>) -> forward _ = x;
5+
id: <T> (x: std::integral_constant<i32, T::value>, y: T) = { assert(x& == y&); }
6+
7+
main: () = {
8+
zero: type == std::integral_constant<i32, 0>;
9+
10+
z: zero;
11+
init<zero>(out z);
12+
assert(id<zero>(z)& == z&);
13+
14+
// Deducible parameters.
15+
_ = :v = 0;
16+
:<T> (_: std::vector<T>) = {}(:std::vector<i32> = ());
17+
:<T> (_: std::vector<std::vector<T>>) = {}(:std::vector<std::vector<i32>> = ());
18+
// _ = :<T, U> (x: std::pair<T, typename U::value_type>, y: U) = {}(:std::pair = (0, 0), z); // Blocked on #727.
19+
:<T, U> (_: std::array<T, U::value>, _: U) = {}(:std::array<i32, 0> = (), z);
20+
init(out z, z);
21+
id(z, z);
22+
23+
// Test that these are emitted unwrapped in case they are deducible.
24+
(copy f := :<T> (_: std::vector<std::type_identity_t<T>>) = {})
25+
static_assert(!std::is_invocable_v<decltype(f), std::vector<i32>>, "`T` is non-deducible.");
26+
(copy f := :<T> (_: std::vector<std::vector<T>>) = {})
27+
static_assert(std::is_invocable_v<decltype(f), std::vector<std::vector<i32>>>, "`T` is deducible.");
28+
}
29+
30+
v: <T> type = {
31+
operator=: (out this, _: T) = { }
32+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
main: () = {
2+
a: type == b;
3+
b: type == a;
4+
_ = a::t;
5+
_ = b::t;
6+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
identity: <T> type == T;
2+
3+
f: <T, V: T::value_type> (x: T::value_type) -> T::value_type = {
4+
assert(x is T::value_type);
5+
y: T::value_type;
6+
y = x;
7+
z: type == T::value_type;
8+
return (:T::value_type = x);
9+
10+
// Dependent *template-id*s.
11+
_ = :identity<T>::value_type = (); // First identifier.
12+
_ = :std::optional<T>::value_type = (); // Non-first identifier.
13+
_ = :std::array<i32, T::value>::value_type = ();
14+
_ = :std::array<i32, T::value + T::value>::value_type = ();
15+
16+
// Emitted `template`.
17+
ptr: type == * T; // Also test lookup through type aliases.
18+
nptr: type == * i32;
19+
_ = :std::pointer_traits<ptr>::rebind<ptr> = (); // Type-only context.
20+
_ = :std::pointer_traits<nptr>::rebind<nptr> = (); // Non-dependent.
21+
_ = :std::pointer_traits<nptr>::rebind<ptr> = (); // Dependent on the nested template.
22+
_ = :std::pointer_traits<ptr>::rebind<nptr> = (); // Dependent on the outer template.
23+
// _ = :identity<typename std::pointer_traits<ptr>::rebind<ptr>> = (); // Non type-only context. Blocked on #727.
24+
25+
// Aliases.
26+
w: type == T;
27+
_ = :w::value_type = x;
28+
v: type == w;
29+
_ = :v::value_type = x;
30+
a: type == T::type;
31+
_ = :a::value_type = x;
32+
33+
{
34+
// Test that there's no prefixed `typename` to....
35+
_ = std::integral_constant<i32, T::value>(); // `T::value`.
36+
_ = :std::type_identity_t<T> = (); // `std::type_identity_t<T>`.
37+
38+
// Test that non-dependent names aren't emitted with `typename`.
39+
a: type == std::integral_constant<i32, 0>;
40+
b: type == a;
41+
c: type == b;
42+
_ = :b::value_type = x;
43+
_ = :c::value_type = x;
44+
}
45+
}
46+
47+
t: @cpp1_rule_of_zero <T: type> type = {
48+
u: type = {
49+
x: T::value_type = ();
50+
this: T::type = ();
51+
// Test that there's no 'typename' in the member initializer list.
52+
operator=: (out this, that) = { }
53+
}
54+
x: T::value_type = 0;
55+
}
56+
57+
main: () = {
58+
zero: type == std::integral_constant<i32, 0>;
59+
_ = f<zero, 0>(0);
60+
61+
// clang-format off
62+
_ = : ::t<zero> = (); // clang-format on
63+
64+
// Emitted `template` (noop, taken care of by the UFCS macro).
65+
// _ = :(move f) = f.operator()<i32>();(:<T> () = {}); // Blocked on #832.
66+
67+
// Nesting is not relevant to lookup.
68+
_ = :<T> () = { _ = :T::value_type = (); };
69+
_ = :() = { _ = :<T> () = { _ = :T::value_type = (); }; };
70+
_ = :() = { _ = :() = { _ = :<T> () = { _ = :T::value_type = (); }; }; };
71+
_ = :() = { _ = :() = { _ = :() = { _ = :<T> () = { _ = :T::value_type = (); }; }; }; };
72+
_ = :() = { _ = :() = { _ = :<T> () = { _ = :() = { _ = :T::value_type = (); }; }; }; };
73+
_ = :() = { _ = :<T> () = { _ = :() = { _ = :() = { _ = :T::value_type = (); }; }; }; };
74+
_ = :<T> () = { _ = :() = { _ = :() = { _ = :() = { _ = :T::value_type = (); }; }; }; };
75+
_ = :<T> () = { _ = :() = { _ = :() = { _ = :(_: T::value_type) = {}; }; }; };
76+
_ = :<T> () = { _ = :() = { _ = :(_: T::value_type) = { _ = :() = {}; }; }; };
77+
_ = :<T> () = { _ = :(_: T::value_type) = { _ = :() = { _ = :() = {}; }; }; };
78+
_ = :<T> (_: T::value_type) = { _ = :() = { _ = :() = { _ = :() = {}; }; }; };
79+
80+
// Lookup.
81+
{
82+
alias: type == std::integral_constant<i32, 0>;
83+
_ = :alias::value_type = 0; // Non-dependent.
84+
}
85+
_ = :<T> (_: T) = {
86+
alias: type == std::integral_constant<T, 0>;
87+
_ = :alias::value_type = 0; // Dependent.
88+
{
89+
alias: type == std::integral_constant<i32, 0>;
90+
_ = :alias::value_type = 0; // Non-dependent.
91+
}
92+
}(0);
93+
94+
_ = :(r) -> std::type_identity_t<decltype(begin(r)*)> = r[0];(std::vector<int>(1));
95+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
#define CPP2_IMPORT_STD Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
#line 1 "pure2-bugfix-for-deducible-parameters.cpp2"
10+
11+
#line 30 "pure2-bugfix-for-deducible-parameters.cpp2"
12+
template<typename T> class v;
13+
14+
15+
//=== Cpp2 type definitions and function declarations ===========================
16+
17+
#line 1 "pure2-bugfix-for-deducible-parameters.cpp2"
18+
// Dependent, non-deducible parameters are wrapped like non-dependent parameters.
19+
#line 2 "pure2-bugfix-for-deducible-parameters.cpp2"
20+
template<typename T> auto init(cpp2::impl::out<std::integral_constant<cpp2::i32,T::value>> x) -> void;
21+
template<typename T> auto init(cpp2::impl::out<std::integral_constant<cpp2::i32,T::value>> x, [[maybe_unused]] T const& unnamed_param_2) -> void;
22+
template<typename T> [[nodiscard]] auto id(cpp2::impl::in<std::integral_constant<cpp2::i32,T::value>> x) -> auto&&;
23+
template<typename T> auto id(cpp2::impl::in<std::integral_constant<cpp2::i32,T::value>> x, T const& y) -> void;
24+
25+
auto main() -> int;
26+
27+
#line 30 "pure2-bugfix-for-deducible-parameters.cpp2"
28+
template<typename T> class v {
29+
public: explicit v([[maybe_unused]] T const& unnamed_param_2);
30+
#line 31 "pure2-bugfix-for-deducible-parameters.cpp2"
31+
public: auto operator=([[maybe_unused]] T const& unnamed_param_2) -> v& ;
32+
public: v(v const&) = delete; /* No 'that' constructor, suppress copy */
33+
public: auto operator=(v const&) -> void = delete;
34+
35+
#line 32 "pure2-bugfix-for-deducible-parameters.cpp2"
36+
};
37+
38+
39+
//=== Cpp2 function definitions =================================================
40+
41+
#line 1 "pure2-bugfix-for-deducible-parameters.cpp2"
42+
43+
#line 2 "pure2-bugfix-for-deducible-parameters.cpp2"
44+
template<typename T> auto init(cpp2::impl::out<std::integral_constant<cpp2::i32,T::value>> x) -> void{x.construct(); }
45+
#line 3 "pure2-bugfix-for-deducible-parameters.cpp2"
46+
template<typename T> auto init(cpp2::impl::out<std::integral_constant<cpp2::i32,T::value>> x, [[maybe_unused]] T const& unnamed_param_2) -> void{x.construct(); }
47+
#line 4 "pure2-bugfix-for-deducible-parameters.cpp2"
48+
template<typename T> [[nodiscard]] auto id(cpp2::impl::in<std::integral_constant<cpp2::i32,T::value>> x) -> auto&& { return x; }
49+
#line 5 "pure2-bugfix-for-deducible-parameters.cpp2"
50+
template<typename T> auto id(cpp2::impl::in<std::integral_constant<cpp2::i32,T::value>> x, T const& y) -> void{if (cpp2::cpp2_default.is_active() && !(&x == &y) ) { cpp2::cpp2_default.report_violation(""); }}
51+
52+
#line 7 "pure2-bugfix-for-deducible-parameters.cpp2"
53+
auto main() -> int{
54+
using zero = std::integral_constant<cpp2::i32,0>;
55+
56+
cpp2::impl::deferred_init<zero> z;
57+
init<zero>(cpp2::impl::out(&z));
58+
if (cpp2::cpp2_default.is_active() && !(&id<zero>(z.value()) == &z.value()) ) { cpp2::cpp2_default.report_violation(""); }
59+
60+
// Deducible parameters.
61+
static_cast<void>(v{ 0});
62+
[]<typename T>([[maybe_unused]] std::vector<T> const& unnamed_param_1) -> void{}(std::vector<cpp2::i32>{});
63+
[]<typename T>([[maybe_unused]] std::vector<std::vector<T>> const& unnamed_param_1) -> void{}(std::vector<std::vector<cpp2::i32>>{});
64+
// _ = :<T, U> (x: std::pair<T, typename U::value_type>, y: U) = {}(:std::pair = (0, 0), z); // Blocked on #727.
65+
[]<typename T, typename U>([[maybe_unused]] std::array<T,U::value> const& unnamed_param_1, [[maybe_unused]] U const& unnamed_param_2) -> void{}(std::array<cpp2::i32,0>{}, z.value());
66+
init(cpp2::impl::out(&z.value()), z.value());
67+
id(z.value(), cpp2::move(z.value()));
68+
{
69+
auto f{[]<typename T>([[maybe_unused]] std::vector<std::type_identity_t<T>> const& unnamed_param_1) -> void{}};
70+
71+
// Test that these are emitted unwrapped in case they are deducible.
72+
73+
#line 25 "pure2-bugfix-for-deducible-parameters.cpp2"
74+
static_assert(!(std::is_invocable_v<decltype(cpp2::move(f)),std::vector<cpp2::i32>>), "`T` is non-deducible.");
75+
}
76+
{
77+
auto f{[]<typename T>([[maybe_unused]] std::vector<std::vector<T>> const& unnamed_param_1) -> void{}};
78+
79+
#line 27 "pure2-bugfix-for-deducible-parameters.cpp2"
80+
static_assert(std::is_invocable_v<decltype(cpp2::move(f)),std::vector<std::vector<cpp2::i32>>>, "`T` is deducible.");
81+
}
82+
#line 28 "pure2-bugfix-for-deducible-parameters.cpp2"
83+
}
84+
85+
#line 31 "pure2-bugfix-for-deducible-parameters.cpp2"
86+
template <typename T> v<T>::v([[maybe_unused]] T const& unnamed_param_2){}
87+
#line 31 "pure2-bugfix-for-deducible-parameters.cpp2"
88+
template <typename T> auto v<T>::operator=([[maybe_unused]] T const& unnamed_param_2) -> v& {
89+
return *this; }
90+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-bugfix-for-deducible-parameters.cpp2... ok (all Cpp2, passes safety checks)
2+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
#define CPP2_IMPORT_STD Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
#line 1 "pure2-bugfix-for-dependent-types-recursion.cpp2"
10+
11+
12+
//=== Cpp2 type definitions and function declarations ===========================
13+
14+
#line 1 "pure2-bugfix-for-dependent-types-recursion.cpp2"
15+
auto main() -> int;
16+
17+
//=== Cpp2 function definitions =================================================
18+
19+
#line 1 "pure2-bugfix-for-dependent-types-recursion.cpp2"
20+
auto main() -> int{
21+
#line 2 "pure2-bugfix-for-dependent-types-recursion.cpp2"
22+
using a = b;
23+
using b = a;
24+
static_cast<void>(a::t);
25+
static_cast<void>(b::t);
26+
}
27+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-bugfix-for-dependent-types-recursion.cpp2... ok (all Cpp2, passes safety checks)
2+
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
2+
#define CPP2_IMPORT_STD Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
#line 1 "pure2-bugfix-for-dependent-types.cpp2"
10+
11+
#line 47 "pure2-bugfix-for-dependent-types.cpp2"
12+
template<typename T> class t;
13+
14+
15+
//=== Cpp2 type definitions and function declarations ===========================
16+
17+
#line 1 "pure2-bugfix-for-dependent-types.cpp2"
18+
template<typename T> using identity = T;
19+
#line 2 "pure2-bugfix-for-dependent-types.cpp2"
20+
21+
template<typename T, T::value_type V> [[nodiscard]] auto f(cpp2::impl::in<typename T::value_type> x) -> T::value_type;
22+
23+
#line 47 "pure2-bugfix-for-dependent-types.cpp2"
24+
template<typename T> class t {
25+
struct u_x_as_base { typename T::value_type x; };
26+
27+
#line 48 "pure2-bugfix-for-dependent-types.cpp2"
28+
public: class u: public u_x_as_base, public T::type {
29+
30+
#line 51 "pure2-bugfix-for-dependent-types.cpp2"
31+
// Test that there's no 'typename' in the member initializer list.
32+
public: u(u const& that);
33+
};
34+
private: T::value_type x {0};
35+
};
36+
37+
auto main() -> int;
38+
39+
//=== Cpp2 function definitions =================================================
40+
41+
#line 1 "pure2-bugfix-for-dependent-types.cpp2"
42+
43+
#line 3 "pure2-bugfix-for-dependent-types.cpp2"
44+
template<typename T, T::value_type V> [[nodiscard]] auto f(cpp2::impl::in<typename T::value_type> x) -> T::value_type{
45+
if (cpp2::cpp2_default.is_active() && !(cpp2::impl::is<typename T::value_type>(x)) ) { cpp2::cpp2_default.report_violation(""); }
46+
cpp2::impl::deferred_init<typename T::value_type> y;
47+
y.construct(x);
48+
using z = T::value_type;
49+
return { typename T::value_type{x} };
50+
51+
// Dependent *template-id*s.
52+
static_cast<void>(typename identity<T>::value_type{});// First identifier.
53+
static_cast<void>(typename std::optional<T>::value_type{});// Non-first identifier.
54+
static_cast<void>(typename std::array<cpp2::i32,T::value>::value_type{});
55+
static_cast<void>(typename std::array<cpp2::i32,T::value + T::value>::value_type{});
56+
57+
// Emitted `template`.
58+
using ptr = T*; // Also test lookup through type aliases.
59+
using nptr = cpp2::i32*;
60+
static_cast<void>(typename std::pointer_traits<ptr>::template rebind<ptr>{});// Type-only context.
61+
static_cast<void>(std::pointer_traits<nptr>::rebind<nptr>{});// Non-dependent.
62+
static_cast<void>(std::pointer_traits<nptr>::rebind<ptr>{});// Dependent on the nested template.
63+
static_cast<void>(typename std::pointer_traits<ptr>::template rebind<nptr>{});// Dependent on the outer template.
64+
// _ = :identity<typename std::pointer_traits<ptr>::rebind<ptr>> = (); // Non type-only context. Blocked on #727.
65+
66+
// Aliases.
67+
using w = T;
68+
static_cast<void>(typename w::value_type{x});
69+
using v = w;
70+
static_cast<void>(typename v::value_type{x});
71+
using a = T::type;
72+
static_cast<void>(typename a::value_type{x});
73+
74+
{
75+
// Test that there's no prefixed `typename` to....
76+
static_cast<void>(std::integral_constant<cpp2::i32,T::value>());// `T::value`.
77+
static_cast<void>(std::type_identity_t<T>{});// `std::type_identity_t<T>`.
78+
79+
// Test that non-dependent names aren't emitted with `typename`.
80+
using a = std::integral_constant<cpp2::i32,0>;
81+
using b = a;
82+
using c = b;
83+
static_cast<void>(b::value_type{x});
84+
static_cast<void>(c::value_type{x});
85+
}
86+
}
87+
88+
#line 52 "pure2-bugfix-for-dependent-types.cpp2"
89+
template <typename T> t<T>::u::u(u const& that)
90+
: u_x_as_base{ that.x }
91+
, T::type{ static_cast<typename T::type const&>(that) }{}
92+
93+
#line 57 "pure2-bugfix-for-dependent-types.cpp2"
94+
auto main() -> int{
95+
using zero = std::integral_constant<cpp2::i32,0>;
96+
static_cast<void>(f<zero,0>(0));
97+
98+
// clang-format off
99+
static_cast<void>(::t<zero>{});// clang-format on
100+
101+
// Emitted `template` (noop, taken care of by the UFCS macro).
102+
// _ = :(move f) = f.operator()<i32>();(:<T> () = {}); // Blocked on #832.
103+
104+
// Nesting is not relevant to lookup.
105+
static_cast<void>([]<typename T>() -> void{static_cast<void>(typename T::value_type{}); });
106+
static_cast<void>([]() -> void{static_cast<void>([]<typename T>() -> void{static_cast<void>(typename T::value_type{}); }); });
107+
static_cast<void>([]() -> void{static_cast<void>([]() -> void{static_cast<void>([]<typename T>() -> void{static_cast<void>(typename T::value_type{}); }); }); });
108+
static_cast<void>([]() -> void{static_cast<void>([]() -> void{static_cast<void>([]() -> void{static_cast<void>([]<typename T>() -> void{static_cast<void>(typename T::value_type{}); }); }); }); });
109+
static_cast<void>([]() -> void{static_cast<void>([]() -> void{static_cast<void>([]<typename T>() -> void{static_cast<void>([]() -> void{static_cast<void>(typename T::value_type{}); }); }); }); });
110+
static_cast<void>([]() -> void{static_cast<void>([]<typename T>() -> void{static_cast<void>([]() -> void{static_cast<void>([]() -> void{static_cast<void>(typename T::value_type{}); }); }); }); });
111+
static_cast<void>([]<typename T>() -> void{static_cast<void>([]() -> void{static_cast<void>([]() -> void{static_cast<void>([]() -> void{static_cast<void>(typename T::value_type{}); }); }); }); });
112+
static_cast<void>([]<typename T>() -> void{static_cast<void>([]() -> void{static_cast<void>([]() -> void{static_cast<void>([]([[maybe_unused]] cpp2::impl::in<typename T::value_type> unnamed_param_1) -> void{}); }); }); });
113+
static_cast<void>([]<typename T>() -> void{static_cast<void>([]() -> void{static_cast<void>([]([[maybe_unused]] cpp2::impl::in<typename T::value_type> unnamed_param_1) -> void{static_cast<void>([]() -> void{}); }); }); });
114+
static_cast<void>([]<typename T>() -> void{static_cast<void>([]([[maybe_unused]] cpp2::impl::in<typename T::value_type> unnamed_param_1) -> void{static_cast<void>([]() -> void{static_cast<void>([]() -> void{}); }); }); });
115+
static_cast<void>([]<typename T>([[maybe_unused]] cpp2::impl::in<typename T::value_type> unnamed_param_1) -> void{static_cast<void>([]() -> void{static_cast<void>([]() -> void{static_cast<void>([]() -> void{}); }); }); });
116+
117+
// Lookup.
118+
{
119+
using alias = std::integral_constant<cpp2::i32,0>;
120+
static_cast<void>(alias::value_type{0});// Non-dependent.
121+
}
122+
static_cast<void>([]<typename T>([[maybe_unused]] T const& unnamed_param_1) -> void{
123+
using alias = std::integral_constant<T,0>;
124+
static_cast<void>(typename alias::value_type{0});// Dependent.
125+
{
126+
using alias = std::integral_constant<cpp2::i32,0>;
127+
static_cast<void>(alias::value_type{0});// Non-dependent.
128+
}
129+
}(0));
130+
131+
static_cast<void>([](auto const& r) -> std::type_identity_t<decltype(*cpp2::impl::assert_not_null(begin(r)))> { return CPP2_ASSERT_IN_BOUNDS_LITERAL(r, 0); }(std::vector<int>(1)));
132+
}
133+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-bugfix-for-dependent-types.cpp2... ok (all Cpp2, passes safety checks)
2+

0 commit comments

Comments
 (0)