mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
Sema: introduce Value.enum_field_0
and use it to fix typeHasOnePossibleValue logic in two different places.
This commit is contained in:
parent
8587e510e4
commit
ad06b249b6
3 changed files with 17 additions and 7 deletions
|
|
@ -33031,7 +33031,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
|
|||
}
|
||||
if (enum_obj.fields.count() == 1) {
|
||||
if (enum_obj.values.count() == 0) {
|
||||
return try mod.intValue(ty, 0); // auto-numbered
|
||||
return Value.enum_field_0; // auto-numbered
|
||||
} else {
|
||||
return enum_obj.values.keys()[0];
|
||||
}
|
||||
|
|
@ -33048,7 +33048,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
|
|||
switch (enum_obj.fields.count()) {
|
||||
0 => return Value.@"unreachable",
|
||||
1 => if (enum_obj.values.count() == 0) {
|
||||
return try mod.intValue(ty, 0); // auto-numbered
|
||||
return Value.enum_field_0; // auto-numbered
|
||||
} else {
|
||||
return enum_obj.values.keys()[0];
|
||||
},
|
||||
|
|
@ -33060,14 +33060,14 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
|
|||
const enum_simple = resolved_ty.castTag(.enum_simple).?.data;
|
||||
switch (enum_simple.fields.count()) {
|
||||
0 => return Value.@"unreachable",
|
||||
1 => return try Value.Tag.enum_field_index.create(sema.arena, 0),
|
||||
1 => return Value.enum_field_0,
|
||||
else => return null,
|
||||
}
|
||||
},
|
||||
.enum_nonexhaustive => {
|
||||
const tag_ty = ty.castTag(.enum_nonexhaustive).?.data.tag_ty;
|
||||
if (tag_ty.zigTypeTag(mod) != .ComptimeInt and !(try sema.typeHasRuntimeBits(tag_ty))) {
|
||||
return try mod.intValue(ty, 0);
|
||||
return Value.enum_field_0;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4027,7 +4027,7 @@ pub const Type = struct {
|
|||
switch (enum_full.fields.count()) {
|
||||
0 => return Value.@"unreachable",
|
||||
1 => if (enum_full.values.count() == 0) {
|
||||
return try mod.intValue(ty, 0); // auto-numbered
|
||||
return Value.enum_field_0; // auto-numbered
|
||||
} else {
|
||||
return enum_full.values.keys()[0];
|
||||
},
|
||||
|
|
@ -4038,14 +4038,14 @@ pub const Type = struct {
|
|||
const enum_simple = ty.castTag(.enum_simple).?.data;
|
||||
switch (enum_simple.fields.count()) {
|
||||
0 => return Value.@"unreachable",
|
||||
1 => return try mod.intValue(ty, 0),
|
||||
1 => return Value.enum_field_0,
|
||||
else => return null,
|
||||
}
|
||||
},
|
||||
.enum_nonexhaustive => {
|
||||
const tag_ty = ty.castTag(.enum_nonexhaustive).?.data.tag_ty;
|
||||
if (!tag_ty.hasRuntimeBits(mod)) {
|
||||
return try mod.intValue(ty, 0);
|
||||
return Value.enum_field_0;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5192,6 +5192,16 @@ pub const Value = struct {
|
|||
pub const generic_poison_type: Value = .{ .ip_index = .generic_poison_type, .legacy = undefined };
|
||||
pub const empty_struct: Value = .{ .ip_index = .empty_struct, .legacy = undefined };
|
||||
|
||||
pub const enum_field_0: Value = .{
|
||||
.ip_index = .none,
|
||||
.legacy = .{ .ptr_otherwise = &enum_field_0_payload.base },
|
||||
};
|
||||
|
||||
var enum_field_0_payload: Payload.U32 = .{
|
||||
.base = .{ .tag = .enum_field_index },
|
||||
.data = 0,
|
||||
};
|
||||
|
||||
pub fn makeBool(x: bool) Value {
|
||||
return if (x) Value.true else Value.false;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue