Sema/parser: fewer false errors on legacy and edge-case code

- Legacy effect syntax (technique blocks, effect object declarations,
  sampler_state assignments, local state blocks) is skipped with dxc's
  deprecation warnings.
- f16 / f32 / f64 float suffixes are accepted with dxc's 202x-extension
  warning (f16 needs -enable-16bit-types).
- Casts of lvalues that keep the component type are lvalues.
- No error for const locals without an initializer (dxc accepts them).
- 'using namespace dx' is accepted in every shader model.

oracle-verify: 85% of expected error lines caught, 12 false (was 51).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Peter Li 2026-10-31 04:27:44 -07:00
parent 1081a728d0
commit 0d16162df7
2 changed files with 97 additions and 7 deletions

View File

@ -699,6 +699,18 @@ pub const Parser = struct {
const save = p.pos; const save = p.pos;
const lead_quals = p.parseQualifiers(); const lead_quals = p.parseQualifiers();
const t = p.tok(); const t = p.tok();
// Legacy effect syntax, skipped with dxc's deprecation warnings.
if (isTechniqueWord(t)) {
_ = p.reportAt(p.pos, .warning, "effect technique ignored - effect syntax is deprecated and will be removed in HLSL 2026", .{});
while (!p.at(.l_brace) and !p.at(.eof)) _ = p.advance();
if (p.at(.l_brace)) try p.skipBalanced(.l_brace, .r_brace);
return;
}
if (isEffectObjectWord(t) and p.peek(1).kind == .identifier) {
_ = p.reportAt(p.pos, .warning, "effect object ignored - effect syntax is deprecated and will be removed in HLSL 2026", .{});
p.skipDeclaration();
return;
}
if (t.isIdent("struct") or t.isIdent("class") or t.isIdent("interface")) { if (t.isIdent("struct") or t.isIdent("class") or t.isIdent("interface")) {
return p.parseStructDeclInto(out, attrs, lead_quals, template_params, ctx); return p.parseStructDeclInto(out, attrs, lead_quals, template_params, ctx);
} }
@ -1060,9 +1072,16 @@ pub const Parser = struct {
// Legacy effect syntax: `<annotations>` and `{ state block }`. // Legacy effect syntax: `<annotations>` and `{ state block }`.
if (ctx != .local and p.at(.less)) try p.skipBalanced(.less, .greater); if (ctx != .local and p.at(.less)) try p.skipBalanced(.less, .greater);
var init_expr: ?*ast.Expr = null; var init_expr: ?*ast.Expr = null;
if (ctx != .local and p.at(.l_brace)) { if (p.at(.l_brace)) {
_ = p.reportAt(p.pos, .warning, "effect state block ignored - effect syntax is deprecated and will be removed in HLSL 2026", .{});
try p.skipBalanced(.l_brace, .r_brace); try p.skipBalanced(.l_brace, .r_brace);
} else if (p.eat(.equal)) init_expr = try p.parseInitializer(); } else if (p.eat(.equal)) {
if (p.atIdent("sampler_state")) {
_ = p.reportAt(p.pos, .warning, "effect sampler_state assignment ignored - effect syntax is deprecated and will be removed in HLSL 2026", .{});
_ = p.advance();
if (p.at(.l_brace)) try p.skipBalanced(.l_brace, .r_brace);
} else init_expr = try p.parseInitializer();
}
try out.append(p.arena, try p.mk(ast.Decl, .{ .loc = name.loc, .kind = .{ .@"var" = .{ try out.append(p.arena, try p.mk(ast.Decl, .{ .loc = name.loc, .kind = .{ .@"var" = .{
.attrs = attrs, .attrs = attrs,
.template_params = template_params, .template_params = template_params,
@ -1086,6 +1105,20 @@ pub const Parser = struct {
/// Skip a balanced `open ... close` group (legacy annotations and state /// Skip a balanced `open ... close` group (legacy annotations and state
/// blocks, which zdxc ignores like dxc does). /// blocks, which zdxc ignores like dxc does).
/// Skips to just past the `;` ending a declaration (outside braces).
fn skipDeclaration(p: *Parser) void {
var depth: u32 = 0;
while (!p.at(.eof)) {
const t = p.advance();
switch (t.kind) {
.l_brace => depth += 1,
.r_brace => depth -|= 1,
.semi => if (depth == 0) return,
else => {},
}
}
}
fn skipBalanced(p: *Parser, open: Kind, close: Kind) Error!void { fn skipBalanced(p: *Parser, open: Kind, close: Kind) Error!void {
_ = try p.expect(open, "group"); _ = try p.expect(open, "group");
var depth: u32 = 1; var depth: u32 = 1;
@ -1405,6 +1438,7 @@ pub const Parser = struct {
std.mem.eql(u8, w, "using") or std.mem.eql(u8, w, "enum") or std.mem.eql(u8, w, "_Static_assert")) std.mem.eql(u8, w, "using") or std.mem.eql(u8, w, "enum") or std.mem.eql(u8, w, "_Static_assert"))
return true; return true;
if (inList(&reserved_declspecs, w)) return true; if (inList(&reserved_declspecs, w)) return true;
if (isEffectObjectWord(t) and p.peek(1).kind == .identifier) return true;
if (qualifierField(w) != null) { if (qualifierField(w) != null) {
if (!isContextualQualifier(w)) return true; if (!isContextualQualifier(w)) return true;
const n = p.peek(1); const n = p.peek(1);
@ -1770,6 +1804,21 @@ const qualifier_words = [_][2][]const u8{
.{ "payload", "payload" }, .{ "center", "center" }, .{ "payload", "payload" }, .{ "center", "center" },
}; };
fn isTechniqueWord(t: Token) bool {
return t.isIdent("technique") or t.isIdent("Technique") or t.isIdent("technique10") or t.isIdent("technique11");
}
/// dxc's g_DeprecatedEffectObjectNames.
fn isEffectObjectWord(t: Token) bool {
const names = [_][]const u8{
"texture", "Texture", "pixelshader", "PixelShader", "vertexshader",
"VertexShader", "pixelfragment", "vertexfragment", "ComputeShader", "DomainShader",
"GeometryShader", "HullShader", "BlendState", "DepthStencilState", "DepthStencilView",
"RasterizerState", "RenderTargetView",
};
return t.kind == .identifier and inList(&names, t.text);
}
/// Type attributes that may precede a type: `unorm float`, /// Type attributes that may precede a type: `unorm float`,
/// `row_major float4x4`. /// `row_major float4x4`.
fn isTypeAttrWord(t: Token) bool { fn isTypeAttrWord(t: Token) bool {

View File

@ -787,7 +787,13 @@ pub const Sema = struct {
}, },
.using_namespace => |n| { .using_namespace => |n| {
// Bring the namespace's names into this scope. // Bring the namespace's names into this scope.
const sym = s.lookupName(n) orelse return s.err(d.loc, "unknown namespace '{s}'", .{n.last()}); const sym = s.lookupName(n) orelse {
// dxc's builtin `dx` namespace (HitObject, linalg, ...)
// exists in every shader model; its contents are not
// modelled as a scope here.
if (eql(n.parts[0], "dx")) return;
return s.err(d.loc, "unknown namespace '{s}'", .{n.last()});
};
if (sym != .namespace) return s.err(d.loc, "'{s}' is not a namespace", .{n.last()}); if (sym != .namespace) return s.err(d.loc, "'{s}' is not a namespace", .{n.last()});
var it = sym.namespace.syms.iterator(); var it = sym.namespace.syms.iterator();
while (it.next()) |kv| { while (it.next()) |kv| {
@ -1332,9 +1338,6 @@ pub const Sema = struct {
// diagnosed again (clang marks it invalid). // diagnosed again (clang marks it invalid).
if (!try s.checkInitializer(ty, init_e)) variable.type = TypeTable.error_type; if (!try s.checkInitializer(ty, init_e)) variable.type = TypeTable.error_type;
}; };
if (v.init == null and q.@"const" and ctx == .local) {
s.err(v.name_loc, "default initialization of an object of const type '{s}'", .{s.typeName(ty)});
}
} }
/// The expression designates (part of) a groupshared variable. /// The expression designates (part of) a groupshared variable.
@ -1588,6 +1591,14 @@ pub const Sema = struct {
if (text[0] == '\'') return true; if (text[0] == '\'') return true;
const lit = scanLiteral(text); const lit = scanLiteral(text);
const suffix = text[lit.end..]; const suffix = text[lit.end..];
if (lit.float) if (fixedFloatSuffix(text)) |suf| if (suf.ptr == suffix.ptr) {
s.warn(loc.advance(@intCast(lit.end)), "hlsl-202x-extensions", "fixed-size floating-point literal suffix '{s}' is a HLSL 202x extension", .{suf});
if (suf[1] == '1' and !s.opts.enable_16bit) {
s.err(loc.advance(@intCast(lit.end)), "16-bit floating-point literal suffix '{s}' requires '-enable-16bit-types'", .{suf});
return false;
}
return true;
};
const ok = if (lit.float) const ok = if (lit.float)
suffix.len == 0 or (suffix.len == 1 and std.mem.indexOfScalar(u8, "fFhHlL", suffix[0]) != null) suffix.len == 0 or (suffix.len == 1 and std.mem.indexOfScalar(u8, "fFhHlL", suffix[0]) != null)
else else
@ -1649,7 +1660,10 @@ pub const Sema = struct {
// is still an lvalue (`(Derived)x = y`). // is still an lvalue (`(Derived)x = y`).
const ft = s.types.get(from.ty); const ft = s.types.get(from.ty);
const tt = s.types.get(target); const tt = s.types.get(target);
const related = target == from.ty or (ft == .@"struct" and tt == .@"struct" and (derivesFrom(ft.@"struct", tt.@"struct") or derivesFrom(tt.@"struct", ft.@"struct"))); // So is a numeric cast that keeps the component type
// (`((int3)i4).x = 1`).
const same_comp = s.types.component(from.ty) != null and s.types.component(from.ty) == s.types.component(target);
const related = target == from.ty or same_comp or (ft == .@"struct" and tt == .@"struct" and (derivesFrom(ft.@"struct", tt.@"struct") or derivesFrom(tt.@"struct", ft.@"struct")));
return .{ .ty = target, .lvalue = from.lvalue and related, .read_only = from.read_only }; return .{ .ty = target, .lvalue = from.lvalue and related, .read_only = from.read_only };
}, },
.member => |m| return s.memberExpr(e, m.base, m.name, m.name_loc), .member => |m| return s.memberExpr(e, m.base, m.name, m.name_loc),
@ -2336,6 +2350,17 @@ pub const Sema = struct {
}; };
} }
fn hasUnmodelledMember(s: *Sema, ty: TypeId) bool {
return switch (s.types.get(ty)) {
.@"error" => true,
.array => |a| s.hasUnmodelledMember(a.elem),
.@"struct" => |info| for (info.fields.items) |f| {
if (s.hasUnmodelledMember(f.type)) break true;
} else false,
else => false,
};
}
fn explicitConvertible(s: *Sema, from: TypeId, to: TypeId) bool { fn explicitConvertible(s: *Sema, from: TypeId, to: TypeId) bool {
if (to == TypeTable.void_type) return true; if (to == TypeTable.void_type) return true;
if (s.conversionCost(from, to) != null) return true; if (s.conversionCost(from, to) != null) return true;
@ -2350,6 +2375,8 @@ pub const Sema = struct {
// Scalar to aggregate (`(S)0`), and flattening casts between // Scalar to aggregate (`(S)0`), and flattening casts between
// aggregates with enough components. // aggregates with enough components.
if (fc == 1 and tc != null) return true; if (fc == 1 and tc != null) return true;
// Structs with members this checker does not model (long vectors).
if (tc == null and s.types.get(to) == .@"struct" and s.hasUnmodelledMember(to)) return true;
if (fc != null and tc != null and fc.? >= tc.?) return true; if (fc != null and tc != null and fc.? >= tc.?) return true;
if (s.types.get(to) == .@"enum" and s.types.isNumeric(from)) return true; if (s.types.get(to) == .@"enum" and s.types.isNumeric(from)) return true;
return false; return false;
@ -2554,6 +2581,15 @@ fn scanLiteral(text: []const u8) struct { end: usize, float: bool, inf: bool = f
return .{ .end = i, .float = float }; return .{ .end = i, .float = float };
} }
/// `f16` / `f32` / `f64` (any case) at the end of a float literal.
fn fixedFloatSuffix(text: []const u8) ?[]const u8 {
if (text.len < 4) return null;
const suf = text[text.len - 3 ..];
if (suf[0] != 'f' and suf[0] != 'F') return null;
for ([_][]const u8{ "16", "32", "64" }) |w| if (std.mem.eql(u8, suf[1..], w)) return suf;
return null;
}
fn isIntSuffix(suffix: []const u8) bool { fn isIntSuffix(suffix: []const u8) bool {
const valid = [_][]const u8{ "", "u", "l", "ul", "lu", "ll", "ull", "llu" }; const valid = [_][]const u8{ "", "u", "l", "ul", "lu", "ll", "ull", "llu" };
var buf: [3]u8 = undefined; var buf: [3]u8 = undefined;
@ -2583,6 +2619,11 @@ fn intLiteralType(text: []const u8) Scalar {
} }
fn floatLiteralType(text: []const u8) Scalar { fn floatLiteralType(text: []const u8) Scalar {
if (fixedFloatSuffix(text)) |suf| return switch (suf[1]) {
'1' => .half, // f16: float16_t with 16-bit types
'3' => .float,
else => .double,
};
const last = text[text.len - 1]; const last = text[text.len - 1];
return switch (last) { return switch (last) {
'f', 'F' => .float, 'f', 'F' => .float,