Commit 92beb2b490

Veikka Tuominen <git@vexu.eu>
2022-02-22 14:04:12
stage2: misc fixes in Sema
1 parent 923b07b
Changed files (5)
lib/std/math.zig
@@ -947,7 +947,7 @@ fn testRem() !void {
 /// Result is an unsigned integer.
 pub fn absCast(x: anytype) switch (@typeInfo(@TypeOf(x))) {
     .ComptimeInt => comptime_int,
-    .Int => |intInfo| std.meta.Int(.unsigned, intInfo.bits),
+    .Int => |int_info| std.meta.Int(.unsigned, int_info.bits),
     else => @compileError("absCast only accepts integers"),
 } {
     switch (@typeInfo(@TypeOf(x))) {
@@ -958,8 +958,9 @@ pub fn absCast(x: anytype) switch (@typeInfo(@TypeOf(x))) {
                 return x;
             }
         },
-        .Int => |intInfo| {
-            const Uint = std.meta.Int(.unsigned, intInfo.bits);
+        .Int => |int_info| {
+            if (int_info.signedness == .unsigned) return x;
+            const Uint = std.meta.Int(.unsigned, int_info.bits);
             if (x < 0) {
                 return ~@bitCast(Uint, x +% -1);
             } else {
src/Sema.zig
@@ -3351,9 +3351,10 @@ fn zirStoreNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!v
     // Check for the possibility of this pattern:
     //   %a = ret_ptr
     //   %b = store(%a, %c)
-    // Where %c is an error union. In such case we need to add to the current function's
-    // inferred error set, if any.
-    if (sema.typeOf(operand).zigTypeTag() == .ErrorUnion and
+    // Where %c is an error union or error set. In such case we need to add
+    // to the current function's inferred error set, if any.
+    if ((sema.typeOf(operand).zigTypeTag() == .ErrorUnion or
+        sema.typeOf(operand).zigTypeTag() == .ErrorSet) and
         sema.fn_ret_ty.zigTypeTag() == .ErrorUnion)
     {
         if (Zir.refToIndex(extra.lhs)) |ptr_index| {
@@ -7665,6 +7666,8 @@ 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.isTuple()) return Air.Inst.Ref.bool_false;
     const namespace = container_type.getNamespace() orelse return sema.fail(
         block,
         lhs_src,
@@ -12186,7 +12189,7 @@ fn checkPtrOperand(
     ty: Type,
 ) CompileError!void {
     switch (ty.zigTypeTag()) {
-        .Pointer => {},
+        .Pointer => return,
         .Fn => {
             const msg = msg: {
                 const msg = try sema.errMsg(
@@ -12203,8 +12206,10 @@ fn checkPtrOperand(
             };
             return sema.failWithOwnedErrorMsg(msg);
         },
-        else => return sema.fail(block, ty_src, "expected pointer, found '{}'", .{ty}),
+        .Optional => if (ty.isPtrLikeOptional()) return,
+        else => {},
     }
+    return sema.fail(block, ty_src, "expected pointer type, found '{}'", .{ty});
 }
 
 fn checkPtrType(
@@ -12214,7 +12219,7 @@ fn checkPtrType(
     ty: Type,
 ) CompileError!void {
     switch (ty.zigTypeTag()) {
-        .Pointer => {},
+        .Pointer => return,
         .Fn => {
             const msg = msg: {
                 const msg = try sema.errMsg(
@@ -12231,8 +12236,10 @@ fn checkPtrType(
             };
             return sema.failWithOwnedErrorMsg(msg);
         },
-        else => return sema.fail(block, ty_src, "expected pointer type, found '{}'", .{ty}),
+        .Optional => if (ty.isPtrLikeOptional()) return,
+        else => {},
     }
+    return sema.fail(block, ty_src, "expected pointer type, found '{}'", .{ty});
 }
 
 fn checkVectorElemType(
src/type.zig
@@ -593,10 +593,12 @@ pub const Type = extern union {
 
                 for (a_info.param_types) |a_param_ty, i| {
                     const b_param_ty = b_info.param_types[i];
-                    if (!eql(a_param_ty, b_param_ty))
+                    if (a_info.comptime_params[i] != b_info.comptime_params[i])
                         return false;
 
-                    if (a_info.comptime_params[i] != b_info.comptime_params[i])
+                    if (a_param_ty.tag() == .generic_poison) continue;
+                    if (b_param_ty.tag() == .generic_poison) continue;
+                    if (!eql(a_param_ty, b_param_ty))
                         return false;
                 }
 
test/behavior/type_info.zig
@@ -323,6 +323,10 @@ fn testOpaque() !void {
 }
 
 test "type info: function type info" {
+    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
+    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
+    if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
+
     // wasm doesn't support align attributes on functions
     if (builtin.target.cpu.arch == .wasm32 or builtin.target.cpu.arch == .wasm64) return error.SkipZigTest;
     try testFunction();
test/behavior.zig
@@ -120,6 +120,15 @@ test {
             _ = @import("behavior/sizeof_and_typeof.zig");
             _ = @import("behavior/switch.zig");
             _ = @import("behavior/widening.zig");
+            _ = @import("behavior/bugs/421.zig");
+            _ = @import("behavior/bugs/726.zig");
+            _ = @import("behavior/bugs/1421.zig");
+            _ = @import("behavior/bugs/2114.zig");
+            _ = @import("behavior/bugs/3742.zig");
+            _ = @import("behavior/struct_contains_null_ptr_itself.zig");
+            _ = @import("behavior/switch_prong_err_enum.zig");
+            _ = @import("behavior/switch_prong_implicit_cast.zig");
+            _ = @import("behavior/union_with_members.zig");
 
             if (builtin.zig_backend == .stage1) {
                 // Tests that only pass for the stage1 backend.
@@ -128,20 +137,15 @@ test {
                     _ = @import("behavior/async_fn.zig");
                 }
                 _ = @import("behavior/await_struct.zig");
-                _ = @import("behavior/bugs/421.zig");
                 _ = @import("behavior/bugs/529.zig");
                 _ = @import("behavior/bugs/718.zig");
-                _ = @import("behavior/bugs/726.zig");
                 _ = @import("behavior/bugs/828.zig");
                 _ = @import("behavior/bugs/920.zig");
                 _ = @import("behavior/bugs/1120.zig");
-                _ = @import("behavior/bugs/1421.zig");
                 _ = @import("behavior/bugs/1442.zig");
                 _ = @import("behavior/bugs/1607.zig");
                 _ = @import("behavior/bugs/1851.zig");
-                _ = @import("behavior/bugs/2114.zig");
                 _ = @import("behavior/bugs/3384.zig");
-                _ = @import("behavior/bugs/3742.zig");
                 _ = @import("behavior/bugs/3779.zig");
                 _ = @import("behavior/bugs/4328.zig");
                 _ = @import("behavior/bugs/5398.zig");
@@ -161,12 +165,8 @@ test {
                 _ = @import("behavior/muladd.zig");
                 _ = @import("behavior/select.zig");
                 _ = @import("behavior/shuffle.zig");
-                _ = @import("behavior/struct_contains_null_ptr_itself.zig");
                 _ = @import("behavior/struct_contains_slice_of_itself.zig");
-                _ = @import("behavior/switch_prong_err_enum.zig");
-                _ = @import("behavior/switch_prong_implicit_cast.zig");
                 _ = @import("behavior/typename.zig");
-                _ = @import("behavior/union_with_members.zig");
                 _ = @import("behavior/vector.zig");
                 if (builtin.target.cpu.arch == .wasm32) {
                     _ = @import("behavior/wasm.zig");