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
signature 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 {...@@ -315,58 +315,61 @@ enum LazyValueId {
315};315};
316316
317struct LazyValue {317struct LazyValue {
318 IrExecutable *exec;
319 LazyValueId id;318 LazyValueId id;
320};319};
321320
322struct LazyValueAlignOf {321struct LazyValueAlignOf {
323 LazyValue base;322 LazyValue base;
324323
325 ConstExprValue *target_type_val;324 IrAnalyze *ira;
326 AstNode *target_type_src_node;325 IrInstruction *target_type;
327};326};
328327
329struct LazyValueSliceType {328struct LazyValueSliceType {
330 LazyValue base;329 LazyValue base;
330
331 IrAnalyze *ira;
332 ZigType *elem_type;
333 IrInstruction *align_inst; // can be null
334
331 bool is_const;335 bool is_const;
332 bool is_volatile;336 bool is_volatile;
333 bool is_allowzero;337 bool is_allowzero;
334
335 ZigType *elem_type;
336 ConstExprValue *align_val; // can be null
337};338};
338339
339struct LazyValuePtrType {340struct LazyValuePtrType {
340 LazyValue base;341 LazyValue base;
341 bool is_const;
342 bool is_volatile;
343 bool is_allowzero;
344342
345 ConstExprValue *elem_type_val;343 IrAnalyze *ira;
346 AstNode *elem_type_src_node;344 IrInstruction *elem_type;
347 ConstExprValue *align_val; // can be null345 IrInstruction *align_inst; // can be null
346
348 PtrLen ptr_len;347 PtrLen ptr_len;
349 uint32_t bit_offset_in_host;348 uint32_t bit_offset_in_host;
349
350 uint32_t host_int_bytes;350 uint32_t host_int_bytes;
351 bool is_const;
352 bool is_volatile;
353 bool is_allowzero;
351};354};
352355
353struct LazyValueOptType {356struct LazyValueOptType {
354 LazyValue base;357 LazyValue base;
355358
356 ConstExprValue *payload_type_val;359 IrAnalyze *ira;
357 AstNode *payload_type_src_node;360 IrInstruction *payload_type;
358};361};
359362
360struct LazyValueFnType {363struct LazyValueFnType {
361 LazyValue base;364 LazyValue base;
362 bool is_generic;
363365
366 IrAnalyze *ira;
364 AstNode *proto_node;367 AstNode *proto_node;
365 ConstExprValue **param_types;368 IrInstruction **param_types;
366 AstNode **param_type_src_nodes;369 IrInstruction *align_inst; // can be null
367 ConstExprValue *align_val; // can be null370 IrInstruction *return_type;
368 ConstExprValue *return_type;371
369 AstNode *return_type_src_node;372 bool is_generic;
370};373};
371374
372struct ConstExprValue {375struct ConstExprValue {
src/analyze.cpp+12-7
...@@ -1000,7 +1000,7 @@ static Error type_val_resolve_zero_bits(CodeGen *g, ConstExprValue *type_val, Zi...@@ -1000,7 +1000,7 @@ static Error type_val_resolve_zero_bits(CodeGen *g, ConstExprValue *type_val, Zi
1000 case LazyValueIdPtrType: {1000 case LazyValueIdPtrType: {
1001 LazyValuePtrType *lazy_ptr_type = reinterpret_cast<LazyValuePtrType *>(type_val->data.x_lazy);1001 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) {
1004 // Does a struct which contains a pointer field to itself have bits? Yes.1004 // Does a struct which contains a pointer field to itself have bits? Yes.
1005 *is_zero_bits = false;1005 *is_zero_bits = false;
1006 return ErrorNone;1006 return ErrorNone;
...@@ -1008,7 +1008,7 @@ static Error type_val_resolve_zero_bits(CodeGen *g, ConstExprValue *type_val, Zi...@@ -1008,7 +1008,7 @@ static Error type_val_resolve_zero_bits(CodeGen *g, ConstExprValue *type_val, Zi
1008 if (parent_type_val == nullptr) {1008 if (parent_type_val == nullptr) {
1009 parent_type_val = type_val;1009 parent_type_val = type_val;
1010 }1010 }
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,
1012 parent_type_val, is_zero_bits);1012 parent_type_val, is_zero_bits);
1013 }1013 }
1014 }1014 }
...@@ -1061,17 +1061,17 @@ static ReqCompTime type_val_resolve_requires_comptime(CodeGen *g, ConstExprValue...@@ -1061,17 +1061,17 @@ static ReqCompTime type_val_resolve_requires_comptime(CodeGen *g, ConstExprValue
1061 }1061 }
1062 case LazyValueIdPtrType: {1062 case LazyValueIdPtrType: {
1063 LazyValuePtrType *lazy_ptr_type = reinterpret_cast<LazyValuePtrType *>(type_val->data.x_lazy);1063 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);
1065 }1065 }
1066 case LazyValueIdOptType: {1066 case LazyValueIdOptType: {
1067 LazyValueOptType *lazy_opt_type = reinterpret_cast<LazyValueOptType *>(type_val->data.x_lazy);1067 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);
1069 }1069 }
1070 case LazyValueIdFnType: {1070 case LazyValueIdFnType: {
1071 LazyValueFnType *lazy_fn_type = reinterpret_cast<LazyValueFnType *>(type_val->data.x_lazy);1071 LazyValueFnType *lazy_fn_type = reinterpret_cast<LazyValueFnType *>(type_val->data.x_lazy);
1072 if (lazy_fn_type->is_generic)1072 if (lazy_fn_type->is_generic)
1073 return ReqCompTimeYes;1073 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)) {
1075 case ReqCompTimeInvalid:1075 case ReqCompTimeInvalid:
1076 return ReqCompTimeInvalid;1076 return ReqCompTimeInvalid;
1077 case ReqCompTimeYes:1077 case ReqCompTimeYes:
...@@ -1084,7 +1084,7 @@ static ReqCompTime type_val_resolve_requires_comptime(CodeGen *g, ConstExprValue...@@ -1084,7 +1084,7 @@ static ReqCompTime type_val_resolve_requires_comptime(CodeGen *g, ConstExprValue
1084 AstNode *param_node = lazy_fn_type->proto_node->data.fn_proto.params.at(i);1084 AstNode *param_node = lazy_fn_type->proto_node->data.fn_proto.params.at(i);
1085 bool param_is_var_args = param_node->data.param_decl.is_var_args;1085 bool param_is_var_args = param_node->data.param_decl.is_var_args;
1086 if (param_is_var_args) break;1086 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)) {
1088 case ReqCompTimeInvalid:1088 case ReqCompTimeInvalid:
1089 return ReqCompTimeInvalid;1089 return ReqCompTimeInvalid;
1090 case ReqCompTimeYes:1090 case ReqCompTimeYes:
...@@ -1124,7 +1124,7 @@ Error type_val_resolve_abi_align(CodeGen *g, ConstExprValue *type_val, uint32_t...@@ -1124,7 +1124,7 @@ Error type_val_resolve_abi_align(CodeGen *g, ConstExprValue *type_val, uint32_t
1124 return ErrorNone;1124 return ErrorNone;
1125 case LazyValueIdOptType: {1125 case LazyValueIdOptType: {
1126 LazyValueOptType *lazy_opt_type = reinterpret_cast<LazyValueOptType *>(type_val->data.x_lazy);1126 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);
1128 }1128 }
1129 }1129 }
1130 zig_unreachable();1130 zig_unreachable();
...@@ -2245,6 +2245,11 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {...@@ -2245,6 +2245,11 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
2245 buf_sprintf("structs and unions, not enums, support field types"));2245 buf_sprintf("structs and unions, not enums, support field types"));
2246 add_error_note(g, msg, decl_node,2246 add_error_note(g, msg, decl_node,
2247 buf_sprintf("consider 'union(enum)' here"));2247 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"));
2248 }2253 }
22492254
2250 auto field_entry = enum_type->data.enumeration.fields_by_name.put_unique(type_enum_field->name, type_enum_field);2255 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...@@ -16190,14 +16190,13 @@ static IrInstruction *ir_analyze_optional_type(IrAnalyze *ira, IrInstructionUnOp
16190 result->value.special = ConstValSpecialLazy;16190 result->value.special = ConstValSpecialLazy;
1619116191
16192 LazyValueOptType *lazy_opt_type = allocate<LazyValueOptType>(1);16192 LazyValueOptType *lazy_opt_type = allocate<LazyValueOptType>(1);
16193 lazy_opt_type->ira = ira;
16193 result->value.data.x_lazy = &lazy_opt_type->base;16194 result->value.data.x_lazy = &lazy_opt_type->base;
16194 lazy_opt_type->base.id = LazyValueIdOptType;16195 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);16197 lazy_opt_type->payload_type = instruction->value->child;
16198 if (lazy_opt_type->payload_type_val == nullptr)16198 if (ir_resolve_type_lazy(ira, lazy_opt_type->payload_type) == nullptr)
16199 return ira->codegen->invalid_instruction;16199 return ira->codegen->invalid_instruction;
16200 lazy_opt_type->payload_type_src_node = instruction->value->source_node;
1620116200
16202 return result;16201 return result;
16203}16202}
...@@ -17936,13 +17935,13 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,...@@ -17936,13 +17935,13 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
17936 result->value.special = ConstValSpecialLazy;17935 result->value.special = ConstValSpecialLazy;
1793717936
17938 LazyValueSliceType *lazy_slice_type = allocate<LazyValueSliceType>(1);17937 LazyValueSliceType *lazy_slice_type = allocate<LazyValueSliceType>(1);
17938 lazy_slice_type->ira = ira;
17939 result->value.data.x_lazy = &lazy_slice_type->base;17939 result->value.data.x_lazy = &lazy_slice_type->base;
17940 lazy_slice_type->base.id = LazyValueIdSliceType;17940 lazy_slice_type->base.id = LazyValueIdSliceType;
17941 lazy_slice_type->base.exec = ira->new_irb.exec;
1794217941
17943 if (slice_type_instruction->align_value != nullptr) {17942 if (slice_type_instruction->align_value != nullptr) {
17944 lazy_slice_type->align_val = ir_resolve_const(ira, slice_type_instruction->align_value->child, LazyOk);17943 lazy_slice_type->align_inst = slice_type_instruction->align_value->child;
17945 if (lazy_slice_type->align_val == nullptr)17944 if (ir_resolve_const(ira, lazy_slice_type->align_inst, LazyOk) == nullptr)
17946 return ira->codegen->invalid_instruction;17945 return ira->codegen->invalid_instruction;
17947 }17946 }
1794817947
...@@ -22371,14 +22370,13 @@ static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstruct...@@ -22371,14 +22370,13 @@ static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstruct
22371 result->value.special = ConstValSpecialLazy;22370 result->value.special = ConstValSpecialLazy;
2237222371
22373 LazyValueAlignOf *lazy_align_of = allocate<LazyValueAlignOf>(1);22372 LazyValueAlignOf *lazy_align_of = allocate<LazyValueAlignOf>(1);
22373 lazy_align_of->ira = ira;
22374 result->value.data.x_lazy = &lazy_align_of->base;22374 result->value.data.x_lazy = &lazy_align_of->base;
22375 lazy_align_of->base.id = LazyValueIdAlignOf;22375 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);22377 lazy_align_of->target_type = instruction->type_value->child;
22379 if (lazy_align_of->target_type_val == nullptr)22378 if (ir_resolve_type_lazy(ira, lazy_align_of->target_type) == nullptr)
22380 return ira->codegen->invalid_instruction;22379 return ira->codegen->invalid_instruction;
22381 lazy_align_of->target_type_src_node = instruction->type_value->source_node;
2238222380
22383 return result;22381 return result;
22384}22382}
...@@ -22856,9 +22854,9 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct...@@ -22856,9 +22854,9 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct
22856 result->value.special = ConstValSpecialLazy;22854 result->value.special = ConstValSpecialLazy;
2285722855
22858 LazyValueFnType *lazy_fn_type = allocate<LazyValueFnType>(1);22856 LazyValueFnType *lazy_fn_type = allocate<LazyValueFnType>(1);
22857 lazy_fn_type->ira = ira;
22859 result->value.data.x_lazy = &lazy_fn_type->base;22858 result->value.data.x_lazy = &lazy_fn_type->base;
22860 lazy_fn_type->base.id = LazyValueIdFnType;22859 lazy_fn_type->base.id = LazyValueIdFnType;
22861 lazy_fn_type->base.exec = ira->new_irb.exec;
2286222860
22863 if (proto_node->data.fn_proto.auto_err_set) {22861 if (proto_node->data.fn_proto.auto_err_set) {
22864 ir_add_error(ira, &instruction->base,22862 ir_add_error(ira, &instruction->base,
...@@ -22868,8 +22866,7 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct...@@ -22868,8 +22866,7 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct
2286822866
22869 size_t param_count = proto_node->data.fn_proto.params.length;22867 size_t param_count = proto_node->data.fn_proto.params.length;
22870 lazy_fn_type->proto_node = proto_node;22868 lazy_fn_type->proto_node = proto_node;
22871 lazy_fn_type->param_types = allocate<ConstExprValue *>(param_count);22869 lazy_fn_type->param_types = allocate<IrInstruction *>(param_count);
22872 lazy_fn_type->param_type_src_nodes = allocate<AstNode *>(param_count);
2287322870
22874 for (size_t param_index = 0; param_index < param_count; param_index += 1) {22871 for (size_t param_index = 0; param_index < param_count; param_index += 1) {
22875 AstNode *param_node = proto_node->data.fn_proto.params.at(param_index);22872 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...@@ -22895,23 +22892,20 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct
22895 IrInstruction *param_type_value = instruction->param_types[param_index]->child;22892 IrInstruction *param_type_value = instruction->param_types[param_index]->child;
22896 if (type_is_invalid(param_type_value->value.type))22893 if (type_is_invalid(param_type_value->value.type))
22897 return ira->codegen->invalid_instruction;22894 return ira->codegen->invalid_instruction;
22898 ConstExprValue *param_type_val = ir_resolve_const(ira, param_type_value, LazyOk);22895 if (ir_resolve_const(ira, param_type_value, LazyOk) == nullptr)
22899 if (param_type_val == nullptr)
22900 return ira->codegen->invalid_instruction;22896 return ira->codegen->invalid_instruction;
22901 lazy_fn_type->param_types[param_index] = param_type_val;22897 lazy_fn_type->param_types[param_index] = param_type_value;
22902 lazy_fn_type->param_type_src_nodes[param_index] = instruction->param_types[param_index]->source_node;
22903 }22898 }
2290422899
22905 if (instruction->align_value != nullptr) {22900 if (instruction->align_value != nullptr) {
22906 lazy_fn_type->align_val = ir_resolve_const(ira, instruction->align_value->child, LazyOk);22901 lazy_fn_type->align_inst = instruction->align_value->child;
22907 if (lazy_fn_type->align_val == nullptr)22902 if (ir_resolve_const(ira, lazy_fn_type->align_inst, LazyOk) == nullptr)
22908 return ira->codegen->invalid_instruction;22903 return ira->codegen->invalid_instruction;
22909 }22904 }
2291022905
22911 lazy_fn_type->return_type = ir_resolve_const(ira, instruction->return_type->child, LazyOk);22906 lazy_fn_type->return_type = instruction->return_type->child;
22912 if (lazy_fn_type->return_type == nullptr)22907 if (ir_resolve_const(ira, lazy_fn_type->return_type, LazyOk) == nullptr)
22913 return ira->codegen->invalid_instruction;22908 return ira->codegen->invalid_instruction;
22914 lazy_fn_type->return_type_src_node = instruction->return_type->source_node;
2291522909
22916 return result;22910 return result;
22917}22911}
...@@ -23890,18 +23884,17 @@ static IrInstruction *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstruct...@@ -23890,18 +23884,17 @@ static IrInstruction *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstruct
23890 result->value.special = ConstValSpecialLazy;23884 result->value.special = ConstValSpecialLazy;
2389123885
23892 LazyValuePtrType *lazy_ptr_type = allocate<LazyValuePtrType>(1);23886 LazyValuePtrType *lazy_ptr_type = allocate<LazyValuePtrType>(1);
23887 lazy_ptr_type->ira = ira;
23893 result->value.data.x_lazy = &lazy_ptr_type->base;23888 result->value.data.x_lazy = &lazy_ptr_type->base;
23894 lazy_ptr_type->base.id = LazyValueIdPtrType;23889 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);23891 lazy_ptr_type->elem_type = instruction->child_type->child;
23898 if (lazy_ptr_type->elem_type_val == nullptr)23892 if (ir_resolve_type_lazy(ira, lazy_ptr_type->elem_type) == nullptr)
23899 return ira->codegen->invalid_instruction;23893 return ira->codegen->invalid_instruction;
23900 lazy_ptr_type->elem_type_src_node = instruction->child_type->source_node;
2390123894
23902 if (instruction->align_value != nullptr) {23895 if (instruction->align_value != nullptr) {
23903 lazy_ptr_type->align_val = ir_resolve_const(ira, instruction->align_value->child, LazyOk);23896 lazy_ptr_type->align_inst = instruction->align_value->child;
23904 if (lazy_ptr_type->align_val == nullptr)23897 if (ir_resolve_const(ira, lazy_ptr_type->align_inst, LazyOk) == nullptr)
23905 return ira->codegen->invalid_instruction;23898 return ira->codegen->invalid_instruction;
23906 }23899 }
2390723900
...@@ -25446,9 +25439,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {...@@ -25446,9 +25439,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
25446 zig_unreachable();25439 zig_unreachable();
25447}25440}
2544825441
25449static ZigType *ir_resolve_lazy_fn_type(CodeGen *codegen, IrExecutable *exec, AstNode *source_node,25442static ZigType *ir_resolve_lazy_fn_type(IrAnalyze *ira, AstNode *source_node, LazyValueFnType *lazy_fn_type) {
25450 LazyValueFnType *lazy_fn_type)
25451{
25452 Error err;25443 Error err;
25453 AstNode *proto_node = lazy_fn_type->proto_node;25444 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...@@ -25465,7 +25456,7 @@ static ZigType *ir_resolve_lazy_fn_type(CodeGen *codegen, IrExecutable *exec, As
25465 fn_type_id.param_count = fn_type_id.next_param_index;25456 fn_type_id.param_count = fn_type_id.next_param_index;
25466 continue;25457 continue;
25467 } else if (fn_type_id.cc == CallingConventionUnspecified) {25458 } 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);
25469 } else {25460 } else {
25470 zig_unreachable();25461 zig_unreachable();
25471 }25462 }
...@@ -25475,34 +25466,33 @@ static ZigType *ir_resolve_lazy_fn_type(CodeGen *codegen, IrExecutable *exec, As...@@ -25475,34 +25466,33 @@ static ZigType *ir_resolve_lazy_fn_type(CodeGen *codegen, IrExecutable *exec, As
2547525466
25476 if (lazy_fn_type->param_types[fn_type_id.next_param_index] == nullptr) {25467 if (lazy_fn_type->param_types[fn_type_id.next_param_index] == nullptr) {
25477 param_info->type = nullptr;25468 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);
25479 } else {25470 } else {
25480 AstNode *param_src_node = lazy_fn_type->param_type_src_nodes[fn_type_id.next_param_index];25471 IrInstruction *param_type_inst = lazy_fn_type->param_types[fn_type_id.next_param_index];
25481 ZigType *param_type = ir_resolve_const_type(codegen, exec, param_src_node,25472 ZigType *param_type = ir_resolve_type(ira, param_type_inst);
25482 lazy_fn_type->param_types[fn_type_id.next_param_index]);
25483 if (type_is_invalid(param_type))25473 if (type_is_invalid(param_type))
25484 return nullptr;25474 return nullptr;
25485 switch (type_requires_comptime(codegen, param_type)) {25475 switch (type_requires_comptime(ira->codegen, param_type)) {
25486 case ReqCompTimeYes:25476 case ReqCompTimeYes:
25487 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {25477 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,
25489 buf_sprintf("parameter of type '%s' not allowed in function with calling convention '%s'",25479 buf_sprintf("parameter of type '%s' not allowed in function with calling convention '%s'",
25490 buf_ptr(&param_type->name), calling_convention_name(fn_type_id.cc)));25480 buf_ptr(&param_type->name), calling_convention_name(fn_type_id.cc)));
25491 return nullptr;25481 return nullptr;
25492 }25482 }
25493 param_info->type = param_type;25483 param_info->type = param_type;
25494 fn_type_id.next_param_index += 1;25484 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);
25496 case ReqCompTimeInvalid:25486 case ReqCompTimeInvalid:
25497 return nullptr;25487 return nullptr;
25498 case ReqCompTimeNo:25488 case ReqCompTimeNo:
25499 break;25489 break;
25500 }25490 }
25501 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {25491 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)))
25503 return nullptr;25493 return nullptr;
25504 if (!type_has_bits(param_type)) {25494 if (!type_has_bits(param_type)) {
25505 exec_add_error_node(codegen, exec, param_src_node,25495 ir_add_error(ira, param_type_inst,
25506 buf_sprintf("parameter of type '%s' has 0 bits; not allowed in function with calling convention '%s'",25496 buf_sprintf("parameter of type '%s' has 0 bits; not allowed in function with calling convention '%s'",
25507 buf_ptr(&param_type->name), calling_convention_name(fn_type_id.cc)));25497 buf_ptr(&param_type->name), calling_convention_name(fn_type_id.cc)));
25508 return nullptr;25498 return nullptr;
...@@ -25512,37 +25502,35 @@ static ZigType *ir_resolve_lazy_fn_type(CodeGen *codegen, IrExecutable *exec, As...@@ -25512,37 +25502,35 @@ static ZigType *ir_resolve_lazy_fn_type(CodeGen *codegen, IrExecutable *exec, As
25512 }25502 }
25513 }25503 }
2551425504
25515 if (lazy_fn_type->align_val != nullptr) {25505 if (lazy_fn_type->align_inst != nullptr) {
25516 if (!ir_resolve_const_align(codegen, exec, source_node, lazy_fn_type->align_val, &fn_type_id.alignment))25506 if (!ir_resolve_align(ira, lazy_fn_type->align_inst, &fn_type_id.alignment))
25517 return nullptr;25507 return nullptr;
25518 }25508 }
2551925509
25520 fn_type_id.return_type = ir_resolve_const_type(codegen, exec, lazy_fn_type->return_type_src_node,25510 fn_type_id.return_type = ir_resolve_type(ira, lazy_fn_type->return_type);
25521 lazy_fn_type->return_type);
25522 if (type_is_invalid(fn_type_id.return_type))25511 if (type_is_invalid(fn_type_id.return_type))
25523 return nullptr;25512 return nullptr;
25524 if (fn_type_id.return_type->id == ZigTypeIdOpaque) {25513 if (fn_type_id.return_type->id == ZigTypeIdOpaque) {
25525 exec_add_error_node(codegen, exec, lazy_fn_type->return_type_src_node,25514 ir_add_error(ira, lazy_fn_type->return_type, buf_create_from_str("return type cannot be opaque"));
25526 buf_create_from_str("return type cannot be opaque"));
25527 return nullptr;25515 return nullptr;
25528 }25516 }
2552925517
25530 return get_fn_type(codegen, &fn_type_id);25518 return get_fn_type(ira->codegen, &fn_type_id);
25531}25519}
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) {
25534 Error err;25522 Error err;
25535 if (val->special != ConstValSpecialLazy)25523 if (val->special != ConstValSpecialLazy)
25536 return ErrorNone;25524 return ErrorNone;
25537 IrExecutable *exec = val->data.x_lazy->exec;
25538 switch (val->data.x_lazy->id) {25525 switch (val->data.x_lazy->id) {
25539 case LazyValueIdInvalid:25526 case LazyValueIdInvalid:
25540 zig_unreachable();25527 zig_unreachable();
25541 case LazyValueIdAlignOf: {25528 case LazyValueIdAlignOf: {
25542 LazyValueAlignOf *lazy_align_of = reinterpret_cast<LazyValueAlignOf *>(val->data.x_lazy);25529 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) {25532 if (lazy_align_of->target_type->value.special == ConstValSpecialStatic) {
25545 switch (lazy_align_of->target_type_val->data.x_type->id) {25533 switch (lazy_align_of->target_type->value.data.x_type->id) {
25546 case ZigTypeIdInvalid:25534 case ZigTypeIdInvalid:
25547 zig_unreachable();25535 zig_unreachable();
25548 case ZigTypeIdMetaType:25536 case ZigTypeIdMetaType:
...@@ -25556,9 +25544,9 @@ static Error ir_resolve_lazy_raw(CodeGen *codegen, AstNode *source_node, ConstEx...@@ -25556,9 +25544,9 @@ static Error ir_resolve_lazy_raw(CodeGen *codegen, AstNode *source_node, ConstEx
25556 case ZigTypeIdArgTuple:25544 case ZigTypeIdArgTuple:
25557 case ZigTypeIdVoid:25545 case ZigTypeIdVoid:
25558 case ZigTypeIdOpaque:25546 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,
25560 buf_sprintf("no align available for type '%s'",25548 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)));
25562 return ErrorSemanticAnalyzeFail;25550 return ErrorSemanticAnalyzeFail;
25563 case ZigTypeIdBool:25551 case ZigTypeIdBool:
25564 case ZigTypeIdInt:25552 case ZigTypeIdInt:
...@@ -25580,8 +25568,11 @@ static Error ir_resolve_lazy_raw(CodeGen *codegen, AstNode *source_node, ConstEx...@@ -25580,8 +25568,11 @@ static Error ir_resolve_lazy_raw(CodeGen *codegen, AstNode *source_node, ConstEx
25580 }25568 }
2558125569
25582 uint32_t align_in_bytes;25570 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 {
25584 return err;25574 return err;
25575 }
2558525576
25586 val->special = ConstValSpecialStatic;25577 val->special = ConstValSpecialStatic;
25587 assert(val->type->id == ZigTypeIdComptimeInt);25578 assert(val->type->id == ZigTypeIdComptimeInt);
...@@ -25590,69 +25581,72 @@ static Error ir_resolve_lazy_raw(CodeGen *codegen, AstNode *source_node, ConstEx...@@ -25590,69 +25581,72 @@ static Error ir_resolve_lazy_raw(CodeGen *codegen, AstNode *source_node, ConstEx
25590 }25581 }
25591 case LazyValueIdSliceType: {25582 case LazyValueIdSliceType: {
25592 LazyValueSliceType *lazy_slice_type = reinterpret_cast<LazyValueSliceType *>(val->data.x_lazy);25583 LazyValueSliceType *lazy_slice_type = reinterpret_cast<LazyValueSliceType *>(val->data.x_lazy);
25584 IrAnalyze *ira = lazy_slice_type->ira;
25585
25593 uint32_t align_bytes = 0;25586 uint32_t align_bytes = 0;
25594 if (lazy_slice_type->align_val != nullptr) {25587 if (lazy_slice_type->align_inst != nullptr) {
25595 if (!ir_resolve_const_align(codegen, exec, source_node, lazy_slice_type->align_val, &align_bytes))25588 if (!ir_resolve_align(ira, lazy_slice_type->align_inst, &align_bytes))
25596 return ErrorSemanticAnalyzeFail;25589 return ErrorSemanticAnalyzeFail;
25597 }25590 }
25598 ResolveStatus needed_status = (align_bytes == 0) ?25591 ResolveStatus needed_status = (align_bytes == 0) ?
25599 ResolveStatusZeroBitsKnown : ResolveStatusAlignmentKnown;25592 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)))
25601 return err;25594 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,
25603 lazy_slice_type->is_const, lazy_slice_type->is_volatile, PtrLenUnknown, align_bytes,25596 lazy_slice_type->is_const, lazy_slice_type->is_volatile, PtrLenUnknown, align_bytes,
25604 0, 0, lazy_slice_type->is_allowzero);25597 0, 0, lazy_slice_type->is_allowzero);
25605 val->special = ConstValSpecialStatic;25598 val->special = ConstValSpecialStatic;
25606 assert(val->type->id == ZigTypeIdMetaType);25599 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);
25608 return ErrorNone;25601 return ErrorNone;
25609 }25602 }
25610 case LazyValueIdPtrType: {25603 case LazyValueIdPtrType: {
25611 LazyValuePtrType *lazy_ptr_type = reinterpret_cast<LazyValuePtrType *>(val->data.x_lazy);25604 LazyValuePtrType *lazy_ptr_type = reinterpret_cast<LazyValuePtrType *>(val->data.x_lazy);
25605 IrAnalyze *ira = lazy_ptr_type->ira;
25606
25612 uint32_t align_bytes = 0;25607 uint32_t align_bytes = 0;
25613 if (lazy_ptr_type->align_val != nullptr) {25608 if (lazy_ptr_type->align_inst != nullptr) {
25614 if (!ir_resolve_const_align(codegen, exec, source_node, lazy_ptr_type->align_val, &align_bytes))25609 if (!ir_resolve_align(ira, lazy_ptr_type->align_inst, &align_bytes))
25615 return ErrorSemanticAnalyzeFail;25610 return ErrorSemanticAnalyzeFail;
25616 }25611 }
25617 ZigType *elem_type = ir_resolve_const_type(codegen, exec, lazy_ptr_type->elem_type_src_node,25612 ZigType *elem_type = ir_resolve_type(ira, lazy_ptr_type->elem_type);
25618 lazy_ptr_type->elem_type_val);
25619 if (type_is_invalid(elem_type))25613 if (type_is_invalid(elem_type))
25620 return ErrorSemanticAnalyzeFail;25614 return ErrorSemanticAnalyzeFail;
2562125615
25622 if (elem_type->id == ZigTypeIdUnreachable) {25616 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,
25624 buf_create_from_str("pointer to noreturn not allowed"));25618 buf_create_from_str("pointer to noreturn not allowed"));
25625 return ErrorSemanticAnalyzeFail;25619 return ErrorSemanticAnalyzeFail;
25626 } else if (elem_type->id == ZigTypeIdOpaque && lazy_ptr_type->ptr_len == PtrLenUnknown) {25620 } 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,
25628 buf_create_from_str("unknown-length pointer to opaque"));25622 buf_create_from_str("unknown-length pointer to opaque"));
25629 return ErrorSemanticAnalyzeFail;25623 return ErrorSemanticAnalyzeFail;
25630 } else if (lazy_ptr_type->ptr_len == PtrLenC) {25624 } else if (lazy_ptr_type->ptr_len == PtrLenC) {
25631 if (!type_allowed_in_extern(codegen, elem_type)) {25625 if (!type_allowed_in_extern(ira->codegen, elem_type)) {
25632 exec_add_error_node(codegen, exec, lazy_ptr_type->elem_type_src_node,25626 ir_add_error(ira, lazy_ptr_type->elem_type,
25633 buf_sprintf("C pointers cannot point to non-C-ABI-compatible type '%s'",25627 buf_sprintf("C pointers cannot point to non-C-ABI-compatible type '%s'",
25634 buf_ptr(&elem_type->name)));25628 buf_ptr(&elem_type->name)));
25635 return ErrorSemanticAnalyzeFail;25629 return ErrorSemanticAnalyzeFail;
25636 } else if (elem_type->id == ZigTypeIdOpaque) {25630 } 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,
25638 buf_sprintf("C pointers cannot point opaque types"));25632 buf_sprintf("C pointers cannot point opaque types"));
25639 return ErrorSemanticAnalyzeFail;25633 return ErrorSemanticAnalyzeFail;
25640 } else if (lazy_ptr_type->is_allowzero) {25634 } 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,
25642 buf_sprintf("C pointers always allow address zero"));25636 buf_sprintf("C pointers always allow address zero"));
25643 return ErrorSemanticAnalyzeFail;25637 return ErrorSemanticAnalyzeFail;
25644 }25638 }
25645 }25639 }
2564625640
25647 if (align_bytes != 0) {25641 if (align_bytes != 0) {
25648 if ((err = type_resolve(codegen, elem_type, ResolveStatusAlignmentKnown)))25642 if ((err = type_resolve(ira->codegen, elem_type, ResolveStatusAlignmentKnown)))
25649 return err;25643 return err;
25650 if (!type_has_bits(elem_type))25644 if (!type_has_bits(elem_type))
25651 align_bytes = 0;25645 align_bytes = 0;
25652 }25646 }
25653 bool allow_zero = lazy_ptr_type->is_allowzero || lazy_ptr_type->ptr_len == PtrLenC;25647 bool allow_zero = lazy_ptr_type->is_allowzero || lazy_ptr_type->ptr_len == PtrLenC;
25654 assert(val->type->id == ZigTypeIdMetaType);25648 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,
25656 lazy_ptr_type->is_const, lazy_ptr_type->is_volatile, lazy_ptr_type->ptr_len, align_bytes,25650 lazy_ptr_type->is_const, lazy_ptr_type->is_volatile, lazy_ptr_type->ptr_len, align_bytes,
25657 lazy_ptr_type->bit_offset_in_host, lazy_ptr_type->host_int_bytes,25651 lazy_ptr_type->bit_offset_in_host, lazy_ptr_type->host_int_bytes,
25658 allow_zero);25652 allow_zero);
...@@ -25661,29 +25655,29 @@ static Error ir_resolve_lazy_raw(CodeGen *codegen, AstNode *source_node, ConstEx...@@ -25661,29 +25655,29 @@ static Error ir_resolve_lazy_raw(CodeGen *codegen, AstNode *source_node, ConstEx
25661 }25655 }
25662 case LazyValueIdOptType: {25656 case LazyValueIdOptType: {
25663 LazyValueOptType *lazy_opt_type = reinterpret_cast<LazyValueOptType *>(val->data.x_lazy);25657 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,25660 ZigType *payload_type = ir_resolve_type(ira, lazy_opt_type->payload_type);
25666 lazy_opt_type->payload_type_val);
25667 if (type_is_invalid(payload_type))25661 if (type_is_invalid(payload_type))
25668 return ErrorSemanticAnalyzeFail;25662 return ErrorSemanticAnalyzeFail;
2566925663
25670 if (payload_type->id == ZigTypeIdOpaque || payload_type->id == ZigTypeIdUnreachable) {25664 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,
25672 buf_sprintf("type '%s' cannot be optional", buf_ptr(&payload_type->name)));25666 buf_sprintf("type '%s' cannot be optional", buf_ptr(&payload_type->name)));
25673 return ErrorSemanticAnalyzeFail;25667 return ErrorSemanticAnalyzeFail;
25674 }25668 }
2567525669
25676 if ((err = type_resolve(codegen, payload_type, ResolveStatusSizeKnown)))25670 if ((err = type_resolve(ira->codegen, payload_type, ResolveStatusSizeKnown)))
25677 return err;25671 return err;
2567825672
25679 assert(val->type->id == ZigTypeIdMetaType);25673 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);
25681 val->special = ConstValSpecialStatic;25675 val->special = ConstValSpecialStatic;
25682 return ErrorNone;25676 return ErrorNone;
25683 }25677 }
25684 case LazyValueIdFnType: {25678 case LazyValueIdFnType: {
25685 ZigType *fn_type = ir_resolve_lazy_fn_type(codegen, exec, source_node,25679 LazyValueFnType *lazy_fn_type = reinterpret_cast<LazyValueFnType *>(val->data.x_lazy);
25686 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);
25687 if (fn_type == nullptr)25681 if (fn_type == nullptr)
25688 return ErrorSemanticAnalyzeFail;25682 return ErrorSemanticAnalyzeFail;
25689 val->special = ConstValSpecialStatic;25683 val->special = ConstValSpecialStatic;
...@@ -25697,7 +25691,7 @@ static Error ir_resolve_lazy_raw(CodeGen *codegen, AstNode *source_node, ConstEx...@@ -25697,7 +25691,7 @@ static Error ir_resolve_lazy_raw(CodeGen *codegen, AstNode *source_node, ConstEx
2569725691
25698Error ir_resolve_lazy(CodeGen *codegen, AstNode *source_node, ConstExprValue *val) {25692Error ir_resolve_lazy(CodeGen *codegen, AstNode *source_node, ConstExprValue *val) {
25699 Error err;25693 Error err;
25700 if ((err = ir_resolve_lazy_raw(codegen, source_node, val))) {25694 if ((err = ir_resolve_lazy_raw(source_node, val))) {
25701 if (codegen->trace_err != nullptr && !source_node->already_traced_this_node) {25695 if (codegen->trace_err != nullptr && !source_node->already_traced_this_node) {
25702 source_node->already_traced_this_node = true;25696 source_node->already_traced_this_node = true;
25703 codegen->trace_err = add_error_note(codegen, codegen->trace_err, source_node,25697 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");...@@ -2,6 +2,33 @@ const tests = @import("tests.zig");
2const builtin = @import("builtin");2const builtin = @import("builtin");
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {4pub 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
5 cases.addCase(x: {32 cases.addCase(x: {
6 var tc = cases.create("variable in inline assembly template cannot be found",33 var tc = cases.create("variable in inline assembly template cannot be found",
7 \\export fn entry() void {34 \\export fn entry() void {