Skip to content

Commit e6a3374

Browse files
committed
Fix misinterpretation of sentinel constant value
Fixes ziglang#3842
1 parent 10e172b commit e6a3374

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/ir.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21786,10 +21786,12 @@ static void ensure_field_index(ZigType *type, const char *field_name, size_t ind
2178621786
Buf *field_name_buf;
2178721787

2178821788
assert(type != nullptr && !type_is_invalid(type));
21789-
// Check for our field by creating a buffer in place then using the comma operator to free it so that we don't
21790-
// leak memory in debug mode.
21791-
assert(find_struct_type_field(type, field_name_buf = buf_create_from_str(field_name))->src_index == index &&
21792-
(buf_deinit(field_name_buf), true));
21789+
field_name_buf = buf_create_from_str(field_name);
21790+
TypeStructField *field = find_struct_type_field(type, field_name_buf);
21791+
buf_deinit(field_name_buf);
21792+
21793+
if (field == nullptr || field->src_index != index)
21794+
zig_panic("reference to unknown field %s", field_name);
2179321795
}
2179421796

2179521797
static ZigType *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, ZigType *root) {
@@ -22113,7 +22115,7 @@ static ZigValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_type_ent
2211322115
zig_unreachable();
2211422116
}
2211522117

22116-
if ((err = type_resolve(ira->codegen, attrs_type->data.pointer.child_type, ResolveStatusAlignmentKnown)))
22118+
if ((err = type_resolve(ira->codegen, attrs_type->data.pointer.child_type, ResolveStatusSizeKnown)))
2211722119
return nullptr;
2211822120

2211922121
ZigType *type_info_pointer_type = ir_type_info_get_type(ira, "Pointer", nullptr);
@@ -22162,11 +22164,10 @@ static ZigValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_type_ent
2216222164
// sentinel: var
2216322165
ensure_field_index(result->type, "sentinel", 6);
2216422166
fields[6]->special = ConstValSpecialStatic;
22167+
fields[6]->type = get_optional_type(ira->codegen, attrs_type->data.pointer.child_type);
2216522168
if (attrs_type->data.pointer.sentinel != nullptr) {
22166-
fields[6]->type = get_optional_type(ira->codegen, attrs_type->data.pointer.child_type);
2216722169
fields[6]->data.x_optional = attrs_type->data.pointer.sentinel;
2216822170
} else {
22169-
fields[6]->type = ira->codegen->builtin_types.entry_null;
2217022171
fields[6]->data.x_optional = nullptr;
2217122172
}
2217222173

@@ -22814,13 +22815,20 @@ static Error get_const_field_sentinel(IrAnalyze *ira, IrInstruction *source_inst
2281422815
ZigValue *field_val = get_const_field(ira, source_instr->source_node, struct_value, name, field_index);
2281522816
if (field_val == nullptr)
2281622817
return ErrorSemanticAnalyzeFail;
22818+
2281722819
IrInstruction *field_inst = ir_const(ira, source_instr, field_val->type);
2281822820
IrInstruction *casted_field_inst = ir_implicit_cast(ira, field_inst,
2281922821
get_optional_type(ira->codegen, elem_type));
2282022822
if (type_is_invalid(casted_field_inst->value->type))
2282122823
return ErrorSemanticAnalyzeFail;
2282222824

22823-
*result = casted_field_inst->value->data.x_optional;
22825+
if (optional_value_is_null(casted_field_inst->value)) {
22826+
*result = nullptr;
22827+
} else {
22828+
assert(type_has_optional_repr(casted_field_inst->value->type));
22829+
*result = casted_field_inst->value->data.x_optional;
22830+
}
22831+
2282422832
return ErrorNone;
2282522833
}
2282622834

0 commit comments

Comments
 (0)