authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-07 13:54:25-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-07 13:54:25-07:00
log38c161afabe1dc7d0df7ad981d70b962ef87120a
treef1944a2512b4567b2e35a0c2ac7b87c47c8d2cb7
parentc467f6693eb815906088b15f25d4e21092526bb4

Sema: fix `@hasDecl` for simple enums


3 files changed, 16 insertions(+), 12 deletions(-)

src/Sema.zig+10-8
...@@ -7747,14 +7747,9 @@ fn zirHasDecl(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air...@@ -7747,14 +7747,9 @@ fn zirHasDecl(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
7747 const container_type = try sema.resolveType(block, lhs_src, extra.lhs);7747 const container_type = try sema.resolveType(block, lhs_src, extra.lhs);
7748 const decl_name = try sema.resolveConstString(block, rhs_src, extra.rhs);7748 const decl_name = try sema.resolveConstString(block, rhs_src, extra.rhs);
77497749
7750 // tuples are structs but they don't have a namespace7750 try checkNamespaceType(sema, block, lhs_src, container_type);
7751 if (container_type.isTupleOrAnonStruct()) return Air.Inst.Ref.bool_false;7751
7752 const namespace = container_type.getNamespace() orelse return sema.fail(7752 const namespace = container_type.getNamespace() orelse return Air.Inst.Ref.bool_false;
7753 block,
7754 lhs_src,
7755 "expected struct, enum, union, or opaque, found '{}'",
7756 .{container_type},
7757 );
7758 if (try sema.lookupInNamespace(block, src, namespace, decl_name, true)) |decl| {7753 if (try sema.lookupInNamespace(block, src, namespace, decl_name, true)) |decl| {
7759 if (decl.is_pub or decl.getFileScope() == block.getFileScope()) {7754 if (decl.is_pub or decl.getFileScope() == block.getFileScope()) {
7760 return Air.Inst.Ref.bool_true;7755 return Air.Inst.Ref.bool_true;
...@@ -12882,6 +12877,13 @@ fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u6...@@ -12882,6 +12877,13 @@ fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u6
12882 }12877 }
12883}12878}
1288412879
12880fn checkNamespaceType(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!void {
12881 switch (ty.zigTypeTag()) {
12882 .Struct, .Enum, .Union, .Opaque => return,
12883 else => return sema.fail(block, src, "expected struct, enum, union, or opaque; found '{}'", .{ty}),
12884 }
12885}
12886
12885/// Returns `true` if the type was a comptime_int.12887/// Returns `true` if the type was a comptime_int.
12886fn checkIntType(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!bool {12888fn checkIntType(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!bool {
12887 switch (ty.zigTypeTag()) {12889 switch (ty.zigTypeTag()) {
test/behavior/switch.zig+5-1
...@@ -631,7 +631,11 @@ test "switch on error set with single else" {...@@ -631,7 +631,11 @@ test "switch on error set with single else" {
631}631}
632632
633test "switch capture copies its payload" {633test "switch capture copies its payload" {
634 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO634 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
635 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
636 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
637 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
638 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
635639
636 const S = struct {640 const S = struct {
637 fn doTheTest() !void {641 fn doTheTest() !void {
test/behavior/union.zig+1-3
...@@ -217,7 +217,7 @@ test "union with specified enum tag" {...@@ -217,7 +217,7 @@ test "union with specified enum tag" {
217 comptime try doTest();217 comptime try doTest();
218}218}
219219
220test "packed union generates correctly aligned LLVM type" {220test "packed union generates correctly aligned type" {
221 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;221 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
222 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;222 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
223 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;223 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
...@@ -1104,8 +1104,6 @@ test "@unionInit on union with tag but no fields" {...@@ -1104,8 +1104,6 @@ test "@unionInit on union with tag but no fields" {
1104}1104}
11051105
1106test "union enum type gets a separate scope" {1106test "union enum type gets a separate scope" {
1107 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
1108
1109 const S = struct {1107 const S = struct {
1110 const U = union(enum) {1108 const U = union(enum) {
1111 a: u8,1109 a: u8,