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.
This commit is contained in:
Peter Li 2026-10-31 11:32:26 -07:00
parent 392df21a12
commit 06bfbe2a06
1 changed files with 19 additions and 24 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")) {
return; 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;
};
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)).?;