authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-11-06 11:09:14-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-11-06 11:09:14-05:00
log63f636e7b775bc8d2881104248f9579a089c4240
tree25e7432ccd228f0507aa3cb8665ad66762e29db6
parenta08b65720b7a6b78ff59b36fff1b3a6910483609
signature Commit is signed but in an unrecognized format.

limit integer types to maximum bit width of 65535

closes #1541

14 files changed, 105 insertions(+), 145 deletions(-)

doc/langref.html.in+7-4
......@@ -8,6 +8,7 @@
88 body{
99 background-color:#111;
1010 color: #bbb;
11 font-family: sans-serif;
1112 }
1213 a {
1314 color: #88f;
......@@ -467,9 +468,10 @@ pub fn main() void {
467468 <p>
468469 In addition to the integer types above, arbitrary bit-width integers can be referenced by using
469470 an identifier of <code>i</code> or </code>u</code> followed by digits. For example, the identifier
470 {#syntax#}i7{#endsyntax#} refers to a signed 7-bit integer.
471 {#syntax#}i7{#endsyntax#} refers to a signed 7-bit integer. The maximum allowed bit-width of an
472 integer type is {#syntax#}65535{#endsyntax#}.
471473 </p>
472 {#see_also|Integers|Floats|void|Errors#}
474 {#see_also|Integers|Floats|void|Errors|@IntType#}
473475 {#header_close#}
474476 {#header_open|Primitive Values#}
475477 <div class="table-wrapper">
......@@ -5814,9 +5816,10 @@ fn add(a: i32, b: i32) i32 { return a + b; }
58145816 {#header_close#}
58155817
58165818 {#header_open|@IntType#}
5817 <pre>{#syntax#}@IntType(comptime is_signed: bool, comptime bit_count: u32) type{#endsyntax#}</pre>
5819 <pre>{#syntax#}@IntType(comptime is_signed: bool, comptime bit_count: u16) type{#endsyntax#}</pre>
58185820 <p>
5819 This function returns an integer type with the given signness and bit count.
5821 This function returns an integer type with the given signness and bit count. The maximum
5822 bit count for an integer type is {#syntax#}65535{#endsyntax#}.
58205823 </p>
58215824 {#header_close#}
58225825 {#header_open|@memberCount#}
src/all_types.hpp-1
......@@ -1222,7 +1222,6 @@ struct ZigType {
12221222 ZigLLVMDIType *di_type;
12231223
12241224 bool zero_bits; // this is denormalized data
1225 bool is_copyable;
12261225 bool gen_h_loop_flag;
12271226
12281227 union {
src/analyze.cpp+24-44
......@@ -367,23 +367,6 @@ uint64_t type_size_bits(CodeGen *g, ZigType *type_entry) {
367367 return LLVMSizeOfTypeInBits(g->target_data_ref, type_entry->type_ref);
368368}
369369
370Result<bool> type_is_copyable(CodeGen *g, ZigType *type_entry) {
371 Error err;
372 if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown)))
373 return err;
374
375 if (!type_has_bits(type_entry))
376 return true;
377
378 if (!handle_is_ptr(type_entry))
379 return true;
380
381 if ((err = ensure_complete_type(g, type_entry)))
382 return err;
383
384 return type_entry->is_copyable;
385}
386
387370static bool is_slice(ZigType *type) {
388371 return type->id == ZigTypeIdStruct && type->data.structure.is_slice;
389372}
......@@ -465,7 +448,6 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
465448 assert(type_is_resolved(child_type, ResolveStatusZeroBitsKnown));
466449
467450 ZigType *entry = new_type_table_entry(ZigTypeIdPointer);
468 entry->is_copyable = true;
469451
470452 const char *star_str = ptr_len == PtrLenSingle ? "*" : "[*]";
471453 const char *const_str = is_const ? "const " : "";
......@@ -581,7 +563,6 @@ ZigType *get_optional_type(CodeGen *g, ZigType *child_type) {
581563
582564 ZigType *entry = new_type_table_entry(ZigTypeIdOptional);
583565 assert(child_type->type_ref || child_type->zero_bits);
584 entry->is_copyable = type_is_copyable(g, child_type).unwrap();
585566
586567 buf_resize(&entry->name, 0);
587568 buf_appendf(&entry->name, "?%s", buf_ptr(&child_type->name));
......@@ -671,7 +652,6 @@ ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payloa
671652 }
672653
673654 ZigType *entry = new_type_table_entry(ZigTypeIdErrorUnion);
674 entry->is_copyable = true;
675655 assert(payload_type->di_type);
676656 assert(type_is_complete(payload_type));
677657
......@@ -766,7 +746,6 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size) {
766746
767747 ZigType *entry = new_type_table_entry(ZigTypeIdArray);
768748 entry->zero_bits = (array_size == 0) || child_type->zero_bits;
769 entry->is_copyable = false;
770749
771750 buf_resize(&entry->name, 0);
772751 buf_appendf(&entry->name, "[%" ZIG_PRI_u64 "]%s", array_size, buf_ptr(&child_type->name));
......@@ -831,7 +810,6 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
831810 }
832811
833812 ZigType *entry = new_type_table_entry(ZigTypeIdStruct);
834 entry->is_copyable = true;
835813
836814 // replace the & with [] to go from a ptr type name to a slice type name
837815 buf_resize(&entry->name, 0);
......@@ -986,7 +964,6 @@ ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const c
986964 ImportTableEntry *import = scope ? get_scope_import(scope) : nullptr;
987965 unsigned line = source_node ? (unsigned)(source_node->line + 1) : 0;
988966
989 entry->is_copyable = false;
990967 entry->type_ref = LLVMInt8Type();
991968 entry->di_type = ZigLLVMCreateDebugForwardDeclType(g->dbuilder,
992969 ZigLLVMTag_DW_structure_type(), buf_ptr(&entry->name),
......@@ -1005,7 +982,6 @@ ZigType *get_bound_fn_type(CodeGen *g, ZigFn *fn_entry) {
1005982 return fn_type->data.fn.bound_fn_parent;
1006983
1007984 ZigType *bound_fn_type = new_type_table_entry(ZigTypeIdBoundFn);
1008 bound_fn_type->is_copyable = false;
1009985 bound_fn_type->data.bound_fn.fn_type = fn_type;
1010986 bound_fn_type->zero_bits = true;
1011987
......@@ -1105,7 +1081,6 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
11051081 }
11061082
11071083 ZigType *fn_type = new_type_table_entry(ZigTypeIdFn);
1108 fn_type->is_copyable = true;
11091084 fn_type->data.fn.fn_type_id = *fn_type_id;
11101085
11111086 bool skip_debug_info = false;
......@@ -1318,7 +1293,6 @@ ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {
13181293
13191294ZigType *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
13201295 ZigType *fn_type = new_type_table_entry(ZigTypeIdFn);
1321 fn_type->is_copyable = false;
13221296 buf_resize(&fn_type->name, 0);
13231297 if (fn_type->data.fn.fn_type_id.cc == CallingConventionAsync) {
13241298 const char *async_allocator_type_str = (fn_type->data.fn.fn_type_id.async_allocator_type == nullptr) ?
......@@ -1526,7 +1500,6 @@ ZigType *get_auto_err_set_type(CodeGen *g, ZigFn *fn_entry) {
15261500 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
15271501 buf_resize(&err_set_type->name, 0);
15281502 buf_appendf(&err_set_type->name, "@typeOf(%s).ReturnType.ErrorSet", buf_ptr(&fn_entry->symbol_name));
1529 err_set_type->is_copyable = true;
15301503 err_set_type->type_ref = g->builtin_types.entry_global_error_set->type_ref;
15311504 err_set_type->di_type = g->builtin_types.entry_global_error_set->di_type;
15321505 err_set_type->data.error_set.err_count = 0;
......@@ -2846,7 +2819,6 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
28462819 tag_type = new_type_table_entry(ZigTypeIdEnum);
28472820 buf_resize(&tag_type->name, 0);
28482821 buf_appendf(&tag_type->name, "@TagType(%s)", buf_ptr(&union_type->name));
2849 tag_type->is_copyable = true;
28502822 tag_type->type_ref = tag_int_type->type_ref;
28512823 tag_type->zero_bits = tag_int_type->zero_bits;
28522824
......@@ -3366,10 +3338,10 @@ static void add_top_level_decl(CodeGen *g, ScopeDecls *decls_scope, Tld *tld) {
33663338 }
33673339
33683340 {
3369 ZigType *type = get_primitive_type(g, tld->name);
3370 if (type != nullptr) {
3341 ZigType *type;
3342 if (get_primitive_type(g, tld->name, &type) != ErrorPrimitiveTypeNotFound) {
33713343 add_node_error(g, tld->source_node,
3372 buf_sprintf("declaration shadows type '%s'", buf_ptr(&type->name)));
3344 buf_sprintf("declaration shadows primitive type '%s'", buf_ptr(tld->name)));
33733345 }
33743346 }
33753347}
......@@ -3613,10 +3585,10 @@ ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf
36133585 add_error_note(g, msg, existing_var->decl_node, buf_sprintf("previous declaration is here"));
36143586 variable_entry->value->type = g->builtin_types.entry_invalid;
36153587 } else {
3616 ZigType *type = get_primitive_type(g, name);
3617 if (type != nullptr) {
3588 ZigType *type;
3589 if (get_primitive_type(g, name, &type) != ErrorPrimitiveTypeNotFound) {
36183590 add_node_error(g, source_node,
3619 buf_sprintf("variable shadows type '%s'", buf_ptr(&type->name)));
3591 buf_sprintf("variable shadows primitive type '%s'", buf_ptr(name)));
36203592 variable_entry->value->type = g->builtin_types.entry_invalid;
36213593 } else {
36223594 Scope *search_scope = nullptr;
......@@ -4435,6 +4407,7 @@ void semantic_analyze(CodeGen *g) {
44354407}
44364408
44374409ZigType *get_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits) {
4410 assert(size_in_bits <= 65535);
44384411 TypeId type_id = {};
44394412 type_id.id = ZigTypeIdInt;
44404413 type_id.data.integer.is_signed = is_signed;
......@@ -4523,7 +4496,7 @@ Buf *get_linux_libc_lib_path(const char *o_file) {
45234496 Termination term;
45244497 Buf *out_stderr = buf_alloc();
45254498 Buf *out_stdout = buf_alloc();
4526 int err;
4499 Error err;
45274500 if ((err = os_exec_process(cc_exe, args, &term, out_stderr, out_stdout))) {
45284501 zig_panic("unable to determine libc lib path: executing C compiler: %s", err_str(err));
45294502 }
......@@ -4552,7 +4525,7 @@ Buf *get_linux_libc_include_path(void) {
45524525 Termination term;
45534526 Buf *out_stderr = buf_alloc();
45544527 Buf *out_stdout = buf_alloc();
4555 int err;
4528 Error err;
45564529 if ((err = os_exec_process(cc_exe, args, &term, out_stderr, out_stdout))) {
45574530 zig_panic("unable to determine libc include path: executing C compiler: %s", err_str(err));
45584531 }
......@@ -5977,8 +5950,8 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
59775950}
59785951
59795952ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits) {
5953 assert(size_in_bits <= 65535);
59805954 ZigType *entry = new_type_table_entry(ZigTypeIdInt);
5981 entry->is_copyable = true;
59825955 entry->type_ref = (size_in_bits == 0) ? LLVMVoidType() : LLVMIntType(size_in_bits);
59835956 entry->zero_bits = (size_in_bits == 0);
59845957
......@@ -6455,7 +6428,10 @@ bool fn_type_can_fail(FnTypeId *fn_type_id) {
64556428 return type_can_fail(fn_type_id->return_type) || fn_type_id->cc == CallingConventionAsync;
64566429}
64576430
6458ZigType *get_primitive_type(CodeGen *g, Buf *name) {
6431// ErrorNone - result pointer has the type
6432// ErrorOverflow - an integer primitive type has too large a bit width
6433// ErrorPrimitiveTypeNotFound - result pointer unchanged
6434Error get_primitive_type(CodeGen *g, Buf *name, ZigType **result) {
64596435 if (buf_len(name) >= 2) {
64606436 uint8_t first_c = buf_ptr(name)[0];
64616437 if (first_c == 'i' || first_c == 'u') {
......@@ -6466,18 +6442,22 @@ ZigType *get_primitive_type(CodeGen *g, Buf *name) {
64666442 }
64676443 }
64686444 bool is_signed = (first_c == 'i');
6469 uint32_t bit_count = atoi(buf_ptr(name) + 1);
6470 return get_int_type(g, is_signed, bit_count);
6445 unsigned long int bit_count = strtoul(buf_ptr(name) + 1, nullptr, 10);
6446 // strtoul returns ULONG_MAX on errors, so this comparison catches that as well.
6447 if (bit_count >= 65536) return ErrorOverflow;
6448 *result = get_int_type(g, is_signed, bit_count);
6449 return ErrorNone;
64716450 }
64726451 }
64736452
64746453not_integer:
64756454
64766455 auto primitive_table_entry = g->primitive_type_table.maybe_get(name);
6477 if (primitive_table_entry != nullptr) {
6478 return primitive_table_entry->value;
6479 }
6480 return nullptr;
6456 if (primitive_table_entry == nullptr)
6457 return ErrorPrimitiveTypeNotFound;
6458
6459 *result = primitive_table_entry->value;
6460 return ErrorNone;
64816461}
64826462
64836463Error file_fetch(CodeGen *g, Buf *resolved_path, Buf *contents) {
src/analyze.hpp+1-3
......@@ -9,7 +9,6 @@
99#define ZIG_ANALYZE_HPP
1010
1111#include "all_types.hpp"
12#include "result.hpp"
1312
1413void semantic_analyze(CodeGen *g);
1514ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg);
......@@ -180,7 +179,6 @@ ZigTypeId type_id_at_index(size_t index);
180179size_t type_id_len();
181180size_t type_id_index(ZigType *entry);
182181ZigType *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id);
183Result<bool> type_is_copyable(CodeGen *g, ZigType *type_entry);
184182LinkLib *create_link_lib(Buf *name);
185183LinkLib *add_link_lib(CodeGen *codegen, Buf *lib);
186184
......@@ -204,7 +202,7 @@ bool type_can_fail(ZigType *type_entry);
204202bool fn_eval_cacheable(Scope *scope, ZigType *return_type);
205203AstNode *type_decl_node(ZigType *type_entry);
206204
207ZigType *get_primitive_type(CodeGen *g, Buf *name);
205Error get_primitive_type(CodeGen *g, Buf *name, ZigType **result);
208206
209207bool calling_convention_allows_zig_types(CallingConvention cc);
210208const char *calling_convention_name(CallingConvention cc);
src/codegen.cpp+3-3
......@@ -7326,7 +7326,7 @@ void codegen_translate_c(CodeGen *g, Buf *full_path) {
73267326 import->di_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname));
73277327
73287328 ZigList<ErrorMsg *> errors = {0};
7329 int err = parse_h_file(import, &errors, buf_ptr(full_path), g, nullptr);
7329 Error err = parse_h_file(import, &errors, buf_ptr(full_path), g, nullptr);
73307330
73317331 if (err == ErrorCCompileErrors && errors.length > 0) {
73327332 for (size_t i = 0; i < errors.length; i += 1) {
......@@ -7446,7 +7446,7 @@ static void gen_root_source(CodeGen *g) {
74467446 return;
74477447
74487448 Buf *source_code = buf_alloc();
7449 int err;
7449 Error err;
74507450 // No need for using the caching system for this file fetch because it is handled
74517451 // separately.
74527452 if ((err = os_fetch_file_path(resolved_path, source_code, true))) {
......@@ -7514,7 +7514,7 @@ void codegen_add_assembly(CodeGen *g, Buf *path) {
75147514
75157515static void gen_global_asm(CodeGen *g) {
75167516 Buf contents = BUF_INIT;
7517 int err;
7517 Error err;
75187518 for (size_t i = 0; i < g->assembly_files.length; i += 1) {
75197519 Buf *asm_file = g->assembly_files.at(i);
75207520 // No need to use the caching system for these fetches because they
src/error.cpp+3-2
......@@ -7,8 +7,8 @@
77
88#include "error.hpp"
99
10const char *err_str(int err) {
11 switch ((enum Error)err) {
10const char *err_str(Error err) {
11 switch (err) {
1212 case ErrorNone: return "(no error)";
1313 case ErrorNoMem: return "out of memory";
1414 case ErrorInvalidFormat: return "invalid format";
......@@ -32,6 +32,7 @@ const char *err_str(int err) {
3232 case ErrorUnsupportedOperatingSystem: return "unsupported operating system";
3333 case ErrorSharingViolation: return "sharing violation";
3434 case ErrorPipeBusy: return "pipe busy";
35 case ErrorPrimitiveTypeNotFound: return "primitive type not found";
3536 }
3637 return "(invalid error)";
3738}
src/error.hpp+8-1
......@@ -8,6 +8,8 @@
88#ifndef ERROR_HPP
99#define ERROR_HPP
1010
11#include <assert.h>
12
1113enum Error {
1214 ErrorNone,
1315 ErrorNoMem,
......@@ -32,8 +34,13 @@ enum Error {
3234 ErrorUnsupportedOperatingSystem,
3335 ErrorSharingViolation,
3436 ErrorPipeBusy,
37 ErrorPrimitiveTypeNotFound,
3538};
3639
37const char *err_str(int err);
40const char *err_str(Error err);
41
42static inline void assertNoError(Error err) {
43 assert(err == ErrorNone);
44}
3845
3946#endif
src/ir.cpp+16-12
......@@ -3055,10 +3055,10 @@ static ZigVar *create_local_var(CodeGen *codegen, AstNode *node, Scope *parent_s
30553055 add_error_note(codegen, msg, existing_var->decl_node, buf_sprintf("previous declaration is here"));
30563056 variable_entry->value->type = codegen->builtin_types.entry_invalid;
30573057 } else {
3058 ZigType *type = get_primitive_type(codegen, name);
3059 if (type != nullptr) {
3058 ZigType *type;
3059 if (get_primitive_type(codegen, name, &type) != ErrorPrimitiveTypeNotFound) {
30603060 add_node_error(codegen, node,
3061 buf_sprintf("variable shadows type '%s'", buf_ptr(&type->name)));
3061 buf_sprintf("variable shadows primitive type '%s'", buf_ptr(name)));
30623062 variable_entry->value->type = codegen->builtin_types.entry_invalid;
30633063 } else {
30643064 Tld *tld = find_decl(codegen, parent_scope, name);
......@@ -3488,6 +3488,7 @@ static IrInstruction *ir_gen_null_literal(IrBuilder *irb, Scope *scope, AstNode
34883488}
34893489
34903490static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval) {
3491 Error err;
34913492 assert(node->type == NodeTypeSymbol);
34923493
34933494 Buf *variable_name = node->data.symbol_expr.symbol;
......@@ -3501,8 +3502,15 @@ static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node,
35013502 return &const_instruction->base;
35023503 }
35033504
3504 ZigType *primitive_type = get_primitive_type(irb->codegen, variable_name);
3505 if (primitive_type != nullptr) {
3505 ZigType *primitive_type;
3506 if ((err = get_primitive_type(irb->codegen, variable_name, &primitive_type))) {
3507 if (err == ErrorOverflow) {
3508 add_node_error(irb->codegen, node,
3509 buf_sprintf("primitive integer type '%s' exceeds maximum bit width of 65535",
3510 buf_ptr(variable_name)));
3511 return irb->codegen->invalid_instruction;
3512 }
3513 } else {
35063514 IrInstruction *value = ir_build_const_type(irb, scope, node, primitive_type);
35073515 if (lval == LValPtr) {
35083516 return ir_build_ref(irb, scope, node, value, false, false);
......@@ -6302,7 +6310,6 @@ static ZigType *get_error_set_union(CodeGen *g, ErrorTableEntry **errors, ZigTyp
63026310 }
63036311 }
63046312
6305 err_set_type->is_copyable = true;
63066313 err_set_type->type_ref = g->builtin_types.entry_global_error_set->type_ref;
63076314 err_set_type->di_type = g->builtin_types.entry_global_error_set->di_type;
63086315 err_set_type->data.error_set.err_count = count;
......@@ -6341,7 +6348,6 @@ static ZigType *make_err_set_with_one_item(CodeGen *g, Scope *parent_scope, AstN
63416348 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
63426349 buf_resize(&err_set_type->name, 0);
63436350 buf_appendf(&err_set_type->name, "error.{%s}", buf_ptr(&err_entry->name));
6344 err_set_type->is_copyable = true;
63456351 err_set_type->type_ref = g->builtin_types.entry_global_error_set->type_ref;
63466352 err_set_type->di_type = g->builtin_types.entry_global_error_set->di_type;
63476353 err_set_type->data.error_set.err_count = 1;
......@@ -6362,7 +6368,6 @@ static IrInstruction *ir_gen_err_set_decl(IrBuilder *irb, Scope *parent_scope, A
63626368 Buf *type_name = get_anon_type_name(irb->codegen, irb->exec, "error set", node);
63636369 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
63646370 buf_init_from_buf(&err_set_type->name, type_name);
6365 err_set_type->is_copyable = true;
63666371 err_set_type->data.error_set.err_count = err_count;
63676372 err_set_type->type_ref = irb->codegen->builtin_types.entry_global_error_set->type_ref;
63686373 err_set_type->di_type = irb->codegen->builtin_types.entry_global_error_set->di_type;
......@@ -8208,7 +8213,6 @@ static ZigType *get_error_set_intersection(IrAnalyze *ira, ZigType *set1, ZigTyp
82088213 }
82098214 free(errors);
82108215
8211 err_set_type->is_copyable = true;
82128216 err_set_type->type_ref = ira->codegen->builtin_types.entry_global_error_set->type_ref;
82138217 err_set_type->di_type = ira->codegen->builtin_types.entry_global_error_set->di_type;
82148218 err_set_type->data.error_set.err_count = intersection_list.length;
......@@ -17650,7 +17654,7 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct
1765017654
1765117655 ZigList<ErrorMsg *> errors = {0};
1765217656
17653 int err;
17657 Error err;
1765417658 if ((err = parse_h_buf(child_import, &errors, &cimport_scope->buf, ira->codegen, node))) {
1765517659 if (err != ErrorCCompileErrors) {
1765617660 ir_add_error_node(ira, node, buf_sprintf("C import failed: %s", err_str(err)));
......@@ -17766,7 +17770,7 @@ static IrInstruction *ir_analyze_instruction_embed_file(IrAnalyze *ira, IrInstru
1776617770
1776717771 // load from file system into const expr
1776817772 Buf *file_contents = buf_alloc();
17769 int err;
17773 Error err;
1777017774 if ((err = file_fetch(ira->codegen, file_path, file_contents))) {
1777117775 if (err == ErrorFileNotFound) {
1777217776 ir_add_error(ira, instruction->name, buf_sprintf("unable to find '%s'", buf_ptr(file_path)));
......@@ -18252,7 +18256,7 @@ static IrInstruction *ir_analyze_instruction_int_type(IrAnalyze *ira, IrInstruct
1825218256
1825318257 IrInstruction *bit_count_value = instruction->bit_count->child;
1825418258 uint64_t bit_count;
18255 if (!ir_resolve_unsigned(ira, bit_count_value, ira->codegen->builtin_types.entry_u32, &bit_count))
18259 if (!ir_resolve_unsigned(ira, bit_count_value, ira->codegen->builtin_types.entry_u16, &bit_count))
1825618260 return ira->codegen->invalid_instruction;
1825718261
1825818262 return ir_const_type(ira, &instruction->base, get_int_type(ira->codegen, is_signed, (uint32_t)bit_count));
src/os.cpp+16-16
......@@ -793,7 +793,7 @@ Error os_file_exists(Buf *full_path, bool *result) {
793793}
794794
795795#if defined(ZIG_OS_POSIX)
796static int os_exec_process_posix(const char *exe, ZigList<const char *> &args,
796static Error os_exec_process_posix(const char *exe, ZigList<const char *> &args,
797797 Termination *term, Buf *out_stderr, Buf *out_stdout)
798798{
799799 int stdin_pipe[2];
......@@ -872,7 +872,7 @@ static int os_exec_process_posix(const char *exe, ZigList<const char *> &args,
872872// LocalFree(messageBuffer);
873873//}
874874
875static int os_exec_process_windows(const char *exe, ZigList<const char *> &args,
875static Error os_exec_process_windows(const char *exe, ZigList<const char *> &args,
876876 Termination *term, Buf *out_stderr, Buf *out_stdout)
877877{
878878 Buf command_line = BUF_INIT;
......@@ -983,7 +983,7 @@ static int os_exec_process_windows(const char *exe, ZigList<const char *> &args,
983983 CloseHandle(piProcInfo.hProcess);
984984 CloseHandle(piProcInfo.hThread);
985985
986 return 0;
986 return ErrorNone;
987987}
988988#endif
989989
......@@ -1003,7 +1003,7 @@ Error os_execv(const char *exe, const char **argv) {
10031003#endif
10041004}
10051005
1006int os_exec_process(const char *exe, ZigList<const char *> &args,
1006Error os_exec_process(const char *exe, ZigList<const char *> &args,
10071007 Termination *term, Buf *out_stderr, Buf *out_stdout)
10081008{
10091009#if defined(ZIG_OS_WINDOWS)
......@@ -1027,7 +1027,7 @@ void os_write_file(Buf *full_path, Buf *contents) {
10271027 zig_panic("close failed");
10281028}
10291029
1030int os_copy_file(Buf *src_path, Buf *dest_path) {
1030Error os_copy_file(Buf *src_path, Buf *dest_path) {
10311031 FILE *src_f = fopen(buf_ptr(src_path), "rb");
10321032 if (!src_f) {
10331033 int err = errno;
......@@ -1074,7 +1074,7 @@ int os_copy_file(Buf *src_path, Buf *dest_path) {
10741074 if (feof(src_f)) {
10751075 fclose(src_f);
10761076 fclose(dest_f);
1077 return 0;
1077 return ErrorNone;
10781078 }
10791079 }
10801080}
......@@ -1197,7 +1197,7 @@ bool os_stderr_tty(void) {
11971197}
11981198
11991199#if defined(ZIG_OS_POSIX)
1200static int os_buf_to_tmp_file_posix(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
1200static Error os_buf_to_tmp_file_posix(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
12011201 const char *tmp_dir = getenv("TMPDIR");
12021202 if (!tmp_dir) {
12031203 tmp_dir = P_tmpdir;
......@@ -1221,12 +1221,12 @@ static int os_buf_to_tmp_file_posix(Buf *contents, Buf *suffix, Buf *out_tmp_pat
12211221 if (fclose(f))
12221222 zig_panic("close failed");
12231223
1224 return 0;
1224 return ErrorNone;
12251225}
12261226#endif
12271227
12281228#if defined(ZIG_OS_WINDOWS)
1229static int os_buf_to_tmp_file_windows(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
1229static Error os_buf_to_tmp_file_windows(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
12301230 char tmp_dir[MAX_PATH + 1];
12311231 if (GetTempPath(MAX_PATH, tmp_dir) == 0) {
12321232 zig_panic("GetTempPath failed");
......@@ -1255,11 +1255,11 @@ static int os_buf_to_tmp_file_windows(Buf *contents, Buf *suffix, Buf *out_tmp_p
12551255 if (fclose(f)) {
12561256 zig_panic("fclose failed");
12571257 }
1258 return 0;
1258 return ErrorNone;
12591259}
12601260#endif
12611261
1262int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
1262Error os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
12631263#if defined(ZIG_OS_WINDOWS)
12641264 return os_buf_to_tmp_file_windows(contents, suffix, out_tmp_path);
12651265#elif defined(ZIG_OS_POSIX)
......@@ -1269,17 +1269,17 @@ int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
12691269#endif
12701270}
12711271
1272int os_delete_file(Buf *path) {
1272Error os_delete_file(Buf *path) {
12731273 if (remove(buf_ptr(path))) {
12741274 return ErrorFileSystem;
12751275 } else {
1276 return 0;
1276 return ErrorNone;
12771277 }
12781278}
12791279
1280int os_rename(Buf *src_path, Buf *dest_path) {
1280Error os_rename(Buf *src_path, Buf *dest_path) {
12811281 if (buf_eql_buf(src_path, dest_path)) {
1282 return 0;
1282 return ErrorNone;
12831283 }
12841284#if defined(ZIG_OS_WINDOWS)
12851285 if (!MoveFileExA(buf_ptr(src_path), buf_ptr(dest_path), MOVEFILE_REPLACE_EXISTING)) {
......@@ -1290,7 +1290,7 @@ int os_rename(Buf *src_path, Buf *dest_path) {
12901290 return ErrorFileSystem;
12911291 }
12921292#endif
1293 return 0;
1293 return ErrorNone;
12941294}
12951295
12961296double os_get_time(void) {
src/os.hpp+5-6
......@@ -13,7 +13,6 @@
1313#include "error.hpp"
1414#include "zig_llvm.h"
1515#include "windows_sdk.h"
16#include "result.hpp"
1716
1817#include <stdio.h>
1918#include <inttypes.h>
......@@ -85,7 +84,7 @@ struct OsTimeStamp {
8584int os_init(void);
8685
8786void os_spawn_process(const char *exe, ZigList<const char *> &args, Termination *term);
88int os_exec_process(const char *exe, ZigList<const char *> &args,
87Error os_exec_process(const char *exe, ZigList<const char *> &args,
8988 Termination *term, Buf *out_stderr, Buf *out_stdout);
9089Error os_execv(const char *exe, const char **argv);
9190
......@@ -109,7 +108,7 @@ Error ATTRIBUTE_MUST_USE os_file_overwrite(OsFile file, Buf *contents);
109108void os_file_close(OsFile file);
110109
111110void os_write_file(Buf *full_path, Buf *contents);
112int os_copy_file(Buf *src_path, Buf *dest_path);
111Error os_copy_file(Buf *src_path, Buf *dest_path);
113112
114113Error ATTRIBUTE_MUST_USE os_fetch_file(FILE *file, Buf *out_contents, bool skip_shebang);
115114Error ATTRIBUTE_MUST_USE os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang);
......@@ -119,12 +118,12 @@ Error ATTRIBUTE_MUST_USE os_get_cwd(Buf *out_cwd);
119118bool os_stderr_tty(void);
120119void os_stderr_set_color(TermColor color);
121120
122int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path);
123int os_delete_file(Buf *path);
121Error os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path);
122Error os_delete_file(Buf *path);
124123
125124Error ATTRIBUTE_MUST_USE os_file_exists(Buf *full_path, bool *result);
126125
127int os_rename(Buf *src_path, Buf *dest_path);
126Error os_rename(Buf *src_path, Buf *dest_path);
128127double os_get_time(void);
129128
130129bool os_is_sep(uint8_t c);
src/result.hpp deleted-36
......@@ -1,36 +0,0 @@
1/*
2 * Copyright (c) 2018 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#ifndef ZIG_RESULT_HPP
9#define ZIG_RESULT_HPP
10
11#include "error.hpp"
12
13#include <assert.h>
14
15static inline void assertNoError(Error err) {
16 assert(err == ErrorNone);
17}
18
19template<typename T>
20struct Result {
21 T data;
22 Error err;
23
24 Result(T x) : data(x), err(ErrorNone) {}
25
26 Result(Error err) : err(err) {
27 assert(err != ErrorNone);
28 }
29
30 T unwrap() {
31 assert(err == ErrorNone);
32 return data;
33 }
34};
35
36#endif
src/translate_c.cpp+6-5
......@@ -436,7 +436,8 @@ static AstNode *get_global(Context *c, Buf *name) {
436436 if (entry)
437437 return entry->value;
438438 }
439 if (get_primitive_type(c->codegen, name) != nullptr) {
439 ZigType *type;
440 if (get_primitive_type(c->codegen, name, &type) != ErrorPrimitiveTypeNotFound) {
440441 return trans_create_node_symbol(c, name);
441442 }
442443 return nullptr;
......@@ -4682,10 +4683,10 @@ static void process_preprocessor_entities(Context *c, ASTUnit &unit) {
46824683 }
46834684}
46844685
4685int parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *source,
4686Error parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *source,
46864687 CodeGen *codegen, AstNode *source_node)
46874688{
4688 int err;
4689 Error err;
46894690 Buf tmp_file_path = BUF_INIT;
46904691 if ((err = os_buf_to_tmp_file(source, buf_create_from_str(".h"), &tmp_file_path))) {
46914692 return err;
......@@ -4698,7 +4699,7 @@ int parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *sour
46984699 return err;
46994700}
47004701
4701int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const char *target_file,
4702Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const char *target_file,
47024703 CodeGen *codegen, AstNode *source_node)
47034704{
47044705 Context context = {0};
......@@ -4865,5 +4866,5 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch
48654866
48664867 import->root = c->root;
48674868
4868 return 0;
4869 return ErrorNone;
48694870}
src/translate_c.hpp+2-2
......@@ -11,10 +11,10 @@
1111
1212#include "all_types.hpp"
1313
14int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const char *target_file,
14Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const char *target_file,
1515 CodeGen *codegen, AstNode *source_node);
1616
17int parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *source,
17Error parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *source,
1818 CodeGen *codegen, AstNode *source_node);
1919
2020#endif
test/compile_errors.zig+14-10
......@@ -1,6 +1,19 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: *tests.CompileErrorContext) void {
4 cases.add(
5 "exceeded maximum bit width of integer",
6 \\export fn entry1() void {
7 \\ const T = @IntType(false, 65536);
8 \\}
9 \\export fn entry2() void {
10 \\ var x: i65536 = 1;
11 \\}
12 ,
13 ".tmp_source.zig:2:31: error: integer value 65536 cannot be implicitly casted to type 'u16'",
14 ".tmp_source.zig:5:12: error: primitive integer type 'i65536' exceeds maximum bit width of 65535",
15 );
16
417 cases.add(
518 "Panic declared with wrong type signature in tests",
619 \\test "" {}
......@@ -534,15 +547,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
534547 ".tmp_source.zig:1:9: error: parameter of type 'fn(var)var' must be declared comptime",
535548 );
536549
537 cases.add(
538 "bit count of @IntType too large",
539 \\comptime {
540 \\ _ = @IntType(false, @import("std").math.maxInt(u32) + 1);
541 \\}
542 ,
543 ".tmp_source.zig:2:57: error: integer value 4294967296 cannot be implicitly casted to type 'u32'",
544 );
545
546550 cases.add(
547551 "optional pointer to void in extern struct",
548552 \\const Foo = extern struct.{
......@@ -4278,7 +4282,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
42784282 \\ const a: u16 = 300;
42794283 \\}
42804284 ,
4281 ".tmp_source.zig:1:1: error: declaration shadows type 'u16'",
4285 ".tmp_source.zig:1:1: error: declaration shadows primitive type 'u16'",
42824286 );
42834287
42844288 cases.add(