Commit 38c161afab

Andrew Kelley <andrew@ziglang.org>
2022-03-07 21:54:25
Sema: fix `@hasDecl` for simple enums
1 parent c467f66
Changed files (3)
src
test
src/Sema.zig
@@ -7747,14 +7747,9 @@ fn zirHasDecl(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
     const container_type = try sema.resolveType(block, lhs_src, extra.lhs);
     const decl_name = try sema.resolveConstString(block, rhs_src, extra.rhs);
 
-    // tuples are structs but they don't have a namespace
-    if (container_type.isTupleOrAnonStruct()) return Air.Inst.Ref.bool_false;
-    const namespace = container_type.getNamespace() orelse return sema.fail(
-        block,
-        lhs_src,
-        "expected struct, enum, union, or opaque, found '{}'",
-        .{container_type},
-    );
+    try checkNamespaceType(sema, block, lhs_src, container_type);
+
+    const namespace = container_type.getNamespace() orelse return Air.Inst.Ref.bool_false;
     if (try sema.lookupInNamespace(block, src, namespace, decl_name, true)) |decl| {
         if (decl.is_pub or decl.getFileScope() == block.getFileScope()) {
             return Air.Inst.Ref.bool_true;
@@ -12882,6 +12877,13 @@ fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u6
     }
 }
 
+fn checkNamespaceType(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!void {
+    switch (ty.zigTypeTag()) {
+        .Struct, .Enum, .Union, .Opaque => return,
+        else => return sema.fail(block, src, "expected struct, enum, union, or opaque; found '{}'", .{ty}),
+    }
+}
+
 /// Returns `true` if the type was a comptime_int.
 fn checkIntType(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!bool {
     switch (ty.zigTypeTag()) {
test/behavior/switch.zig
@@ -631,7 +631,11 @@ test "switch on error set with single else" {
 }
 
 test "switch capture copies its payload" {
-    if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
+    if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
+    if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
+    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
+    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+    if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
 
     const S = struct {
         fn doTheTest() !void {
test/behavior/union.zig
@@ -217,7 +217,7 @@ test "union with specified enum tag" {
     comptime try doTest();
 }
 
-test "packed union generates correctly aligned LLVM type" {
+test "packed union generates correctly aligned type" {
     if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
     if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
@@ -1104,8 +1104,6 @@ test "@unionInit on union with tag but no fields" {
 }
 
 test "union enum type gets a separate scope" {
-    if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
-
     const S = struct {
         const U = union(enum) {
             a: u8,