Skip to content

Commit e2e3633

Browse files
authored
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.
2 parents f857bf7 + 58b8b1a commit e2e3633

14 files changed

Lines changed: 433 additions & 871 deletions

File tree

lib/std/zig/AstGen.zig

Lines changed: 161 additions & 260 deletions
Large diffs are not rendered by default.

lib/std/zig/Zir.zig

Lines changed: 12 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,16 +1462,6 @@ pub const MapIndex = enum(u32) {
14621462
}
14631463
};
14641464

1465-
pub 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-
14751465
pub const ComptimeAllocIndex = enum(u32) { _ };
14761466

14771467
pub 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,

0 commit comments

Comments
 (0)