Skip to content

Commit 15b7551

Browse files
committed
fix(cpp1): improve recognition of dependent types and deducible parameters
1 parent efd185f commit 15b7551

17 files changed

+722
-64
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
// Uncomment once `typename` is supported for template arguments.
20+
// _ = :<T, U> (x: std::pair<T, typename U::value_type>, y: U) = {}(:std::pair = (0, 0), z);
21+
_ = :<T, U> (x: std::array<T, U::value>, y: U) = {}(:std::array<i32, 0> = (), z);
22+
init(out z, z);
23+
id(z, z);
24+
25+
// Test that these are emitted unwrapped in case they are deducible.
26+
(copy f := :<T> (x: std::vector<std::type_identity_t<T>>) = {})
27+
static_assert(!std::is_invocable_v<decltype(f), std::vector<i32>>);
28+
(copy f := :<T> (x: std::vector<std::vector<T>>) = {})
29+
static_assert(std::is_invocable_v<decltype(f), std::vector<std::vector<i32>>>);
30+
}
31+
32+
v: <T> type = {
33+
operator=: (out this, x: T) = { }
34+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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; // Needed, pending #502.
18+
nptr: type == * i32; // Needed, pending #502.
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+
// Uncomment once `typename` is supported for template arguments.
24+
// _ = :identity<typename std::pointer_traits<ptr>::rebind<ptr>> = (); // Non type-only context.
25+
26+
// Aliases.
27+
w: type == T;
28+
_ = :w::value_type = x;
29+
v: type == w;
30+
_ = :v::value_type = x;
31+
a: type == T::type;
32+
_ = :a::value_type = x;
33+
34+
{
35+
// Test that there's no prefixed `typename` to....
36+
_ = std::integral_constant<i32, T::value>(); // `T::value`.
37+
_ = :std::type_identity_t<T> = (); // `std::type_identity_t<T>`.
38+
39+
// Test that non-dependent names aren't emitted with `typename`.
40+
a: type == std::integral_constant<i32, 0>;
41+
b: type == a;
42+
c: type == b;
43+
_ = :b::value_type = x;
44+
_ = :c::value_type = x;
45+
}
46+
}
47+
48+
t: @struct <T: type> type = {
49+
u: @struct type = {
50+
x: T::value_type = ();
51+
this: T::type = ();
52+
}
53+
x: T::value_type = 0;
54+
}
55+
56+
main: () = {
57+
zero: type == std::integral_constant<i32, 0>;
58+
_ = f<zero, 0>(0);
59+
60+
// clang-format off
61+
_ = : ::t<zero> = (); // clang-format on
62+
63+
// Emitted `template` (noop, taken care of by the UFCS macro).
64+
_ = :(f) = { _ = f.operator() <i32>(); }(:<T> () = {});
65+
66+
// Nesting is irrelevant.
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> () = { _ = :() = { _ = :() = { _ = :() = { _ = :T::value_type = (); }; }; }; };
74+
_ = :<T> () = { _ = :() = { _ = :() = { _ = :(x: T::value_type) = {}; }; }; };
75+
_ = :<T> () = { _ = :() = { _ = :(x: T::value_type) = { _ = :() = {}; }; }; };
76+
_ = :<T> () = { _ = :(x: T::value_type) = { _ = :() = { _ = :() = {}; }; }; };
77+
_ = :<T> (x: T::value_type) = { _ = :() = { _ = :() = { _ = :() = {}; }; }; };
78+
79+
// Lookup.
80+
{
81+
alias: type == std::integral_constant<i32, 0>;
82+
_ = :alias::value_type = 0; // Non-dependent.
83+
}
84+
_ = :<T> (_: T) = {
85+
alias: type == std::integral_constant<T, 0>;
86+
_ = :alias::value_type = 0; // Dependent.
87+
{
88+
alias: type == std::integral_constant<i32, 0>;
89+
_ = :alias::value_type = 0; // Non-dependent.
90+
}
91+
}(0);
92+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
clang version 18.0.0 (https://github.com/llvm/llvm-project.git 3723ede3cf5324827f8fbbe7f484c2ee4d7a7204)
2+
Target: x86_64-pc-linux-gnu
3+
Thread model: posix
4+
InstalledDir: /home/johel/root/clang-main/bin

regression-tests/test-results/clang-18/pure2-bugfix-for-deducible-parameters.cpp.execution

Whitespace-only changes.

regression-tests/test-results/clang-18/pure2-bugfix-for-deducible-parameters.cpp.output

Whitespace-only changes.

regression-tests/test-results/clang-18/pure2-bugfix-for-dependent-types.cpp.execution

Whitespace-only changes.

regression-tests/test-results/clang-18/pure2-bugfix-for-dependent-types.cpp.output

Whitespace-only changes.

regression-tests/test-results/clang-18/pure2-bugfix-for-non-local-function-expression.cpp.execution

Whitespace-only changes.

regression-tests/test-results/clang-18/pure2-bugfix-for-non-local-function-expression.cpp.output

Whitespace-only changes.

regression-tests/test-results/clang-18/pure2-print.cpp.output

Whitespace-only changes.

0 commit comments

Comments
 (0)