authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-23 16:10:17-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-02-23 16:10:17-05:00
logecf56d85efa227acf1bd9cab9ad2d5af05f7efe5
treed83ccefd98555c33d492273cb20f86fe78965416
parent88d1258e08e668e620d5f8f4681315e555acbcd2
parentab4d693cfc82465c42021ba6f18c65fdd5f969e6
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #10969 from Vexu/stage2

stage2: fn typeinfo params

9 files changed, 127 insertions(+), 56 deletions(-)

lib/std/builtin.zig+11-9
......@@ -346,14 +346,8 @@ pub const TypeInfo = union(enum) {
346346 decls: []const Declaration,
347347 };
348348
349 /// This data structure is used by the Zig language code generation and
350 /// therefore must be kept in sync with the compiler implementation.
351 /// TODO rename to Param and put inside `Fn`.
352 pub const FnArg = struct {
353 is_generic: bool,
354 is_noalias: bool,
355 arg_type: ?type,
356 };
349 /// TODO deprecated use Fn.Param
350 pub const FnArg = Fn.Param;
357351
358352 /// This data structure is used by the Zig language code generation and
359353 /// therefore must be kept in sync with the compiler implementation.
......@@ -363,7 +357,15 @@ pub const TypeInfo = union(enum) {
363357 is_generic: bool,
364358 is_var_args: bool,
365359 return_type: ?type,
366 args: []const FnArg,
360 args: []const Param,
361
362 /// This data structure is used by the Zig language code generation and
363 /// therefore must be kept in sync with the compiler implementation.
364 pub const Param = struct {
365 is_generic: bool,
366 is_noalias: bool,
367 arg_type: ?type,
368 };
367369 };
368370
369371 /// This data structure is used by the Zig language code generation and
lib/std/math.zig+4-3
......@@ -947,7 +947,7 @@ fn testRem() !void {
947947/// Result is an unsigned integer.
948948pub fn absCast(x: anytype) switch (@typeInfo(@TypeOf(x))) {
949949 .ComptimeInt => comptime_int,
950 .Int => |intInfo| std.meta.Int(.unsigned, intInfo.bits),
950 .Int => |int_info| std.meta.Int(.unsigned, int_info.bits),
951951 else => @compileError("absCast only accepts integers"),
952952} {
953953 switch (@typeInfo(@TypeOf(x))) {
......@@ -958,8 +958,9 @@ pub fn absCast(x: anytype) switch (@typeInfo(@TypeOf(x))) {
958958 return x;
959959 }
960960 },
961 .Int => |intInfo| {
962 const Uint = std.meta.Int(.unsigned, intInfo.bits);
961 .Int => |int_info| {
962 if (int_info.signedness == .unsigned) return x;
963 const Uint = std.meta.Int(.unsigned, int_info.bits);
963964 if (x < 0) {
964965 return ~@bitCast(Uint, x +% -1);
965966 } else {
src/Sema.zig+76-11
......@@ -3351,9 +3351,10 @@ fn zirStoreNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!v
33513351 // Check for the possibility of this pattern:
33523352 // %a = ret_ptr
33533353 // %b = store(%a, %c)
3354 // Where %c is an error union. In such case we need to add to the current function's
3355 // inferred error set, if any.
3356 if (sema.typeOf(operand).zigTypeTag() == .ErrorUnion and
3354 // Where %c is an error union or error set. In such case we need to add
3355 // to the current function's inferred error set, if any.
3356 if ((sema.typeOf(operand).zigTypeTag() == .ErrorUnion or
3357 sema.typeOf(operand).zigTypeTag() == .ErrorSet) and
33573358 sema.fn_ret_ty.zigTypeTag() == .ErrorUnion)
33583359 {
33593360 if (Zir.refToIndex(extra.lhs)) |ptr_index| {
......@@ -7665,6 +7666,8 @@ fn zirHasDecl(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
76657666 const container_type = try sema.resolveType(block, lhs_src, extra.lhs);
76667667 const decl_name = try sema.resolveConstString(block, rhs_src, extra.rhs);
76677668
7669 // tuples are structs but they don't have a namespace
7670 if (container_type.isTuple()) return Air.Inst.Ref.bool_false;
76687671 const namespace = container_type.getNamespace() orelse return sema.fail(
76697672 block,
76707673 lhs_src,
......@@ -9886,7 +9889,65 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
98869889 }),
98879890 ),
98889891 .Fn => {
9892 // TODO: look into memoizing this result.
98899893 const info = ty.fnInfo();
9894 var params_anon_decl = try block.startAnonDecl(src);
9895 defer params_anon_decl.deinit();
9896
9897 const param_vals = try params_anon_decl.arena().alloc(Value, info.param_types.len);
9898 for (param_vals) |*param_val, i| {
9899 const param_ty = info.param_types[i];
9900 const is_generic = param_ty.tag() == .generic_poison;
9901 const param_ty_val = if (is_generic)
9902 Value.@"null"
9903 else
9904 try Value.Tag.opt_payload.create(
9905 params_anon_decl.arena(),
9906 try Value.Tag.ty.create(params_anon_decl.arena(), param_ty),
9907 );
9908
9909 const param_fields = try params_anon_decl.arena().create([3]Value);
9910 param_fields.* = .{
9911 // is_generic: bool,
9912 Value.makeBool(is_generic),
9913 // is_noalias: bool,
9914 Value.@"false", // TODO
9915 // arg_type: ?type,
9916 param_ty_val,
9917 };
9918 param_val.* = try Value.Tag.@"struct".create(params_anon_decl.arena(), param_fields);
9919 }
9920
9921 const args_val = v: {
9922 const fn_info_decl = (try sema.namespaceLookup(
9923 block,
9924 src,
9925 type_info_ty.getNamespace().?,
9926 "Fn",
9927 )).?;
9928 try sema.mod.declareDeclDependency(sema.owner_decl, fn_info_decl);
9929 try sema.ensureDeclAnalyzed(fn_info_decl);
9930 const param_info_decl = (try sema.namespaceLookup(
9931 block,
9932 src,
9933 fn_info_decl.val.castTag(.ty).?.data.getNamespace().?,
9934 "Param",
9935 )).?;
9936 try sema.mod.declareDeclDependency(sema.owner_decl, param_info_decl);
9937 try sema.ensureDeclAnalyzed(param_info_decl);
9938 const new_decl = try params_anon_decl.finish(
9939 try Type.Tag.array.create(params_anon_decl.arena(), .{
9940 .len = param_vals.len,
9941 .elem_type = param_info_decl.ty,
9942 }),
9943 try Value.Tag.array.create(
9944 params_anon_decl.arena(),
9945 param_vals,
9946 ),
9947 );
9948 break :v try Value.Tag.decl_ref.create(sema.arena, new_decl);
9949 };
9950
98909951 const field_values = try sema.arena.alloc(Value, 6);
98919952 // calling_convention: CallingConvention,
98929953 field_values[0] = try Value.Tag.enum_field_index.create(sema.arena, @enumToInt(info.cc));
......@@ -9897,9 +9958,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
98979958 // is_var_args: bool,
98989959 field_values[3] = Value.makeBool(info.is_var_args);
98999960 // return_type: ?type,
9900 field_values[4] = try Value.Tag.ty.create(sema.arena, ty.fnReturnType());
9901 // args: []const FnArg,
9902 field_values[5] = Value.@"null"; // TODO
9961 field_values[4] = try Value.Tag.ty.create(sema.arena, info.return_type);
9962 // args: []const Fn.Param,
9963 field_values[5] = args_val;
99039964
99049965 return sema.addConstant(
99059966 type_info_ty,
......@@ -10102,7 +10163,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1010210163 }),
1010310164 try Value.Tag.array.create(
1010410165 fields_anon_decl.arena(),
10105 try fields_anon_decl.arena().dupe(Value, enum_field_vals),
10166 enum_field_vals,
1010610167 ),
1010710168 );
1010810169 break :v try Value.Tag.decl_ref.create(sema.arena, new_decl);
......@@ -12128,7 +12189,7 @@ fn checkPtrOperand(
1212812189 ty: Type,
1212912190) CompileError!void {
1213012191 switch (ty.zigTypeTag()) {
12131 .Pointer => {},
12192 .Pointer => return,
1213212193 .Fn => {
1213312194 const msg = msg: {
1213412195 const msg = try sema.errMsg(
......@@ -12145,8 +12206,10 @@ fn checkPtrOperand(
1214512206 };
1214612207 return sema.failWithOwnedErrorMsg(msg);
1214712208 },
12148 else => return sema.fail(block, ty_src, "expected pointer, found '{}'", .{ty}),
12209 .Optional => if (ty.isPtrLikeOptional()) return,
12210 else => {},
1214912211 }
12212 return sema.fail(block, ty_src, "expected pointer type, found '{}'", .{ty});
1215012213}
1215112214
1215212215fn checkPtrType(
......@@ -12156,7 +12219,7 @@ fn checkPtrType(
1215612219 ty: Type,
1215712220) CompileError!void {
1215812221 switch (ty.zigTypeTag()) {
12159 .Pointer => {},
12222 .Pointer => return,
1216012223 .Fn => {
1216112224 const msg = msg: {
1216212225 const msg = try sema.errMsg(
......@@ -12173,8 +12236,10 @@ fn checkPtrType(
1217312236 };
1217412237 return sema.failWithOwnedErrorMsg(msg);
1217512238 },
12176 else => return sema.fail(block, ty_src, "expected pointer type, found '{}'", .{ty}),
12239 .Optional => if (ty.isPtrLikeOptional()) return,
12240 else => {},
1217712241 }
12242 return sema.fail(block, ty_src, "expected pointer type, found '{}'", .{ty});
1217812243}
1217912244
1218012245fn checkVectorElemType(
src/stage1/ir.cpp+4-5
......@@ -18750,8 +18750,8 @@ static Error ir_make_type_info_value(IrAnalyze *ira, Scope *scope, AstNode *sour
1875018750 return_type->data.x_type = type_entry->data.fn.fn_type_id.return_type;
1875118751 fields[4]->data.x_optional = return_type;
1875218752 }
18753 // args: []TypeInfo.FnArg
18754 ZigType *type_info_fn_arg_type = ir_type_info_get_type(ira, "FnArg", nullptr);
18753 // args: []TypeInfo.Fn.Param
18754 ZigType *type_info_fn_arg_type = ir_type_info_get_type(ira, "Param", result->type);
1875518755 if ((err = type_resolve(g, type_info_fn_arg_type, ResolveStatusSizeKnown))) {
1875618756 zig_unreachable();
1875718757 }
......@@ -19614,14 +19614,13 @@ static ZigType *type_info_to_type(IrAnalyze *ira, Scope *scope, AstNode *source_
1961419614 assert(args_arr->data.x_array.special == ConstArraySpecialNone);
1961519615 for (size_t i = 0; i < args_len; i++) {
1961619616 ZigValue *arg_value = &args_arr->data.x_array.data.s_none.elements[i];
19617 assert(arg_value->type == ir_type_info_get_type(ira, "FnArg", nullptr));
1961819617 FnTypeParamInfo *info = &fn_type_id.param_info[i];
1961919618 Error err;
1962019619 bool is_generic;
1962119620 if ((err = get_const_field_bool(ira, source_node, arg_value, "is_generic", 0, &is_generic)))
1962219621 return ira->codegen->invalid_inst_gen->value->type;
1962319622 if (is_generic) {
19624 ir_add_error_node(ira, source_node, buf_sprintf("TypeInfo.FnArg.is_generic must be false for @Type"));
19623 ir_add_error_node(ira, source_node, buf_sprintf("TypeInfo.Fn.Param.is_generic must be false for @Type"));
1962519624 return ira->codegen->invalid_inst_gen->value->type;
1962619625 }
1962719626 if ((err = get_const_field_bool(ira, source_node, arg_value, "is_noalias", 1, &info->is_noalias)))
......@@ -19629,7 +19628,7 @@ static ZigType *type_info_to_type(IrAnalyze *ira, Scope *scope, AstNode *source_
1962919628 ZigType *type = get_const_field_meta_type_optional(
1963019629 ira, source_node, arg_value, "arg_type", 2);
1963119630 if (type == nullptr) {
19632 ir_add_error_node(ira, source_node, buf_sprintf("TypeInfo.FnArg.arg_type must be non-null for @Type"));
19631 ir_add_error_node(ira, source_node, buf_sprintf("TypeInfo.Fn.Param.arg_type must be non-null for @Type"));
1963319632 return ira->codegen->invalid_inst_gen->value->type;
1963419633 }
1963519634 info->type = type;
src/type.zig+4-2
......@@ -593,10 +593,12 @@ pub const Type = extern union {
593593
594594 for (a_info.param_types) |a_param_ty, i| {
595595 const b_param_ty = b_info.param_types[i];
596 if (!eql(a_param_ty, b_param_ty))
596 if (a_info.comptime_params[i] != b_info.comptime_params[i])
597597 return false;
598598
599 if (a_info.comptime_params[i] != b_info.comptime_params[i])
599 if (a_param_ty.tag() == .generic_poison) continue;
600 if (b_param_ty.tag() == .generic_poison) continue;
601 if (!eql(a_param_ty, b_param_ty))
600602 return false;
601603 }
602604
test/behavior.zig+9-9
......@@ -120,6 +120,15 @@ test {
120120 _ = @import("behavior/sizeof_and_typeof.zig");
121121 _ = @import("behavior/switch.zig");
122122 _ = @import("behavior/widening.zig");
123 _ = @import("behavior/bugs/421.zig");
124 _ = @import("behavior/bugs/726.zig");
125 _ = @import("behavior/bugs/1421.zig");
126 _ = @import("behavior/bugs/2114.zig");
127 _ = @import("behavior/bugs/3742.zig");
128 _ = @import("behavior/struct_contains_null_ptr_itself.zig");
129 _ = @import("behavior/switch_prong_err_enum.zig");
130 _ = @import("behavior/switch_prong_implicit_cast.zig");
131 _ = @import("behavior/union_with_members.zig");
123132
124133 if (builtin.zig_backend == .stage1) {
125134 // Tests that only pass for the stage1 backend.
......@@ -128,20 +137,15 @@ test {
128137 _ = @import("behavior/async_fn.zig");
129138 }
130139 _ = @import("behavior/await_struct.zig");
131 _ = @import("behavior/bugs/421.zig");
132140 _ = @import("behavior/bugs/529.zig");
133141 _ = @import("behavior/bugs/718.zig");
134 _ = @import("behavior/bugs/726.zig");
135142 _ = @import("behavior/bugs/828.zig");
136143 _ = @import("behavior/bugs/920.zig");
137144 _ = @import("behavior/bugs/1120.zig");
138 _ = @import("behavior/bugs/1421.zig");
139145 _ = @import("behavior/bugs/1442.zig");
140146 _ = @import("behavior/bugs/1607.zig");
141147 _ = @import("behavior/bugs/1851.zig");
142 _ = @import("behavior/bugs/2114.zig");
143148 _ = @import("behavior/bugs/3384.zig");
144 _ = @import("behavior/bugs/3742.zig");
145149 _ = @import("behavior/bugs/3779.zig");
146150 _ = @import("behavior/bugs/4328.zig");
147151 _ = @import("behavior/bugs/5398.zig");
......@@ -161,12 +165,8 @@ test {
161165 _ = @import("behavior/muladd.zig");
162166 _ = @import("behavior/select.zig");
163167 _ = @import("behavior/shuffle.zig");
164 _ = @import("behavior/struct_contains_null_ptr_itself.zig");
165168 _ = @import("behavior/struct_contains_slice_of_itself.zig");
166 _ = @import("behavior/switch_prong_err_enum.zig");
167 _ = @import("behavior/switch_prong_implicit_cast.zig");
168169 _ = @import("behavior/typename.zig");
169 _ = @import("behavior/union_with_members.zig");
170170 _ = @import("behavior/vector.zig");
171171 if (builtin.target.cpu.arch == .wasm32) {
172172 _ = @import("behavior/wasm.zig");
test/behavior/src.zig+12-13
......@@ -1,21 +1,20 @@
1const std = @import("std");
2const builtin = @import("builtin");
3const expect = std.testing.expect;
4
5test "@src" {
6 // TODO why is this failing on stage1?
7 return error.SkipZigTest;
8
9 // try doTheTest();
10}
11
121fn doTheTest() !void {
13 const src = @src();
2 const src = @src(); // do not move
143
15 try expect(src.line == 9);
4 try expect(src.line == 2);
165 try expect(src.column == 17);
176 try expect(std.mem.endsWith(u8, src.fn_name, "doTheTest"));
187 try expect(std.mem.endsWith(u8, src.file, "src.zig"));
198 try expect(src.fn_name[src.fn_name.len] == 0);
209 try expect(src.file[src.file.len] == 0);
2110}
11
12const std = @import("std");
13const builtin = @import("builtin");
14const expect = std.testing.expect;
15
16test "@src" {
17 if (builtin.zig_backend != .stage1) return error.SkipZigTest;
18
19 try doTheTest();
20}
test/behavior/type_info.zig+4-1
......@@ -323,7 +323,9 @@ fn testOpaque() !void {
323323}
324324
325325test "type info: function type info" {
326 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
326 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
327 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
328 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
327329
328330 // wasm doesn't support align attributes on functions
329331 if (builtin.target.cpu.arch == .wasm32 or builtin.target.cpu.arch == .wasm64) return error.SkipZigTest;
......@@ -343,6 +345,7 @@ fn testFunction() !void {
343345 const fn_aligned_info = @typeInfo(@TypeOf(fooAligned));
344346 try expect(fn_aligned_info.Fn.alignment == 4);
345347
348 if (builtin.zig_backend != .stage1) return; // no bound fn in stage2
346349 const test_instance: TestPackedStruct = undefined;
347350 const bound_fn_info = @typeInfo(@TypeOf(test_instance.foo));
348351 try expect(bound_fn_info == .BoundFn);
test/compile_errors.zig+3-3
......@@ -450,7 +450,7 @@ pub fn addCases(ctx: *TestContext) !void {
450450 \\ .is_generic = true,
451451 \\ .is_var_args = false,
452452 \\ .return_type = u0,
453 \\ .args = &[_]@import("std").builtin.TypeInfo.FnArg{},
453 \\ .args = &[_]@import("std").builtin.TypeInfo.Fn.Param{},
454454 \\ },
455455 \\});
456456 \\comptime { _ = Foo; }
......@@ -466,7 +466,7 @@ pub fn addCases(ctx: *TestContext) !void {
466466 \\ .is_generic = false,
467467 \\ .is_var_args = true,
468468 \\ .return_type = u0,
469 \\ .args = &[_]@import("std").builtin.TypeInfo.FnArg{},
469 \\ .args = &[_]@import("std").builtin.TypeInfo.Fn.Param{},
470470 \\ },
471471 \\});
472472 \\comptime { _ = Foo; }
......@@ -482,7 +482,7 @@ pub fn addCases(ctx: *TestContext) !void {
482482 \\ .is_generic = false,
483483 \\ .is_var_args = false,
484484 \\ .return_type = null,
485 \\ .args = &[_]@import("std").builtin.TypeInfo.FnArg{},
485 \\ .args = &[_]@import("std").builtin.TypeInfo.Fn.Param{},
486486 \\ },
487487 \\});
488488 \\comptime { _ = Foo; }