Skip to content

Commit 770ac0c

Browse files
authored
[HLSL2021] Disable operators that rely on references (#4133) (#4135)
* Disable operators that rely on references After lots of discussion, we think it is best to disable operators that should rely on reference parameters or return types for HLSL 2021. The main driver of this is that we believe that references will get added to HLSL in the near future, and we'd like to make it easier to maintain compatability between HLSL 2021 and future versions of HLSL where references are supported. * Removing assignment operator test case (cherry picked from commit ade5e47)
1 parent 751b453 commit 770ac0c

6 files changed

Lines changed: 44 additions & 67 deletions

File tree

tools/clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14+
#include "clang/Basic/OperatorKinds.h"
1415
#include "clang/Sema/SemaInternal.h"
1516
#include "clang/AST/ASTConsumer.h"
1617
#include "clang/AST/ASTContext.h"
@@ -11638,7 +11639,10 @@ bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) {
1163811639
// HLSL Change Starts
1163911640
if (LangOpts.HLSL) {
1164011641
if (Op == OO_Delete || Op == OO_Array_Delete || Op == OO_New ||
11641-
Op == OO_Array_New) {
11642+
Op == OO_Array_New || Op == OO_Equal ||
11643+
(Op >= OO_PlusEqual && Op <= OO_GreaterGreaterEqual) ||
11644+
Op == OO_PlusPlus || Op == OO_MinusMinus || Op == OO_ArrowStar ||
11645+
Op == OO_Arrow) {
1164211646
return Diag(FnDecl->getLocation(),
1164311647
diag::err_hlsl_overloading_new_delete_operator)
1164411648
<< FnDecl->getDeclName();

tools/clang/test/CodeGenSPIRV/operator.overloading.assign.hlsl

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -ffreestanding -verify -enable-operator-overloading %s
2+
// RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -ffreestanding -verify -HV 2021 %s
3+
4+
// This test checks that dxcompiler generates errors when overloading operators
5+
// that are not supported for overloading in HLSL 2021
6+
7+
struct S
8+
{
9+
float foo;
10+
void operator=(S s) {} // expected-error {{overloading 'operator=' is not allowed}}
11+
void operator+=(S s) {} // expected-error {{overloading 'operator+=' is not allowed}}
12+
void operator-=(S s) {} // expected-error {{overloading 'operator-=' is not allowed}}
13+
void operator*=(S s) {} // expected-error {{overloading 'operator*=' is not allowed}}
14+
void operator/=(S s) {} // expected-error {{overloading 'operator/=' is not allowed}}
15+
void operator%=(S s) {} // expected-error {{overloading 'operator%=' is not allowed}}
16+
void operator^=(S s) {} // expected-error {{overloading 'operator^=' is not allowed}}
17+
void operator&=(S s) {} // expected-error {{overloading 'operator&=' is not allowed}}
18+
void operator|=(S s) {} // expected-error {{overloading 'operator|=' is not allowed}}
19+
void operator<<(S s) {} // expected-error {{overloading 'operator<<' is not allowed}}
20+
void operator>>(S s) {} // expected-error {{overloading 'operator>>' is not allowed}}
21+
void operator<<=(S s) {} // expected-error {{overloading 'operator<<=' is not allowed}}
22+
void operator>>=(S s) {} // expected-error {{overloading 'operator>>=' is not allowed}}
23+
void operator->*(S s) {} // expected-error {{overloading 'operator->*' is not allowed}}
24+
void operator->(S s) {} // expected-error {{overloading 'operator->' is not allowed}}
25+
void operator++(S s) {} // expected-error {{overloading 'operator++' is not allowed}}
26+
void operator--(S s) {} // expected-error {{overloading 'operator--' is not allowed}}
27+
};
28+
29+
[numthreads(1,1,1)]
30+
void main() {}

tools/clang/test/HLSLFileCheck/hlsl/operator_overloading/operator.overloading.assign.hlsl

Lines changed: 0 additions & 21 deletions
This file was deleted.

tools/clang/test/HLSLFileCheck/hlsl/operator_overloading/operator.overloading.lookup.definition.hlsl

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
struct S {
66
float a;
77

8-
void operator=(float x) {
9-
a = x;
8+
float operator-(float x) {
9+
return a - x;
1010
}
1111

1212
float operator+(float x) {
@@ -17,20 +17,18 @@ struct S {
1717
struct Number {
1818
int n;
1919

20-
void operator=(float x) {
21-
n = x;
20+
int operator+(float x) {
21+
return n + x;
2222
}
2323
};
2424

2525
int main(float4 pos: SV_Position) : SV_Target {
26-
S s1;
27-
S s2;
28-
s1 = s2;
29-
s1 = 0.2;
30-
s1 = s1 + 0.1;
26+
S s1 = {0.2};
27+
S s2 = {0.2};
28+
float f = s1 + 0.1;
3129

3230
Number a = {pos.x};
3331
Number b = {pos.y};
34-
a = pos.x;
35-
return a.n;
32+
a.n = b + pos.x;
33+
return b + pos.y;
3634
}

tools/clang/unittests/SPIRV/CodeGenSpirvTest.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,6 @@ TEST_F(FileTest, VarVFACEInterface) {
228228
runFileTest("var.vface.interface.hlsl", Expect::Warning);
229229
}
230230

231-
TEST_F(FileTest, OperatorOverloadingAssign) {
232-
runFileTest("operator.overloading.assign.hlsl");
233-
}
234231
TEST_F(FileTest, OperatorOverloadingCall) {
235232
runFileTest("operator.overloading.call.hlsl");
236233
}

0 commit comments

Comments
 (0)