FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cppjit/test/test_cpp23features.py at main · compiler-research/cppjit · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
compiler-research
/
cppjit
Public
Notifications
You must be signed in to change notification settings
Fork
8
Star
12
Code
Issues
6
Pull requests
5
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cppjit
/
test
/
test_cpp23features.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
428 lines (353 loc) · 13.4 KB
Breadcrumbs
cppjit
/
test
/
test_cpp23features.py
Copy path
File metadata and controls
428 lines (353 loc) · 13.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
from
pytest
import
mark
from
support
import
IS_CPP23
# The interpreter is a process-wide singleton whose C++ standard is pinned at
# the first `import cppjit`, so these tests only run when the suite itself is
# under C++23 (the cxx-standard=23 CI cells). To run them locally (the env var
# reaches CreateInterpreter for both clang-repl and cling):
#
# CPPINTEROP_EXTRA_INTERPRETER_ARGS=-std=c++23 pytest -v test_cpp23features.py
@
mark
.
skipif
(
not
IS_CPP23
,
reason
=
"C++23 tests need the interpreter to run under -std=c++23"
,
)
class
TestCPP23FEATURES
:
"""C++23 features driven via cppjit.cppdef through the JIT (clang-repl/cling)."""
@
classmethod
def
setup_class
(
cls
):
import
cppjit
cls
.
cppjit
=
cppjit
def
test01_deducing_this_basic
(
self
):
"""Explicit object parameter on a member function (P0847R7)"""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
namespace Cpp23DeducingThis {
struct Widget {
int value = 42;
int get(this Widget& self) { return self.value; }
};
}
"""
)
w
=
cppjit
.
gbl
.
Cpp23DeducingThis
.
Widget
()
assert
w
.
value
==
42
assert
w
.
get
()
==
42
def
test02_deducing_this_const
(
self
):
"""const explicit object parameter (this const Widget& self)"""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
namespace Cpp23DeducingThis {
struct CWidget {
int value = 7;
int read(this const CWidget& self) { return self.value; }
};
}
"""
)
w
=
cppjit
.
gbl
.
Cpp23DeducingThis
.
CWidget
()
assert
w
.
read
()
==
7
def
test03_deducing_this_by_value
(
self
):
"""by-value explicit object parameter (this Widget self)"""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
namespace Cpp23DeducingThis {
struct VWidget {
int value = 5;
int snapshot(this VWidget self) { return self.value; }
};
}
"""
)
w
=
cppjit
.
gbl
.
Cpp23DeducingThis
.
VWidget
()
w
.
value
=
11
assert
w
.
snapshot
()
==
11
def
test04_deducing_this_with_extra_args
(
self
):
"""explicit object parameter alongside regular arguments"""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
namespace Cpp23DeducingThis {
struct AWidget {
int base = 100;
int add(this AWidget& self, int x, int y) { return self.base + x + y; }
};
}
"""
)
w
=
cppjit
.
gbl
.
Cpp23DeducingThis
.
AWidget
()
assert
w
.
add
(
20
,
3
)
==
123
def
test05_deducing_this_rvalue_ref
(
self
):
"""rvalue-ref-qualified explicit object parameter (this Widget&&)"""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
namespace Cpp23DeducingThis {
struct RWidget {
int value = 13;
int consume(this RWidget&& self) { return self.value; }
};
}
"""
)
w
=
cppjit
.
gbl
.
Cpp23DeducingThis
.
RWidget
()
assert
w
.
consume
()
==
13
def
test05b_traditional_rvalue_ref_qualifier
(
self
):
"""traditional rvalue-ref-qualified member (int f() &&), no deducing this"""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
namespace Cpp23DeducingThis {
struct QWidget {
int value = 17;
int consume() && { return value; }
};
}
"""
)
w
=
cppjit
.
gbl
.
Cpp23DeducingThis
.
QWidget
()
assert
w
.
consume
()
==
17
def
test06_deducing_this_chaining
(
self
):
"""builder-style chaining: explicit object parameter returns self"""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
namespace Cpp23DeducingThis {
struct BWidget {
int value = 0;
BWidget& set(this BWidget& self, int v) { self.value = v; return self; }
};
}
"""
)
w
=
cppjit
.
gbl
.
Cpp23DeducingThis
.
BWidget
()
r
=
w
.
set
(
5
).
set
(
8
)
assert
r
.
value
==
8
assert
w
.
value
==
8
# same object returned by reference
def
test07_deducing_this_default_arg
(
self
):
"""explicit object parameter with a defaulted regular argument"""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
namespace Cpp23DeducingThis {
struct DWidget {
int base = 3;
int scale(this DWidget& self, int factor = 4) { return self.base * factor; }
};
}
"""
)
w
=
cppjit
.
gbl
.
Cpp23DeducingThis
.
DWidget
()
assert
w
.
scale
()
==
12
# default factor=4
assert
w
.
scale
(
10
)
==
30
def
test08_deducing_this_call_operator
(
self
):
"""call operator with an explicit object parameter"""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
namespace Cpp23DeducingThis {
struct Adder {
int base = 100;
int operator()(this Adder& self, int x) { return self.base + x; }
};
}
"""
)
a
=
cppjit
.
gbl
.
Cpp23DeducingThis
.
Adder
()
assert
a
(
23
)
==
123
def
test09_deducing_this_mixed_overload
(
self
):
"""overload set mixing an explicit-object and a normal member function"""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
namespace Cpp23DeducingThis {
struct MWidget {
int value = 50;
int get(this MWidget& self) { return self.value; }
int get(int extra) { return value + extra; }
};
}
"""
)
w
=
cppjit
.
gbl
.
Cpp23DeducingThis
.
MWidget
()
assert
w
.
get
()
==
50
# explicit-object overload
assert
w
.
get
(
7
)
==
57
# normal overload
def
test10_deducing_this_templated
(
self
):
"""templated explicit object parameter (the CRTP-replacement idiom)"""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
namespace Cpp23DeducingThis {
struct TWidget {
int value = 9;
template <class Self>
int via(this Self&& self) { return self.value; }
};
}
"""
)
w
=
cppjit
.
gbl
.
Cpp23DeducingThis
.
TWidget
()
assert
w
.
via
()
==
9
def
test11_deducing_this_templated_with_args
(
self
):
"""templated explicit object parameter alongside a regular argument"""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
namespace Cpp23DeducingThis {
struct TAWidget {
int base = 40;
template <class Self>
int plus(this Self&& self, int x) { return self.base + x; }
};
}
"""
)
w
=
cppjit
.
gbl
.
Cpp23DeducingThis
.
TAWidget
()
assert
w
.
plus
(
2
)
==
42
def
test12_deducing_this_templated_returns_self_type
(
self
):
"""deduced Self drives the return: returns the deduced object's field"""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
namespace Cpp23DeducingThis {
struct TRWidget {
int value = 99;
template <class Self>
auto identity(this Self&& self) { return self.value; }
};
}
"""
)
w
=
cppjit
.
gbl
.
Cpp23DeducingThis
.
TRWidget
()
assert
w
.
identity
()
==
99
def
test13_deducing_this_abbreviated_auto
(
self
):
"""abbreviated `this auto&&` form (the canonical CRTP-replacement idiom)"""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
namespace Cpp23DeducingThis {
struct AAWidget {
int value = 23;
int get(this auto&& self) { return self.value; }
};
}
"""
)
w
=
cppjit
.
gbl
.
Cpp23DeducingThis
.
AAWidget
()
assert
w
.
get
()
==
23
def
test14_deducing_this_by_value_copy_semantics
(
self
):
"""by-value `this W self` operates on an independent copy"""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
namespace Cpp23DeducingThis {
struct CopyWidget {
int value = 1;
int bump(this CopyWidget self) { self.value += 100; return self.value; }
};
}
"""
)
w
=
cppjit
.
gbl
.
Cpp23DeducingThis
.
CopyWidget
()
assert
w
.
bump
()
==
101
# the copy is mutated
assert
w
.
value
==
1
# ... the original is untouched
def
test15_deducing_this_inheritance
(
self
):
"""base-class explicit object method invoked on a derived object"""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
namespace Cpp23DeducingThis {
struct Base {
int value = 8;
int get(this Base& self) { return self.value; }
};
struct Derived : Base { };
}
"""
)
d
=
cppjit
.
gbl
.
Cpp23DeducingThis
.
Derived
()
assert
d
.
get
()
==
8
# ------------------------------------------------------------------ #
# Use-cases from the Microsoft C++ blog post "C++23's Deducing this":
# https://devblogs.microsoft.com/cppblog/cpp23-deducing-this/
# ------------------------------------------------------------------ #
def
test16_blog_deduplication
(
self
):
"""Blog use-case 1: code de-duplication of cv/ref accessors."""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
#include <utility>
namespace Cpp23DeducingThis {
struct Optional {
int m_value = 5;
// one accessor, all value categories (returns a reference)
template <class Self> auto&& value(this Self&& self) {
return std::forward<Self>(self).m_value;
}
// by-value flavor, for a clean read from Python
template <class Self> auto read(this Self&& self) {
return std::forward<Self>(self).m_value;
}
};
}
"""
)
o
=
cppjit
.
gbl
.
Cpp23DeducingThis
.
Optional
()
assert
o
.
read
()
==
5
# forwarding read
o
.
value
()[
0
]
=
17
# write through the forwarded reference
assert
o
.
read
()
==
17
# ... mutation is visible on the object
def
test17_blog_crtp_postfix_increment
(
self
):
"""Blog use-case 2: CRTP postfix increment without templating the base.
The using-declaration re-exposes the inherited postfix (standard name
hiding); driven from C++ as Python has no postfix-increment syntax.
"""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
namespace Cpp23DeducingThis {
struct add_postfix_increment {
template <typename Self>
auto operator++(this Self&& self, int) { auto tmp = self; ++self; return tmp; }
};
struct some_type : add_postfix_increment {
using add_postfix_increment::operator++; // un-hide inherited postfix
int v = 0;
some_type& operator++() { ++v; return *this; }
};
// old.v should be the pre-increment value, c.v the post.
int drive_postfix() { some_type c; auto old = c++; return old.v * 100 + c.v; }
}
"""
)
assert
cppjit
.
gbl
.
Cpp23DeducingThis
.
drive_postfix
()
==
1
# old.v=0, c.v=1
def
test18_blog_recursive_lambda
(
self
):
"""Blog use-case 4: recursive lambda via the explicit object parameter."""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
namespace Cpp23DeducingThis {
int fib(int n) {
auto f = [](this auto const& self, int n) -> int {
return n < 2 ? n : self(n - 1) + self(n - 2);
};
return f(n);
}
}
"""
)
assert
cppjit
.
gbl
.
Cpp23DeducingThis
.
fib
(
10
)
==
55
def
test19_blog_lambda_forwarding
(
self
):
"""Blog use-case 3: closure with an explicit object parameter.
The blog uses std::forward_like (not in the host libstdc++); this
exercises the explicit-object closure mechanism itself.
"""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
#include <utility>
namespace Cpp23DeducingThis {
struct Scheduler { int submitted = 0; int submit(int m) { submitted = m; return m; } };
int run_callback() {
Scheduler scheduler;
int message = 42;
auto callback = [message, &scheduler](this auto&& self) -> int {
return scheduler.submit(message);
};
return callback();
}
}
"""
)
assert
cppjit
.
gbl
.
Cpp23DeducingThis
.
run_callback
()
==
42
def
test20_blog_pass_by_value
(
self
):
"""Blog use-case 5: pass the object by value for better codegen on small types."""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
namespace Cpp23DeducingThis {
struct just_a_little_guy {
int how_smol = 21;
int uwu(this just_a_little_guy self) { return self.how_smol * 2; }
};
}
"""
)
assert
cppjit
.
gbl
.
Cpp23DeducingThis
.
just_a_little_guy
().
uwu
()
==
42
def
test21_blog_sfinae_friendly_transform
(
self
):
"""Blog use-case 6: SFINAE-friendly optional::transform.
Driven from C++: cppjit can't resolve a two-template-parameter
explicit-object method against a Python callable.
"""
cppjit
=
self
.
cppjit
assert
cppjit
.
cppdef
(
"""
namespace Cpp23DeducingThis {
template <class T>
struct Optional6 {
T m_value;
template <class Self, class F>
auto transform(this Self&& self, F&& f) { return f(self.m_value); }
};
int triple(int x) { return x * 3; }
int drive_transform() { Optional6<int> o{14}; return o.transform(triple); }
}
"""
)
assert
cppjit
.
gbl
.
Cpp23DeducingThis
.
drive_transform
()
==
42
Back
|
FazBrowse Home
|
New Git URL