Skip to content

Commit 5e48cdc

Browse files
committed
compiler: disallow callconv etc from depending on function parameters
Resolves: #22261
1 parent 0722f86 commit 5e48cdc

File tree

8 files changed

+320
-841
lines changed

8 files changed

+320
-841
lines changed

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

@@ -4268,36 +4238,6 @@ fn findTrackableInner(
42684238
var extra_index: usize = extra.end;
42694239
extra_index += @intFromBool(extra.data.bits.has_lib_name);
42704240

4271-
if (extra.data.bits.has_align_body) {
4272-
const body_len = zir.extra[extra_index];
4273-
extra_index += 1;
4274-
const body = zir.bodySlice(extra_index, body_len);
4275-
try zir.findTrackableBody(gpa, contents, defers, body);
4276-
extra_index += body.len;
4277-
} else if (extra.data.bits.has_align_ref) {
4278-
extra_index += 1;
4279-
}
4280-
4281-
if (extra.data.bits.has_addrspace_body) {
4282-
const body_len = zir.extra[extra_index];
4283-
extra_index += 1;
4284-
const body = zir.bodySlice(extra_index, body_len);
4285-
try zir.findTrackableBody(gpa, contents, defers, body);
4286-
extra_index += body.len;
4287-
} else if (extra.data.bits.has_addrspace_ref) {
4288-
extra_index += 1;
4289-
}
4290-
4291-
if (extra.data.bits.has_section_body) {
4292-
const body_len = zir.extra[extra_index];
4293-
extra_index += 1;
4294-
const body = zir.bodySlice(extra_index, body_len);
4295-
try zir.findTrackableBody(gpa, contents, defers, body);
4296-
extra_index += body.len;
4297-
} else if (extra.data.bits.has_section_ref) {
4298-
extra_index += 1;
4299-
}
4300-
43014241
if (extra.data.bits.has_cc_body) {
43024242
const body_len = zir.extra[extra_index];
43034243
extra_index += 1;
@@ -4586,21 +4526,6 @@ pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo {
45864526
var ret_ty_body: []const Inst.Index = &.{};
45874527

45884528
extra_index += @intFromBool(extra.data.bits.has_lib_name);
4589-
if (extra.data.bits.has_align_body) {
4590-
extra_index += zir.extra[extra_index] + 1;
4591-
} else if (extra.data.bits.has_align_ref) {
4592-
extra_index += 1;
4593-
}
4594-
if (extra.data.bits.has_addrspace_body) {
4595-
extra_index += zir.extra[extra_index] + 1;
4596-
} else if (extra.data.bits.has_addrspace_ref) {
4597-
extra_index += 1;
4598-
}
4599-
if (extra.data.bits.has_section_body) {
4600-
extra_index += zir.extra[extra_index] + 1;
4601-
} else if (extra.data.bits.has_section_ref) {
4602-
extra_index += 1;
4603-
}
46044529
if (extra.data.bits.has_cc_body) {
46054530
extra_index += zir.extra[extra_index] + 1;
46064531
} else if (extra.data.bits.has_cc_ref) {
@@ -4711,18 +4636,6 @@ pub fn getAssociatedSrcHash(zir: Zir, inst: Zir.Inst.Index) ?std.zig.SrcHash {
47114636
const bits = extra.data.bits;
47124637
var extra_index = extra.end;
47134638
extra_index += @intFromBool(bits.has_lib_name);
4714-
if (bits.has_align_body) {
4715-
const body_len = zir.extra[extra_index];
4716-
extra_index += 1 + body_len;
4717-
} else extra_index += @intFromBool(bits.has_align_ref);
4718-
if (bits.has_addrspace_body) {
4719-
const body_len = zir.extra[extra_index];
4720-
extra_index += 1 + body_len;
4721-
} else extra_index += @intFromBool(bits.has_addrspace_ref);
4722-
if (bits.has_section_body) {
4723-
const body_len = zir.extra[extra_index];
4724-
extra_index += 1 + body_len;
4725-
} else extra_index += @intFromBool(bits.has_section_ref);
47264639
if (bits.has_cc_body) {
47274640
const body_len = zir.extra[extra_index];
47284641
extra_index += 1 + body_len;

src/InternPool.zig

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,9 +1961,6 @@ pub const Key = union(enum) {
19611961
is_var_args: bool,
19621962
is_generic: bool,
19631963
is_noinline: bool,
1964-
cc_is_generic: bool,
1965-
section_is_generic: bool,
1966-
addrspace_is_generic: bool,
19671964

19681965
pub fn paramIsComptime(self: @This(), i: u5) bool {
19691966
assert(i < self.param_types.len);
@@ -5456,10 +5453,7 @@ pub const Tag = enum(u8) {
54565453
has_comptime_bits: bool,
54575454
has_noalias_bits: bool,
54585455
is_noinline: bool,
5459-
cc_is_generic: bool,
5460-
section_is_generic: bool,
5461-
addrspace_is_generic: bool,
5462-
_: u6 = 0,
5456+
_: u9 = 0,
54635457
};
54645458
};
54655459

@@ -6885,9 +6879,6 @@ fn extraFuncType(tid: Zcu.PerThread.Id, extra: Local.Extra, extra_index: u32) Ke
68856879
.cc = type_function.data.flags.cc.unpack(),
68866880
.is_var_args = type_function.data.flags.is_var_args,
68876881
.is_noinline = type_function.data.flags.is_noinline,
6888-
.cc_is_generic = type_function.data.flags.cc_is_generic,
6889-
.section_is_generic = type_function.data.flags.section_is_generic,
6890-
.addrspace_is_generic = type_function.data.flags.addrspace_is_generic,
68916882
.is_generic = type_function.data.flags.is_generic,
68926883
};
68936884
}
@@ -8529,9 +8520,6 @@ pub fn getFuncType(
85298520
.has_noalias_bits = key.noalias_bits != 0,
85308521
.is_generic = key.is_generic,
85318522
.is_noinline = key.is_noinline,
8532-
.cc_is_generic = key.cc == null,
8533-
.section_is_generic = key.section_is_generic,
8534-
.addrspace_is_generic = key.addrspace_is_generic,
85358523
},
85368524
});
85378525

@@ -8703,10 +8691,6 @@ pub const GetFuncDeclIesKey = struct {
87038691
bare_return_type: Index,
87048692
/// null means generic.
87058693
cc: ?std.builtin.CallingConvention,
8706-
/// null means generic.
8707-
alignment: ?Alignment,
8708-
section_is_generic: bool,
8709-
addrspace_is_generic: bool,
87108694
is_var_args: bool,
87118695
is_generic: bool,
87128696
is_noinline: bool,
@@ -8792,9 +8776,6 @@ pub fn getFuncDeclIes(
87928776
.has_noalias_bits = key.noalias_bits != 0,
87938777
.is_generic = key.is_generic,
87948778
.is_noinline = key.is_noinline,
8795-
.cc_is_generic = key.cc == null,
8796-
.section_is_generic = key.section_is_generic,
8797-
.addrspace_is_generic = key.addrspace_is_generic,
87988779
},
87998780
});
88008781
if (key.comptime_bits != 0) extra.appendAssumeCapacity(.{key.comptime_bits});
@@ -8926,9 +8907,6 @@ pub const GetFuncInstanceKey = struct {
89268907
comptime_args: []const Index,
89278908
noalias_bits: u32,
89288909
bare_return_type: Index,
8929-
cc: std.builtin.CallingConvention,
8930-
alignment: Alignment,
8931-
section: OptionalNullTerminatedString,
89328910
is_noinline: bool,
89338911
generic_owner: Index,
89348912
inferred_error_set: bool,
@@ -8943,11 +8921,14 @@ pub fn getFuncInstance(
89438921
if (arg.inferred_error_set)
89448922
return getFuncInstanceIes(ip, gpa, tid, arg);
89458923

8924+
const generic_owner = unwrapCoercedFunc(ip, arg.generic_owner);
8925+
const generic_owner_ty = ip.indexToKey(ip.funcDeclInfo(generic_owner).ty).func_type;
8926+
89468927
const func_ty = try ip.getFuncType(gpa, tid, .{
89478928
.param_types = arg.param_types,
89488929
.return_type = arg.bare_return_type,
89498930
.noalias_bits = arg.noalias_bits,
8950-
.cc = arg.cc,
8931+
.cc = generic_owner_ty.cc,
89518932
.is_noinline = arg.is_noinline,
89528933
});
89538934

@@ -8957,8 +8938,6 @@ pub fn getFuncInstance(
89578938
try extra.ensureUnusedCapacity(@typeInfo(Tag.FuncInstance).@"struct".fields.len +
89588939
arg.comptime_args.len);
89598940

8960-
const generic_owner = unwrapCoercedFunc(ip, arg.generic_owner);
8961-
89628941
assert(arg.comptime_args.len == ip.funcTypeParamsLen(ip.typeOf(generic_owner)));
89638942

89648943
const prev_extra_len = extra.mutate.len;
@@ -9005,8 +8984,6 @@ pub fn getFuncInstance(
90058984
generic_owner,
90068985
func_index,
90078986
func_extra_index,
9008-
arg.alignment,
9009-
arg.section,
90108987
);
90118988
return gop.put();
90128989
}
@@ -9031,6 +9008,7 @@ pub fn getFuncInstanceIes(
90319008
try items.ensureUnusedCapacity(4);
90329009

90339010
const generic_owner = unwrapCoercedFunc(ip, arg.generic_owner);
9011+
const generic_owner_ty = ip.indexToKey(ip.funcDeclInfo(arg.generic_owner).ty).func_type;
90349012

90359013
// The strategy here is to add the function decl unconditionally, then to
90369014
// ask if it already exists, and if so, revert the lengths of the mutated
@@ -9086,15 +9064,12 @@ pub fn getFuncInstanceIes(
90869064
.params_len = params_len,
90879065
.return_type = error_union_type,
90889066
.flags = .{
9089-
.cc = .pack(arg.cc),
9067+
.cc = .pack(generic_owner_ty.cc),
90909068
.is_var_args = false,
90919069
.has_comptime_bits = false,
90929070
.has_noalias_bits = arg.noalias_bits != 0,
90939071
.is_generic = false,
90949072
.is_noinline = arg.is_noinline,
9095-
.cc_is_generic = false,
9096-
.section_is_generic = false,
9097-
.addrspace_is_generic = false,
90989073
},
90999074
});
91009075
// no comptime_bits because has_comptime_bits is false
@@ -9158,8 +9133,6 @@ pub fn getFuncInstanceIes(
91589133
generic_owner,
91599134
func_index,
91609135
func_extra_index,
9161-
arg.alignment,
9162-
arg.section,
91639136
);
91649137

91659138
func_gop.putFinal(func_index);
@@ -9177,8 +9150,6 @@ fn finishFuncInstance(
91779150
generic_owner: Index,
91789151
func_index: Index,
91799152
func_extra_index: u32,
9180-
alignment: Alignment,
9181-
section: OptionalNullTerminatedString,
91829153
) Allocator.Error!void {
91839154
const fn_owner_nav = ip.getNav(ip.funcDeclInfo(generic_owner).owner_nav);
91849155
const fn_namespace = ip.getCau(fn_owner_nav.analysis_owner.unwrap().?).namespace;
@@ -9191,8 +9162,8 @@ fn finishFuncInstance(
91919162
.name = nav_name,
91929163
.fqn = try ip.namespacePtr(fn_namespace).internFullyQualifiedName(ip, gpa, tid, nav_name),
91939164
.val = func_index,
9194-
.alignment = alignment,
9195-
.@"linksection" = section,
9165+
.alignment = fn_owner_nav.status.resolved.alignment,
9166+
.@"linksection" = fn_owner_nav.status.resolved.@"linksection",
91969167
.@"addrspace" = fn_owner_nav.status.resolved.@"addrspace",
91979168
});
91989169

0 commit comments

Comments
 (0)