Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
2 changes: 2 additions & 0 deletions tools/clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -7550,6 +7550,8 @@ def err_hlsl_matrix_member_zero_in_one_based: Error<
"the digit '0' is used in '%0', but the syntax is for one-based rows and columns">;
def err_hlsl_overloading_operator_disallowed: Error<
"overloading %select{|non-member }1%0 is not allowed">;
def err_hlsl_unsupported_conversion_operator: Error<
"conversion operator overloading is not allowed">;
def err_hlsl_vector_member_bad_format: Error<
"invalid format for vector swizzle '%0'">;
def err_hlsl_vector_member_empty: Error<
Expand Down
8 changes: 8 additions & 0 deletions tools/clang/lib/Sema/SemaDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6956,6 +6956,14 @@ static void extendRight(SourceRange &R, const SourceRange &After) {
/// well-formed type for the conversion operator.
void Sema::CheckConversionDeclarator(Declarator &D, QualType &R,
StorageClass& SC) {
// HLSL Change Starts
if (getLangOpts().HLSL) {
Diag(D.getIdentifierLoc(), diag::err_hlsl_unsupported_conversion_operator);
D.setInvalidType();
return;
}
// HLSL Change Ends

// C++ [class.conv.fct]p1:
// Neither parameter types nor return type can be specified. The
// type of a conversion function (8.3.5) is "function taking no
Expand Down
27 changes: 27 additions & 0 deletions tools/clang/test/SemaHLSL/conversion-operator-errors.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: %dxc -Tlib_6_3 -verify -HV 2021 %s

// This test verifies that dxcompiler generates an error when defining
// a conversion operator (cast operator), which is not supported in HLSL.

struct MyStruct {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a test for the template-dependent case?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a template-dependent test case (template<typename T> struct TemplateStruct { operator T() { ... } }) in 2fcaf368. The error is caught at parse time in CheckConversionDeclarator, so it fires even without template instantiation.

float4 f;

// expected-error@+1 {{conversion operator overloading is not allowed}}
operator float4() {
return 42;
}
};

struct AnotherStruct {
int x;

// expected-error@+1 {{conversion operator overloading is not allowed}}
operator int() {
return x;
}

// expected-error@+1 {{conversion operator overloading is not allowed}}
operator bool() {
return x != 0;
}
};