Skip to content

Commit b13e386

Browse files
damyanpCopilot
andauthored
Emit an error when attempting add a user-defined conversion function (#8206)
Previously, user-defined conversion functions were silently ignored. This change reports an error if one is defined. Fixes #5103 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: damyanp <8118402+damyanp@users.noreply.github.com>
1 parent a156fed commit b13e386

4 files changed

Lines changed: 55 additions & 2 deletions

File tree

docs/ReleaseNotes.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,16 @@ line upon naming the release. Refer to previous for appropriate section names.
3838

3939
#### Bug Fixes
4040

41-
- Fixed non-deterministic DXIL/PDB output when compiling shaders with resource arrays, debug info, and SM 6.6+. [#8171](https://github.com/microsoft/DirectXShaderCompiler/issues/8171)
41+
- Fixed non-deterministic DXIL/PDB output when compiling shaders with resource
42+
arrays, debug info, and SM 6.6+.
43+
[#8171](https://github.com/microsoft/DirectXShaderCompiler/issues/8171)
4244
- Fixed mesh shader semantics that were incorrectly case sensitive.
45+
- User-defined conversion operators (e.g., `operator float4()`) now produce an
46+
error instead of being silently ignored.
47+
[#5103](https://github.com/microsoft/DirectXShaderCompiler/pull/8206)
4348
- DXIL validation: added validation for `CreateHandleFromBinding`.
44-
- DXIL validation now rejects non-standard integer bit widths (e.g. `i25`) in instructions.
49+
- DXIL validation now rejects non-standard integer bit widths (e.g. `i25`) in
50+
instructions.
4551

4652
#### Other Changes
4753

tools/clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7551,6 +7551,8 @@ def err_hlsl_matrix_member_zero_in_one_based: Error<
75517551
"the digit '0' is used in '%0', but the syntax is for one-based rows and columns">;
75527552
def err_hlsl_overloading_operator_disallowed: Error<
75537553
"overloading %select{|non-member }1%0 is not allowed">;
7554+
def err_hlsl_unsupported_conversion_operator: Error<
7555+
"conversion operator overloading is not allowed">;
75547556
def err_hlsl_vector_member_bad_format: Error<
75557557
"invalid format for vector swizzle '%0'">;
75567558
def err_hlsl_vector_member_empty: Error<

tools/clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6956,6 +6956,14 @@ static void extendRight(SourceRange &R, const SourceRange &After) {
69566956
/// well-formed type for the conversion operator.
69576957
void Sema::CheckConversionDeclarator(Declarator &D, QualType &R,
69586958
StorageClass& SC) {
6959+
// HLSL Change Starts
6960+
if (getLangOpts().HLSL) {
6961+
Diag(D.getIdentifierLoc(), diag::err_hlsl_unsupported_conversion_operator);
6962+
D.setInvalidType();
6963+
return;
6964+
}
6965+
// HLSL Change Ends
6966+
69596967
// C++ [class.conv.fct]p1:
69606968
// Neither parameter types nor return type can be specified. The
69616969
// type of a conversion function (8.3.5) is "function taking no
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %dxc -Tlib_6_3 -verify -HV 2021 %s
2+
3+
// This test verifies that dxcompiler generates an error when defining
4+
// a conversion operator (cast operator), which is not supported in HLSL.
5+
6+
struct MyStruct {
7+
float4 f;
8+
9+
// expected-error@+1 {{conversion operator overloading is not allowed}}
10+
operator float4() {
11+
return 42;
12+
}
13+
};
14+
15+
struct AnotherStruct {
16+
int x;
17+
18+
// expected-error@+1 {{conversion operator overloading is not allowed}}
19+
operator int() {
20+
return x;
21+
}
22+
23+
// expected-error@+1 {{conversion operator overloading is not allowed}}
24+
operator bool() {
25+
return x != 0;
26+
}
27+
};
28+
29+
template<typename T>
30+
struct TemplateStruct {
31+
T value;
32+
33+
// expected-error@+1 {{conversion operator overloading is not allowed}}
34+
operator T() {
35+
return value;
36+
}
37+
};

0 commit comments

Comments
 (0)