Skip to content

[HLSL2021] Disable operators that rely on references (#4133) #4135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion tools/clang/lib/Sema/SemaDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//
//===----------------------------------------------------------------------===//

#include "clang/Basic/OperatorKinds.h"
#include "clang/Sema/SemaInternal.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
Expand Down Expand Up @@ -11638,7 +11639,10 @@ bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) {
// HLSL Change Starts
if (LangOpts.HLSL) {
if (Op == OO_Delete || Op == OO_Array_Delete || Op == OO_New ||
Op == OO_Array_New) {
Op == OO_Array_New || Op == OO_Equal ||
(Op >= OO_PlusEqual && Op <= OO_GreaterGreaterEqual) ||
Op == OO_PlusPlus || Op == OO_MinusMinus || Op == OO_ArrowStar ||
Op == OO_Arrow) {
return Diag(FnDecl->getLocation(),
diag::err_hlsl_overloading_new_delete_operator)
<< FnDecl->getDeclName();
Expand Down
31 changes: 0 additions & 31 deletions tools/clang/test/CodeGenSPIRV/operator.overloading.assign.hlsl

This file was deleted.

30 changes: 30 additions & 0 deletions tools/clang/test/HLSL/overloading-unsupported-operators.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -ffreestanding -verify -enable-operator-overloading %s
// RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -ffreestanding -verify -HV 2021 %s

// This test checks that dxcompiler generates errors when overloading operators
// that are not supported for overloading in HLSL 2021

struct S
{
float foo;
void operator=(S s) {} // expected-error {{overloading 'operator=' is not allowed}}
void operator+=(S s) {} // expected-error {{overloading 'operator+=' is not allowed}}
void operator-=(S s) {} // expected-error {{overloading 'operator-=' is not allowed}}
void operator*=(S s) {} // expected-error {{overloading 'operator*=' is not allowed}}
void operator/=(S s) {} // expected-error {{overloading 'operator/=' is not allowed}}
void operator%=(S s) {} // expected-error {{overloading 'operator%=' is not allowed}}
void operator^=(S s) {} // expected-error {{overloading 'operator^=' is not allowed}}
void operator&=(S s) {} // expected-error {{overloading 'operator&=' is not allowed}}
void operator|=(S s) {} // expected-error {{overloading 'operator|=' is not allowed}}
void operator<<(S s) {} // expected-error {{overloading 'operator<<' is not allowed}}
void operator>>(S s) {} // expected-error {{overloading 'operator>>' is not allowed}}
void operator<<=(S s) {} // expected-error {{overloading 'operator<<=' is not allowed}}
void operator>>=(S s) {} // expected-error {{overloading 'operator>>=' is not allowed}}
void operator->*(S s) {} // expected-error {{overloading 'operator->*' is not allowed}}
void operator->(S s) {} // expected-error {{overloading 'operator->' is not allowed}}
void operator++(S s) {} // expected-error {{overloading 'operator++' is not allowed}}
void operator--(S s) {} // expected-error {{overloading 'operator--' is not allowed}}
};

[numthreads(1,1,1)]
void main() {}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
struct S {
float a;

void operator=(float x) {
a = x;
float operator-(float x) {
return a - x;
}

float operator+(float x) {
Expand All @@ -17,20 +17,18 @@ struct S {
struct Number {
int n;

void operator=(float x) {
n = x;
int operator+(float x) {
return n + x;
}
};

int main(float4 pos: SV_Position) : SV_Target {
S s1;
S s2;
s1 = s2;
s1 = 0.2;
s1 = s1 + 0.1;
S s1 = {0.2};
S s2 = {0.2};
float f = s1 + 0.1;

Number a = {pos.x};
Number b = {pos.y};
a = pos.x;
return a.n;
a.n = b + pos.x;
return b + pos.y;
}
3 changes: 0 additions & 3 deletions tools/clang/unittests/SPIRV/CodeGenSpirvTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,6 @@ TEST_F(FileTest, VarVFACEInterface) {
runFileTest("var.vface.interface.hlsl", Expect::Warning);
}

TEST_F(FileTest, OperatorOverloadingAssign) {
runFileTest("operator.overloading.assign.hlsl");
}
TEST_F(FileTest, OperatorOverloadingCall) {
runFileTest("operator.overloading.call.hlsl");
}
Expand Down