|
| 1 | +// RUN: %dxc -Tlib_6_3 -Wno-unused-value -verify %s |
| 2 | +// RUN: %dxc -Tcs_6_0 -Wno-unused-value -verify %s |
| 3 | + |
| 4 | +// Verify that pointer and reference types produced by __decltype cannot be |
| 5 | +// used as struct instance variable types or local variable types in HLSL. |
| 6 | +// Also verifies that template instantiation with pointer/reference type |
| 7 | +// arguments is rejected, and that valid __decltype uses are not affected. |
| 8 | + |
| 9 | +// __decltype is the GCC way of saying 'decltype', but doesn't require C++11. |
| 10 | + |
| 11 | +// groupshared variables are mutable (unlike plain global variables in HLSL), |
| 12 | +// which allows their lvalue expressions to produce reference types via |
| 13 | +// __decltype. |
| 14 | +groupshared int g; |
| 15 | + |
| 16 | +// --- Struct field: reference type from __decltype --- |
| 17 | +struct RefField { |
| 18 | + __decltype(++g) ref_field; // expected-error {{references are unsupported in HLSL}} |
| 19 | +}; |
| 20 | + |
| 21 | +// --- Struct field: pointer type via explicit cast in __decltype --- |
| 22 | +struct PtrField { |
| 23 | + __decltype((int *)0) ptr_field; // expected-error {{pointers are unsupported in HLSL}} |
| 24 | +}; |
| 25 | + |
| 26 | +// --- Struct field: plain (non-reference) type from __decltype is valid --- |
| 27 | +struct PlainField { |
| 28 | + __decltype(0 + 1) plain_field; // no error: int rvalue, not a reference |
| 29 | +}; |
| 30 | + |
| 31 | +// --- Local variable: reference type from __decltype --- |
| 32 | +void test_local_ref() { |
| 33 | + int x = 0; |
| 34 | + __decltype(++x) local_ref; // expected-error {{references are unsupported in HLSL}} |
| 35 | +} |
| 36 | + |
| 37 | +// --- Local variable: pointer type via __decltype --- |
| 38 | +void test_local_ptr() { |
| 39 | + __decltype((int *)0) local_ptr; // expected-error {{pointers are unsupported in HLSL}} |
| 40 | +} |
| 41 | + |
| 42 | +// --- Template: field with explicit reference type is rejected at definition --- |
| 43 | +template <typename T> |
| 44 | +struct RefContainer { |
| 45 | + T &ref_field; // expected-error {{references are unsupported in HLSL}} |
| 46 | +}; |
| 47 | + |
| 48 | +// --- Template instantiation: type argument is an explicit pointer type --- |
| 49 | +// This verifies that pointer instance variables cannot be created through |
| 50 | +// template instantiation with pointer type arguments. |
| 51 | +template <typename T> |
| 52 | +struct Container { |
| 53 | + T field; |
| 54 | +}; |
| 55 | + |
| 56 | +void test_template_ptr_explicit() { |
| 57 | + Container<int *> c; // expected-error {{pointers are unsupported in HLSL}} |
| 58 | +} |
| 59 | + |
| 60 | +// --- Regression: __decltype in is_same template argument still works --- |
| 61 | +// HLSL has a non-standard is_same extension that treats T and T& as the same |
| 62 | +// type. This is used in scalar-operators tests and must not be broken. |
| 63 | +void test_is_same_regression() { |
| 64 | + int x = 0; |
| 65 | + _Static_assert(std::is_same<int, __decltype(x += 1)>::value, |
| 66 | + "regression: is_same with lvalue __decltype must still work"); |
| 67 | +} |
0 commit comments