Match dxc's operator overloading rules

Operators dxc forbids (=, compound assignments, ++, --, ->, new/delete,
non-member operators) are errors at the declaration; unary operators and
compound assignment on structs are 'scalar, vector, or matrix expected',
as in dxc. test/dxil/check.sh now fails when dxc rejects a test instead
of comparing with stale output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Peter Li 2026-10-31 11:32:26 -07:00
parent 47c1edf791
commit 9f5cfebc17
4 changed files with 29 additions and 33 deletions

View File

@ -1892,10 +1892,19 @@ pub const Sema = struct {
o.has_operators = true; o.has_operators = true;
}; };
if (f.template_params.len > 0) return s.declareTemplate(name, d); if (f.template_params.len > 0) return s.declareTemplate(name, d);
// HLSL 2021 overloads operators as member functions only. // dxc's limits: member operators only, and not assignment,
if (std.mem.startsWith(u8, name, "operator") and owner == null) { // compound assignment, ++, --, -> or new/delete.
s.err(d.loc, "non-member operator overloading is not supported yet", .{}); if (std.mem.startsWith(u8, name, "operator")) {
const op = name["operator".len..];
const disallowed = [_][]const u8{ "=", "+=", "-=", "*=", "/=", "%=", "^=", "&=", "|=", "<<=", ">>=", "++", "--", "->", "->*", "new", "delete", "new[]", "delete[]" };
for (disallowed) |x| if (eql(op, x)) {
s.err(d.loc, "overloading '{s}' is not allowed", .{name});
return; return;
};
if (owner == null) {
s.err(d.loc, "overloading non-member '{s}' is not allowed", .{name});
return;
}
} }
if (f.name.parts.len > 1) return; // out-of-line method definition: not modelled yet if (f.name.parts.len > 1) return; // out-of-line method definition: not modelled yet
// `auto` return types are deduced in HLSL 202x; unconstrained here. // `auto` return types are deduced in HLSL 202x; unconstrained here.
@ -2325,17 +2334,9 @@ pub const Sema = struct {
fn unaryExpr(s: *Sema, e: *ast.Expr, op: ast.UnaryOp, operand: *ast.Expr) Allocator.Error!Typed { fn unaryExpr(s: *Sema, e: *ast.Expr, op: ast.UnaryOp, operand: *ast.Expr) Allocator.Error!Typed {
const t = try s.expr(operand); const t = try s.expr(operand);
if (t.ty == TypeTable.error_type) return err_typed; if (t.ty == TypeTable.error_type) return err_typed;
// dxc never applies unary operator overloads.
if (s.types.get(t.ty) == .@"struct" and s.types.get(t.ty).@"struct".overloadsOperators()) { if (s.types.get(t.ty) == .@"struct" and s.types.get(t.ty).@"struct".overloadsOperators()) {
const name: []const u8 = switch (op) { s.err(e.loc, "scalar, vector, or matrix expected", .{});
.plus => "operator+",
.minus => "operator-",
.not => "operator!",
.bit_not => "operator~",
.pre_inc, .post_inc => "operator++",
.pre_dec, .post_dec => "operator--",
};
if (try s.operatorCall(e, t.ty, name, &.{})) |rt| return rvalue(rt);
s.err(e.loc, "invalid argument type '{s}' to unary expression", .{s.typeName(t.ty)});
return err_typed; return err_typed;
} }
const inc_dec = switch (op) { const inc_dec = switch (op) {
@ -2550,16 +2551,10 @@ pub const Sema = struct {
} }
} else { } else {
const r = try s.expr(rhs); const r = try s.expr(rhs);
// `a op= b` on a struct: its operator op= (or operator op). // Compound assignment never uses operator overloads (dxc).
if (s.types.get(l.ty) == .@"struct" and r.ty != TypeTable.error_type) { if (s.types.get(l.ty) == .@"struct" and s.types.get(l.ty).@"struct".overloadsOperators()) {
const bop = std.meta.stringToEnum(ast.BinaryOp, @tagName(op)).?; s.err(e.loc, "scalar, vector, or matrix expected", .{});
const base = binaryOpName(bop).?; return err_typed;
const compound = try std.fmt.allocPrint(s.arena, "{s}=", .{base});
if (try s.operatorCall(e, l.ty, compound, &.{s.argInfo(rhs, r)})) |rt| return rvalue(rt);
if (try s.operatorCall(e, l.ty, base, &.{s.argInfo(rhs, r)})) |rt| {
if (s.conversionCost(rt, l.ty) == null) s.convertError(e.loc, rt, l.ty);
return .{ .ty = l.ty };
}
} }
if (r.ty != TypeTable.error_type) { if (r.ty != TypeTable.error_type) {
const bop = std.meta.stringToEnum(ast.BinaryOp, @tagName(op)).?; const bop = std.meta.stringToEnum(ast.BinaryOp, @tagName(op)).?;

View File

@ -33,7 +33,10 @@ for f in test/dxil/*.hlsl; do
fail=$((fail + 1)); echo "FAIL $name: dxv rejected the output"; head -3 "$TMP/log"; continue fail=$((fail + 1)); echo "FAIL $name: dxv rejected the output"; head -3 "$TMP/log"; continue
fi fi
"$DXC" -dumpbin "$TMP/z.dxo" > "$TMP/z.ll" 2>&1 "$DXC" -dumpbin "$TMP/z.dxo" > "$TMP/z.ll" 2>&1
"$DXC" -T "$profile" "$f" -Fc "$TMP/d.ll" > /dev/null 2>&1 rm -f "$TMP/d.ll"
if ! "$DXC" -T "$profile" "$f" -Fc "$TMP/d.ll" > "$TMP/log" 2>&1; then
fail=$((fail + 1)); echo "FAIL $name: dxc rejects this test"; head -3 "$TMP/log"; continue
fi
ok=1 ok=1
sigtables "$TMP/z.ll" > "$TMP/z.sig" sigtables "$TMP/z.ll" > "$TMP/z.sig"
sigtables "$TMP/d.ll" > "$TMP/d.sig" sigtables "$TMP/d.ll" > "$TMP/d.sig"

View File

@ -1,17 +1,16 @@
// PROFILE: cs_6_0 // PROFILE: cs_6_0
// Operator overloading: binary, unary, [], (), compound assignment. // Operator overloading: binary operators, [] and () (dxc's subset).
struct V2 { struct V2 {
float x, y; float x, y;
V2 operator+(V2 o) { V2 r; r.x = x + o.x; r.y = y + o.y; return r; } V2 operator+(V2 o) { V2 r; r.x = x + o.x; r.y = y + o.y; return r; }
V2 operator*(float k) { V2 r; r.x = x * k; r.y = y * k; return r; } V2 operator*(float k) { V2 r; r.x = x * k; r.y = y * k; return r; }
bool operator==(V2 o) { return x == o.x && y == o.y; } bool operator==(V2 o) { return x == o.x && y == o.y; }
float operator[](uint i) { return i == 0 ? x : y; } float operator[](uint i) { return i == 0 ? x : y; }
V2 operator-() { V2 r; r.x = -x; r.y = -y; return r; }
float operator()(float a, float b) { return x * a + y * b; } float operator()(float a, float b) { return x * a + y * b; }
}; };
RWStructuredBuffer<float> o : register(u0); RWStructuredBuffer<float> o : register(u0);
[numthreads(1,1,1)] void main(uint i : SV_DispatchThreadID) { [numthreads(1,1,1)] void main(uint i : SV_DispatchThreadID) {
V2 a; a.x = 1; a.y = 2; V2 b = a * 3.0; V2 a; a.x = 1; a.y = 2; V2 b = a * 3.0;
V2 c = a + b; c += a; V2 d = -c; V2 c = a + b; c = c + a;
o[i] = c[1] + d(1, 2) + (a == a ? 1 : 0) + d.x; o[i] = c[1] + c(1, 2) + (a == a ? 1 : 0);
} }

View File

@ -1,17 +1,16 @@
// PROFILE: cs_6_0 // PROFILE: cs_6_0
// Operator overloading: binary, unary, [], (), compound assignment. // Operator overloading: binary operators, [] and () (dxc's subset).
struct V2 { struct V2 {
float x, y; float x, y;
V2 operator+(V2 o) { V2 r; r.x = x + o.x; r.y = y + o.y; return r; } V2 operator+(V2 o) { V2 r; r.x = x + o.x; r.y = y + o.y; return r; }
V2 operator*(float k) { V2 r; r.x = x * k; r.y = y * k; return r; } V2 operator*(float k) { V2 r; r.x = x * k; r.y = y * k; return r; }
bool operator==(V2 o) { return x == o.x && y == o.y; } bool operator==(V2 o) { return x == o.x && y == o.y; }
float operator[](uint i) { return i == 0 ? x : y; } float operator[](uint i) { return i == 0 ? x : y; }
V2 operator-() { V2 r; r.x = -x; r.y = -y; return r; }
float operator()(float a, float b) { return x * a + y * b; } float operator()(float a, float b) { return x * a + y * b; }
}; };
RWStructuredBuffer<float> o : register(u0); RWStructuredBuffer<float> o : register(u0);
[numthreads(1,1,1)] void main(uint i : SV_DispatchThreadID) { [numthreads(1,1,1)] void main(uint i : SV_DispatchThreadID) {
V2 a; a.x = 1; a.y = 2; V2 b = a * 3.0; V2 a; a.x = 1; a.y = 2; V2 b = a * 3.0;
V2 c = a + b; c += a; V2 d = -c; V2 c = a + b; c = c + a;
o[i] = c[1] + d(1, 2) + (a == a ? 1 : 0) + d.x; o[i] = c[1] + c(1, 2) + (a == a ? 1 : 0);
} }