Skip to content

Commit 1033d71

Browse files
mluggandrewrk
authored andcommitted
Sema,type: unify type query functions
The following pairs of functions have been combined using the "advanced" pattern used for other type queries: * `Sema.fnHasRuntimeBits`, `Type.isFnOrHasRuntimeBits` * `Sema.typeRequiresComptime`, `Type.comptimeOnly`
1 parent 7edba14 commit 1033d71

File tree

2 files changed

+94
-233
lines changed

2 files changed

+94
-233
lines changed

src/Sema.zig

Lines changed: 5 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -35301,7 +35301,7 @@ fn resolveSimpleType(sema: *Sema, simple_type: InternPool.SimpleType) CompileErr
3530135301
_ = try sema.getBuiltinType(builtin_type_name);
3530235302
}
3530335303

35304-
fn resolveTypeFieldsStruct(
35304+
pub fn resolveTypeFieldsStruct(
3530535305
sema: *Sema,
3530635306
ty: InternPool.Index,
3530735307
struct_type: InternPool.Key.StructType,
@@ -35340,7 +35340,7 @@ fn resolveTypeFieldsStruct(
3534035340
try semaStructFields(mod, sema.arena, struct_type);
3534135341
}
3534235342

35343-
fn resolveTypeFieldsUnion(sema: *Sema, ty: Type, union_type: InternPool.Key.UnionType) CompileError!void {
35343+
pub fn resolveTypeFieldsUnion(sema: *Sema, ty: Type, union_type: InternPool.Key.UnionType) CompileError!void {
3534435344
const mod = sema.mod;
3534535345
const ip = &mod.intern_pool;
3534635346
const owner_decl = mod.declPtr(union_type.decl);
@@ -37083,185 +37083,9 @@ fn typePtrOrOptionalPtrTy(sema: *Sema, ty: Type) !?Type {
3708337083
}
3708437084

3708537085
/// `generic_poison` will return false.
37086-
/// This function returns false negatives when structs and unions are having their
37087-
/// field types resolved.
37088-
/// TODO assert the return value matches `ty.comptimeOnly`
37089-
/// TODO merge these implementations together with the "advanced"/opt_sema pattern seen
37090-
/// elsewhere in value.zig
37086+
/// May return false negatives when structs and unions are having their field types resolved.
3709137087
pub fn typeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
37092-
const mod = sema.mod;
37093-
const ip = &mod.intern_pool;
37094-
return switch (ty.toIntern()) {
37095-
.empty_struct_type => false,
37096-
37097-
else => switch (ip.indexToKey(ty.toIntern())) {
37098-
.int_type => return false,
37099-
.ptr_type => |ptr_type| {
37100-
const child_ty = ptr_type.child.toType();
37101-
switch (child_ty.zigTypeTag(mod)) {
37102-
.Fn => return !try sema.fnHasRuntimeBits(child_ty),
37103-
.Opaque => return false,
37104-
else => return sema.typeRequiresComptime(child_ty),
37105-
}
37106-
},
37107-
.anyframe_type => |child| {
37108-
if (child == .none) return false;
37109-
return sema.typeRequiresComptime(child.toType());
37110-
},
37111-
.array_type => |array_type| return sema.typeRequiresComptime(array_type.child.toType()),
37112-
.vector_type => |vector_type| return sema.typeRequiresComptime(vector_type.child.toType()),
37113-
.opt_type => |child| return sema.typeRequiresComptime(child.toType()),
37114-
37115-
.error_union_type => |error_union_type| {
37116-
return sema.typeRequiresComptime(error_union_type.payload_type.toType());
37117-
},
37118-
37119-
.error_set_type, .inferred_error_set_type => false,
37120-
37121-
.func_type => true,
37122-
37123-
.simple_type => |t| switch (t) {
37124-
.f16,
37125-
.f32,
37126-
.f64,
37127-
.f80,
37128-
.f128,
37129-
.usize,
37130-
.isize,
37131-
.c_char,
37132-
.c_short,
37133-
.c_ushort,
37134-
.c_int,
37135-
.c_uint,
37136-
.c_long,
37137-
.c_ulong,
37138-
.c_longlong,
37139-
.c_ulonglong,
37140-
.c_longdouble,
37141-
.anyopaque,
37142-
.bool,
37143-
.void,
37144-
.anyerror,
37145-
.adhoc_inferred_error_set,
37146-
.noreturn,
37147-
.generic_poison,
37148-
.atomic_order,
37149-
.atomic_rmw_op,
37150-
.calling_convention,
37151-
.address_space,
37152-
.float_mode,
37153-
.reduce_op,
37154-
.call_modifier,
37155-
.prefetch_options,
37156-
.export_options,
37157-
.extern_options,
37158-
=> false,
37159-
37160-
.type,
37161-
.comptime_int,
37162-
.comptime_float,
37163-
.null,
37164-
.undefined,
37165-
.enum_literal,
37166-
.type_info,
37167-
=> true,
37168-
},
37169-
.struct_type => |struct_type| {
37170-
if (struct_type.layout == .Packed) {
37171-
// packed structs cannot be comptime-only because they have a well-defined
37172-
// memory layout and every field has a well-defined bit pattern.
37173-
return false;
37174-
}
37175-
switch (struct_type.flagsPtr(ip).requires_comptime) {
37176-
.no, .wip => return false,
37177-
.yes => return true,
37178-
.unknown => {
37179-
if (struct_type.flagsPtr(ip).field_types_wip)
37180-
return false;
37181-
37182-
try sema.resolveTypeFieldsStruct(ty.toIntern(), struct_type);
37183-
37184-
struct_type.flagsPtr(ip).requires_comptime = .wip;
37185-
37186-
for (0..struct_type.field_types.len) |i_usize| {
37187-
const i: u32 = @intCast(i_usize);
37188-
if (struct_type.fieldIsComptime(ip, i)) continue;
37189-
const field_ty = struct_type.field_types.get(ip)[i];
37190-
if (try sema.typeRequiresComptime(field_ty.toType())) {
37191-
// Note that this does not cause the layout to
37192-
// be considered resolved. Comptime-only types
37193-
// still maintain a layout of their
37194-
// runtime-known fields.
37195-
struct_type.flagsPtr(ip).requires_comptime = .yes;
37196-
return true;
37197-
}
37198-
}
37199-
struct_type.flagsPtr(ip).requires_comptime = .no;
37200-
return false;
37201-
},
37202-
}
37203-
},
37204-
.anon_struct_type => |tuple| {
37205-
for (tuple.types.get(ip), tuple.values.get(ip)) |field_ty, val| {
37206-
const have_comptime_val = val != .none;
37207-
if (!have_comptime_val and try sema.typeRequiresComptime(field_ty.toType())) {
37208-
return true;
37209-
}
37210-
}
37211-
return false;
37212-
},
37213-
37214-
.union_type => |union_type| {
37215-
switch (union_type.flagsPtr(ip).requires_comptime) {
37216-
.no, .wip => return false,
37217-
.yes => return true,
37218-
.unknown => {
37219-
if (union_type.flagsPtr(ip).status == .field_types_wip)
37220-
return false;
37221-
37222-
try sema.resolveTypeFieldsUnion(ty, union_type);
37223-
const union_obj = ip.loadUnionType(union_type);
37224-
37225-
union_obj.flagsPtr(ip).requires_comptime = .wip;
37226-
for (0..union_obj.field_types.len) |field_index| {
37227-
const field_ty = union_obj.field_types.get(ip)[field_index];
37228-
if (try sema.typeRequiresComptime(field_ty.toType())) {
37229-
union_obj.flagsPtr(ip).requires_comptime = .yes;
37230-
return true;
37231-
}
37232-
}
37233-
union_obj.flagsPtr(ip).requires_comptime = .no;
37234-
return false;
37235-
},
37236-
}
37237-
},
37238-
37239-
.opaque_type => false,
37240-
.enum_type => |enum_type| try sema.typeRequiresComptime(enum_type.tag_ty.toType()),
37241-
37242-
// values, not types
37243-
.undef,
37244-
.runtime_value,
37245-
.simple_value,
37246-
.variable,
37247-
.extern_func,
37248-
.func,
37249-
.int,
37250-
.err,
37251-
.error_union,
37252-
.enum_literal,
37253-
.enum_tag,
37254-
.empty_enum_value,
37255-
.float,
37256-
.ptr,
37257-
.opt,
37258-
.aggregate,
37259-
.un,
37260-
// memoization, not types
37261-
.memoized_call,
37262-
=> unreachable,
37263-
},
37264-
};
37088+
return ty.comptimeOnlyAdvanced(sema.mod, sema);
3726537089
}
3726637090

3726737091
pub fn typeHasRuntimeBits(sema: *Sema, ty: Type) CompileError!bool {
@@ -37316,21 +37140,8 @@ fn structFieldAlignment(
3731637140
return ty_abi_align;
3731737141
}
3731837142

37319-
/// Synchronize logic with `Type.isFnOrHasRuntimeBits`.
3732037143
pub fn fnHasRuntimeBits(sema: *Sema, ty: Type) CompileError!bool {
37321-
const mod = sema.mod;
37322-
const fn_info = mod.typeToFunc(ty).?;
37323-
if (fn_info.is_generic) return false;
37324-
if (fn_info.is_var_args) return true;
37325-
switch (fn_info.cc) {
37326-
// If there was a comptime calling convention, it should also return false here.
37327-
.Inline => return false,
37328-
else => {},
37329-
}
37330-
if (try sema.typeRequiresComptime(fn_info.return_type.toType())) {
37331-
return false;
37332-
}
37333-
return true;
37144+
return ty.fnHasRuntimeBitsAdvanced(sema.mod, sema);
3733437145
}
3733537146

3733637147
fn unionFieldIndex(

0 commit comments

Comments
 (0)