authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-27 12:54:50-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-27 12:54:50-04:00
logd9ed55f017bdd23054cd1d7f50ac1da517b5a5e4
tree6c4373b33345846a4ee9e467e430830b8b42c0a5
parent7d34e55a71b1d929c6e847337a3639f3e65a547e
signaturelock-open Commit is signed but in an unrecognized format.

fix not properly casting align values

and add check for alignment specified on enum fields

4 files changed, 137 insertions(+), 108 deletions(-)

src/all_types.hpp+23-20
......@@ -315,58 +315,61 @@ enum LazyValueId {
315315};
316316
317317struct LazyValue {
318 IrExecutable *exec;
319318 LazyValueId id;
320319};
321320
322321struct LazyValueAlignOf {
323322 LazyValue base;
324323
325 ConstExprValue *target_type_val;
326 AstNode *target_type_src_node;
324 IrAnalyze *ira;
325 IrInstruction *target_type;
327326};
328327
329328struct LazyValueSliceType {
330329 LazyValue base;
330
331 IrAnalyze *ira;
332 ZigType *elem_type;
333 IrInstruction *align_inst; // can be null
334
331335 bool is_const;
332336 bool is_volatile;
333337 bool is_allowzero;
334
335 ZigType *elem_type;
336 ConstExprValue *align_val; // can be null
337338};
338339
339340struct LazyValuePtrType {
340341 LazyValue base;
341 bool is_const;
342 bool is_volatile;
343 bool is_allowzero;
344342
345 ConstExprValue *elem_type_val;
346 AstNode *elem_type_src_node;
347 ConstExprValue *align_val; // can be null
343 IrAnalyze *ira;
344 IrInstruction *elem_type;
345 IrInstruction *align_inst; // can be null
346
348347 PtrLen ptr_len;
349348 uint32_t bit_offset_in_host;
349
350350 uint32_t host_int_bytes;
351 bool is_const;
352 bool is_volatile;
353 bool is_allowzero;
351354};
352355
353356struct LazyValueOptType {
354357 LazyValue base;
355358
356 ConstExprValue *payload_type_val;
357 AstNode *payload_type_src_node;
359 IrAnalyze *ira;
360 IrInstruction *payload_type;
358361};
359362
360363struct LazyValueFnType {
361364 LazyValue base;
362 bool is_generic;
363365
366 IrAnalyze *ira;
364367 AstNode *proto_node;
365 ConstExprValue **param_types;
366 AstNode **param_type_src_nodes;
367 ConstExprValue *align_val; // can be null
368 ConstExprValue *return_type;
369 AstNode *return_type_src_node;
368 IrInstruction **param_types;
369 IrInstruction *align_inst; // can be null
370 IrInstruction *return_type;
371
372 bool is_generic;
370373};
371374
372375struct ConstExprValue {
src/analyze.cpp+12-7
......@@ -1000,7 +1000,7 @@ static Error type_val_resolve_zero_bits(CodeGen *g, ConstExprValue *type_val, Zi
10001000 case LazyValueIdPtrType: {
10011001 LazyValuePtrType *lazy_ptr_type = reinterpret_cast<LazyValuePtrType *>(type_val->data.x_lazy);
10021002
1003 if (parent_type_val == lazy_ptr_type->elem_type_val) {
1003 if (parent_type_val == &lazy_ptr_type->elem_type->value) {
10041004 // Does a struct which contains a pointer field to itself have bits? Yes.
10051005 *is_zero_bits = false;
10061006 return ErrorNone;
......@@ -1008,7 +1008,7 @@ static Error type_val_resolve_zero_bits(CodeGen *g, ConstExprValue *type_val, Zi
10081008 if (parent_type_val == nullptr) {
10091009 parent_type_val = type_val;
10101010 }
1011 return type_val_resolve_zero_bits(g, lazy_ptr_type->elem_type_val, parent_type,
1011 return type_val_resolve_zero_bits(g, &lazy_ptr_type->elem_type->value, parent_type,
10121012 parent_type_val, is_zero_bits);
10131013 }
10141014 }
......@@ -1061,17 +1061,17 @@ static ReqCompTime type_val_resolve_requires_comptime(CodeGen *g, ConstExprValue
10611061 }
10621062 case LazyValueIdPtrType: {
10631063 LazyValuePtrType *lazy_ptr_type = reinterpret_cast<LazyValuePtrType *>(type_val->data.x_lazy);
1064 return type_val_resolve_requires_comptime(g, lazy_ptr_type->elem_type_val);
1064 return type_val_resolve_requires_comptime(g, &lazy_ptr_type->elem_type->value);
10651065 }
10661066 case LazyValueIdOptType: {
10671067 LazyValueOptType *lazy_opt_type = reinterpret_cast<LazyValueOptType *>(type_val->data.x_lazy);
1068 return type_val_resolve_requires_comptime(g, lazy_opt_type->payload_type_val);
1068 return type_val_resolve_requires_comptime(g, &lazy_opt_type->payload_type->value);
10691069 }
10701070 case LazyValueIdFnType: {
10711071 LazyValueFnType *lazy_fn_type = reinterpret_cast<LazyValueFnType *>(type_val->data.x_lazy);
10721072 if (lazy_fn_type->is_generic)
10731073 return ReqCompTimeYes;
1074 switch (type_val_resolve_requires_comptime(g, lazy_fn_type->return_type)) {
1074 switch (type_val_resolve_requires_comptime(g, &lazy_fn_type->return_type->value)) {
10751075 case ReqCompTimeInvalid:
10761076 return ReqCompTimeInvalid;
10771077 case ReqCompTimeYes:
......@@ -1084,7 +1084,7 @@ static ReqCompTime type_val_resolve_requires_comptime(CodeGen *g, ConstExprValue
10841084 AstNode *param_node = lazy_fn_type->proto_node->data.fn_proto.params.at(i);
10851085 bool param_is_var_args = param_node->data.param_decl.is_var_args;
10861086 if (param_is_var_args) break;
1087 switch (type_val_resolve_requires_comptime(g, lazy_fn_type->param_types[i])) {
1087 switch (type_val_resolve_requires_comptime(g, &lazy_fn_type->param_types[i]->value)) {
10881088 case ReqCompTimeInvalid:
10891089 return ReqCompTimeInvalid;
10901090 case ReqCompTimeYes:
......@@ -1124,7 +1124,7 @@ Error type_val_resolve_abi_align(CodeGen *g, ConstExprValue *type_val, uint32_t
11241124 return ErrorNone;
11251125 case LazyValueIdOptType: {
11261126 LazyValueOptType *lazy_opt_type = reinterpret_cast<LazyValueOptType *>(type_val->data.x_lazy);
1127 return type_val_resolve_abi_align(g, lazy_opt_type->payload_type_val, abi_align);
1127 return type_val_resolve_abi_align(g, &lazy_opt_type->payload_type->value, abi_align);
11281128 }
11291129 }
11301130 zig_unreachable();
......@@ -2245,6 +2245,11 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
22452245 buf_sprintf("structs and unions, not enums, support field types"));
22462246 add_error_note(g, msg, decl_node,
22472247 buf_sprintf("consider 'union(enum)' here"));
2248 } else if (field_node->data.struct_field.align_expr != nullptr) {
2249 ErrorMsg *msg = add_node_error(g, field_node->data.struct_field.align_expr,
2250 buf_sprintf("structs and unions, not enums, support field alignment"));
2251 add_error_note(g, msg, decl_node,
2252 buf_sprintf("consider 'union(enum)' here"));
22482253 }
22492254
22502255 auto field_entry = enum_type->data.enumeration.fields_by_name.put_unique(type_enum_field->name, type_enum_field);
src/ir.cpp+75-81
......@@ -16190,14 +16190,13 @@ static IrInstruction *ir_analyze_optional_type(IrAnalyze *ira, IrInstructionUnOp
1619016190 result->value.special = ConstValSpecialLazy;
1619116191
1619216192 LazyValueOptType *lazy_opt_type = allocate<LazyValueOptType>(1);
16193 lazy_opt_type->ira = ira;
1619316194 result->value.data.x_lazy = &lazy_opt_type->base;
1619416195 lazy_opt_type->base.id = LazyValueIdOptType;
16195 lazy_opt_type->base.exec = ira->new_irb.exec;
1619616196
16197 lazy_opt_type->payload_type_val = ir_resolve_type_lazy(ira, instruction->value->child);
16198 if (lazy_opt_type->payload_type_val == nullptr)
16197 lazy_opt_type->payload_type = instruction->value->child;
16198 if (ir_resolve_type_lazy(ira, lazy_opt_type->payload_type) == nullptr)
1619916199 return ira->codegen->invalid_instruction;
16200 lazy_opt_type->payload_type_src_node = instruction->value->source_node;
1620116200
1620216201 return result;
1620316202}
......@@ -17936,13 +17935,13 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1793617935 result->value.special = ConstValSpecialLazy;
1793717936
1793817937 LazyValueSliceType *lazy_slice_type = allocate<LazyValueSliceType>(1);
17938 lazy_slice_type->ira = ira;
1793917939 result->value.data.x_lazy = &lazy_slice_type->base;
1794017940 lazy_slice_type->base.id = LazyValueIdSliceType;
17941 lazy_slice_type->base.exec = ira->new_irb.exec;
1794217941
1794317942 if (slice_type_instruction->align_value != nullptr) {
17944 lazy_slice_type->align_val = ir_resolve_const(ira, slice_type_instruction->align_value->child, LazyOk);
17945 if (lazy_slice_type->align_val == nullptr)
17943 lazy_slice_type->align_inst = slice_type_instruction->align_value->child;
17944 if (ir_resolve_const(ira, lazy_slice_type->align_inst, LazyOk) == nullptr)
1794617945 return ira->codegen->invalid_instruction;
1794717946 }
1794817947
......@@ -22371,14 +22370,13 @@ static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstruct
2237122370 result->value.special = ConstValSpecialLazy;
2237222371
2237322372 LazyValueAlignOf *lazy_align_of = allocate<LazyValueAlignOf>(1);
22373 lazy_align_of->ira = ira;
2237422374 result->value.data.x_lazy = &lazy_align_of->base;
2237522375 lazy_align_of->base.id = LazyValueIdAlignOf;
22376 lazy_align_of->base.exec = ira->new_irb.exec;
2237722376
22378 lazy_align_of->target_type_val = ir_resolve_type_lazy(ira, instruction->type_value->child);
22379 if (lazy_align_of->target_type_val == nullptr)
22377 lazy_align_of->target_type = instruction->type_value->child;
22378 if (ir_resolve_type_lazy(ira, lazy_align_of->target_type) == nullptr)
2238022379 return ira->codegen->invalid_instruction;
22381 lazy_align_of->target_type_src_node = instruction->type_value->source_node;
2238222380
2238322381 return result;
2238422382}
......@@ -22856,9 +22854,9 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct
2285622854 result->value.special = ConstValSpecialLazy;
2285722855
2285822856 LazyValueFnType *lazy_fn_type = allocate<LazyValueFnType>(1);
22857 lazy_fn_type->ira = ira;
2285922858 result->value.data.x_lazy = &lazy_fn_type->base;
2286022859 lazy_fn_type->base.id = LazyValueIdFnType;
22861 lazy_fn_type->base.exec = ira->new_irb.exec;
2286222860
2286322861 if (proto_node->data.fn_proto.auto_err_set) {
2286422862 ir_add_error(ira, &instruction->base,
......@@ -22868,8 +22866,7 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct
2286822866
2286922867 size_t param_count = proto_node->data.fn_proto.params.length;
2287022868 lazy_fn_type->proto_node = proto_node;
22871 lazy_fn_type->param_types = allocate<ConstExprValue *>(param_count);
22872 lazy_fn_type->param_type_src_nodes = allocate<AstNode *>(param_count);
22869 lazy_fn_type->param_types = allocate<IrInstruction *>(param_count);
2287322870
2287422871 for (size_t param_index = 0; param_index < param_count; param_index += 1) {
2287522872 AstNode *param_node = proto_node->data.fn_proto.params.at(param_index);
......@@ -22895,23 +22892,20 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct
2289522892 IrInstruction *param_type_value = instruction->param_types[param_index]->child;
2289622893 if (type_is_invalid(param_type_value->value.type))
2289722894 return ira->codegen->invalid_instruction;
22898 ConstExprValue *param_type_val = ir_resolve_const(ira, param_type_value, LazyOk);
22899 if (param_type_val == nullptr)
22895 if (ir_resolve_const(ira, param_type_value, LazyOk) == nullptr)
2290022896 return ira->codegen->invalid_instruction;
22901 lazy_fn_type->param_types[param_index] = param_type_val;
22902 lazy_fn_type->param_type_src_nodes[param_index] = instruction->param_types[param_index]->source_node;
22897 lazy_fn_type->param_types[param_index] = param_type_value;
2290322898 }
2290422899
2290522900 if (instruction->align_value != nullptr) {
22906 lazy_fn_type->align_val = ir_resolve_const(ira, instruction->align_value->child, LazyOk);
22907 if (lazy_fn_type->align_val == nullptr)
22901 lazy_fn_type->align_inst = instruction->align_value->child;
22902 if (ir_resolve_const(ira, lazy_fn_type->align_inst, LazyOk) == nullptr)
2290822903 return ira->codegen->invalid_instruction;
2290922904 }
2291022905
22911 lazy_fn_type->return_type = ir_resolve_const(ira, instruction->return_type->child, LazyOk);
22912 if (lazy_fn_type->return_type == nullptr)
22906 lazy_fn_type->return_type = instruction->return_type->child;
22907 if (ir_resolve_const(ira, lazy_fn_type->return_type, LazyOk) == nullptr)
2291322908 return ira->codegen->invalid_instruction;
22914 lazy_fn_type->return_type_src_node = instruction->return_type->source_node;
2291522909
2291622910 return result;
2291722911}
......@@ -23890,18 +23884,17 @@ static IrInstruction *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstruct
2389023884 result->value.special = ConstValSpecialLazy;
2389123885
2389223886 LazyValuePtrType *lazy_ptr_type = allocate<LazyValuePtrType>(1);
23887 lazy_ptr_type->ira = ira;
2389323888 result->value.data.x_lazy = &lazy_ptr_type->base;
2389423889 lazy_ptr_type->base.id = LazyValueIdPtrType;
23895 lazy_ptr_type->base.exec = ira->new_irb.exec;
2389623890
23897 lazy_ptr_type->elem_type_val = ir_resolve_type_lazy(ira, instruction->child_type->child);
23898 if (lazy_ptr_type->elem_type_val == nullptr)
23891 lazy_ptr_type->elem_type = instruction->child_type->child;
23892 if (ir_resolve_type_lazy(ira, lazy_ptr_type->elem_type) == nullptr)
2389923893 return ira->codegen->invalid_instruction;
23900 lazy_ptr_type->elem_type_src_node = instruction->child_type->source_node;
2390123894
2390223895 if (instruction->align_value != nullptr) {
23903 lazy_ptr_type->align_val = ir_resolve_const(ira, instruction->align_value->child, LazyOk);
23904 if (lazy_ptr_type->align_val == nullptr)
23896 lazy_ptr_type->align_inst = instruction->align_value->child;
23897 if (ir_resolve_const(ira, lazy_ptr_type->align_inst, LazyOk) == nullptr)
2390523898 return ira->codegen->invalid_instruction;
2390623899 }
2390723900
......@@ -25446,9 +25439,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2544625439 zig_unreachable();
2544725440}
2544825441
25449static ZigType *ir_resolve_lazy_fn_type(CodeGen *codegen, IrExecutable *exec, AstNode *source_node,
25450 LazyValueFnType *lazy_fn_type)
25451{
25442static ZigType *ir_resolve_lazy_fn_type(IrAnalyze *ira, AstNode *source_node, LazyValueFnType *lazy_fn_type) {
2545225443 Error err;
2545325444 AstNode *proto_node = lazy_fn_type->proto_node;
2545425445
......@@ -25465,7 +25456,7 @@ static ZigType *ir_resolve_lazy_fn_type(CodeGen *codegen, IrExecutable *exec, As
2546525456 fn_type_id.param_count = fn_type_id.next_param_index;
2546625457 continue;
2546725458 } else if (fn_type_id.cc == CallingConventionUnspecified) {
25468 return get_generic_fn_type(codegen, &fn_type_id);
25459 return get_generic_fn_type(ira->codegen, &fn_type_id);
2546925460 } else {
2547025461 zig_unreachable();
2547125462 }
......@@ -25475,34 +25466,33 @@ static ZigType *ir_resolve_lazy_fn_type(CodeGen *codegen, IrExecutable *exec, As
2547525466
2547625467 if (lazy_fn_type->param_types[fn_type_id.next_param_index] == nullptr) {
2547725468 param_info->type = nullptr;
25478 return get_generic_fn_type(codegen, &fn_type_id);
25469 return get_generic_fn_type(ira->codegen, &fn_type_id);
2547925470 } else {
25480 AstNode *param_src_node = lazy_fn_type->param_type_src_nodes[fn_type_id.next_param_index];
25481 ZigType *param_type = ir_resolve_const_type(codegen, exec, param_src_node,
25482 lazy_fn_type->param_types[fn_type_id.next_param_index]);
25471 IrInstruction *param_type_inst = lazy_fn_type->param_types[fn_type_id.next_param_index];
25472 ZigType *param_type = ir_resolve_type(ira, param_type_inst);
2548325473 if (type_is_invalid(param_type))
2548425474 return nullptr;
25485 switch (type_requires_comptime(codegen, param_type)) {
25475 switch (type_requires_comptime(ira->codegen, param_type)) {
2548625476 case ReqCompTimeYes:
2548725477 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
25488 exec_add_error_node(codegen, exec, param_src_node,
25478 ir_add_error(ira, param_type_inst,
2548925479 buf_sprintf("parameter of type '%s' not allowed in function with calling convention '%s'",
2549025480 buf_ptr(&param_type->name), calling_convention_name(fn_type_id.cc)));
2549125481 return nullptr;
2549225482 }
2549325483 param_info->type = param_type;
2549425484 fn_type_id.next_param_index += 1;
25495 return get_generic_fn_type(codegen, &fn_type_id);
25485 return get_generic_fn_type(ira->codegen, &fn_type_id);
2549625486 case ReqCompTimeInvalid:
2549725487 return nullptr;
2549825488 case ReqCompTimeNo:
2549925489 break;
2550025490 }
2550125491 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
25502 if ((err = type_resolve(codegen, param_type, ResolveStatusZeroBitsKnown)))
25492 if ((err = type_resolve(ira->codegen, param_type, ResolveStatusZeroBitsKnown)))
2550325493 return nullptr;
2550425494 if (!type_has_bits(param_type)) {
25505 exec_add_error_node(codegen, exec, param_src_node,
25495 ir_add_error(ira, param_type_inst,
2550625496 buf_sprintf("parameter of type '%s' has 0 bits; not allowed in function with calling convention '%s'",
2550725497 buf_ptr(&param_type->name), calling_convention_name(fn_type_id.cc)));
2550825498 return nullptr;
......@@ -25512,37 +25502,35 @@ static ZigType *ir_resolve_lazy_fn_type(CodeGen *codegen, IrExecutable *exec, As
2551225502 }
2551325503 }
2551425504
25515 if (lazy_fn_type->align_val != nullptr) {
25516 if (!ir_resolve_const_align(codegen, exec, source_node, lazy_fn_type->align_val, &fn_type_id.alignment))
25505 if (lazy_fn_type->align_inst != nullptr) {
25506 if (!ir_resolve_align(ira, lazy_fn_type->align_inst, &fn_type_id.alignment))
2551725507 return nullptr;
2551825508 }
2551925509
25520 fn_type_id.return_type = ir_resolve_const_type(codegen, exec, lazy_fn_type->return_type_src_node,
25521 lazy_fn_type->return_type);
25510 fn_type_id.return_type = ir_resolve_type(ira, lazy_fn_type->return_type);
2552225511 if (type_is_invalid(fn_type_id.return_type))
2552325512 return nullptr;
2552425513 if (fn_type_id.return_type->id == ZigTypeIdOpaque) {
25525 exec_add_error_node(codegen, exec, lazy_fn_type->return_type_src_node,
25526 buf_create_from_str("return type cannot be opaque"));
25514 ir_add_error(ira, lazy_fn_type->return_type, buf_create_from_str("return type cannot be opaque"));
2552725515 return nullptr;
2552825516 }
2552925517
25530 return get_fn_type(codegen, &fn_type_id);
25518 return get_fn_type(ira->codegen, &fn_type_id);
2553125519}
2553225520
25533static Error ir_resolve_lazy_raw(CodeGen *codegen, AstNode *source_node, ConstExprValue *val) {
25521static Error ir_resolve_lazy_raw(AstNode *source_node, ConstExprValue *val) {
2553425522 Error err;
2553525523 if (val->special != ConstValSpecialLazy)
2553625524 return ErrorNone;
25537 IrExecutable *exec = val->data.x_lazy->exec;
2553825525 switch (val->data.x_lazy->id) {
2553925526 case LazyValueIdInvalid:
2554025527 zig_unreachable();
2554125528 case LazyValueIdAlignOf: {
2554225529 LazyValueAlignOf *lazy_align_of = reinterpret_cast<LazyValueAlignOf *>(val->data.x_lazy);
25530 IrAnalyze *ira = lazy_align_of->ira;
2554325531
25544 if (lazy_align_of->target_type_val->special == ConstValSpecialStatic) {
25545 switch (lazy_align_of->target_type_val->data.x_type->id) {
25532 if (lazy_align_of->target_type->value.special == ConstValSpecialStatic) {
25533 switch (lazy_align_of->target_type->value.data.x_type->id) {
2554625534 case ZigTypeIdInvalid:
2554725535 zig_unreachable();
2554825536 case ZigTypeIdMetaType:
......@@ -25556,9 +25544,9 @@ static Error ir_resolve_lazy_raw(CodeGen *codegen, AstNode *source_node, ConstEx
2555625544 case ZigTypeIdArgTuple:
2555725545 case ZigTypeIdVoid:
2555825546 case ZigTypeIdOpaque:
25559 exec_add_error_node(codegen, exec, lazy_align_of->target_type_src_node,
25547 ir_add_error(ira, lazy_align_of->target_type,
2556025548 buf_sprintf("no align available for type '%s'",
25561 buf_ptr(&lazy_align_of->target_type_val->data.x_type->name)));
25549 buf_ptr(&lazy_align_of->target_type->value.data.x_type->name)));
2556225550 return ErrorSemanticAnalyzeFail;
2556325551 case ZigTypeIdBool:
2556425552 case ZigTypeIdInt:
......@@ -25580,8 +25568,11 @@ static Error ir_resolve_lazy_raw(CodeGen *codegen, AstNode *source_node, ConstEx
2558025568 }
2558125569
2558225570 uint32_t align_in_bytes;
25583 if ((err = type_val_resolve_abi_align(codegen, lazy_align_of->target_type_val, &align_in_bytes)))
25571 if ((err = type_val_resolve_abi_align(ira->codegen, &lazy_align_of->target_type->value,
25572 &align_in_bytes)))
25573 {
2558425574 return err;
25575 }
2558525576
2558625577 val->special = ConstValSpecialStatic;
2558725578 assert(val->type->id == ZigTypeIdComptimeInt);
......@@ -25590,69 +25581,72 @@ static Error ir_resolve_lazy_raw(CodeGen *codegen, AstNode *source_node, ConstEx
2559025581 }
2559125582 case LazyValueIdSliceType: {
2559225583 LazyValueSliceType *lazy_slice_type = reinterpret_cast<LazyValueSliceType *>(val->data.x_lazy);
25584 IrAnalyze *ira = lazy_slice_type->ira;
25585
2559325586 uint32_t align_bytes = 0;
25594 if (lazy_slice_type->align_val != nullptr) {
25595 if (!ir_resolve_const_align(codegen, exec, source_node, lazy_slice_type->align_val, &align_bytes))
25587 if (lazy_slice_type->align_inst != nullptr) {
25588 if (!ir_resolve_align(ira, lazy_slice_type->align_inst, &align_bytes))
2559625589 return ErrorSemanticAnalyzeFail;
2559725590 }
2559825591 ResolveStatus needed_status = (align_bytes == 0) ?
2559925592 ResolveStatusZeroBitsKnown : ResolveStatusAlignmentKnown;
25600 if ((err = type_resolve(codegen, lazy_slice_type->elem_type, needed_status)))
25593 if ((err = type_resolve(ira->codegen, lazy_slice_type->elem_type, needed_status)))
2560125594 return err;
25602 ZigType *slice_ptr_type = get_pointer_to_type_extra(codegen, lazy_slice_type->elem_type,
25595 ZigType *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, lazy_slice_type->elem_type,
2560325596 lazy_slice_type->is_const, lazy_slice_type->is_volatile, PtrLenUnknown, align_bytes,
2560425597 0, 0, lazy_slice_type->is_allowzero);
2560525598 val->special = ConstValSpecialStatic;
2560625599 assert(val->type->id == ZigTypeIdMetaType);
25607 val->data.x_type = get_slice_type(codegen, slice_ptr_type);
25600 val->data.x_type = get_slice_type(ira->codegen, slice_ptr_type);
2560825601 return ErrorNone;
2560925602 }
2561025603 case LazyValueIdPtrType: {
2561125604 LazyValuePtrType *lazy_ptr_type = reinterpret_cast<LazyValuePtrType *>(val->data.x_lazy);
25605 IrAnalyze *ira = lazy_ptr_type->ira;
25606
2561225607 uint32_t align_bytes = 0;
25613 if (lazy_ptr_type->align_val != nullptr) {
25614 if (!ir_resolve_const_align(codegen, exec, source_node, lazy_ptr_type->align_val, &align_bytes))
25608 if (lazy_ptr_type->align_inst != nullptr) {
25609 if (!ir_resolve_align(ira, lazy_ptr_type->align_inst, &align_bytes))
2561525610 return ErrorSemanticAnalyzeFail;
2561625611 }
25617 ZigType *elem_type = ir_resolve_const_type(codegen, exec, lazy_ptr_type->elem_type_src_node,
25618 lazy_ptr_type->elem_type_val);
25612 ZigType *elem_type = ir_resolve_type(ira, lazy_ptr_type->elem_type);
2561925613 if (type_is_invalid(elem_type))
2562025614 return ErrorSemanticAnalyzeFail;
2562125615
2562225616 if (elem_type->id == ZigTypeIdUnreachable) {
25623 exec_add_error_node(codegen, exec, lazy_ptr_type->elem_type_src_node,
25617 ir_add_error(ira, lazy_ptr_type->elem_type,
2562425618 buf_create_from_str("pointer to noreturn not allowed"));
2562525619 return ErrorSemanticAnalyzeFail;
2562625620 } else if (elem_type->id == ZigTypeIdOpaque && lazy_ptr_type->ptr_len == PtrLenUnknown) {
25627 exec_add_error_node(codegen, exec, lazy_ptr_type->elem_type_src_node,
25621 ir_add_error(ira, lazy_ptr_type->elem_type,
2562825622 buf_create_from_str("unknown-length pointer to opaque"));
2562925623 return ErrorSemanticAnalyzeFail;
2563025624 } else if (lazy_ptr_type->ptr_len == PtrLenC) {
25631 if (!type_allowed_in_extern(codegen, elem_type)) {
25632 exec_add_error_node(codegen, exec, lazy_ptr_type->elem_type_src_node,
25625 if (!type_allowed_in_extern(ira->codegen, elem_type)) {
25626 ir_add_error(ira, lazy_ptr_type->elem_type,
2563325627 buf_sprintf("C pointers cannot point to non-C-ABI-compatible type '%s'",
2563425628 buf_ptr(&elem_type->name)));
2563525629 return ErrorSemanticAnalyzeFail;
2563625630 } else if (elem_type->id == ZigTypeIdOpaque) {
25637 exec_add_error_node(codegen, exec, lazy_ptr_type->elem_type_src_node,
25631 ir_add_error(ira, lazy_ptr_type->elem_type,
2563825632 buf_sprintf("C pointers cannot point opaque types"));
2563925633 return ErrorSemanticAnalyzeFail;
2564025634 } else if (lazy_ptr_type->is_allowzero) {
25641 exec_add_error_node(codegen, exec, lazy_ptr_type->elem_type_src_node,
25635 ir_add_error(ira, lazy_ptr_type->elem_type,
2564225636 buf_sprintf("C pointers always allow address zero"));
2564325637 return ErrorSemanticAnalyzeFail;
2564425638 }
2564525639 }
2564625640
2564725641 if (align_bytes != 0) {
25648 if ((err = type_resolve(codegen, elem_type, ResolveStatusAlignmentKnown)))
25642 if ((err = type_resolve(ira->codegen, elem_type, ResolveStatusAlignmentKnown)))
2564925643 return err;
2565025644 if (!type_has_bits(elem_type))
2565125645 align_bytes = 0;
2565225646 }
2565325647 bool allow_zero = lazy_ptr_type->is_allowzero || lazy_ptr_type->ptr_len == PtrLenC;
2565425648 assert(val->type->id == ZigTypeIdMetaType);
25655 val->data.x_type = get_pointer_to_type_extra(codegen, elem_type,
25649 val->data.x_type = get_pointer_to_type_extra(ira->codegen, elem_type,
2565625650 lazy_ptr_type->is_const, lazy_ptr_type->is_volatile, lazy_ptr_type->ptr_len, align_bytes,
2565725651 lazy_ptr_type->bit_offset_in_host, lazy_ptr_type->host_int_bytes,
2565825652 allow_zero);
......@@ -25661,29 +25655,29 @@ static Error ir_resolve_lazy_raw(CodeGen *codegen, AstNode *source_node, ConstEx
2566125655 }
2566225656 case LazyValueIdOptType: {
2566325657 LazyValueOptType *lazy_opt_type = reinterpret_cast<LazyValueOptType *>(val->data.x_lazy);
25658 IrAnalyze *ira = lazy_opt_type->ira;
2566425659
25665 ZigType *payload_type = ir_resolve_const_type(codegen, exec, lazy_opt_type->payload_type_src_node,
25666 lazy_opt_type->payload_type_val);
25660 ZigType *payload_type = ir_resolve_type(ira, lazy_opt_type->payload_type);
2566725661 if (type_is_invalid(payload_type))
2566825662 return ErrorSemanticAnalyzeFail;
2566925663
2567025664 if (payload_type->id == ZigTypeIdOpaque || payload_type->id == ZigTypeIdUnreachable) {
25671 exec_add_error_node(codegen, exec, lazy_opt_type->payload_type_src_node,
25665 ir_add_error(ira, lazy_opt_type->payload_type,
2567225666 buf_sprintf("type '%s' cannot be optional", buf_ptr(&payload_type->name)));
2567325667 return ErrorSemanticAnalyzeFail;
2567425668 }
2567525669
25676 if ((err = type_resolve(codegen, payload_type, ResolveStatusSizeKnown)))
25670 if ((err = type_resolve(ira->codegen, payload_type, ResolveStatusSizeKnown)))
2567725671 return err;
2567825672
2567925673 assert(val->type->id == ZigTypeIdMetaType);
25680 val->data.x_type = get_optional_type(codegen, payload_type);
25674 val->data.x_type = get_optional_type(ira->codegen, payload_type);
2568125675 val->special = ConstValSpecialStatic;
2568225676 return ErrorNone;
2568325677 }
2568425678 case LazyValueIdFnType: {
25685 ZigType *fn_type = ir_resolve_lazy_fn_type(codegen, exec, source_node,
25686 reinterpret_cast<LazyValueFnType *>(val->data.x_lazy));
25679 LazyValueFnType *lazy_fn_type = reinterpret_cast<LazyValueFnType *>(val->data.x_lazy);
25680 ZigType *fn_type = ir_resolve_lazy_fn_type(lazy_fn_type->ira, source_node, lazy_fn_type);
2568725681 if (fn_type == nullptr)
2568825682 return ErrorSemanticAnalyzeFail;
2568925683 val->special = ConstValSpecialStatic;
......@@ -25697,7 +25691,7 @@ static Error ir_resolve_lazy_raw(CodeGen *codegen, AstNode *source_node, ConstEx
2569725691
2569825692Error ir_resolve_lazy(CodeGen *codegen, AstNode *source_node, ConstExprValue *val) {
2569925693 Error err;
25700 if ((err = ir_resolve_lazy_raw(codegen, source_node, val))) {
25694 if ((err = ir_resolve_lazy_raw(source_node, val))) {
2570125695 if (codegen->trace_err != nullptr && !source_node->already_traced_this_node) {
2570225696 source_node->already_traced_this_node = true;
2570325697 codegen->trace_err = add_error_note(codegen, codegen->trace_err, source_node,
test/compile_errors.zig+27
......@@ -2,6 +2,33 @@ const tests = @import("tests.zig");
22const builtin = @import("builtin");
33
44pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add(
6 "alignment of enum field specified",
7 \\const Number = enum {
8 \\ a,
9 \\ b align(i32),
10 \\};
11 \\export fn entry1() void {
12 \\ var x: Number = undefined;
13 \\}
14 ,
15 "tmp.zig:3:13: error: structs and unions, not enums, support field alignment",
16 "tmp.zig:1:16: note: consider 'union(enum)' here",
17 );
18
19 cases.add(
20 "bad alignment type",
21 \\export fn entry1() void {
22 \\ var x: []align(true) i32 = undefined;
23 \\}
24 \\export fn entry2() void {
25 \\ var x: *align(f64(12.34)) i32 = undefined;
26 \\}
27 ,
28 "tmp.zig:2:20: error: expected type 'u29', found 'bool'",
29 "tmp.zig:5:22: error: fractional component prevents float value 12.340000 from being casted to type 'u29'",
30 );
31
532 cases.addCase(x: {
633 var tc = cases.create("variable in inline assembly template cannot be found",
734 \\export fn entry() void {