authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-12-19 07:57:16+00:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-12-19 07:57:16+00:00
loge2e3633612be656177512873a9de6524082d04dd
treec71b592a592e21b108fccc0be3f99a45b6a13c6e
parentf857bf72e2239718bbbe4cba08d6961ad77fc69a
parent58b8b1ac2af642e886f3a561bf6dfd971bb80a96
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #22264 from mlugg/no-generic-callconv

compiler: disallow `callconv` etc from depending on function parameters Also, disallow `align`/`linksection`/`addrspace` annotations on container-level declarations with comptime-only types.

14 files changed, 433 insertions(+), 871 deletions(-)

lib/std/zig/AstGen.zig+161-260
......@@ -1420,19 +1420,9 @@ fn fnProtoExpr(
14201420
14211421 .cc_ref = cc,
14221422 .cc_gz = null,
1423 .align_ref = .none,
1424 .align_gz = null,
14251423 .ret_ref = ret_ty,
14261424 .ret_gz = null,
1427 .section_ref = .none,
1428 .section_gz = null,
1429 .addrspace_ref = .none,
1430 .addrspace_gz = null,
1431
1432 .align_param_refs = &.{},
1433 .addrspace_param_refs = &.{},
1434 .section_param_refs = &.{},
1435 .cc_param_refs = &.{},
1425
14361426 .ret_param_refs = &.{},
14371427 .param_insts = &.{},
14381428
......@@ -4129,6 +4119,8 @@ fn fnDecl(
41294119 const decl_inst = try gz.makeDeclaration(fn_proto.ast.proto_node);
41304120 astgen.advanceSourceCursorToNode(decl_node);
41314121
4122 const saved_cursor = astgen.saveSourceCursor();
4123
41324124 var decl_gz: GenZir = .{
41334125 .is_comptime = true,
41344126 .decl_node_index = fn_proto.ast.proto_node,
......@@ -4140,17 +4132,6 @@ fn fnDecl(
41404132 };
41414133 defer decl_gz.unstack();
41424134
4143 var fn_gz: GenZir = .{
4144 .is_comptime = false,
4145 .decl_node_index = fn_proto.ast.proto_node,
4146 .decl_line = decl_gz.decl_line,
4147 .parent = &decl_gz.base,
4148 .astgen = astgen,
4149 .instructions = gz.instructions,
4150 .instructions_top = GenZir.unstacked_top,
4151 };
4152 defer fn_gz.unstack();
4153
41544135 const decl_column = astgen.source_column;
41554136
41564137 // Set this now, since parameter types, return type, etc may be generic.
......@@ -4182,7 +4163,7 @@ fn fnDecl(
41824163 var param_insts: std.ArrayListUnmanaged(Zir.Inst.Index) = try .initCapacity(astgen.arena, fn_proto.ast.params.len);
41834164
41844165 var noalias_bits: u32 = 0;
4185 var params_scope = &fn_gz.base;
4166 var params_scope = scope;
41864167 const is_var_args = is_var_args: {
41874168 var param_type_i: usize = 0;
41884169 var it = fn_proto.iterate(tree);
......@@ -4305,47 +4286,26 @@ fn fnDecl(
43054286 // instructions inside the expression blocks for align, addrspace, cc, and ret_ty
43064287 // to use the function instruction as the "block" to break from.
43074288
4308 var align_gz = decl_gz.makeSubBlock(params_scope);
4309 defer align_gz.unstack();
4310 const align_ref: Zir.Inst.Ref = if (fn_proto.ast.align_expr == 0) .none else inst: {
4311 const inst = try expr(&decl_gz, params_scope, coerced_align_ri, fn_proto.ast.align_expr);
4312 if (align_gz.instructionsSlice().len == 0) {
4313 // In this case we will send a len=0 body which can be encoded more efficiently.
4314 break :inst inst;
4315 }
4316 _ = try align_gz.addBreak(.break_inline, @enumFromInt(0), inst);
4317 break :inst inst;
4318 };
4319 const align_body_param_refs = try astgen.fetchRemoveRefEntries(param_insts.items);
4320
4321 var addrspace_gz = decl_gz.makeSubBlock(params_scope);
4322 defer addrspace_gz.unstack();
4323 const addrspace_ref: Zir.Inst.Ref = if (fn_proto.ast.addrspace_expr == 0) .none else inst: {
4324 const addrspace_ty = try decl_gz.addBuiltinValue(fn_proto.ast.addrspace_expr, .address_space);
4325 const inst = try expr(&decl_gz, params_scope, .{ .rl = .{ .coerced_ty = addrspace_ty } }, fn_proto.ast.addrspace_expr);
4326 if (addrspace_gz.instructionsSlice().len == 0) {
4289 var ret_gz = decl_gz.makeSubBlock(params_scope);
4290 defer ret_gz.unstack();
4291 const ret_ref: Zir.Inst.Ref = inst: {
4292 // Parameters are in scope for the return type, so we use `params_scope` here.
4293 // The calling convention will not have parameters in scope, so we'll just use `scope`.
4294 // See #22263 for a proposal to solve the inconsistency here.
4295 const inst = try fullBodyExpr(&ret_gz, params_scope, coerced_type_ri, fn_proto.ast.return_type, .normal);
4296 if (ret_gz.instructionsSlice().len == 0) {
43274297 // In this case we will send a len=0 body which can be encoded more efficiently.
43284298 break :inst inst;
43294299 }
4330 _ = try addrspace_gz.addBreak(.break_inline, @enumFromInt(0), inst);
4300 _ = try ret_gz.addBreak(.break_inline, @enumFromInt(0), inst);
43314301 break :inst inst;
43324302 };
4333 const addrspace_body_param_refs = try astgen.fetchRemoveRefEntries(param_insts.items);
4303 const ret_body_param_refs = try astgen.fetchRemoveRefEntries(param_insts.items);
43344304
4335 var section_gz = decl_gz.makeSubBlock(params_scope);
4336 defer section_gz.unstack();
4337 const section_ref: Zir.Inst.Ref = if (fn_proto.ast.section_expr == 0) .none else inst: {
4338 const inst = try expr(&decl_gz, params_scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, fn_proto.ast.section_expr);
4339 if (section_gz.instructionsSlice().len == 0) {
4340 // In this case we will send a len=0 body which can be encoded more efficiently.
4341 break :inst inst;
4342 }
4343 _ = try section_gz.addBreak(.break_inline, @enumFromInt(0), inst);
4344 break :inst inst;
4345 };
4346 const section_body_param_refs = try astgen.fetchRemoveRefEntries(param_insts.items);
4305 // We're jumping back in source, so restore the cursor.
4306 astgen.restoreSourceCursor(saved_cursor);
43474307
4348 var cc_gz = decl_gz.makeSubBlock(params_scope);
4308 var cc_gz = decl_gz.makeSubBlock(scope);
43494309 defer cc_gz.unstack();
43504310 const cc_ref: Zir.Inst.Ref = blk: {
43514311 if (fn_proto.ast.callconv_expr != 0) {
......@@ -4358,7 +4318,7 @@ fn fnDecl(
43584318 }
43594319 const inst = try expr(
43604320 &cc_gz,
4361 params_scope,
4321 scope,
43624322 .{ .rl = .{ .coerced_ty = try cc_gz.addBuiltinValue(fn_proto.ast.callconv_expr, .calling_convention) } },
43634323 fn_proto.ast.callconv_expr,
43644324 );
......@@ -4380,20 +4340,6 @@ fn fnDecl(
43804340 break :blk .none;
43814341 }
43824342 };
4383 const cc_body_param_refs = try astgen.fetchRemoveRefEntries(param_insts.items);
4384
4385 var ret_gz = decl_gz.makeSubBlock(params_scope);
4386 defer ret_gz.unstack();
4387 const ret_ref: Zir.Inst.Ref = inst: {
4388 const inst = try fullBodyExpr(&ret_gz, params_scope, coerced_type_ri, fn_proto.ast.return_type, .normal);
4389 if (ret_gz.instructionsSlice().len == 0) {
4390 // In this case we will send a len=0 body which can be encoded more efficiently.
4391 break :inst inst;
4392 }
4393 _ = try ret_gz.addBreak(.break_inline, @enumFromInt(0), inst);
4394 break :inst inst;
4395 };
4396 const ret_body_param_refs = try astgen.fetchRemoveRefEntries(param_insts.items);
43974343
43984344 const func_inst: Zir.Inst.Ref = if (body_node == 0) func: {
43994345 if (!is_extern) {
......@@ -4406,19 +4352,9 @@ fn fnDecl(
44064352 .src_node = decl_node,
44074353 .cc_ref = cc_ref,
44084354 .cc_gz = &cc_gz,
4409 .cc_param_refs = cc_body_param_refs,
4410 .align_ref = align_ref,
4411 .align_gz = &align_gz,
4412 .align_param_refs = align_body_param_refs,
44134355 .ret_ref = ret_ref,
44144356 .ret_gz = &ret_gz,
44154357 .ret_param_refs = ret_body_param_refs,
4416 .section_ref = section_ref,
4417 .section_gz = &section_gz,
4418 .section_param_refs = section_body_param_refs,
4419 .addrspace_ref = addrspace_ref,
4420 .addrspace_gz = &addrspace_gz,
4421 .addrspace_param_refs = addrspace_body_param_refs,
44224358 .param_block = decl_inst,
44234359 .param_insts = param_insts.items,
44244360 .body_gz = null,
......@@ -4432,8 +4368,23 @@ fn fnDecl(
44324368 .proto_hash = undefined, // ignored for `body_gz == null`
44334369 });
44344370 } else func: {
4435 // as a scope, fn_gz encloses ret_gz, but for instruction list, fn_gz stacks on ret_gz
4436 fn_gz.instructions_top = ret_gz.instructions.items.len;
4371 var body_gz: GenZir = .{
4372 .is_comptime = false,
4373 .decl_node_index = fn_proto.ast.proto_node,
4374 .decl_line = decl_gz.decl_line,
4375 .parent = params_scope,
4376 .astgen = astgen,
4377 .instructions = gz.instructions,
4378 .instructions_top = gz.instructions.items.len,
4379 };
4380 defer body_gz.unstack();
4381
4382 // We want `params_scope` to be stacked like this:
4383 // body_gz (top)
4384 // param2
4385 // param1
4386 // param0
4387 // decl_gz (bottom)
44374388
44384389 // Construct the prototype hash.
44394390 // Leave `astgen.src_hasher` unmodified; this will be used for hashing
......@@ -4450,13 +4401,13 @@ fn fnDecl(
44504401 astgen.fn_block = prev_fn_block;
44514402 astgen.fn_ret_ty = prev_fn_ret_ty;
44524403 }
4453 astgen.fn_block = &fn_gz;
4404 astgen.fn_block = &body_gz;
44544405 astgen.fn_ret_ty = if (is_inferred_error or ret_ref.toIndex() != null) r: {
44554406 // We're essentially guaranteed to need the return type at some point,
44564407 // since the return type is likely not `void` or `noreturn` so there
44574408 // will probably be an explicit return requiring RLS. Fetch this
44584409 // return type now so the rest of the function can use it.
4459 break :r try fn_gz.addNode(.ret_type, decl_node);
4410 break :r try body_gz.addNode(.ret_type, decl_node);
44604411 } else ret_ref;
44614412
44624413 const prev_var_args = astgen.fn_var_args;
......@@ -4467,39 +4418,29 @@ fn fnDecl(
44674418 const lbrace_line = astgen.source_line - decl_gz.decl_line;
44684419 const lbrace_column = astgen.source_column;
44694420
4470 _ = try fullBodyExpr(&fn_gz, params_scope, .{ .rl = .none }, body_node, .allow_branch_hint);
4471 try checkUsed(gz, &fn_gz.base, params_scope);
4421 _ = try fullBodyExpr(&body_gz, &body_gz.base, .{ .rl = .none }, body_node, .allow_branch_hint);
4422 try checkUsed(gz, scope, params_scope);
44724423
4473 if (!fn_gz.endsWithNoReturn()) {
4424 if (!body_gz.endsWithNoReturn()) {
44744425 // As our last action before the return, "pop" the error trace if needed
4475 _ = try fn_gz.addRestoreErrRetIndex(.ret, .always, decl_node);
4426 _ = try body_gz.addRestoreErrRetIndex(.ret, .always, decl_node);
44764427
44774428 // Add implicit return at end of function.
4478 _ = try fn_gz.addUnTok(.ret_implicit, .void_value, tree.lastToken(body_node));
4429 _ = try body_gz.addUnTok(.ret_implicit, .void_value, tree.lastToken(body_node));
44794430 }
44804431
44814432 break :func try decl_gz.addFunc(.{
44824433 .src_node = decl_node,
44834434 .cc_ref = cc_ref,
44844435 .cc_gz = &cc_gz,
4485 .cc_param_refs = cc_body_param_refs,
4486 .align_ref = align_ref,
4487 .align_gz = &align_gz,
4488 .align_param_refs = align_body_param_refs,
44894436 .ret_ref = ret_ref,
44904437 .ret_gz = &ret_gz,
44914438 .ret_param_refs = ret_body_param_refs,
4492 .section_ref = section_ref,
4493 .section_gz = &section_gz,
4494 .section_param_refs = section_body_param_refs,
4495 .addrspace_ref = addrspace_ref,
4496 .addrspace_gz = &addrspace_gz,
4497 .addrspace_param_refs = addrspace_body_param_refs,
44984439 .lbrace_line = lbrace_line,
44994440 .lbrace_column = lbrace_column,
45004441 .param_block = decl_inst,
45014442 .param_insts = param_insts.items,
4502 .body_gz = &fn_gz,
4443 .body_gz = &body_gz,
45034444 .lib_name = lib_name,
45044445 .is_var_args = is_var_args,
45054446 .is_inferred_error = is_inferred_error,
......@@ -4511,13 +4452,39 @@ fn fnDecl(
45114452 });
45124453 };
45134454
4455 // Before we stack more stuff onto `decl_gz`, add its final instruction.
4456 _ = try decl_gz.addBreak(.break_inline, decl_inst, func_inst);
4457
4458 // Now that `cc_gz,` `ret_gz`, and `body_gz` are unstacked, we evaluate align, addrspace, and linksection.
4459
4460 // We're jumping back in source, so restore the cursor.
4461 astgen.restoreSourceCursor(saved_cursor);
4462
4463 var align_gz = decl_gz.makeSubBlock(scope);
4464 defer align_gz.unstack();
4465 if (fn_proto.ast.align_expr != 0) {
4466 const inst = try expr(&decl_gz, &decl_gz.base, coerced_align_ri, fn_proto.ast.align_expr);
4467 _ = try align_gz.addBreak(.break_inline, decl_inst, inst);
4468 }
4469
4470 var section_gz = align_gz.makeSubBlock(scope);
4471 defer section_gz.unstack();
4472 if (fn_proto.ast.section_expr != 0) {
4473 const inst = try expr(&decl_gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, fn_proto.ast.section_expr);
4474 _ = try section_gz.addBreak(.break_inline, decl_inst, inst);
4475 }
4476
4477 var addrspace_gz = section_gz.makeSubBlock(scope);
4478 defer addrspace_gz.unstack();
4479 if (fn_proto.ast.addrspace_expr != 0) {
4480 const addrspace_ty = try decl_gz.addBuiltinValue(fn_proto.ast.addrspace_expr, .address_space);
4481 const inst = try expr(&decl_gz, scope, .{ .rl = .{ .coerced_ty = addrspace_ty } }, fn_proto.ast.addrspace_expr);
4482 _ = try addrspace_gz.addBreak(.break_inline, decl_inst, inst);
4483 }
4484
45144485 // *Now* we can incorporate the full source code into the hasher.
45154486 astgen.src_hasher.update(tree.getNodeSource(decl_node));
45164487
4517 // We add this at the end so that its instruction index marks the end range
4518 // of the top level declaration. addFunc already unstacked fn_gz and ret_gz.
4519 _ = try decl_gz.addBreak(.break_inline, decl_inst, func_inst);
4520
45214488 var hash: std.zig.SrcHash = undefined;
45224489 astgen.src_hasher.final(&hash);
45234490 try setDeclaration(
......@@ -4529,9 +4496,11 @@ fn fnDecl(
45294496 is_pub,
45304497 is_export,
45314498 &decl_gz,
4532 // align, linksection, and addrspace are passed in the func instruction in this case.
4533 // TODO: move them from the function instruction to the declaration instruction?
4534 null,
4499 .{
4500 .align_gz = &align_gz,
4501 .linksection_gz = &section_gz,
4502 .addrspace_gz = &addrspace_gz,
4503 },
45354504 );
45364505}
45374506
......@@ -4986,19 +4955,9 @@ fn testDecl(
49864955
49874956 .cc_ref = .none,
49884957 .cc_gz = null,
4989 .align_ref = .none,
4990 .align_gz = null,
49914958 .ret_ref = .anyerror_void_error_union_type,
49924959 .ret_gz = null,
4993 .section_ref = .none,
4994 .section_gz = null,
4995 .addrspace_ref = .none,
4996 .addrspace_gz = null,
4997
4998 .align_param_refs = &.{},
4999 .addrspace_param_refs = &.{},
5000 .section_param_refs = &.{},
5001 .cc_param_refs = &.{},
4960
50024961 .ret_param_refs = &.{},
50034962 .param_insts = &.{},
50044963
......@@ -11952,6 +11911,14 @@ const GenZir = struct {
1195211911 self.instructions.items[self.instructions_top..];
1195311912 }
1195411913
11914 fn instructionsSliceUptoOpt(gz: *const GenZir, maybe_stacked_gz: ?*GenZir) []Zir.Inst.Index {
11915 if (maybe_stacked_gz) |stacked_gz| {
11916 return gz.instructionsSliceUpto(stacked_gz);
11917 } else {
11918 return gz.instructionsSlice();
11919 }
11920 }
11921
1195511922 fn makeSubBlock(gz: *GenZir, scope: *Scope) GenZir {
1195611923 return .{
1195711924 .is_comptime = gz.is_comptime,
......@@ -12088,11 +12055,8 @@ const GenZir = struct {
1208812055
1208912056 /// Must be called with the following stack set up:
1209012057 /// * gz (bottom)
12091 /// * align_gz
12092 /// * addrspace_gz
12093 /// * section_gz
12094 /// * cc_gz
1209512058 /// * ret_gz
12059 /// * cc_gz
1209612060 /// * body_gz (top)
1209712061 /// Unstacks all of those except for `gz`.
1209812062 fn addFunc(
......@@ -12103,23 +12067,13 @@ const GenZir = struct {
1210312067 lbrace_column: u32 = 0,
1210412068 param_block: Zir.Inst.Index,
1210512069
12106 align_gz: ?*GenZir,
12107 addrspace_gz: ?*GenZir,
12108 section_gz: ?*GenZir,
12109 cc_gz: ?*GenZir,
1211012070 ret_gz: ?*GenZir,
1211112071 body_gz: ?*GenZir,
12072 cc_gz: ?*GenZir,
1211212073
12113 align_param_refs: []Zir.Inst.Index,
12114 addrspace_param_refs: []Zir.Inst.Index,
12115 section_param_refs: []Zir.Inst.Index,
12116 cc_param_refs: []Zir.Inst.Index,
1211712074 ret_param_refs: []Zir.Inst.Index,
1211812075 param_insts: []Zir.Inst.Index, // refs to params in `body_gz` should still be in `astgen.ref_table`
1211912076
12120 align_ref: Zir.Inst.Ref,
12121 addrspace_ref: Zir.Inst.Ref,
12122 section_ref: Zir.Inst.Ref,
1212312077 cc_ref: Zir.Inst.Ref,
1212412078 ret_ref: Zir.Inst.Ref,
1212512079
......@@ -12141,13 +12095,31 @@ const GenZir = struct {
1214112095 const ret_ref = if (args.ret_ref == .void_type) .none else args.ret_ref;
1214212096 const new_index: Zir.Inst.Index = @enumFromInt(astgen.instructions.len);
1214312097
12098 try gz.instructions.ensureUnusedCapacity(gpa, 1);
1214412099 try astgen.instructions.ensureUnusedCapacity(gpa, 1);
1214512100
12146 var body: []Zir.Inst.Index = &[0]Zir.Inst.Index{};
12147 var ret_body: []Zir.Inst.Index = &[0]Zir.Inst.Index{};
12101 const body, const cc_body, const ret_body = bodies: {
12102 var stacked_gz: ?*GenZir = null;
12103 const body: []const Zir.Inst.Index = if (args.body_gz) |body_gz| body: {
12104 const body = body_gz.instructionsSliceUptoOpt(stacked_gz);
12105 stacked_gz = body_gz;
12106 break :body body;
12107 } else &.{};
12108 const cc_body: []const Zir.Inst.Index = if (args.cc_gz) |cc_gz| body: {
12109 const cc_body = cc_gz.instructionsSliceUptoOpt(stacked_gz);
12110 stacked_gz = cc_gz;
12111 break :body cc_body;
12112 } else &.{};
12113 const ret_body: []const Zir.Inst.Index = if (args.ret_gz) |ret_gz| body: {
12114 const ret_body = ret_gz.instructionsSliceUptoOpt(stacked_gz);
12115 stacked_gz = ret_gz;
12116 break :body ret_body;
12117 } else &.{};
12118 break :bodies .{ body, cc_body, ret_body };
12119 };
12120
1214812121 var src_locs_and_hash_buffer: [7]u32 = undefined;
12149 var src_locs_and_hash: []u32 = src_locs_and_hash_buffer[0..0];
12150 if (args.body_gz) |body_gz| {
12122 const src_locs_and_hash: []const u32 = if (args.body_gz != null) src_locs_and_hash: {
1215112123 const tree = astgen.tree;
1215212124 const node_tags = tree.nodes.items(.tag);
1215312125 const node_datas = tree.nodes.items(.data);
......@@ -12173,39 +12145,19 @@ const GenZir = struct {
1217312145 proto_hash_arr[2],
1217412146 proto_hash_arr[3],
1217512147 };
12176 src_locs_and_hash = &src_locs_and_hash_buffer;
12148 break :src_locs_and_hash &src_locs_and_hash_buffer;
12149 } else &.{};
1217712150
12178 body = body_gz.instructionsSlice();
12179 if (args.ret_gz) |ret_gz|
12180 ret_body = ret_gz.instructionsSliceUpto(body_gz);
12181 } else {
12182 if (args.ret_gz) |ret_gz|
12183 ret_body = ret_gz.instructionsSlice();
12184 }
1218512151 const body_len = astgen.countBodyLenAfterFixupsExtraRefs(body, args.param_insts);
1218612152
12187 if (args.cc_ref != .none or args.lib_name != .empty or args.is_var_args or args.is_test or
12188 args.is_extern or args.align_ref != .none or args.section_ref != .none or
12189 args.addrspace_ref != .none or args.noalias_bits != 0 or args.is_noinline)
12190 {
12191 var align_body: []Zir.Inst.Index = &.{};
12192 var addrspace_body: []Zir.Inst.Index = &.{};
12193 var section_body: []Zir.Inst.Index = &.{};
12194 var cc_body: []Zir.Inst.Index = &.{};
12195 if (args.ret_gz != null) {
12196 align_body = args.align_gz.?.instructionsSliceUpto(args.addrspace_gz.?);
12197 addrspace_body = args.addrspace_gz.?.instructionsSliceUpto(args.section_gz.?);
12198 section_body = args.section_gz.?.instructionsSliceUpto(args.cc_gz.?);
12199 cc_body = args.cc_gz.?.instructionsSliceUpto(args.ret_gz.?);
12200 }
12201
12153 const tag: Zir.Inst.Tag, const payload_index: u32 = if (args.cc_ref != .none or args.lib_name != .empty or
12154 args.is_var_args or args.is_test or args.is_extern or
12155 args.noalias_bits != 0 or args.is_noinline)
12156 inst_info: {
1220212157 try astgen.extra.ensureUnusedCapacity(
1220312158 gpa,
1220412159 @typeInfo(Zir.Inst.FuncFancy).@"struct".fields.len +
12205 fancyFnExprExtraLen(astgen, args.align_param_refs, align_body, args.align_ref) +
12206 fancyFnExprExtraLen(astgen, args.addrspace_param_refs, addrspace_body, args.addrspace_ref) +
12207 fancyFnExprExtraLen(astgen, args.section_param_refs, section_body, args.section_ref) +
12208 fancyFnExprExtraLen(astgen, args.cc_param_refs, cc_body, args.cc_ref) +
12160 fancyFnExprExtraLen(astgen, &.{}, cc_body, args.cc_ref) +
1220912161 fancyFnExprExtraLen(astgen, args.ret_param_refs, ret_body, ret_ref) +
1221012162 body_len + src_locs_and_hash.len +
1221112163 @intFromBool(args.lib_name != .empty) +
......@@ -12223,15 +12175,9 @@ const GenZir = struct {
1222312175 .has_lib_name = args.lib_name != .empty,
1222412176 .has_any_noalias = args.noalias_bits != 0,
1222512177
12226 .has_align_ref = args.align_ref != .none,
12227 .has_addrspace_ref = args.addrspace_ref != .none,
12228 .has_section_ref = args.section_ref != .none,
1222912178 .has_cc_ref = args.cc_ref != .none,
1223012179 .has_ret_ty_ref = ret_ref != .none,
1223112180
12232 .has_align_body = align_body.len != 0,
12233 .has_addrspace_body = addrspace_body.len != 0,
12234 .has_section_body = section_body.len != 0,
1223512181 .has_cc_body = cc_body.len != 0,
1223612182 .has_ret_ty_body = ret_body.len != 0,
1223712183 },
......@@ -12241,53 +12187,8 @@ const GenZir = struct {
1224112187 }
1224212188
1224312189 const zir_datas = astgen.instructions.items(.data);
12244 if (align_body.len != 0) {
12245 astgen.extra.appendAssumeCapacity(
12246 astgen.countBodyLenAfterFixups(args.align_param_refs) +
12247 astgen.countBodyLenAfterFixups(align_body),
12248 );
12249 astgen.appendBodyWithFixups(args.align_param_refs);
12250 astgen.appendBodyWithFixups(align_body);
12251 const break_extra = zir_datas[@intFromEnum(align_body[align_body.len - 1])].@"break".payload_index;
12252 astgen.extra.items[break_extra + std.meta.fieldIndex(Zir.Inst.Break, "block_inst").?] =
12253 @intFromEnum(new_index);
12254 } else if (args.align_ref != .none) {
12255 astgen.extra.appendAssumeCapacity(@intFromEnum(args.align_ref));
12256 }
12257 if (addrspace_body.len != 0) {
12258 astgen.extra.appendAssumeCapacity(
12259 astgen.countBodyLenAfterFixups(args.addrspace_param_refs) +
12260 astgen.countBodyLenAfterFixups(addrspace_body),
12261 );
12262 astgen.appendBodyWithFixups(args.addrspace_param_refs);
12263 astgen.appendBodyWithFixups(addrspace_body);
12264 const break_extra =
12265 zir_datas[@intFromEnum(addrspace_body[addrspace_body.len - 1])].@"break".payload_index;
12266 astgen.extra.items[break_extra + std.meta.fieldIndex(Zir.Inst.Break, "block_inst").?] =
12267 @intFromEnum(new_index);
12268 } else if (args.addrspace_ref != .none) {
12269 astgen.extra.appendAssumeCapacity(@intFromEnum(args.addrspace_ref));
12270 }
12271 if (section_body.len != 0) {
12272 astgen.extra.appendAssumeCapacity(
12273 astgen.countBodyLenAfterFixups(args.section_param_refs) +
12274 astgen.countBodyLenAfterFixups(section_body),
12275 );
12276 astgen.appendBodyWithFixups(args.section_param_refs);
12277 astgen.appendBodyWithFixups(section_body);
12278 const break_extra =
12279 zir_datas[@intFromEnum(section_body[section_body.len - 1])].@"break".payload_index;
12280 astgen.extra.items[break_extra + std.meta.fieldIndex(Zir.Inst.Break, "block_inst").?] =
12281 @intFromEnum(new_index);
12282 } else if (args.section_ref != .none) {
12283 astgen.extra.appendAssumeCapacity(@intFromEnum(args.section_ref));
12284 }
1228512190 if (cc_body.len != 0) {
12286 astgen.extra.appendAssumeCapacity(
12287 astgen.countBodyLenAfterFixups(args.cc_param_refs) +
12288 astgen.countBodyLenAfterFixups(cc_body),
12289 );
12290 astgen.appendBodyWithFixups(args.cc_param_refs);
12191 astgen.extra.appendAssumeCapacity(astgen.countBodyLenAfterFixups(cc_body));
1229112192 astgen.appendBodyWithFixups(cc_body);
1229212193 const break_extra = zir_datas[@intFromEnum(cc_body[cc_body.len - 1])].@"break".payload_index;
1229312194 astgen.extra.items[break_extra + std.meta.fieldIndex(Zir.Inst.Break, "block_inst").?] =
......@@ -12316,28 +12217,8 @@ const GenZir = struct {
1231612217 astgen.appendBodyWithFixupsExtraRefsArrayList(&astgen.extra, body, args.param_insts);
1231712218 astgen.extra.appendSliceAssumeCapacity(src_locs_and_hash);
1231812219
12319 // Order is important when unstacking.
12320 if (args.body_gz) |body_gz| body_gz.unstack();
12321 if (args.ret_gz != null) {
12322 args.ret_gz.?.unstack();
12323 args.cc_gz.?.unstack();
12324 args.section_gz.?.unstack();
12325 args.addrspace_gz.?.unstack();
12326 args.align_gz.?.unstack();
12327 }
12328
12329 try gz.instructions.ensureUnusedCapacity(gpa, 1);
12330
12331 astgen.instructions.appendAssumeCapacity(.{
12332 .tag = .func_fancy,
12333 .data = .{ .pl_node = .{
12334 .src_node = gz.nodeIndexToRelative(args.src_node),
12335 .payload_index = payload_index,
12336 } },
12337 });
12338 gz.instructions.appendAssumeCapacity(new_index);
12339 return new_index.toRef();
12340 } else {
12220 break :inst_info .{ .func_fancy, payload_index };
12221 } else inst_info: {
1234112222 try astgen.extra.ensureUnusedCapacity(
1234212223 gpa,
1234312224 @typeInfo(Zir.Inst.Func).@"struct".fields.len + 1 +
......@@ -12369,30 +12250,29 @@ const GenZir = struct {
1236912250 astgen.appendBodyWithFixupsExtraRefsArrayList(&astgen.extra, body, args.param_insts);
1237012251 astgen.extra.appendSliceAssumeCapacity(src_locs_and_hash);
1237112252
12372 // Order is important when unstacking.
12373 if (args.body_gz) |body_gz| body_gz.unstack();
12374 if (args.ret_gz) |ret_gz| ret_gz.unstack();
12375 if (args.cc_gz) |cc_gz| cc_gz.unstack();
12376 if (args.section_gz) |section_gz| section_gz.unstack();
12377 if (args.addrspace_gz) |addrspace_gz| addrspace_gz.unstack();
12378 if (args.align_gz) |align_gz| align_gz.unstack();
12253 break :inst_info .{
12254 if (args.is_inferred_error) .func_inferred else .func,
12255 payload_index,
12256 };
12257 };
1237912258
12380 try gz.instructions.ensureUnusedCapacity(gpa, 1);
12259 // Order is important when unstacking.
12260 if (args.body_gz) |body_gz| body_gz.unstack();
12261 if (args.cc_gz) |cc_gz| cc_gz.unstack();
12262 if (args.ret_gz) |ret_gz| ret_gz.unstack();
1238112263
12382 const tag: Zir.Inst.Tag = if (args.is_inferred_error) .func_inferred else .func;
12383 astgen.instructions.appendAssumeCapacity(.{
12384 .tag = tag,
12385 .data = .{ .pl_node = .{
12386 .src_node = gz.nodeIndexToRelative(args.src_node),
12387 .payload_index = payload_index,
12388 } },
12389 });
12390 gz.instructions.appendAssumeCapacity(new_index);
12391 return new_index.toRef();
12392 }
12264 astgen.instructions.appendAssumeCapacity(.{
12265 .tag = tag,
12266 .data = .{ .pl_node = .{
12267 .src_node = gz.nodeIndexToRelative(args.src_node),
12268 .payload_index = payload_index,
12269 } },
12270 });
12271 gz.instructions.appendAssumeCapacity(new_index);
12272 return new_index.toRef();
1239312273 }
1239412274
12395 fn fancyFnExprExtraLen(astgen: *AstGen, param_refs_body: []Zir.Inst.Index, main_body: []Zir.Inst.Index, ref: Zir.Inst.Ref) u32 {
12275 fn fancyFnExprExtraLen(astgen: *AstGen, param_refs_body: []const Zir.Inst.Index, main_body: []const Zir.Inst.Index, ref: Zir.Inst.Ref) u32 {
1239612276 return countBodyLenAfterFixups(astgen, param_refs_body) +
1239712277 countBodyLenAfterFixups(astgen, main_body) +
1239812278 // If there is a body, we need an element for its length; otherwise, if there is a ref, we need to include that.
......@@ -13576,6 +13456,27 @@ fn advanceSourceCursor(astgen: *AstGen, end: usize) void {
1357613456 astgen.source_column = column;
1357713457}
1357813458
13459const SourceCursor = struct {
13460 offset: u32,
13461 line: u32,
13462 column: u32,
13463};
13464
13465/// Get the current source cursor, to be restored later with `restoreSourceCursor`.
13466/// This is useful when analyzing source code out-of-order.
13467fn saveSourceCursor(astgen: *const AstGen) SourceCursor {
13468 return .{
13469 .offset = astgen.source_offset,
13470 .line = astgen.source_line,
13471 .column = astgen.source_column,
13472 };
13473}
13474fn restoreSourceCursor(astgen: *AstGen, cursor: SourceCursor) void {
13475 astgen.source_offset = cursor.offset;
13476 astgen.source_line = cursor.line;
13477 astgen.source_column = cursor.column;
13478}
13479
1357913480/// Detects name conflicts for decls and fields, and populates `namespace.decls` with all named declarations.
1358013481/// Returns the number of declarations in the namespace, including unnamed declarations (e.g. `comptime` decls).
1358113482fn scanContainer(
lib/std/zig/Zir.zig+12-99
......@@ -2494,46 +2494,25 @@ pub const Inst = struct {
24942494
24952495 /// Trailing:
24962496 /// 0. lib_name: NullTerminatedString, // null terminated string index, if has_lib_name is set
2497 /// if (has_align_ref and !has_align_body) {
2498 /// 1. align: Ref,
2499 /// }
2500 /// if (has_align_body) {
2501 /// 2. align_body_len: u32
2502 /// 3. align_body: u32 // for each align_body_len
2503 /// }
2504 /// if (has_addrspace_ref and !has_addrspace_body) {
2505 /// 4. addrspace: Ref,
2506 /// }
2507 /// if (has_addrspace_body) {
2508 /// 5. addrspace_body_len: u32
2509 /// 6. addrspace_body: u32 // for each addrspace_body_len
2510 /// }
2511 /// if (has_section_ref and !has_section_body) {
2512 /// 7. section: Ref,
2513 /// }
2514 /// if (has_section_body) {
2515 /// 8. section_body_len: u32
2516 /// 9. section_body: u32 // for each section_body_len
2517 /// }
25182497 /// if (has_cc_ref and !has_cc_body) {
2519 /// 10. cc: Ref,
2498 /// 1. cc: Ref,
25202499 /// }
25212500 /// if (has_cc_body) {
2522 /// 11. cc_body_len: u32
2523 /// 12. cc_body: u32 // for each cc_body_len
2501 /// 2. cc_body_len: u32
2502 /// 3. cc_body: u32 // for each cc_body_len
25242503 /// }
25252504 /// if (has_ret_ty_ref and !has_ret_ty_body) {
2526 /// 13. ret_ty: Ref,
2505 /// 4. ret_ty: Ref,
25272506 /// }
25282507 /// if (has_ret_ty_body) {
2529 /// 14. ret_ty_body_len: u32
2530 /// 15. ret_ty_body: u32 // for each ret_ty_body_len
2508 /// 5. ret_ty_body_len: u32
2509 /// 6. ret_ty_body: u32 // for each ret_ty_body_len
25312510 /// }
2532 /// 16. noalias_bits: u32 // if has_any_noalias
2533 /// - each bit starting with LSB corresponds to parameter indexes
2534 /// 17. body: Index // for each body_len
2535 /// 18. src_locs: Func.SrcLocs // if body_len != 0
2536 /// 19. proto_hash: std.zig.SrcHash // if body_len != 0; hash of function prototype
2511 /// 7. noalias_bits: u32 // if has_any_noalias
2512 /// - each bit starting with LSB corresponds to parameter indexes
2513 /// 8. body: Index // for each body_len
2514 /// 9. src_locs: Func.SrcLocs // if body_len != 0
2515 /// 10. proto_hash: std.zig.SrcHash // if body_len != 0; hash of function prototype
25372516 pub const FuncFancy = struct {
25382517 /// Points to the block that contains the param instructions for this function.
25392518 /// If this is a `declaration`, it refers to the declaration's value body.
......@@ -2542,29 +2521,20 @@ pub const Inst = struct {
25422521 bits: Bits,
25432522
25442523 /// If both has_cc_ref and has_cc_body are false, it means auto calling convention.
2545 /// If both has_align_ref and has_align_body are false, it means default alignment.
25462524 /// If both has_ret_ty_ref and has_ret_ty_body are false, it means void return type.
2547 /// If both has_section_ref and has_section_body are false, it means default section.
2548 /// If both has_addrspace_ref and has_addrspace_body are false, it means default addrspace.
25492525 pub const Bits = packed struct {
25502526 is_var_args: bool,
25512527 is_inferred_error: bool,
25522528 is_test: bool,
25532529 is_extern: bool,
25542530 is_noinline: bool,
2555 has_align_ref: bool,
2556 has_align_body: bool,
2557 has_addrspace_ref: bool,
2558 has_addrspace_body: bool,
2559 has_section_ref: bool,
2560 has_section_body: bool,
25612531 has_cc_ref: bool,
25622532 has_cc_body: bool,
25632533 has_ret_ty_ref: bool,
25642534 has_ret_ty_body: bool,
25652535 has_lib_name: bool,
25662536 has_any_noalias: bool,
2567 _: u15 = undefined,
2537 _: u21 = undefined,
25682538 };
25692539 };
25702540
......@@ -4269,36 +4239,6 @@ fn findTrackableInner(
42694239 var extra_index: usize = extra.end;
42704240 extra_index += @intFromBool(extra.data.bits.has_lib_name);
42714241
4272 if (extra.data.bits.has_align_body) {
4273 const body_len = zir.extra[extra_index];
4274 extra_index += 1;
4275 const body = zir.bodySlice(extra_index, body_len);
4276 try zir.findTrackableBody(gpa, contents, defers, body);
4277 extra_index += body.len;
4278 } else if (extra.data.bits.has_align_ref) {
4279 extra_index += 1;
4280 }
4281
4282 if (extra.data.bits.has_addrspace_body) {
4283 const body_len = zir.extra[extra_index];
4284 extra_index += 1;
4285 const body = zir.bodySlice(extra_index, body_len);
4286 try zir.findTrackableBody(gpa, contents, defers, body);
4287 extra_index += body.len;
4288 } else if (extra.data.bits.has_addrspace_ref) {
4289 extra_index += 1;
4290 }
4291
4292 if (extra.data.bits.has_section_body) {
4293 const body_len = zir.extra[extra_index];
4294 extra_index += 1;
4295 const body = zir.bodySlice(extra_index, body_len);
4296 try zir.findTrackableBody(gpa, contents, defers, body);
4297 extra_index += body.len;
4298 } else if (extra.data.bits.has_section_ref) {
4299 extra_index += 1;
4300 }
4301
43024242 if (extra.data.bits.has_cc_body) {
43034243 const body_len = zir.extra[extra_index];
43044244 extra_index += 1;
......@@ -4587,21 +4527,6 @@ pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo {
45874527 var ret_ty_body: []const Inst.Index = &.{};
45884528
45894529 extra_index += @intFromBool(extra.data.bits.has_lib_name);
4590 if (extra.data.bits.has_align_body) {
4591 extra_index += zir.extra[extra_index] + 1;
4592 } else if (extra.data.bits.has_align_ref) {
4593 extra_index += 1;
4594 }
4595 if (extra.data.bits.has_addrspace_body) {
4596 extra_index += zir.extra[extra_index] + 1;
4597 } else if (extra.data.bits.has_addrspace_ref) {
4598 extra_index += 1;
4599 }
4600 if (extra.data.bits.has_section_body) {
4601 extra_index += zir.extra[extra_index] + 1;
4602 } else if (extra.data.bits.has_section_ref) {
4603 extra_index += 1;
4604 }
46054530 if (extra.data.bits.has_cc_body) {
46064531 extra_index += zir.extra[extra_index] + 1;
46074532 } else if (extra.data.bits.has_cc_ref) {
......@@ -4712,18 +4637,6 @@ pub fn getAssociatedSrcHash(zir: Zir, inst: Zir.Inst.Index) ?std.zig.SrcHash {
47124637 const bits = extra.data.bits;
47134638 var extra_index = extra.end;
47144639 extra_index += @intFromBool(bits.has_lib_name);
4715 if (bits.has_align_body) {
4716 const body_len = zir.extra[extra_index];
4717 extra_index += 1 + body_len;
4718 } else extra_index += @intFromBool(bits.has_align_ref);
4719 if (bits.has_addrspace_body) {
4720 const body_len = zir.extra[extra_index];
4721 extra_index += 1 + body_len;
4722 } else extra_index += @intFromBool(bits.has_addrspace_ref);
4723 if (bits.has_section_body) {
4724 const body_len = zir.extra[extra_index];
4725 extra_index += 1 + body_len;
4726 } else extra_index += @intFromBool(bits.has_section_ref);
47274640 if (bits.has_cc_body) {
47284641 const body_len = zir.extra[extra_index];
47294642 extra_index += 1 + body_len;
src/InternPool.zig+9-50
......@@ -1462,16 +1462,6 @@ pub const MapIndex = enum(u32) {
14621462 }
14631463};
14641464
1465pub const RuntimeIndex = enum(u32) {
1466 zero = 0,
1467 comptime_field_ptr = std.math.maxInt(u32),
1468 _,
1469
1470 pub fn increment(ri: *RuntimeIndex) void {
1471 ri.* = @enumFromInt(@intFromEnum(ri.*) + 1);
1472 }
1473};
1474
14751465pub const ComptimeAllocIndex = enum(u32) { _ };
14761466
14771467pub const NamespaceIndex = enum(u32) {
......@@ -1971,9 +1961,6 @@ pub const Key = union(enum) {
19711961 is_var_args: bool,
19721962 is_generic: bool,
19731963 is_noinline: bool,
1974 cc_is_generic: bool,
1975 section_is_generic: bool,
1976 addrspace_is_generic: bool,
19771964
19781965 pub fn paramIsComptime(self: @This(), i: u5) bool {
19791966 assert(i < self.param_types.len);
......@@ -5466,10 +5453,7 @@ pub const Tag = enum(u8) {
54665453 has_comptime_bits: bool,
54675454 has_noalias_bits: bool,
54685455 is_noinline: bool,
5469 cc_is_generic: bool,
5470 section_is_generic: bool,
5471 addrspace_is_generic: bool,
5472 _: u6 = 0,
5456 _: u9 = 0,
54735457 };
54745458 };
54755459
......@@ -6895,9 +6879,6 @@ fn extraFuncType(tid: Zcu.PerThread.Id, extra: Local.Extra, extra_index: u32) Ke
68956879 .cc = type_function.data.flags.cc.unpack(),
68966880 .is_var_args = type_function.data.flags.is_var_args,
68976881 .is_noinline = type_function.data.flags.is_noinline,
6898 .cc_is_generic = type_function.data.flags.cc_is_generic,
6899 .section_is_generic = type_function.data.flags.section_is_generic,
6900 .addrspace_is_generic = type_function.data.flags.addrspace_is_generic,
69016882 .is_generic = type_function.data.flags.is_generic,
69026883 };
69036884}
......@@ -8539,9 +8520,6 @@ pub fn getFuncType(
85398520 .has_noalias_bits = key.noalias_bits != 0,
85408521 .is_generic = key.is_generic,
85418522 .is_noinline = key.is_noinline,
8542 .cc_is_generic = key.cc == null,
8543 .section_is_generic = key.section_is_generic,
8544 .addrspace_is_generic = key.addrspace_is_generic,
85458523 },
85468524 });
85478525
......@@ -8713,10 +8691,6 @@ pub const GetFuncDeclIesKey = struct {
87138691 bare_return_type: Index,
87148692 /// null means generic.
87158693 cc: ?std.builtin.CallingConvention,
8716 /// null means generic.
8717 alignment: ?Alignment,
8718 section_is_generic: bool,
8719 addrspace_is_generic: bool,
87208694 is_var_args: bool,
87218695 is_generic: bool,
87228696 is_noinline: bool,
......@@ -8802,9 +8776,6 @@ pub fn getFuncDeclIes(
88028776 .has_noalias_bits = key.noalias_bits != 0,
88038777 .is_generic = key.is_generic,
88048778 .is_noinline = key.is_noinline,
8805 .cc_is_generic = key.cc == null,
8806 .section_is_generic = key.section_is_generic,
8807 .addrspace_is_generic = key.addrspace_is_generic,
88088779 },
88098780 });
88108781 if (key.comptime_bits != 0) extra.appendAssumeCapacity(.{key.comptime_bits});
......@@ -8936,9 +8907,6 @@ pub const GetFuncInstanceKey = struct {
89368907 comptime_args: []const Index,
89378908 noalias_bits: u32,
89388909 bare_return_type: Index,
8939 cc: std.builtin.CallingConvention,
8940 alignment: Alignment,
8941 section: OptionalNullTerminatedString,
89428910 is_noinline: bool,
89438911 generic_owner: Index,
89448912 inferred_error_set: bool,
......@@ -8953,11 +8921,14 @@ pub fn getFuncInstance(
89538921 if (arg.inferred_error_set)
89548922 return getFuncInstanceIes(ip, gpa, tid, arg);
89558923
8924 const generic_owner = unwrapCoercedFunc(ip, arg.generic_owner);
8925 const generic_owner_ty = ip.indexToKey(ip.funcDeclInfo(generic_owner).ty).func_type;
8926
89568927 const func_ty = try ip.getFuncType(gpa, tid, .{
89578928 .param_types = arg.param_types,
89588929 .return_type = arg.bare_return_type,
89598930 .noalias_bits = arg.noalias_bits,
8960 .cc = arg.cc,
8931 .cc = generic_owner_ty.cc,
89618932 .is_noinline = arg.is_noinline,
89628933 });
89638934
......@@ -8967,8 +8938,6 @@ pub fn getFuncInstance(
89678938 try extra.ensureUnusedCapacity(@typeInfo(Tag.FuncInstance).@"struct".fields.len +
89688939 arg.comptime_args.len);
89698940
8970 const generic_owner = unwrapCoercedFunc(ip, arg.generic_owner);
8971
89728941 assert(arg.comptime_args.len == ip.funcTypeParamsLen(ip.typeOf(generic_owner)));
89738942
89748943 const prev_extra_len = extra.mutate.len;
......@@ -9015,8 +8984,6 @@ pub fn getFuncInstance(
90158984 generic_owner,
90168985 func_index,
90178986 func_extra_index,
9018 arg.alignment,
9019 arg.section,
90208987 );
90218988 return gop.put();
90228989}
......@@ -9041,6 +9008,7 @@ pub fn getFuncInstanceIes(
90419008 try items.ensureUnusedCapacity(4);
90429009
90439010 const generic_owner = unwrapCoercedFunc(ip, arg.generic_owner);
9011 const generic_owner_ty = ip.indexToKey(ip.funcDeclInfo(arg.generic_owner).ty).func_type;
90449012
90459013 // The strategy here is to add the function decl unconditionally, then to
90469014 // ask if it already exists, and if so, revert the lengths of the mutated
......@@ -9096,15 +9064,12 @@ pub fn getFuncInstanceIes(
90969064 .params_len = params_len,
90979065 .return_type = error_union_type,
90989066 .flags = .{
9099 .cc = .pack(arg.cc),
9067 .cc = .pack(generic_owner_ty.cc),
91009068 .is_var_args = false,
91019069 .has_comptime_bits = false,
91029070 .has_noalias_bits = arg.noalias_bits != 0,
91039071 .is_generic = false,
91049072 .is_noinline = arg.is_noinline,
9105 .cc_is_generic = false,
9106 .section_is_generic = false,
9107 .addrspace_is_generic = false,
91089073 },
91099074 });
91109075 // no comptime_bits because has_comptime_bits is false
......@@ -9168,8 +9133,6 @@ pub fn getFuncInstanceIes(
91689133 generic_owner,
91699134 func_index,
91709135 func_extra_index,
9171 arg.alignment,
9172 arg.section,
91739136 );
91749137
91759138 func_gop.putFinal(func_index);
......@@ -9187,8 +9150,6 @@ fn finishFuncInstance(
91879150 generic_owner: Index,
91889151 func_index: Index,
91899152 func_extra_index: u32,
9190 alignment: Alignment,
9191 section: OptionalNullTerminatedString,
91929153) Allocator.Error!void {
91939154 const fn_owner_nav = ip.getNav(ip.funcDeclInfo(generic_owner).owner_nav);
91949155 const fn_namespace = ip.getCau(fn_owner_nav.analysis_owner.unwrap().?).namespace;
......@@ -9201,8 +9162,8 @@ fn finishFuncInstance(
92019162 .name = nav_name,
92029163 .fqn = try ip.namespacePtr(fn_namespace).internFullyQualifiedName(ip, gpa, tid, nav_name),
92039164 .val = func_index,
9204 .alignment = alignment,
9205 .@"linksection" = section,
9165 .alignment = fn_owner_nav.status.resolved.alignment,
9166 .@"linksection" = fn_owner_nav.status.resolved.@"linksection",
92069167 .@"addrspace" = fn_owner_nav.status.resolved.@"addrspace",
92079168 });
92089169
......@@ -9788,7 +9749,6 @@ fn addExtraAssumeCapacity(extra: Local.Extra.Mutable, item: anytype) u32 {
97889749 OptionalNamespaceIndex,
97899750 MapIndex,
97909751 OptionalMapIndex,
9791 RuntimeIndex,
97929752 String,
97939753 NullTerminatedString,
97949754 OptionalNullTerminatedString,
......@@ -9852,7 +9812,6 @@ fn extraDataTrail(extra: Local.Extra, comptime T: type, index: u32) struct { dat
98529812 OptionalNamespaceIndex,
98539813 MapIndex,
98549814 OptionalMapIndex,
9855 RuntimeIndex,
98569815 String,
98579816 NullTerminatedString,
98589817 OptionalNullTerminatedString,
src/Sema.zig+102-272
......@@ -122,9 +122,19 @@ allow_memoize: bool = true,
122122/// This state is on `Sema` so that `cold` hints can be propagated up through blocks with less special handling.
123123branch_hint: ?std.builtin.BranchHint = null,
124124
125const RuntimeIndex = enum(u32) {
126 zero = 0,
127 comptime_field_ptr = std.math.maxInt(u32),
128 _,
129
130 pub fn increment(ri: *RuntimeIndex) void {
131 ri.* = @enumFromInt(@intFromEnum(ri.*) + 1);
132 }
133};
134
125135const MaybeComptimeAlloc = struct {
126136 /// The runtime index of the `alloc` instruction.
127 runtime_index: Value.RuntimeIndex,
137 runtime_index: RuntimeIndex,
128138 /// Backed by sema.arena. Tracks all comptime-known stores to this `alloc`. Due to
129139 /// RLS, a single comptime-known allocation may have arbitrarily many stores.
130140 /// This list also contains `set_union_tag`, `optional_payload_ptr_set`, and
......@@ -144,7 +154,7 @@ const ComptimeAlloc = struct {
144154 /// This is the `runtime_index` at the point of this allocation. If an store
145155 /// to this alloc ever occurs with a runtime index greater than this one, it
146156 /// is behind a runtime condition, so a compile error will be emitted.
147 runtime_index: Value.RuntimeIndex,
157 runtime_index: RuntimeIndex,
148158};
149159
150160fn newComptimeAlloc(sema: *Sema, block: *Block, ty: Type, alignment: Alignment) !ComptimeAllocIndex {
......@@ -364,7 +374,7 @@ pub const Block = struct {
364374 runtime_loop: ?LazySrcLoc = null,
365375 /// Non zero if a non-inline loop or a runtime conditional have been encountered.
366376 /// Stores to comptime variables are only allowed when var.runtime_index <= runtime_index.
367 runtime_index: Value.RuntimeIndex = .zero,
377 runtime_index: RuntimeIndex = .zero,
368378 inline_block: Zir.Inst.OptionalIndex = .none,
369379
370380 comptime_reason: ?*const ComptimeReason = null,
......@@ -7805,11 +7815,9 @@ fn analyzeCall(
78057815 .param_types = new_param_types,
78067816 .return_type = owner_info.return_type,
78077817 .noalias_bits = owner_info.noalias_bits,
7808 .cc = if (owner_info.cc_is_generic) null else owner_info.cc,
7818 .cc = owner_info.cc,
78097819 .is_var_args = owner_info.is_var_args,
78107820 .is_noinline = owner_info.is_noinline,
7811 .section_is_generic = owner_info.section_is_generic,
7812 .addrspace_is_generic = owner_info.addrspace_is_generic,
78137821 .is_generic = owner_info.is_generic,
78147822 };
78157823
......@@ -9545,9 +9553,6 @@ fn zirFunc(
95459553 block,
95469554 inst_data.src_node,
95479555 inst,
9548 .none,
9549 target_util.defaultAddressSpace(target, .function),
9550 .default,
95519556 cc,
95529557 ret_ty,
95539558 false,
......@@ -9833,13 +9838,7 @@ fn funcCommon(
98339838 block: *Block,
98349839 src_node_offset: i32,
98359840 func_inst: Zir.Inst.Index,
9836 /// null means generic poison
9837 alignment: ?Alignment,
9838 /// null means generic poison
9839 address_space: ?std.builtin.AddressSpace,
9840 section: Section,
9841 /// null means generic poison
9842 cc: ?std.builtin.CallingConvention,
9841 cc: std.builtin.CallingConvention,
98439842 /// this might be Type.generic_poison
98449843 bare_return_type: Type,
98459844 var_args: bool,
......@@ -9860,26 +9859,17 @@ fn funcCommon(
98609859 const cc_src = block.src(.{ .node_offset_fn_type_cc = src_node_offset });
98619860 const func_src = block.nodeOffset(src_node_offset);
98629861
9863 var is_generic = bare_return_type.isGenericPoison() or
9864 alignment == null or
9865 address_space == null or
9866 section == .generic or
9867 cc == null;
9862 var is_generic = bare_return_type.isGenericPoison();
98689863
98699864 if (var_args) {
98709865 if (is_generic) {
98719866 return sema.fail(block, func_src, "generic function cannot be variadic", .{});
98729867 }
9873 try sema.checkCallConvSupportsVarArgs(block, cc_src, cc.?);
9868 try sema.checkCallConvSupportsVarArgs(block, cc_src, cc);
98749869 }
98759870
98769871 const is_source_decl = sema.generic_owner == .none;
98779872
9878 // In the case of generic calling convention, or generic alignment, we use
9879 // default values which are only meaningful for the generic function, *not*
9880 // the instantiation, which can depend on comptime parameters.
9881 // Related proposal: https://github.com/ziglang/zig/issues/11834
9882 const cc_resolved = cc orelse .auto;
98839873 var comptime_bits: u32 = 0;
98849874 for (block.params.items(.ty), block.params.items(.is_comptime), 0..) |param_ty_ip, param_is_comptime, i| {
98859875 const param_ty = Type.fromInterned(param_ty_ip);
......@@ -9897,11 +9887,11 @@ fn funcCommon(
98979887 }
98989888 const this_generic = param_ty.isGenericPoison();
98999889 is_generic = is_generic or this_generic;
9900 if (param_is_comptime and !target_util.fnCallConvAllowsZigTypes(cc_resolved)) {
9901 return sema.fail(block, param_src, "comptime parameters not allowed in function with calling convention '{s}'", .{@tagName(cc_resolved)});
9890 if (param_is_comptime and !target_util.fnCallConvAllowsZigTypes(cc)) {
9891 return sema.fail(block, param_src, "comptime parameters not allowed in function with calling convention '{s}'", .{@tagName(cc)});
99029892 }
9903 if (this_generic and !sema.no_partial_func_ty and !target_util.fnCallConvAllowsZigTypes(cc_resolved)) {
9904 return sema.fail(block, param_src, "generic parameters not allowed in function with calling convention '{s}'", .{@tagName(cc_resolved)});
9893 if (this_generic and !sema.no_partial_func_ty and !target_util.fnCallConvAllowsZigTypes(cc)) {
9894 return sema.fail(block, param_src, "generic parameters not allowed in function with calling convention '{s}'", .{@tagName(cc)});
99059895 }
99069896 if (!param_ty.isValidParamType(zcu)) {
99079897 const opaque_str = if (param_ty.zigTypeTag(zcu) == .@"opaque") "opaque " else "";
......@@ -9909,10 +9899,10 @@ fn funcCommon(
99099899 opaque_str, param_ty.fmt(pt),
99109900 });
99119901 }
9912 if (!this_generic and !target_util.fnCallConvAllowsZigTypes(cc_resolved) and !try sema.validateExternType(param_ty, .param_ty)) {
9902 if (!this_generic and !target_util.fnCallConvAllowsZigTypes(cc) and !try sema.validateExternType(param_ty, .param_ty)) {
99139903 const msg = msg: {
99149904 const msg = try sema.errMsg(param_src, "parameter of type '{}' not allowed in function with calling convention '{s}'", .{
9915 param_ty.fmt(pt), @tagName(cc_resolved),
9905 param_ty.fmt(pt), @tagName(cc),
99169906 });
99179907 errdefer msg.destroy(sema.gpa);
99189908
......@@ -9942,13 +9932,13 @@ fn funcCommon(
99429932 {
99439933 return sema.fail(block, param_src, "non-pointer parameter declared noalias", .{});
99449934 }
9945 switch (cc_resolved) {
9935 switch (cc) {
99469936 .x86_64_interrupt, .x86_interrupt => {
99479937 const err_code_size = target.ptrBitWidth();
99489938 switch (i) {
9949 0 => if (param_ty.zigTypeTag(zcu) != .pointer) return sema.fail(block, param_src, "first parameter of function with '{s}' calling convention must be a pointer type", .{@tagName(cc_resolved)}),
9950 1 => if (param_ty.bitSize(zcu) != err_code_size) return sema.fail(block, param_src, "second parameter of function with '{s}' calling convention must be a {d}-bit integer", .{ @tagName(cc_resolved), err_code_size }),
9951 else => return sema.fail(block, param_src, "'{s}' calling convention supports up to 2 parameters, found {d}", .{ @tagName(cc_resolved), i + 1 }),
9939 0 => if (param_ty.zigTypeTag(zcu) != .pointer) return sema.fail(block, param_src, "first parameter of function with '{s}' calling convention must be a pointer type", .{@tagName(cc)}),
9940 1 => if (param_ty.bitSize(zcu) != err_code_size) return sema.fail(block, param_src, "second parameter of function with '{s}' calling convention must be a {d}-bit integer", .{ @tagName(cc), err_code_size }),
9941 else => return sema.fail(block, param_src, "'{s}' calling convention supports up to 2 parameters, found {d}", .{ @tagName(cc), i + 1 }),
99529942 }
99539943 },
99549944 .arm_interrupt,
......@@ -9960,7 +9950,7 @@ fn funcCommon(
99609950 .csky_interrupt,
99619951 .m68k_interrupt,
99629952 .avr_signal,
9963 => return sema.fail(block, param_src, "parameters are not allowed with '{s}' calling convention", .{@tagName(cc_resolved)}),
9953 => return sema.fail(block, param_src, "parameters are not allowed with '{s}' calling convention", .{@tagName(cc)}),
99649954 else => {},
99659955 }
99669956 }
......@@ -9975,9 +9965,6 @@ fn funcCommon(
99759965 assert(has_body);
99769966 assert(!is_generic);
99779967 assert(comptime_bits == 0);
9978 assert(cc != null);
9979 assert(section != .generic);
9980 assert(address_space != null);
99819968 assert(!var_args);
99829969 if (inferred_error_set) {
99839970 try sema.validateErrorUnionPayloadType(block, bare_return_type, ret_ty_src);
......@@ -9986,13 +9973,6 @@ fn funcCommon(
99869973 .param_types = param_types,
99879974 .noalias_bits = noalias_bits,
99889975 .bare_return_type = bare_return_type.toIntern(),
9989 .cc = cc_resolved,
9990 .alignment = alignment.?,
9991 .section = switch (section) {
9992 .generic => unreachable,
9993 .default => .none,
9994 .explicit => |x| x.toOptional(),
9995 },
99969976 .is_noinline = is_noinline,
99979977 .inferred_error_set = inferred_error_set,
99989978 .generic_owner = sema.generic_owner,
......@@ -10006,7 +9986,7 @@ fn funcCommon(
100069986 ret_poison,
100079987 bare_return_type,
100089988 ret_ty_src,
10009 cc_resolved,
9989 cc,
100109990 is_source_decl,
100119991 ret_ty_requires_comptime,
100129992 func_inst,
......@@ -10017,12 +9997,6 @@ fn funcCommon(
100179997 );
100189998 }
100199999
10020 const section_name: InternPool.OptionalNullTerminatedString = switch (section) {
10021 .generic => .none,
10022 .default => .none,
10023 .explicit => |name| name.toOptional(),
10024 };
10025
1002610000 if (inferred_error_set) {
1002710001 assert(!is_extern);
1002810002 assert(has_body);
......@@ -10036,9 +10010,6 @@ fn funcCommon(
1003610010 .comptime_bits = comptime_bits,
1003710011 .bare_return_type = bare_return_type.toIntern(),
1003810012 .cc = cc,
10039 .alignment = alignment,
10040 .section_is_generic = section == .generic,
10041 .addrspace_is_generic = address_space == null,
1004210013 .is_var_args = var_args,
1004310014 .is_generic = final_is_generic,
1004410015 .is_noinline = is_noinline,
......@@ -10049,13 +10020,6 @@ fn funcCommon(
1004910020 .lbrace_column = @as(u16, @truncate(src_locs.columns)),
1005010021 .rbrace_column = @as(u16, @truncate(src_locs.columns >> 16)),
1005110022 });
10052 // func_decl functions take ownership of the `Nav` of Sema'a owner `Cau`.
10053 ip.resolveNavValue(sema.getOwnerCauNav(), .{
10054 .val = func_index,
10055 .alignment = alignment orelse .none,
10056 .@"linksection" = section_name,
10057 .@"addrspace" = address_space orelse .generic,
10058 });
1005910023 return finishFunc(
1006010024 sema,
1006110025 block,
......@@ -10064,7 +10028,7 @@ fn funcCommon(
1006410028 ret_poison,
1006510029 bare_return_type,
1006610030 ret_ty_src,
10067 cc_resolved,
10031 cc,
1006810032 is_source_decl,
1006910033 ret_ty_requires_comptime,
1007010034 func_inst,
......@@ -10081,8 +10045,6 @@ fn funcCommon(
1008110045 .comptime_bits = comptime_bits,
1008210046 .return_type = bare_return_type.toIntern(),
1008310047 .cc = cc,
10084 .section_is_generic = section == .generic,
10085 .addrspace_is_generic = address_space == null,
1008610048 .is_var_args = var_args,
1008710049 .is_generic = final_is_generic,
1008810050 .is_noinline = is_noinline,
......@@ -10090,38 +10052,20 @@ fn funcCommon(
1009010052
1009110053 if (is_extern) {
1009210054 assert(comptime_bits == 0);
10093 assert(cc != null);
10094 assert(alignment != null);
10095 assert(section != .generic);
10096 assert(address_space != null);
1009710055 assert(!is_generic);
1009810056 if (opt_lib_name) |lib_name| try sema.handleExternLibName(block, block.src(.{
1009910057 .node_offset_lib_name = src_node_offset,
1010010058 }), lib_name);
10101 const func_index = try pt.getExtern(.{
10102 .name = sema.getOwnerCauNavName(),
10103 .ty = func_ty,
10104 .lib_name = try ip.getOrPutStringOpt(gpa, pt.tid, opt_lib_name, .no_embedded_nulls),
10105 .is_const = true,
10106 .is_threadlocal = false,
10107 .is_weak_linkage = false,
10108 .is_dll_import = false,
10109 .alignment = alignment orelse .none,
10110 .@"addrspace" = address_space orelse .generic,
10111 .zir_index = sema.getOwnerCauDeclInst(), // `declaration` instruction
10112 .owner_nav = undefined, // ignored by `getExtern`
10113 });
10114 // Note that unlike function declaration, extern functions don't touch the
10115 // Sema's owner Cau's owner Nav. The alignment etc were passed above.
10059 const extern_func_index = try sema.resolveExternDecl(block, .fromInterned(func_ty), opt_lib_name, true, false);
1011610060 return finishFunc(
1011710061 sema,
1011810062 block,
10119 func_index,
10063 extern_func_index,
1012010064 func_ty,
1012110065 ret_poison,
1012210066 bare_return_type,
1012310067 ret_ty_src,
10124 cc_resolved,
10068 cc,
1012510069 is_source_decl,
1012610070 ret_ty_requires_comptime,
1012710071 func_inst,
......@@ -10144,13 +10088,6 @@ fn funcCommon(
1014410088 .lbrace_column = @as(u16, @truncate(src_locs.columns)),
1014510089 .rbrace_column = @as(u16, @truncate(src_locs.columns >> 16)),
1014610090 });
10147 // func_decl functions take ownership of the `Nav` of Sema'a owner `Cau`.
10148 ip.resolveNavValue(sema.getOwnerCauNav(), .{
10149 .val = func_index,
10150 .alignment = alignment orelse .none,
10151 .@"linksection" = section_name,
10152 .@"addrspace" = address_space orelse .generic,
10153 });
1015410091 return finishFunc(
1015510092 sema,
1015610093 block,
......@@ -10159,7 +10096,7 @@ fn funcCommon(
1015910096 ret_poison,
1016010097 bare_return_type,
1016110098 ret_ty_src,
10162 cc_resolved,
10099 cc,
1016310100 is_source_decl,
1016410101 ret_ty_requires_comptime,
1016510102 func_inst,
......@@ -10178,7 +10115,7 @@ fn funcCommon(
1017810115 ret_poison,
1017910116 bare_return_type,
1018010117 ret_ty_src,
10181 cc_resolved,
10118 cc,
1018210119 is_source_decl,
1018310120 ret_ty_requires_comptime,
1018410121 func_inst,
......@@ -26829,52 +26766,8 @@ fn zirVarExtended(
2682926766 try sema.validateVarType(block, ty_src, var_ty, small.is_extern);
2683026767
2683126768 if (small.is_extern) {
26832 // We need to resolve the alignment and addrspace early.
26833 // Keep in sync with logic in `Zcu.PerThread.semaCau`.
26834 const align_src = block.src(.{ .node_offset_var_decl_align = 0 });
26835 const addrspace_src = block.src(.{ .node_offset_var_decl_addrspace = 0 });
26836
26837 const decl_inst, const decl_bodies = decl: {
26838 const decl_inst = sema.getOwnerCauDeclInst().resolve(ip) orelse return error.AnalysisFail;
26839 const zir_decl, const extra_end = sema.code.getDeclaration(decl_inst);
26840 break :decl .{ decl_inst, zir_decl.getBodies(extra_end, sema.code) };
26841 };
26842
26843 const alignment: InternPool.Alignment = a: {
26844 const align_body = decl_bodies.align_body orelse break :a .none;
26845 const align_ref = try sema.resolveInlineBody(block, align_body, decl_inst);
26846 break :a try sema.analyzeAsAlign(block, align_src, align_ref);
26847 };
26848
26849 const @"addrspace": std.builtin.AddressSpace = as: {
26850 const addrspace_ctx: Sema.AddressSpaceContext = switch (ip.indexToKey(var_ty.toIntern())) {
26851 .func_type => .function,
26852 else => .variable,
26853 };
26854 const target = zcu.getTarget();
26855 const addrspace_body = decl_bodies.addrspace_body orelse break :as switch (addrspace_ctx) {
26856 .function => target_util.defaultAddressSpace(target, .function),
26857 .variable => target_util.defaultAddressSpace(target, .global_mutable),
26858 .constant => target_util.defaultAddressSpace(target, .global_constant),
26859 else => unreachable,
26860 };
26861 const addrspace_ref = try sema.resolveInlineBody(block, addrspace_body, decl_inst);
26862 break :as try sema.analyzeAsAddressSpace(block, addrspace_src, addrspace_ref, addrspace_ctx);
26863 };
26864
26865 return Air.internedToRef(try pt.getExtern(.{
26866 .name = sema.getOwnerCauNavName(),
26867 .ty = var_ty.toIntern(),
26868 .lib_name = try ip.getOrPutStringOpt(sema.gpa, pt.tid, lib_name, .no_embedded_nulls),
26869 .is_const = small.is_const,
26870 .is_threadlocal = small.is_threadlocal,
26871 .is_weak_linkage = false,
26872 .is_dll_import = false,
26873 .alignment = alignment,
26874 .@"addrspace" = @"addrspace",
26875 .zir_index = sema.getOwnerCauDeclInst(), // `declaration` instruction
26876 .owner_nav = undefined, // ignored by `getExtern`
26877 }));
26769 const extern_val = try sema.resolveExternDecl(block, var_ty, lib_name, small.is_const, small.is_threadlocal);
26770 return Air.internedToRef(extern_val);
2687826771 }
2687926772 assert(!small.is_const); // non-const non-extern variable is not legal
2688026773 return Air.internedToRef(try pt.intern(.{ .variable = .{
......@@ -26887,6 +26780,66 @@ fn zirVarExtended(
2688726780 } }));
2688826781}
2688926782
26783fn resolveExternDecl(
26784 sema: *Sema,
26785 block: *Block,
26786 ty: Type,
26787 opt_lib_name: ?[]const u8,
26788 is_const: bool,
26789 is_threadlocal: bool,
26790) CompileError!InternPool.Index {
26791 const pt = sema.pt;
26792 const zcu = pt.zcu;
26793 const ip = &zcu.intern_pool;
26794
26795 // We need to resolve the alignment and addrspace early.
26796 // Keep in sync with logic in `Zcu.PerThread.semaCau`.
26797 const align_src = block.src(.{ .node_offset_var_decl_align = 0 });
26798 const addrspace_src = block.src(.{ .node_offset_var_decl_addrspace = 0 });
26799
26800 const decl_inst, const decl_bodies = decl: {
26801 const decl_inst = sema.getOwnerCauDeclInst().resolve(ip) orelse return error.AnalysisFail;
26802 const zir_decl, const extra_end = sema.code.getDeclaration(decl_inst);
26803 break :decl .{ decl_inst, zir_decl.getBodies(extra_end, sema.code) };
26804 };
26805
26806 const alignment: InternPool.Alignment = a: {
26807 const align_body = decl_bodies.align_body orelse break :a .none;
26808 const align_ref = try sema.resolveInlineBody(block, align_body, decl_inst);
26809 break :a try sema.analyzeAsAlign(block, align_src, align_ref);
26810 };
26811
26812 const @"addrspace": std.builtin.AddressSpace = as: {
26813 const addrspace_ctx: Sema.AddressSpaceContext = switch (ip.indexToKey(ty.toIntern())) {
26814 .func_type => .function,
26815 else => .variable,
26816 };
26817 const target = zcu.getTarget();
26818 const addrspace_body = decl_bodies.addrspace_body orelse break :as switch (addrspace_ctx) {
26819 .function => target_util.defaultAddressSpace(target, .function),
26820 .variable => target_util.defaultAddressSpace(target, .global_mutable),
26821 .constant => target_util.defaultAddressSpace(target, .global_constant),
26822 else => unreachable,
26823 };
26824 const addrspace_ref = try sema.resolveInlineBody(block, addrspace_body, decl_inst);
26825 break :as try sema.analyzeAsAddressSpace(block, addrspace_src, addrspace_ref, addrspace_ctx);
26826 };
26827
26828 return pt.getExtern(.{
26829 .name = sema.getOwnerCauNavName(),
26830 .ty = ty.toIntern(),
26831 .lib_name = try ip.getOrPutStringOpt(sema.gpa, pt.tid, opt_lib_name, .no_embedded_nulls),
26832 .is_const = is_const,
26833 .is_threadlocal = is_threadlocal,
26834 .is_weak_linkage = false,
26835 .is_dll_import = false,
26836 .alignment = alignment,
26837 .@"addrspace" = @"addrspace",
26838 .zir_index = sema.getOwnerCauDeclInst(), // `declaration` instruction
26839 .owner_nav = undefined, // ignored by `getExtern`
26840 });
26841}
26842
2689026843fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
2689126844 const tracy = trace(@src());
2689226845 defer tracy.end();
......@@ -26898,9 +26851,6 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2689826851 const extra = sema.code.extraData(Zir.Inst.FuncFancy, inst_data.payload_index);
2689926852 const target = zcu.getTarget();
2690026853
26901 const align_src = block.src(.{ .node_offset_fn_type_align = inst_data.src_node });
26902 const addrspace_src = block.src(.{ .node_offset_fn_type_addrspace = inst_data.src_node });
26903 const section_src = block.src(.{ .node_offset_fn_type_section = inst_data.src_node });
2690426854 const cc_src = block.src(.{ .node_offset_fn_type_cc = inst_data.src_node });
2690526855 const ret_src = block.src(.{ .node_offset_fn_type_ret_ty = inst_data.src_node });
2690626856 const has_body = extra.data.body_len != 0;
......@@ -26914,112 +26864,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2691426864 break :blk lib_name;
2691526865 } else null;
2691626866
26917 if (has_body and
26918 (extra.data.bits.has_align_body or extra.data.bits.has_align_ref) and
26919 !target_util.supportsFunctionAlignment(target))
26920 {
26921 return sema.fail(block, align_src, "target does not support function alignment", .{});
26922 }
26923
26924 const @"align": ?Alignment = if (extra.data.bits.has_align_body) blk: {
26925 const body_len = sema.code.extra[extra_index];
26926 extra_index += 1;
26927 const body = sema.code.bodySlice(extra_index, body_len);
26928 extra_index += body.len;
26929
26930 const val = try sema.resolveGenericBody(block, align_src, body, inst, Type.u29, .{
26931 .needed_comptime_reason = "alignment must be comptime-known",
26932 });
26933 if (val.isGenericPoison()) {
26934 break :blk null;
26935 }
26936 break :blk try sema.validateAlign(block, align_src, try val.toUnsignedIntSema(pt));
26937 } else if (extra.data.bits.has_align_ref) blk: {
26938 const align_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);
26939 extra_index += 1;
26940 const uncoerced_align = sema.resolveInst(align_ref) catch |err| switch (err) {
26941 error.GenericPoison => break :blk null,
26942 else => |e| return e,
26943 };
26944 const coerced_align = sema.coerce(block, Type.u29, uncoerced_align, align_src) catch |err| switch (err) {
26945 error.GenericPoison => break :blk null,
26946 else => |e| return e,
26947 };
26948 const align_val = sema.resolveConstDefinedValue(block, align_src, coerced_align, .{
26949 .needed_comptime_reason = "alignment must be comptime-known",
26950 }) catch |err| switch (err) {
26951 error.GenericPoison => break :blk null,
26952 else => |e| return e,
26953 };
26954 break :blk try sema.validateAlign(block, align_src, try align_val.toUnsignedIntSema(pt));
26955 } else .none;
26956
26957 const @"addrspace": ?std.builtin.AddressSpace = if (extra.data.bits.has_addrspace_body) blk: {
26958 const body_len = sema.code.extra[extra_index];
26959 extra_index += 1;
26960 const body = sema.code.bodySlice(extra_index, body_len);
26961 extra_index += body.len;
26962
26963 const addrspace_ty = try sema.getBuiltinType("AddressSpace");
26964 const val = try sema.resolveGenericBody(block, addrspace_src, body, inst, addrspace_ty, .{
26965 .needed_comptime_reason = "addrspace must be comptime-known",
26966 });
26967 if (val.isGenericPoison()) {
26968 break :blk null;
26969 }
26970 break :blk zcu.toEnum(std.builtin.AddressSpace, val);
26971 } else if (extra.data.bits.has_addrspace_ref) blk: {
26972 const addrspace_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);
26973 extra_index += 1;
26974 const addrspace_ty = try sema.getBuiltinType("AddressSpace");
26975 const uncoerced_addrspace = sema.resolveInst(addrspace_ref) catch |err| switch (err) {
26976 error.GenericPoison => break :blk null,
26977 else => |e| return e,
26978 };
26979 const coerced_addrspace = sema.coerce(block, addrspace_ty, uncoerced_addrspace, addrspace_src) catch |err| switch (err) {
26980 error.GenericPoison => break :blk null,
26981 else => |e| return e,
26982 };
26983 const addrspace_val = sema.resolveConstDefinedValue(block, addrspace_src, coerced_addrspace, .{
26984 .needed_comptime_reason = "addrspace must be comptime-known",
26985 }) catch |err| switch (err) {
26986 error.GenericPoison => break :blk null,
26987 else => |e| return e,
26988 };
26989 break :blk zcu.toEnum(std.builtin.AddressSpace, addrspace_val);
26990 } else target_util.defaultAddressSpace(target, .function);
26991
26992 const section: Section = if (extra.data.bits.has_section_body) blk: {
26993 const body_len = sema.code.extra[extra_index];
26994 extra_index += 1;
26995 const body = sema.code.bodySlice(extra_index, body_len);
26996 extra_index += body.len;
26997
26998 const ty = Type.slice_const_u8;
26999 const val = try sema.resolveGenericBody(block, section_src, body, inst, ty, .{
27000 .needed_comptime_reason = "linksection must be comptime-known",
27001 });
27002 if (val.isGenericPoison()) {
27003 break :blk .generic;
27004 }
27005 break :blk .{ .explicit = try sema.sliceToIpString(block, section_src, val, .{
27006 .needed_comptime_reason = "linksection must be comptime-known",
27007 }) };
27008 } else if (extra.data.bits.has_section_ref) blk: {
27009 const section_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);
27010 extra_index += 1;
27011 const section_name = sema.resolveConstStringIntern(block, section_src, section_ref, .{
27012 .needed_comptime_reason = "linksection must be comptime-known",
27013 }) catch |err| switch (err) {
27014 error.GenericPoison => {
27015 break :blk .generic;
27016 },
27017 else => |e| return e,
27018 };
27019 break :blk .{ .explicit = section_name };
27020 } else .default;
27021
27022 const cc: ?std.builtin.CallingConvention = if (extra.data.bits.has_cc_body) blk: {
26867 const cc: std.builtin.CallingConvention = if (extra.data.bits.has_cc_body) blk: {
2702326868 const body_len = sema.code.extra[extra_index];
2702426869 extra_index += 1;
2702526870 const body = sema.code.bodySlice(extra_index, body_len);
......@@ -27029,28 +26874,16 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2702926874 const val = try sema.resolveGenericBody(block, cc_src, body, inst, cc_ty, .{
2703026875 .needed_comptime_reason = "calling convention must be comptime-known",
2703126876 });
27032 if (val.isGenericPoison()) {
27033 break :blk null;
27034 }
2703526877 break :blk try sema.analyzeValueAsCallconv(block, cc_src, val);
2703626878 } else if (extra.data.bits.has_cc_ref) blk: {
2703726879 const cc_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);
2703826880 extra_index += 1;
2703926881 const cc_ty = try sema.getBuiltinType("CallingConvention");
27040 const uncoerced_cc = sema.resolveInst(cc_ref) catch |err| switch (err) {
27041 error.GenericPoison => break :blk null,
27042 else => |e| return e,
27043 };
27044 const coerced_cc = sema.coerce(block, cc_ty, uncoerced_cc, cc_src) catch |err| switch (err) {
27045 error.GenericPoison => break :blk null,
27046 else => |e| return e,
27047 };
27048 const cc_val = sema.resolveConstDefinedValue(block, cc_src, coerced_cc, .{
26882 const uncoerced_cc = try sema.resolveInst(cc_ref);
26883 const coerced_cc = try sema.coerce(block, cc_ty, uncoerced_cc, cc_src);
26884 const cc_val = try sema.resolveConstDefinedValue(block, cc_src, coerced_cc, .{
2704926885 .needed_comptime_reason = "calling convention must be comptime-known",
27050 }) catch |err| switch (err) {
27051 error.GenericPoison => break :blk null,
27052 else => |e| return e,
27053 };
26886 });
2705426887 break :blk try sema.analyzeValueAsCallconv(block, cc_src, cc_val);
2705526888 } else cc: {
2705626889 if (has_body) {
......@@ -27132,9 +26965,6 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2713226965 block,
2713326966 inst_data.src_node,
2713426967 inst,
27135 @"align",
27136 @"addrspace",
27137 section,
2713826968 cc,
2713926969 ret_ty,
2714026970 is_var_args,
src/Value.zig-2
......@@ -3710,8 +3710,6 @@ pub fn makeBool(x: bool) Value {
37103710 return if (x) Value.true else Value.false;
37113711}
37123712
3713pub const RuntimeIndex = InternPool.RuntimeIndex;
3714
37153713/// `parent_ptr` must be a single-pointer to some optional.
37163714/// Returns a pointer to the payload of the optional.
37173715/// May perform type resolution.
src/Zcu.zig+24-6
......@@ -836,20 +836,38 @@ pub const SrcLoc = struct {
836836 .node_offset_var_decl_align => |node_off| {
837837 const tree = try src_loc.file_scope.getTree(gpa);
838838 const node = src_loc.relativeToNodeIndex(node_off);
839 const full = tree.fullVarDecl(node).?;
840 return tree.nodeToSpan(full.ast.align_node);
839 var buf: [1]Ast.Node.Index = undefined;
840 const align_node = if (tree.fullVarDecl(node)) |v|
841 v.ast.align_node
842 else if (tree.fullFnProto(&buf, node)) |f|
843 f.ast.align_expr
844 else
845 unreachable;
846 return tree.nodeToSpan(align_node);
841847 },
842848 .node_offset_var_decl_section => |node_off| {
843849 const tree = try src_loc.file_scope.getTree(gpa);
844850 const node = src_loc.relativeToNodeIndex(node_off);
845 const full = tree.fullVarDecl(node).?;
846 return tree.nodeToSpan(full.ast.section_node);
851 var buf: [1]Ast.Node.Index = undefined;
852 const section_node = if (tree.fullVarDecl(node)) |v|
853 v.ast.section_node
854 else if (tree.fullFnProto(&buf, node)) |f|
855 f.ast.section_expr
856 else
857 unreachable;
858 return tree.nodeToSpan(section_node);
847859 },
848860 .node_offset_var_decl_addrspace => |node_off| {
849861 const tree = try src_loc.file_scope.getTree(gpa);
850862 const node = src_loc.relativeToNodeIndex(node_off);
851 const full = tree.fullVarDecl(node).?;
852 return tree.nodeToSpan(full.ast.addrspace_node);
863 var buf: [1]Ast.Node.Index = undefined;
864 const addrspace_node = if (tree.fullVarDecl(node)) |v|
865 v.ast.addrspace_node
866 else if (tree.fullFnProto(&buf, node)) |f|
867 f.ast.addrspace_expr
868 else
869 unreachable;
870 return tree.nodeToSpan(addrspace_node);
853871 },
854872 .node_offset_var_decl_init => |node_off| {
855873 const tree = try src_loc.file_scope.getTree(gpa);
src/Zcu/PerThread.zig+70-54
......@@ -1314,68 +1314,84 @@ fn semaCau(pt: Zcu.PerThread, cau_index: InternPool.Cau.Index) !SemaCauResult {
13141314 };
13151315 }
13161316
1317 const nav_already_populated, const queue_linker_work = switch (ip.indexToKey(decl_val.toIntern())) {
1318 .func => |f| .{ f.owner_nav == nav_index, true },
1319 .variable => |v| .{ false, v.owner_nav == nav_index },
1320 .@"extern" => .{ false, false },
1321 else => .{ false, true },
1317 const queue_linker_work, const is_owned_fn = switch (ip.indexToKey(decl_val.toIntern())) {
1318 .func => |f| .{ true, f.owner_nav == nav_index }, // note that this lets function aliases reach codegen
1319 .variable => |v| .{ v.owner_nav == nav_index, false },
1320 .@"extern" => |e| .{ false, Type.fromInterned(e.ty).zigTypeTag(zcu) == .@"fn" },
1321 else => .{ true, false },
13221322 };
13231323
1324 if (nav_already_populated) {
1325 // This is a function declaration.
1326 // Logic in `Sema.funcCommon` has already populated the `Nav` for us.
1327 assert(ip.getNav(nav_index).status.resolved.val == decl_val.toIntern());
1328 } else {
1329 // Keep in sync with logic in `Sema.zirVarExtended`.
1330 const alignment: InternPool.Alignment = a: {
1331 const align_body = decl_bodies.align_body orelse break :a .none;
1332 const align_ref = try sema.resolveInlineBody(&block, align_body, inst_info.inst);
1333 break :a try sema.analyzeAsAlign(&block, align_src, align_ref);
1334 };
1324 // Keep in sync with logic in `Sema.zirVarExtended`.
1325 const alignment: InternPool.Alignment = a: {
1326 const align_body = decl_bodies.align_body orelse break :a .none;
1327 const align_ref = try sema.resolveInlineBody(&block, align_body, inst_info.inst);
1328 break :a try sema.analyzeAsAlign(&block, align_src, align_ref);
1329 };
13351330
1336 const @"linksection": InternPool.OptionalNullTerminatedString = ls: {
1337 const linksection_body = decl_bodies.linksection_body orelse break :ls .none;
1338 const linksection_ref = try sema.resolveInlineBody(&block, linksection_body, inst_info.inst);
1339 const bytes = try sema.toConstString(&block, section_src, linksection_ref, .{
1340 .needed_comptime_reason = "linksection must be comptime-known",
1341 });
1342 if (std.mem.indexOfScalar(u8, bytes, 0) != null) {
1343 return sema.fail(&block, section_src, "linksection cannot contain null bytes", .{});
1344 } else if (bytes.len == 0) {
1345 return sema.fail(&block, section_src, "linksection cannot be empty", .{});
1346 }
1347 break :ls try ip.getOrPutStringOpt(gpa, pt.tid, bytes, .no_embedded_nulls);
1348 };
1331 const @"linksection": InternPool.OptionalNullTerminatedString = ls: {
1332 const linksection_body = decl_bodies.linksection_body orelse break :ls .none;
1333 const linksection_ref = try sema.resolveInlineBody(&block, linksection_body, inst_info.inst);
1334 const bytes = try sema.toConstString(&block, section_src, linksection_ref, .{
1335 .needed_comptime_reason = "linksection must be comptime-known",
1336 });
1337 if (std.mem.indexOfScalar(u8, bytes, 0) != null) {
1338 return sema.fail(&block, section_src, "linksection cannot contain null bytes", .{});
1339 } else if (bytes.len == 0) {
1340 return sema.fail(&block, section_src, "linksection cannot be empty", .{});
1341 }
1342 break :ls try ip.getOrPutStringOpt(gpa, pt.tid, bytes, .no_embedded_nulls);
1343 };
13491344
1350 const @"addrspace": std.builtin.AddressSpace = as: {
1351 const addrspace_ctx: Sema.AddressSpaceContext = switch (ip.indexToKey(decl_val.toIntern())) {
1352 .func => .function,
1353 .variable => .variable,
1354 .@"extern" => |e| if (ip.indexToKey(e.ty) == .func_type)
1355 .function
1356 else
1357 .variable,
1358 else => .constant,
1359 };
1360 const target = zcu.getTarget();
1361 const addrspace_body = decl_bodies.addrspace_body orelse break :as switch (addrspace_ctx) {
1362 .function => target_util.defaultAddressSpace(target, .function),
1363 .variable => target_util.defaultAddressSpace(target, .global_mutable),
1364 .constant => target_util.defaultAddressSpace(target, .global_constant),
1365 else => unreachable,
1366 };
1367 const addrspace_ref = try sema.resolveInlineBody(&block, addrspace_body, inst_info.inst);
1368 break :as try sema.analyzeAsAddressSpace(&block, addrspace_src, addrspace_ref, addrspace_ctx);
1345 const @"addrspace": std.builtin.AddressSpace = as: {
1346 const addrspace_ctx: Sema.AddressSpaceContext = switch (ip.indexToKey(decl_val.toIntern())) {
1347 .func => .function,
1348 .variable => .variable,
1349 .@"extern" => |e| if (ip.indexToKey(e.ty) == .func_type)
1350 .function
1351 else
1352 .variable,
1353 else => .constant,
13691354 };
1355 const target = zcu.getTarget();
1356 const addrspace_body = decl_bodies.addrspace_body orelse break :as switch (addrspace_ctx) {
1357 .function => target_util.defaultAddressSpace(target, .function),
1358 .variable => target_util.defaultAddressSpace(target, .global_mutable),
1359 .constant => target_util.defaultAddressSpace(target, .global_constant),
1360 else => unreachable,
1361 };
1362 const addrspace_ref = try sema.resolveInlineBody(&block, addrspace_body, inst_info.inst);
1363 break :as try sema.analyzeAsAddressSpace(&block, addrspace_src, addrspace_ref, addrspace_ctx);
1364 };
13701365
1371 ip.resolveNavValue(nav_index, .{
1372 .val = decl_val.toIntern(),
1373 .alignment = alignment,
1374 .@"linksection" = @"linksection",
1375 .@"addrspace" = @"addrspace",
1376 });
1366 if (is_owned_fn) {
1367 // linksection etc are legal, except some targets do not support function alignment.
1368 if (decl_bodies.align_body != null and !target_util.supportsFunctionAlignment(zcu.getTarget())) {
1369 return sema.fail(&block, align_src, "target does not support function alignment", .{});
1370 }
1371 } else if (try decl_ty.comptimeOnlySema(pt)) {
1372 // alignment, linksection, addrspace annotations are not allowed for comptime-only types.
1373 const reason: []const u8 = switch (ip.indexToKey(decl_val.toIntern())) {
1374 .func => "function alias", // slightly clearer message, since you *can* specify these on function *declarations*
1375 else => "comptime-only type",
1376 };
1377 if (decl_bodies.align_body != null) {
1378 return sema.fail(&block, align_src, "cannot specify alignment of {s}", .{reason});
1379 }
1380 if (decl_bodies.linksection_body != null) {
1381 return sema.fail(&block, section_src, "cannot specify linksection of {s}", .{reason});
1382 }
1383 if (decl_bodies.addrspace_body != null) {
1384 return sema.fail(&block, addrspace_src, "cannot specify addrspace of {s}", .{reason});
1385 }
13771386 }
13781387
1388 ip.resolveNavValue(nav_index, .{
1389 .val = decl_val.toIntern(),
1390 .alignment = alignment,
1391 .@"linksection" = @"linksection",
1392 .@"addrspace" = @"addrspace",
1393 });
1394
13791395 // Mark the `Cau` as completed before evaluating the export!
13801396 assert(zcu.analysis_in_progress.swapRemove(anal_unit));
13811397
src/print_zir.zig-54
......@@ -2349,12 +2349,6 @@ const Writer = struct {
23492349 false,
23502350 false,
23512351
2352 .none,
2353 &.{},
2354 .none,
2355 &.{},
2356 .none,
2357 &.{},
23582352 .none,
23592353 &.{},
23602354 ret_ty_ref,
......@@ -2372,12 +2366,6 @@ const Writer = struct {
23722366 const extra = self.code.extraData(Zir.Inst.FuncFancy, inst_data.payload_index);
23732367
23742368 var extra_index: usize = extra.end;
2375 var align_ref: Zir.Inst.Ref = .none;
2376 var align_body: []const Zir.Inst.Index = &.{};
2377 var addrspace_ref: Zir.Inst.Ref = .none;
2378 var addrspace_body: []const Zir.Inst.Index = &.{};
2379 var section_ref: Zir.Inst.Ref = .none;
2380 var section_body: []const Zir.Inst.Index = &.{};
23812369 var cc_ref: Zir.Inst.Ref = .none;
23822370 var cc_body: []const Zir.Inst.Index = &.{};
23832371 var ret_ty_ref: Zir.Inst.Ref = .none;
......@@ -2390,33 +2378,6 @@ const Writer = struct {
23902378 }
23912379 try self.writeFlag(stream, "test, ", extra.data.bits.is_test);
23922380
2393 if (extra.data.bits.has_align_body) {
2394 const body_len = self.code.extra[extra_index];
2395 extra_index += 1;
2396 align_body = self.code.bodySlice(extra_index, body_len);
2397 extra_index += align_body.len;
2398 } else if (extra.data.bits.has_align_ref) {
2399 align_ref = @as(Zir.Inst.Ref, @enumFromInt(self.code.extra[extra_index]));
2400 extra_index += 1;
2401 }
2402 if (extra.data.bits.has_addrspace_body) {
2403 const body_len = self.code.extra[extra_index];
2404 extra_index += 1;
2405 addrspace_body = self.code.bodySlice(extra_index, body_len);
2406 extra_index += addrspace_body.len;
2407 } else if (extra.data.bits.has_addrspace_ref) {
2408 addrspace_ref = @as(Zir.Inst.Ref, @enumFromInt(self.code.extra[extra_index]));
2409 extra_index += 1;
2410 }
2411 if (extra.data.bits.has_section_body) {
2412 const body_len = self.code.extra[extra_index];
2413 extra_index += 1;
2414 section_body = self.code.bodySlice(extra_index, body_len);
2415 extra_index += section_body.len;
2416 } else if (extra.data.bits.has_section_ref) {
2417 section_ref = @as(Zir.Inst.Ref, @enumFromInt(self.code.extra[extra_index]));
2418 extra_index += 1;
2419 }
24202381 if (extra.data.bits.has_cc_body) {
24212382 const body_len = self.code.extra[extra_index];
24222383 extra_index += 1;
......@@ -2455,12 +2416,6 @@ const Writer = struct {
24552416 extra.data.bits.is_var_args,
24562417 extra.data.bits.is_extern,
24572418 extra.data.bits.is_noinline,
2458 align_ref,
2459 align_body,
2460 addrspace_ref,
2461 addrspace_body,
2462 section_ref,
2463 section_body,
24642419 cc_ref,
24652420 cc_body,
24662421 ret_ty_ref,
......@@ -2651,12 +2606,6 @@ const Writer = struct {
26512606 var_args: bool,
26522607 is_extern: bool,
26532608 is_noinline: bool,
2654 align_ref: Zir.Inst.Ref,
2655 align_body: []const Zir.Inst.Index,
2656 addrspace_ref: Zir.Inst.Ref,
2657 addrspace_body: []const Zir.Inst.Index,
2658 section_ref: Zir.Inst.Ref,
2659 section_body: []const Zir.Inst.Index,
26602609 cc_ref: Zir.Inst.Ref,
26612610 cc_body: []const Zir.Inst.Index,
26622611 ret_ty_ref: Zir.Inst.Ref,
......@@ -2666,9 +2615,6 @@ const Writer = struct {
26662615 src_locs: Zir.Inst.Func.SrcLocs,
26672616 noalias_bits: u32,
26682617 ) !void {
2669 try self.writeOptionalInstRefOrBody(stream, "align=", align_ref, align_body);
2670 try self.writeOptionalInstRefOrBody(stream, "addrspace=", addrspace_ref, addrspace_body);
2671 try self.writeOptionalInstRefOrBody(stream, "section=", section_ref, section_body);
26722618 try self.writeOptionalInstRefOrBody(stream, "cc=", cc_ref, cc_body);
26732619 try self.writeOptionalInstRefOrBody(stream, "ret_ty=", ret_ty_ref, ret_ty_body);
26742620 try self.writeFlag(stream, "vargs, ", var_args);
test/behavior/align.zig-41
......@@ -361,47 +361,6 @@ fn simple4() align(4) i32 {
361361 return 0x19;
362362}
363363
364test "function align expression depends on generic parameter" {
365 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
366 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
367 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
368
369 // function alignment is a compile error on wasm32/wasm64
370 if (native_arch == .wasm32 or native_arch == .wasm64) return error.SkipZigTest;
371 if (native_arch == .thumb) return error.SkipZigTest;
372
373 const S = struct {
374 fn doTheTest() !void {
375 try expect(foobar(1) == 2);
376 try expect(foobar(4) == 5);
377 try expect(foobar(8) == 9);
378 }
379
380 fn foobar(comptime align_bytes: u8) align(align_bytes) u8 {
381 return align_bytes + 1;
382 }
383 };
384 try S.doTheTest();
385 try comptime S.doTheTest();
386}
387
388test "function callconv expression depends on generic parameter" {
389 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
390
391 const S = struct {
392 fn doTheTest() !void {
393 try expect(foobar(.C, 1) == 2);
394 try expect(foobar(.Unspecified, 2) == 3);
395 }
396
397 fn foobar(comptime cc: std.builtin.CallingConvention, arg: u8) callconv(cc) u8 {
398 return arg + 1;
399 }
400 };
401 try S.doTheTest();
402 try comptime S.doTheTest();
403}
404
405364test "runtime-known array index has best alignment possible" {
406365 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
407366
test/behavior/fn.zig-25
......@@ -637,24 +637,6 @@ test "address of function parameter is consistent in other parameter type" {
637637 S.paramAddrMatch(1, 2);
638638}
639639
640test "address of function parameter is consistent in function align" {
641 switch (builtin.target.cpu.arch) {
642 .wasm32, .wasm64 => return, // function alignment not supported
643 else => {},
644 }
645 const S = struct {
646 fn paramAddrMatch(comptime x: u8) align(if (&x != &x) unreachable else 1) void {}
647 };
648 S.paramAddrMatch(1);
649}
650
651test "address of function parameter is consistent in function callconv" {
652 const S = struct {
653 fn paramAddrMatch(comptime x: u8) callconv(if (&x != &x) unreachable else .auto) void {}
654 };
655 S.paramAddrMatch(1);
656}
657
658640test "address of function parameter is consistent in function return type" {
659641 const S = struct {
660642 fn paramAddrMatch(comptime x: u8) if (&x != &x) unreachable else void {}
......@@ -662,13 +644,6 @@ test "address of function parameter is consistent in function return type" {
662644 S.paramAddrMatch(1);
663645}
664646
665test "address of function parameter is consistent in function addrspace" {
666 const S = struct {
667 fn paramAddrMatch(comptime x: u8) addrspace(if (&x != &x) unreachable else .generic) void {}
668 };
669 S.paramAddrMatch(1);
670}
671
672647test "function parameter self equality" {
673648 const S = struct {
674649 fn equal(x: u32) bool {
test/behavior/type_info.zig+11
......@@ -373,6 +373,17 @@ fn testFunction() !void {
373373 try expect(!foo_ptr_fn_info.pointer.is_allowzero);
374374 try expect(foo_ptr_fn_info.pointer.sentinel == null);
375375
376 // Avoid looking at `typeInfoFooAligned` on targets which don't support function alignment.
377 switch (builtin.target.cpu.arch) {
378 .spirv,
379 .spirv32,
380 .spirv64,
381 .wasm32,
382 .wasm64,
383 => return,
384 else => {},
385 }
386
376387 const aligned_foo_fn_type = @TypeOf(typeInfoFooAligned);
377388 const aligned_foo_fn_info = @typeInfo(aligned_foo_fn_type);
378389 try expect(aligned_foo_fn_info.@"fn".calling_convention.eql(.c));
test/cases/compile_errors/comptime_only_global_align_section_addrspace.zig created+37
......@@ -0,0 +1,37 @@
1fn okay_func() void {}
2
3const a align(64) = okay_func;
4const b addrspace(.generic) = okay_func;
5const c linksection("irrelevant") = okay_func;
6
7const d align(64) = 1.23;
8const e addrspace(.generic) = 1.23;
9const f linksection("irrelevant") = 1.23;
10
11const g: comptime_float align(64) = 1.23;
12const h: comptime_float addrspace(.generic) = 1.23;
13const i: comptime_float linksection("irrelevant") = 1.23;
14
15// zig fmt: off
16comptime { _ = a; }
17comptime { _ = b; }
18comptime { _ = c; }
19comptime { _ = d; }
20comptime { _ = e; }
21comptime { _ = f; }
22comptime { _ = g; }
23comptime { _ = h; }
24comptime { _ = i; }
25// zig fmt: on
26
27// error
28//
29// :3:15: error: cannot specify alignment of function alias
30// :4:20: error: cannot specify addrspace of function alias
31// :5:21: error: cannot specify linksection of function alias
32// :7:15: error: cannot specify alignment of comptime-only type
33// :8:20: error: cannot specify addrspace of comptime-only type
34// :9:21: error: cannot specify linksection of comptime-only type
35// :11:31: error: cannot specify alignment of comptime-only type
36// :12:36: error: cannot specify addrspace of comptime-only type
37// :13:37: error: cannot specify linksection of comptime-only type
test/cases/compile_errors/invalid_variadic_function.zig-5
......@@ -1,13 +1,9 @@
11fn foo(...) void {}
2fn bar(a: anytype, ...) callconv(a) void {}
32inline fn foo2(...) void {}
43
54comptime {
65 _ = foo;
76}
8comptime {
9 _ = bar;
10}
117comptime {
128 _ = foo2;
139}
......@@ -19,4 +15,3 @@ comptime {
1915// :1:1: note: supported calling conventions: 'x86_64_sysv', 'x86_64_win'
2016// :1:1: error: variadic function does not support 'inline' calling convention
2117// :1:1: note: supported calling conventions: 'x86_64_sysv', 'x86_64_win'
22// :2:1: error: generic function cannot be variadic
test/link/macho.zig+7-3
......@@ -937,9 +937,13 @@ fn testLinksection(b: *Build, opts: Options) *Step {
937937 const obj = addObject(b, opts, .{ .name = "main", .zig_source_bytes =
938938 \\export var test_global: u32 linksection("__DATA,__TestGlobal") = undefined;
939939 \\export fn testFn() linksection("__TEXT,__TestFn") callconv(.C) void {
940 \\ testGenericFn("A");
940 \\ TestGenericFn("A").f();
941 \\}
942 \\fn TestGenericFn(comptime suffix: []const u8) type {
943 \\ return struct {
944 \\ fn f() linksection("__TEXT,__TestGenFn" ++ suffix) void {}
945 \\ };
941946 \\}
942 \\fn testGenericFn(comptime suffix: []const u8) linksection("__TEXT,__TestGenFn" ++ suffix) void {}
943947 });
944948
945949 const check = obj.checkObject();
......@@ -950,7 +954,7 @@ fn testLinksection(b: *Build, opts: Options) *Step {
950954
951955 if (opts.optimize == .Debug) {
952956 check.checkInSymtab();
953 check.checkContains("(__TEXT,__TestGenFnA) _main.testGenericFn__anon_");
957 check.checkContains("(__TEXT,__TestGenFnA) _main.TestGenericFn(");
954958 }
955959
956960 test_step.dependOn(&check.step);