@@ -9012,7 +9012,42 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc
9012
9012
return false;
9013
9013
}
9014
9014
9015
- ConstExprValue *const_val = ir_resolve_const(ira, instruction, UndefBad);
9015
+ ConstExprValue *const_val = ir_resolve_const(ira, instruction, LazyOkNoUndef);
9016
+ if (const_val == nullptr)
9017
+ return false;
9018
+
9019
+ if (const_val->special == ConstValSpecialLazy) {
9020
+ switch (const_val->data.x_lazy->id) {
9021
+ case LazyValueIdAlignOf: {
9022
+ // This is guaranteed to fit into a u29
9023
+ if (other_type->id == ZigTypeIdComptimeInt)
9024
+ return true;
9025
+ size_t align_bits = get_align_amt_type(ira->codegen)->data.integral.bit_count;
9026
+ if (other_type->id == ZigTypeIdInt && !other_type->data.integral.is_signed &&
9027
+ other_type->data.integral.bit_count >= align_bits)
9028
+ {
9029
+ return true;
9030
+ }
9031
+ break;
9032
+ }
9033
+ case LazyValueIdSizeOf: {
9034
+ // This is guaranteed to fit into a usize
9035
+ if (other_type->id == ZigTypeIdComptimeInt)
9036
+ return true;
9037
+ size_t usize_bits = ira->codegen->builtin_types.entry_usize->data.integral.bit_count;
9038
+ if (other_type->id == ZigTypeIdInt && !other_type->data.integral.is_signed &&
9039
+ other_type->data.integral.bit_count >= usize_bits)
9040
+ {
9041
+ return true;
9042
+ }
9043
+ break;
9044
+ }
9045
+ default:
9046
+ break;
9047
+ }
9048
+ }
9049
+
9050
+ const_val = ir_resolve_const(ira, instruction, UndefBad);
9016
9051
if (const_val == nullptr)
9017
9052
return false;
9018
9053
@@ -10262,7 +10297,7 @@ static void copy_const_val(ConstExprValue *dest, ConstExprValue *src, bool same_
10262
10297
memcpy(dest, src, sizeof(ConstExprValue));
10263
10298
if (!same_global_refs) {
10264
10299
dest->global_refs = global_refs;
10265
- if (src->special == ConstValSpecialUndef )
10300
+ if (src->special != ConstValSpecialStatic )
10266
10301
return;
10267
10302
if (dest->type->id == ZigTypeIdStruct) {
10268
10303
dest->data.x_struct.fields = create_const_vals(dest->type->data.structure.src_field_count);
@@ -11213,7 +11248,7 @@ static IrInstruction *ir_get_ref(IrAnalyze *ira, IrInstruction *source_instructi
11213
11248
return ira->codegen->invalid_instruction;
11214
11249
11215
11250
if (instr_is_comptime(value)) {
11216
- ConstExprValue *val = ir_resolve_const(ira, value, UndefOk );
11251
+ ConstExprValue *val = ir_resolve_const(ira, value, LazyOk );
11217
11252
if (!val)
11218
11253
return ira->codegen->invalid_instruction;
11219
11254
return ir_get_const_ptr(ira, source_instruction, val, value->value.type,
@@ -12125,7 +12160,8 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
12125
12160
if (wanted_type->id == ZigTypeIdComptimeInt || wanted_type->id == ZigTypeIdInt) {
12126
12161
IrInstruction *result = ir_const(ira, source_instr, wanted_type);
12127
12162
if (actual_type->id == ZigTypeIdComptimeInt || actual_type->id == ZigTypeIdInt) {
12128
- bigint_init_bigint(&result->value.data.x_bigint, &value->value.data.x_bigint);
12163
+ copy_const_val(&result->value, &value->value, false);
12164
+ result->value.type = wanted_type;
12129
12165
} else {
12130
12166
float_init_bigint(&result->value.data.x_bigint, &value->value);
12131
12167
}
@@ -15301,7 +15337,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
15301
15337
}
15302
15338
}
15303
15339
15304
- bool comptime_arg = param_decl_node->data.param_decl.is_inline ||
15340
+ bool comptime_arg = param_decl_node->data.param_decl.is_comptime ||
15305
15341
casted_arg->value.type->id == ZigTypeIdComptimeInt || casted_arg->value.type->id == ZigTypeIdComptimeFloat;
15306
15342
15307
15343
ConstExprValue *arg_val;
@@ -17594,6 +17630,11 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
17594
17630
ConstExprValue *child_val = const_ptr_pointee(ira, ira->codegen, container_ptr_val, source_node);
17595
17631
if (child_val == nullptr)
17596
17632
return ira->codegen->invalid_instruction;
17633
+ if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec,
17634
+ field_ptr_instruction->base.source_node, child_val, UndefBad)))
17635
+ {
17636
+ return ira->codegen->invalid_instruction;
17637
+ }
17597
17638
ZigType *child_type = child_val->data.x_type;
17598
17639
17599
17640
if (type_is_invalid(child_type)) {
@@ -21293,8 +21334,10 @@ static IrInstruction *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstru
21293
21334
src_ptr_align = get_abi_alignment(ira->codegen, target->value.type);
21294
21335
}
21295
21336
21296
- if ((err = type_resolve(ira->codegen, dest_child_type, ResolveStatusSizeKnown)))
21297
- return ira->codegen->invalid_instruction;
21337
+ if (src_ptr_align != 0) {
21338
+ if ((err = type_resolve(ira->codegen, dest_child_type, ResolveStatusAlignmentKnown)))
21339
+ return ira->codegen->invalid_instruction;
21340
+ }
21298
21341
21299
21342
ZigType *dest_ptr_type = get_pointer_to_type_extra(ira->codegen, dest_child_type,
21300
21343
src_ptr_const, src_ptr_volatile, PtrLenUnknown,
@@ -21337,6 +21380,8 @@ static IrInstruction *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstru
21337
21380
}
21338
21381
21339
21382
if (have_known_len) {
21383
+ if ((err = type_resolve(ira->codegen, dest_child_type, ResolveStatusSizeKnown)))
21384
+ return ira->codegen->invalid_instruction;
21340
21385
uint64_t child_type_size = type_size(ira->codegen, dest_child_type);
21341
21386
uint64_t remainder = known_len % child_type_size;
21342
21387
if (remainder != 0) {
@@ -23963,15 +24008,23 @@ static IrInstruction *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstruct
23963
24008
}
23964
24009
23965
24010
static IrInstruction *ir_analyze_instruction_align_cast(IrAnalyze *ira, IrInstructionAlignCast *instruction) {
23966
- uint32_t align_bytes;
23967
- IrInstruction *align_bytes_inst = instruction->align_bytes->child;
23968
- if (!ir_resolve_align(ira, align_bytes_inst, nullptr, &align_bytes))
23969
- return ira->codegen->invalid_instruction;
23970
-
23971
24011
IrInstruction *target = instruction->target->child;
23972
24012
if (type_is_invalid(target->value.type))
23973
24013
return ira->codegen->invalid_instruction;
23974
24014
24015
+ ZigType *elem_type = nullptr;
24016
+ if (is_slice(target->value.type)) {
24017
+ ZigType *slice_ptr_type = target->value.type->data.structure.fields[slice_ptr_index].type_entry;
24018
+ elem_type = slice_ptr_type->data.pointer.child_type;
24019
+ } else if (target->value.type->id == ZigTypeIdPointer) {
24020
+ elem_type = target->value.type->data.pointer.child_type;
24021
+ }
24022
+
24023
+ uint32_t align_bytes;
24024
+ IrInstruction *align_bytes_inst = instruction->align_bytes->child;
24025
+ if (!ir_resolve_align(ira, align_bytes_inst, elem_type, &align_bytes))
24026
+ return ira->codegen->invalid_instruction;
24027
+
23975
24028
IrInstruction *result = ir_align_cast(ira, target, align_bytes, true);
23976
24029
if (type_is_invalid(result->value.type))
23977
24030
return ira->codegen->invalid_instruction;
@@ -25644,7 +25697,7 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ConstExprValue *val) {
25644
25697
}
25645
25698
25646
25699
val->special = ConstValSpecialStatic;
25647
- assert(val->type->id == ZigTypeIdComptimeInt);
25700
+ assert(val->type->id == ZigTypeIdComptimeInt || val->type->id == ZigTypeIdInt );
25648
25701
bigint_init_unsigned(&val->data.x_bigint, align_in_bytes);
25649
25702
return ErrorNone;
25650
25703
}
@@ -25699,7 +25752,7 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ConstExprValue *val) {
25699
25752
}
25700
25753
25701
25754
val->special = ConstValSpecialStatic;
25702
- assert(val->type->id == ZigTypeIdComptimeInt);
25755
+ assert(val->type->id == ZigTypeIdComptimeInt || val->type->id == ZigTypeIdInt );
25703
25756
bigint_init_unsigned(&val->data.x_bigint, abi_size);
25704
25757
return ErrorNone;
25705
25758
}
@@ -25885,7 +25938,7 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ConstExprValue *val) {
25885
25938
Error ir_resolve_lazy(CodeGen *codegen, AstNode *source_node, ConstExprValue *val) {
25886
25939
Error err;
25887
25940
if ((err = ir_resolve_lazy_raw(source_node, val))) {
25888
- if (codegen->trace_err != nullptr && !source_node->already_traced_this_node) {
25941
+ if (codegen->trace_err != nullptr && source_node != nullptr && !source_node->already_traced_this_node) {
25889
25942
source_node->already_traced_this_node = true;
25890
25943
codegen->trace_err = add_error_note(codegen, codegen->trace_err, source_node,
25891
25944
buf_create_from_str("referenced here"));
0 commit comments