Skip to content

Commit ae91505

Browse files
committed
fix(to_cpp1): improve recognition of dependent types and deducible parameters
1 parent 0b333f3 commit ae91505

16 files changed

+762
-60
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Dependent, non-deducible parameters
2+
// are wrapped like non-dependent parameters.
3+
init: <T> (out x: std::integral_constant<i32, T::value>) = { x = (); }
4+
init: <T> (out x: std::integral_constant<i32, T::value>, _: T) = { x = (); }
5+
id: <T> (x: std::integral_constant<i32, T::value>) -> forward _ = x;
6+
id: <T> (x: std::integral_constant<i32, T::value>, y: T) = { assert(x& == y&); }
7+
8+
main: () = {
9+
zero: type == std::integral_constant<i32, 0>;
10+
11+
z: zero;
12+
init<zero>(out z);
13+
assert(id<zero>(z)& == z&);
14+
15+
// Deducible parameters.
16+
_ = :v = 0;
17+
_ = :<T> (x: std::vector<T>) = {}(:std::vector<i32> = ());
18+
_ = :<T> (x: std::vector<std::vector<T>>) = {}(:std::vector<std::vector<i32>> = ());
19+
// _ = :<T, U> (x: std::pair<T, typename U::value_type>, y: U) = {}(:std::pair = (0, 0), z); // Blocked on #727.
20+
_ = :<T, U> (x: std::array<T, U::value>, y: U) = {}(:std::array<i32, 0> = (), z);
21+
init(out z, z);
22+
id(z, z);
23+
24+
// Test that these are emitted unwrapped in case they are deducible.
25+
(copy f := :<T> (x: std::vector<std::type_identity_t<T>>) = {})
26+
static_assert(!std::is_invocable_v<decltype(f), std::vector<i32>>, "`T` is non-deducible.");
27+
(copy f := :<T> (x: std::vector<std::vector<T>>) = {})
28+
static_assert(std::is_invocable_v<decltype(f), std::vector<std::vector<i32>>>, "`T` is deducible.");
29+
}
30+
31+
v: <T> type = {
32+
operator=: (out this, x: T) = { }
33+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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: @struct <T: type> type = {
48+
u: @struct type = {
49+
x: T::value_type = ();
50+
this: T::type = ();
51+
}
52+
x: T::value_type = 0;
53+
}
54+
55+
main: () = {
56+
zero: type == std::integral_constant<i32, 0>;
57+
_ = f<zero, 0>(0);
58+
59+
// clang-format off
60+
_ = : ::t<zero> = (); // clang-format on
61+
62+
// Emitted `template` (noop, taken care of by the UFCS macro).
63+
_ = :(f) = { _ = f.operator()<i32>(); }(:<T> () = {});
64+
65+
// Nesting is not relevant to lookup.
66+
_ = :<T> () = { _ = :T::value_type = (); };
67+
_ = :() = { _ = :<T> () = { _ = :T::value_type = (); }; };
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> () = { _ = :() = { _ = :() = { _ = :(x: T::value_type) = {}; }; }; };
74+
_ = :<T> () = { _ = :() = { _ = :(x: T::value_type) = { _ = :() = {}; }; }; };
75+
_ = :<T> () = { _ = :(x: T::value_type) = { _ = :() = { _ = :() = {}; }; }; };
76+
_ = :<T> (x: T::value_type) = { _ = :() = { _ = :() = { _ = :() = {}; }; }; };
77+
78+
// Lookup.
79+
{
80+
alias: type == std::integral_constant<i32, 0>;
81+
_ = :alias::value_type = 0; // Non-dependent.
82+
}
83+
_ = :<T> (_: T) = {
84+
alias: type == std::integral_constant<T, 0>;
85+
_ = :alias::value_type = 0; // Dependent.
86+
{
87+
alias: type == std::integral_constant<i32, 0>;
88+
_ = :alias::value_type = 0; // Non-dependent.
89+
}
90+
}(0);
91+
}

regression-tests/test-results/gcc-13/pure2-bugfix-for-deducible-parameters.cpp.execution

Whitespace-only changes.

regression-tests/test-results/gcc-13/pure2-bugfix-for-deducible-parameters.cpp.output

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
pure2-bugfix-for-dependent-types-recursion.cpp: In function ‘int main()’:
2+
pure2-bugfix-for-dependent-types-recursion.cpp:19:13: error: ‘b’ does not name a type
3+
19 | using a = b;
4+
| ^
5+
pure2-bugfix-for-dependent-types-recursion.cpp:20:13: error: ‘a’ does not name a type
6+
20 | using b = a;
7+
| ^
8+
pure2-bugfix-for-dependent-types-recursion.cpp:21:21: error: ‘a’ has not been declared
9+
21 | static_cast<void>(a::t);
10+
| ^
11+
pure2-bugfix-for-dependent-types-recursion.cpp:22:21: error: ‘b’ has not been declared
12+
22 | static_cast<void>(b::t);
13+
| ^

regression-tests/test-results/gcc-13/pure2-bugfix-for-dependent-types.cpp.execution

Whitespace-only changes.

regression-tests/test-results/gcc-13/pure2-bugfix-for-dependent-types.cpp.output

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