Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compiler: disallow callconv etc from depending on function parameters #22264

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
421 changes: 161 additions & 260 deletions lib/std/zig/AstGen.zig

Large diffs are not rendered by default.

111 changes: 12 additions & 99 deletions lib/std/zig/Zir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2494,46 +2494,25 @@ pub const Inst = struct {

/// Trailing:
/// 0. lib_name: NullTerminatedString, // null terminated string index, if has_lib_name is set
/// if (has_align_ref and !has_align_body) {
/// 1. align: Ref,
/// }
/// if (has_align_body) {
/// 2. align_body_len: u32
/// 3. align_body: u32 // for each align_body_len
/// }
/// if (has_addrspace_ref and !has_addrspace_body) {
/// 4. addrspace: Ref,
/// }
/// if (has_addrspace_body) {
/// 5. addrspace_body_len: u32
/// 6. addrspace_body: u32 // for each addrspace_body_len
/// }
/// if (has_section_ref and !has_section_body) {
/// 7. section: Ref,
/// }
/// if (has_section_body) {
/// 8. section_body_len: u32
/// 9. section_body: u32 // for each section_body_len
/// }
/// if (has_cc_ref and !has_cc_body) {
/// 10. cc: Ref,
/// 1. cc: Ref,
/// }
/// if (has_cc_body) {
/// 11. cc_body_len: u32
/// 12. cc_body: u32 // for each cc_body_len
/// 2. cc_body_len: u32
/// 3. cc_body: u32 // for each cc_body_len
/// }
/// if (has_ret_ty_ref and !has_ret_ty_body) {
/// 13. ret_ty: Ref,
/// 4. ret_ty: Ref,
/// }
/// if (has_ret_ty_body) {
/// 14. ret_ty_body_len: u32
/// 15. ret_ty_body: u32 // for each ret_ty_body_len
/// 5. ret_ty_body_len: u32
/// 6. ret_ty_body: u32 // for each ret_ty_body_len
/// }
/// 16. noalias_bits: u32 // if has_any_noalias
/// - each bit starting with LSB corresponds to parameter indexes
/// 17. body: Index // for each body_len
/// 18. src_locs: Func.SrcLocs // if body_len != 0
/// 19. proto_hash: std.zig.SrcHash // if body_len != 0; hash of function prototype
/// 7. noalias_bits: u32 // if has_any_noalias
/// - each bit starting with LSB corresponds to parameter indexes
/// 8. body: Index // for each body_len
/// 9. src_locs: Func.SrcLocs // if body_len != 0
/// 10. proto_hash: std.zig.SrcHash // if body_len != 0; hash of function prototype
pub const FuncFancy = struct {
/// Points to the block that contains the param instructions for this function.
/// If this is a `declaration`, it refers to the declaration's value body.
Expand All @@ -2542,29 +2521,20 @@ pub const Inst = struct {
bits: Bits,

/// If both has_cc_ref and has_cc_body are false, it means auto calling convention.
/// If both has_align_ref and has_align_body are false, it means default alignment.
/// If both has_ret_ty_ref and has_ret_ty_body are false, it means void return type.
/// If both has_section_ref and has_section_body are false, it means default section.
/// If both has_addrspace_ref and has_addrspace_body are false, it means default addrspace.
pub const Bits = packed struct {
is_var_args: bool,
is_inferred_error: bool,
is_test: bool,
is_extern: bool,
is_noinline: bool,
has_align_ref: bool,
has_align_body: bool,
has_addrspace_ref: bool,
has_addrspace_body: bool,
has_section_ref: bool,
has_section_body: bool,
has_cc_ref: bool,
has_cc_body: bool,
has_ret_ty_ref: bool,
has_ret_ty_body: bool,
has_lib_name: bool,
has_any_noalias: bool,
_: u15 = undefined,
_: u21 = undefined,
};
};

Expand Down Expand Up @@ -4269,36 +4239,6 @@ fn findTrackableInner(
var extra_index: usize = extra.end;
extra_index += @intFromBool(extra.data.bits.has_lib_name);

if (extra.data.bits.has_align_body) {
const body_len = zir.extra[extra_index];
extra_index += 1;
const body = zir.bodySlice(extra_index, body_len);
try zir.findTrackableBody(gpa, contents, defers, body);
extra_index += body.len;
} else if (extra.data.bits.has_align_ref) {
extra_index += 1;
}

if (extra.data.bits.has_addrspace_body) {
const body_len = zir.extra[extra_index];
extra_index += 1;
const body = zir.bodySlice(extra_index, body_len);
try zir.findTrackableBody(gpa, contents, defers, body);
extra_index += body.len;
} else if (extra.data.bits.has_addrspace_ref) {
extra_index += 1;
}

if (extra.data.bits.has_section_body) {
const body_len = zir.extra[extra_index];
extra_index += 1;
const body = zir.bodySlice(extra_index, body_len);
try zir.findTrackableBody(gpa, contents, defers, body);
extra_index += body.len;
} else if (extra.data.bits.has_section_ref) {
extra_index += 1;
}

if (extra.data.bits.has_cc_body) {
const body_len = zir.extra[extra_index];
extra_index += 1;
Expand Down Expand Up @@ -4587,21 +4527,6 @@ pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo {
var ret_ty_body: []const Inst.Index = &.{};

extra_index += @intFromBool(extra.data.bits.has_lib_name);
if (extra.data.bits.has_align_body) {
extra_index += zir.extra[extra_index] + 1;
} else if (extra.data.bits.has_align_ref) {
extra_index += 1;
}
if (extra.data.bits.has_addrspace_body) {
extra_index += zir.extra[extra_index] + 1;
} else if (extra.data.bits.has_addrspace_ref) {
extra_index += 1;
}
if (extra.data.bits.has_section_body) {
extra_index += zir.extra[extra_index] + 1;
} else if (extra.data.bits.has_section_ref) {
extra_index += 1;
}
if (extra.data.bits.has_cc_body) {
extra_index += zir.extra[extra_index] + 1;
} else if (extra.data.bits.has_cc_ref) {
Expand Down Expand Up @@ -4712,18 +4637,6 @@ pub fn getAssociatedSrcHash(zir: Zir, inst: Zir.Inst.Index) ?std.zig.SrcHash {
const bits = extra.data.bits;
var extra_index = extra.end;
extra_index += @intFromBool(bits.has_lib_name);
if (bits.has_align_body) {
const body_len = zir.extra[extra_index];
extra_index += 1 + body_len;
} else extra_index += @intFromBool(bits.has_align_ref);
if (bits.has_addrspace_body) {
const body_len = zir.extra[extra_index];
extra_index += 1 + body_len;
} else extra_index += @intFromBool(bits.has_addrspace_ref);
if (bits.has_section_body) {
const body_len = zir.extra[extra_index];
extra_index += 1 + body_len;
} else extra_index += @intFromBool(bits.has_section_ref);
if (bits.has_cc_body) {
const body_len = zir.extra[extra_index];
extra_index += 1 + body_len;
Expand Down
59 changes: 9 additions & 50 deletions src/InternPool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1462,16 +1462,6 @@ pub const MapIndex = enum(u32) {
}
};

pub const RuntimeIndex = enum(u32) {
zero = 0,
comptime_field_ptr = std.math.maxInt(u32),
_,

pub fn increment(ri: *RuntimeIndex) void {
ri.* = @enumFromInt(@intFromEnum(ri.*) + 1);
}
};

pub const ComptimeAllocIndex = enum(u32) { _ };

pub const NamespaceIndex = enum(u32) {
Expand Down Expand Up @@ -1971,9 +1961,6 @@ pub const Key = union(enum) {
is_var_args: bool,
is_generic: bool,
is_noinline: bool,
cc_is_generic: bool,
section_is_generic: bool,
addrspace_is_generic: bool,

pub fn paramIsComptime(self: @This(), i: u5) bool {
assert(i < self.param_types.len);
Expand Down Expand Up @@ -5466,10 +5453,7 @@ pub const Tag = enum(u8) {
has_comptime_bits: bool,
has_noalias_bits: bool,
is_noinline: bool,
cc_is_generic: bool,
section_is_generic: bool,
addrspace_is_generic: bool,
_: u6 = 0,
_: u9 = 0,
};
};

Expand Down Expand Up @@ -6895,9 +6879,6 @@ fn extraFuncType(tid: Zcu.PerThread.Id, extra: Local.Extra, extra_index: u32) Ke
.cc = type_function.data.flags.cc.unpack(),
.is_var_args = type_function.data.flags.is_var_args,
.is_noinline = type_function.data.flags.is_noinline,
.cc_is_generic = type_function.data.flags.cc_is_generic,
.section_is_generic = type_function.data.flags.section_is_generic,
.addrspace_is_generic = type_function.data.flags.addrspace_is_generic,
.is_generic = type_function.data.flags.is_generic,
};
}
Expand Down Expand Up @@ -8539,9 +8520,6 @@ pub fn getFuncType(
.has_noalias_bits = key.noalias_bits != 0,
.is_generic = key.is_generic,
.is_noinline = key.is_noinline,
.cc_is_generic = key.cc == null,
.section_is_generic = key.section_is_generic,
.addrspace_is_generic = key.addrspace_is_generic,
},
});

Expand Down Expand Up @@ -8713,10 +8691,6 @@ pub const GetFuncDeclIesKey = struct {
bare_return_type: Index,
/// null means generic.
cc: ?std.builtin.CallingConvention,
/// null means generic.
alignment: ?Alignment,
section_is_generic: bool,
addrspace_is_generic: bool,
is_var_args: bool,
is_generic: bool,
is_noinline: bool,
Expand Down Expand Up @@ -8802,9 +8776,6 @@ pub fn getFuncDeclIes(
.has_noalias_bits = key.noalias_bits != 0,
.is_generic = key.is_generic,
.is_noinline = key.is_noinline,
.cc_is_generic = key.cc == null,
.section_is_generic = key.section_is_generic,
.addrspace_is_generic = key.addrspace_is_generic,
},
});
if (key.comptime_bits != 0) extra.appendAssumeCapacity(.{key.comptime_bits});
Expand Down Expand Up @@ -8936,9 +8907,6 @@ pub const GetFuncInstanceKey = struct {
comptime_args: []const Index,
noalias_bits: u32,
bare_return_type: Index,
cc: std.builtin.CallingConvention,
alignment: Alignment,
section: OptionalNullTerminatedString,
is_noinline: bool,
generic_owner: Index,
inferred_error_set: bool,
Expand All @@ -8953,11 +8921,14 @@ pub fn getFuncInstance(
if (arg.inferred_error_set)
return getFuncInstanceIes(ip, gpa, tid, arg);

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

const func_ty = try ip.getFuncType(gpa, tid, .{
.param_types = arg.param_types,
.return_type = arg.bare_return_type,
.noalias_bits = arg.noalias_bits,
.cc = arg.cc,
.cc = generic_owner_ty.cc,
.is_noinline = arg.is_noinline,
});

Expand All @@ -8967,8 +8938,6 @@ pub fn getFuncInstance(
try extra.ensureUnusedCapacity(@typeInfo(Tag.FuncInstance).@"struct".fields.len +
arg.comptime_args.len);

const generic_owner = unwrapCoercedFunc(ip, arg.generic_owner);

assert(arg.comptime_args.len == ip.funcTypeParamsLen(ip.typeOf(generic_owner)));

const prev_extra_len = extra.mutate.len;
Expand Down Expand Up @@ -9015,8 +8984,6 @@ pub fn getFuncInstance(
generic_owner,
func_index,
func_extra_index,
arg.alignment,
arg.section,
);
return gop.put();
}
Expand All @@ -9041,6 +9008,7 @@ pub fn getFuncInstanceIes(
try items.ensureUnusedCapacity(4);

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

// The strategy here is to add the function decl unconditionally, then to
// ask if it already exists, and if so, revert the lengths of the mutated
Expand Down Expand Up @@ -9096,15 +9064,12 @@ pub fn getFuncInstanceIes(
.params_len = params_len,
.return_type = error_union_type,
.flags = .{
.cc = .pack(arg.cc),
.cc = .pack(generic_owner_ty.cc),
.is_var_args = false,
.has_comptime_bits = false,
.has_noalias_bits = arg.noalias_bits != 0,
.is_generic = false,
.is_noinline = arg.is_noinline,
.cc_is_generic = false,
.section_is_generic = false,
.addrspace_is_generic = false,
},
});
// no comptime_bits because has_comptime_bits is false
Expand Down Expand Up @@ -9168,8 +9133,6 @@ pub fn getFuncInstanceIes(
generic_owner,
func_index,
func_extra_index,
arg.alignment,
arg.section,
);

func_gop.putFinal(func_index);
Expand All @@ -9187,8 +9150,6 @@ fn finishFuncInstance(
generic_owner: Index,
func_index: Index,
func_extra_index: u32,
alignment: Alignment,
section: OptionalNullTerminatedString,
) Allocator.Error!void {
const fn_owner_nav = ip.getNav(ip.funcDeclInfo(generic_owner).owner_nav);
const fn_namespace = ip.getCau(fn_owner_nav.analysis_owner.unwrap().?).namespace;
Expand All @@ -9201,8 +9162,8 @@ fn finishFuncInstance(
.name = nav_name,
.fqn = try ip.namespacePtr(fn_namespace).internFullyQualifiedName(ip, gpa, tid, nav_name),
.val = func_index,
.alignment = alignment,
.@"linksection" = section,
.alignment = fn_owner_nav.status.resolved.alignment,
.@"linksection" = fn_owner_nav.status.resolved.@"linksection",
.@"addrspace" = fn_owner_nav.status.resolved.@"addrspace",
});

Expand Down Expand Up @@ -9788,7 +9749,6 @@ fn addExtraAssumeCapacity(extra: Local.Extra.Mutable, item: anytype) u32 {
OptionalNamespaceIndex,
MapIndex,
OptionalMapIndex,
RuntimeIndex,
String,
NullTerminatedString,
OptionalNullTerminatedString,
Expand Down Expand Up @@ -9852,7 +9812,6 @@ fn extraDataTrail(extra: Local.Extra, comptime T: type, index: u32) struct { dat
OptionalNamespaceIndex,
MapIndex,
OptionalMapIndex,
RuntimeIndex,
String,
NullTerminatedString,
OptionalNullTerminatedString,
Expand Down
Loading
Loading