authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-11-27 20:36:44-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-11-27 20:36:44-05:00
loge3bf40742d5b93c10b8e68b202294badb508c3e7
tree73a9219fd0f5ec7985eb9f121390fa2db24654f9
parentbb3ac177a83caf52556818e9ea163ac166bb6c07
parent1fb15be05f1037aad53d2db32d13123363365d10
signature Commit is signed but in an unrecognized format.

Merge branch 'master' into freebsd2


21 files changed, 876 insertions(+), 551 deletions(-)

README.md+65-27
......@@ -42,33 +42,71 @@ clarity.
4242 * In addition to creating executables, creating a C library is a primary use
4343 case. You can export an auto-generated .h file.
4444
45### Support Table
46
47Freestanding means that you do not directly interact with the OS
48or you are writing your own OS.
49
50Note that if you use libc or other libraries to interact with the OS,
51that counts as "freestanding" for the purposes of this table.
52
53| | freestanding | linux | macosx | windows | other |
54|-------------|--------------|---------|---------|---------|---------|
55|i386 | OK | planned | OK | planned | planned |
56|x86_64 | OK | OK | OK | OK | planned |
57|arm | OK | planned | planned | planned | planned |
58|bpf | OK | planned | N/A | N/A | planned |
59|hexagon | OK | planned | N/A | N/A | planned |
60|mips | OK | planned | N/A | N/A | planned |
61|powerpc | OK | planned | N/A | N/A | planned |
62|r600 | OK | planned | N/A | N/A | planned |
63|amdgcn | OK | planned | N/A | N/A | planned |
64|sparc | OK | planned | N/A | N/A | planned |
65|s390x | OK | planned | N/A | N/A | planned |
66|spir | OK | planned | N/A | N/A | planned |
67|lanai | OK | planned | N/A | N/A | planned |
68|wasm32 | planned | N/A | N/A | N/A | N/A |
69|wasm64 | planned | N/A | N/A | N/A | N/A |
70|riscv32 | planned | planned | N/A | N/A | planned |
71|riscv64 | planned | planned | N/A | N/A | planned |
45### Supported Targets
46
47#### Tier 1 Support
48
49 * Not only can Zig generate machine code for these targets, but the standard
50 library cross-platform abstractions have implementations for these targets.
51 Thus it is practical to write a pure Zig application with no dependency on
52 libc.
53 * The CI server automatically tests these targets on every commit to master
54 branch, and updates ziglang.org/download with links to pre-built binaries.
55 * These targets have debug info capabilities and therefore produce stack
56 traces on failed assertions.
57
58#### Tier 2 Support
59
60 * There may be some standard library implementations, but many abstractions
61 will give an "Unsupported OS" compile error. One can link with libc or other
62 libraries to fill in the gaps in the standard library.
63 * These targets are known to work, but are not automatically tested, so there
64 are occasional regressions.
65 * Some tests may be disabled for these targets as we work toward Tier 1
66 support.
67
68#### Tier 3 Support
69
70 * The standard library has little to no knowledge of the existence of this
71 target.
72 * Because Zig is based on LLVM, it has the capability to build for these
73 targets, and LLVM has the target enabled by default.
74 * These targets are not frequently tested; one will likely need to contribute
75 to Zig in order to build for these targets.
76 * The Zig compiler might need to be updated with a few things such as
77 - what sizes are the C integer types
78 - C ABI calling convention for this target
79 - bootstrap code and default panic handler
80
81#### Tier 4 Support
82
83 * Support for these targets is entirely experimental.
84 * LLVM may have the target as an experimental target, which means that you
85 need to use Zig-provided binaries for the target to be available, or
86 build LLVM from source with special configure flags.
87
88#### Support Table
89
90| | freestanding | linux | macosx | windows | freebsd | other |
91|--------|--------------|--------|--------|---------|---------|--------|
92|x86_64 | Tier 2 | Tier 1 | Tier 1 | Tier 1 | Tier 3 | Tier 3 |
93|i386 | Tier 2 | Tier 2 | Tier 2 | Tier 2 | Tier 3 | Tier 3 |
94|arm | Tier 2 | Tier 3 | Tier 3 | Tier 3 | Tier 3 | Tier 3 |
95|arm64 | Tier 2 | Tier 2 | Tier 3 | Tier 3 | Tier 3 | Tier 3 |
96|bpf | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 |
97|hexagon | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 |
98|mips | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 |
99|powerpc | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 |
100|r600 | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 |
101|amdgcn | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 |
102|sparc | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 |
103|s390x | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 |
104|spir | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 |
105|lanai | Tier 3 | Tier 3 | N/A | N/A | Tier 3 | Tier 3 |
106|wasm32 | Tier 4 | N/A | N/A | N/A | N/A | N/A |
107|wasm64 | Tier 4 | N/A | N/A | N/A | N/A | N/A |
108|riscv32 | Tier 4 | Tier 4 | N/A | N/A | Tier 4 | Tier 4 |
109|riscv64 | Tier 4 | Tier 4 | N/A | N/A | Tier 4 | Tier 4 |
72110
73111## Community
74112
src/analyze.cpp+49-36
......@@ -1619,13 +1619,16 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
16191619 case ZigTypeIdUnion:
16201620 case ZigTypeIdFn:
16211621 case ZigTypeIdPromise:
1622 if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown)))
1623 return g->builtin_types.entry_invalid;
1624 if (type_requires_comptime(type_entry)) {
1625 add_node_error(g, param_node->data.param_decl.type,
1626 buf_sprintf("parameter of type '%s' must be declared comptime",
1627 buf_ptr(&type_entry->name)));
1628 return g->builtin_types.entry_invalid;
1622 switch (type_requires_comptime(g, type_entry)) {
1623 case ReqCompTimeNo:
1624 break;
1625 case ReqCompTimeYes:
1626 add_node_error(g, param_node->data.param_decl.type,
1627 buf_sprintf("parameter of type '%s' must be declared comptime",
1628 buf_ptr(&type_entry->name)));
1629 return g->builtin_types.entry_invalid;
1630 case ReqCompTimeInvalid:
1631 return g->builtin_types.entry_invalid;
16291632 }
16301633 break;
16311634 }
......@@ -1711,10 +1714,13 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
17111714 case ZigTypeIdUnion:
17121715 case ZigTypeIdFn:
17131716 case ZigTypeIdPromise:
1714 if ((err = type_resolve(g, fn_type_id.return_type, ResolveStatusZeroBitsKnown)))
1715 return g->builtin_types.entry_invalid;
1716 if (type_requires_comptime(fn_type_id.return_type)) {
1717 return get_generic_fn_type(g, &fn_type_id);
1717 switch (type_requires_comptime(g, fn_type_id.return_type)) {
1718 case ReqCompTimeInvalid:
1719 return g->builtin_types.entry_invalid;
1720 case ReqCompTimeYes:
1721 return get_generic_fn_type(g, &fn_type_id);
1722 case ReqCompTimeNo:
1723 break;
17181724 }
17191725 break;
17201726 }
......@@ -2560,8 +2566,6 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
25602566static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
25612567 assert(struct_type->id == ZigTypeIdStruct);
25622568
2563 Error err;
2564
25652569 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
25662570 return ErrorSemanticAnalyzeFail;
25672571 if (struct_type->data.structure.resolve_status >= ResolveStatusZeroBitsKnown)
......@@ -2619,13 +2623,15 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
26192623 buf_sprintf("enums, not structs, support field assignment"));
26202624 }
26212625
2622 if ((err = type_resolve(g, field_type, ResolveStatusZeroBitsKnown))) {
2623 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2624 continue;
2625 }
2626
2627 if (type_requires_comptime(field_type)) {
2628 struct_type->data.structure.requires_comptime = true;
2626 switch (type_requires_comptime(g, field_type)) {
2627 case ReqCompTimeYes:
2628 struct_type->data.structure.requires_comptime = true;
2629 break;
2630 case ReqCompTimeInvalid:
2631 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2632 continue;
2633 case ReqCompTimeNo:
2634 break;
26292635 }
26302636
26312637 if (!type_has_bits(field_type))
......@@ -2890,11 +2896,17 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
28902896 }
28912897 union_field->type_entry = field_type;
28922898
2893 if (type_requires_comptime(field_type)) {
2894 union_type->data.unionation.requires_comptime = true;
2899 switch (type_requires_comptime(g, field_type)) {
2900 case ReqCompTimeInvalid:
2901 union_type->data.unionation.is_invalid = true;
2902 continue;
2903 case ReqCompTimeYes:
2904 union_type->data.unionation.requires_comptime = true;
2905 break;
2906 case ReqCompTimeNo:
2907 break;
28952908 }
28962909
2897
28982910 if (field_node->data.struct_field.value != nullptr && !decl_node->data.container_decl.auto_enum) {
28992911 ErrorMsg *msg = add_node_error(g, field_node->data.struct_field.value,
29002912 buf_sprintf("non-enum union field assignment"));
......@@ -5096,7 +5108,10 @@ bool type_has_bits(ZigType *type_entry) {
50965108 return !type_entry->zero_bits;
50975109}
50985110
5099bool type_requires_comptime(ZigType *type_entry) {
5111ReqCompTime type_requires_comptime(CodeGen *g, ZigType *type_entry) {
5112 Error err;
5113 if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown)))
5114 return ReqCompTimeInvalid;
51005115 switch (type_entry->id) {
51015116 case ZigTypeIdInvalid:
51025117 case ZigTypeIdOpaque:
......@@ -5109,27 +5124,25 @@ bool type_requires_comptime(ZigType *type_entry) {
51095124 case ZigTypeIdNamespace:
51105125 case ZigTypeIdBoundFn:
51115126 case ZigTypeIdArgTuple:
5112 return true;
5127 return ReqCompTimeYes;
51135128 case ZigTypeIdArray:
5114 return type_requires_comptime(type_entry->data.array.child_type);
5129 return type_requires_comptime(g, type_entry->data.array.child_type);
51155130 case ZigTypeIdStruct:
5116 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
5117 return type_entry->data.structure.requires_comptime;
5131 return type_entry->data.structure.requires_comptime ? ReqCompTimeYes : ReqCompTimeNo;
51185132 case ZigTypeIdUnion:
5119 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
5120 return type_entry->data.unionation.requires_comptime;
5133 return type_entry->data.unionation.requires_comptime ? ReqCompTimeYes : ReqCompTimeNo;
51215134 case ZigTypeIdOptional:
5122 return type_requires_comptime(type_entry->data.maybe.child_type);
5135 return type_requires_comptime(g, type_entry->data.maybe.child_type);
51235136 case ZigTypeIdErrorUnion:
5124 return type_requires_comptime(type_entry->data.error_union.payload_type);
5137 return type_requires_comptime(g, type_entry->data.error_union.payload_type);
51255138 case ZigTypeIdPointer:
51265139 if (type_entry->data.pointer.child_type->id == ZigTypeIdOpaque) {
5127 return false;
5140 return ReqCompTimeNo;
51285141 } else {
5129 return type_requires_comptime(type_entry->data.pointer.child_type);
5142 return type_requires_comptime(g, type_entry->data.pointer.child_type);
51305143 }
51315144 case ZigTypeIdFn:
5132 return type_entry->data.fn.is_generic;
5145 return type_entry->data.fn.is_generic ? ReqCompTimeYes : ReqCompTimeNo;
51335146 case ZigTypeIdEnum:
51345147 case ZigTypeIdErrorSet:
51355148 case ZigTypeIdBool:
......@@ -5138,7 +5151,7 @@ bool type_requires_comptime(ZigType *type_entry) {
51385151 case ZigTypeIdVoid:
51395152 case ZigTypeIdUnreachable:
51405153 case ZigTypeIdPromise:
5141 return false;
5154 return ReqCompTimeNo;
51425155 }
51435156 zig_unreachable();
51445157}
src/analyze.hpp+7-1
......@@ -87,7 +87,6 @@ ZigFn *create_fn(CodeGen *g, AstNode *proto_node);
8787ZigFn *create_fn_raw(CodeGen *g, FnInline inline_value);
8888void init_fn_type_id(FnTypeId *fn_type_id, AstNode *proto_node, size_t param_count_alloc);
8989AstNode *get_param_decl_node(ZigFn *fn_entry, size_t index);
90bool type_requires_comptime(ZigType *type_entry);
9190Error ATTRIBUTE_MUST_USE ensure_complete_type(CodeGen *g, ZigType *type_entry);
9291Error ATTRIBUTE_MUST_USE type_resolve(CodeGen *g, ZigType *type_entry, ResolveStatus status);
9392void complete_enum(CodeGen *g, ZigType *enum_type);
......@@ -216,4 +215,11 @@ bool want_first_arg_sret(CodeGen *g, FnTypeId *fn_type_id);
216215
217216uint32_t get_host_int_bytes(CodeGen *g, ZigType *struct_type, TypeStructField *field);
218217
218enum ReqCompTime {
219 ReqCompTimeInvalid,
220 ReqCompTimeNo,
221 ReqCompTimeYes,
222};
223ReqCompTime type_requires_comptime(CodeGen *g, ZigType *type_entry);
224
219225#endif
src/cache_hash.cpp+4-2
......@@ -352,8 +352,9 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) {
352352 // if the mtime matches we can trust the digest
353353 OsFile this_file;
354354 if ((err = os_file_open_r(chf->path, &this_file))) {
355 fprintf(stderr, "Unable to open %s\n: %s", buf_ptr(chf->path), err_str(err));
355356 os_file_close(ch->manifest_file);
356 return err;
357 return ErrorCacheUnavailable;
357358 }
358359 OsTimeStamp actual_mtime;
359360 if ((err = os_file_mtime(this_file, &actual_mtime))) {
......@@ -392,8 +393,9 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) {
392393 for (; file_i < input_file_count; file_i += 1) {
393394 CacheHashFile *chf = &ch->files.at(file_i);
394395 if ((err = populate_file_hash(ch, chf, nullptr))) {
396 fprintf(stderr, "Unable to hash %s: %s\n", buf_ptr(chf->path), err_str(err));
395397 os_file_close(ch->manifest_file);
396 return err;
398 return ErrorCacheUnavailable;
397399 }
398400 }
399401 return ErrorNone;
src/codegen.cpp+19-4
......@@ -129,6 +129,11 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
129129 Buf *src_dir = buf_alloc();
130130 os_path_split(root_src_path, src_dir, src_basename);
131131
132 if (buf_len(src_basename) == 0) {
133 fprintf(stderr, "Invalid root source path: %s\n", buf_ptr(root_src_path));
134 exit(1);
135 }
136
132137 g->root_package = new_package(buf_ptr(src_dir), buf_ptr(src_basename));
133138 g->std_package = new_package(buf_ptr(g->zig_std_dir), "index.zig");
134139 g->root_package->package_table.put(buf_create_from_str("std"), g->std_package);
......@@ -1645,7 +1650,7 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z
16451650 zig_unreachable();
16461651 }
16471652
1648 if (actual_bits >= wanted_bits && actual_type->id == ZigTypeIdInt &&
1653 if (actual_type->id == ZigTypeIdInt &&
16491654 !wanted_type->data.integral.is_signed && actual_type->data.integral.is_signed &&
16501655 want_runtime_safety)
16511656 {
......@@ -6281,8 +6286,14 @@ static void do_code_gen(CodeGen *g) {
62816286 }
62826287 if (ir_get_var_is_comptime(var))
62836288 continue;
6284 if (type_requires_comptime(var->value->type))
6285 continue;
6289 switch (type_requires_comptime(g, var->value->type)) {
6290 case ReqCompTimeInvalid:
6291 zig_unreachable();
6292 case ReqCompTimeYes:
6293 continue;
6294 case ReqCompTimeNo:
6295 break;
6296 }
62866297
62876298 if (var->src_arg_index == SIZE_MAX) {
62886299 var->value_ref = build_alloca(g, var->value->type, buf_ptr(&var->name), var->align_bytes);
......@@ -8172,7 +8183,11 @@ void codegen_build_and_link(CodeGen *g) {
81728183 os_path_join(stage1_dir, buf_create_from_str("build"), manifest_dir);
81738184
81748185 if ((err = check_cache(g, manifest_dir, &digest))) {
8175 fprintf(stderr, "Unable to check cache: %s\n", err_str(err));
8186 if (err == ErrorCacheUnavailable) {
8187 // message already printed
8188 } else {
8189 fprintf(stderr, "Unable to check cache: %s\n", err_str(err));
8190 }
81768191 exit(1);
81778192 }
81788193
src/error.cpp+1
......@@ -33,6 +33,7 @@ const char *err_str(Error err) {
3333 case ErrorSharingViolation: return "sharing violation";
3434 case ErrorPipeBusy: return "pipe busy";
3535 case ErrorPrimitiveTypeNotFound: return "primitive type not found";
36 case ErrorCacheUnavailable: return "cache unavailable";
3637 }
3738 return "(invalid error)";
3839}
src/error.hpp+1
......@@ -35,6 +35,7 @@ enum Error {
3535 ErrorSharingViolation,
3636 ErrorPipeBusy,
3737 ErrorPrimitiveTypeNotFound,
38 ErrorCacheUnavailable,
3839};
3940
4041const char *err_str(Error err);
src/ir.cpp+65-45
......@@ -11276,7 +11276,6 @@ static bool optional_value_is_null(ConstExprValue *val) {
1127611276}
1127711277
1127811278static IrInstruction *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *bin_op_instruction) {
11279 Error err;
1128011279 IrInstruction *op1 = bin_op_instruction->op1->child;
1128111280 if (type_is_invalid(op1->value.type))
1128211281 return ira->codegen->invalid_instruction;
......@@ -11470,10 +11469,19 @@ static IrInstruction *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *
1147011469 if (casted_op2 == ira->codegen->invalid_instruction)
1147111470 return ira->codegen->invalid_instruction;
1147211471
11473 if ((err = type_resolve(ira->codegen, resolved_type, ResolveStatusZeroBitsKnown)))
11474 return ira->codegen->invalid_instruction;
11472 bool requires_comptime;
11473 switch (type_requires_comptime(ira->codegen, resolved_type)) {
11474 case ReqCompTimeYes:
11475 requires_comptime = true;
11476 break;
11477 case ReqCompTimeNo:
11478 requires_comptime = false;
11479 break;
11480 case ReqCompTimeInvalid:
11481 return ira->codegen->invalid_instruction;
11482 }
1147511483
11476 bool one_possible_value = !type_requires_comptime(resolved_type) && !type_has_bits(resolved_type);
11484 bool one_possible_value = !requires_comptime && !type_has_bits(resolved_type);
1147711485 if (one_possible_value || (instr_is_comptime(casted_op1) && instr_is_comptime(casted_op2))) {
1147811486 ConstExprValue *op1_val = one_possible_value ? &casted_op1->value : ir_resolve_const(ira, casted_op1, UndefBad);
1147911487 if (op1_val == nullptr)
......@@ -12406,42 +12414,41 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruct
1240612414 ZigType *result_type = casted_init_value->value.type;
1240712415 if (type_is_invalid(result_type)) {
1240812416 result_type = ira->codegen->builtin_types.entry_invalid;
12409 } else {
12410 if ((err = type_resolve(ira->codegen, result_type, ResolveStatusZeroBitsKnown))) {
12411 result_type = ira->codegen->builtin_types.entry_invalid;
12412 }
12417 } else if (result_type->id == ZigTypeIdUnreachable || result_type->id == ZigTypeIdOpaque) {
12418 ir_add_error_node(ira, source_node,
12419 buf_sprintf("variable of type '%s' not allowed", buf_ptr(&result_type->name)));
12420 result_type = ira->codegen->builtin_types.entry_invalid;
1241312421 }
1241412422
12415 if (!type_is_invalid(result_type)) {
12416 if (result_type->id == ZigTypeIdUnreachable ||
12417 result_type->id == ZigTypeIdOpaque)
12418 {
12423 switch (type_requires_comptime(ira->codegen, result_type)) {
12424 case ReqCompTimeInvalid:
12425 result_type = ira->codegen->builtin_types.entry_invalid;
12426 break;
12427 case ReqCompTimeYes: {
12428 var_class_requires_const = true;
12429 if (!var->gen_is_const && !is_comptime_var) {
1241912430 ir_add_error_node(ira, source_node,
12420 buf_sprintf("variable of type '%s' not allowed", buf_ptr(&result_type->name)));
12431 buf_sprintf("variable of type '%s' must be const or comptime",
12432 buf_ptr(&result_type->name)));
1242112433 result_type = ira->codegen->builtin_types.entry_invalid;
12422 } else if (type_requires_comptime(result_type)) {
12434 }
12435 break;
12436 }
12437 case ReqCompTimeNo:
12438 if (casted_init_value->value.special == ConstValSpecialStatic &&
12439 casted_init_value->value.type->id == ZigTypeIdFn &&
12440 casted_init_value->value.data.x_ptr.data.fn.fn_entry->fn_inline == FnInlineAlways)
12441 {
1242312442 var_class_requires_const = true;
12424 if (!var->gen_is_const && !is_comptime_var) {
12425 ir_add_error_node(ira, source_node,
12426 buf_sprintf("variable of type '%s' must be const or comptime",
12427 buf_ptr(&result_type->name)));
12443 if (!var->src_is_const && !is_comptime_var) {
12444 ErrorMsg *msg = ir_add_error_node(ira, source_node,
12445 buf_sprintf("functions marked inline must be stored in const or comptime var"));
12446 AstNode *proto_node = casted_init_value->value.data.x_ptr.data.fn.fn_entry->proto_node;
12447 add_error_note(ira->codegen, msg, proto_node, buf_sprintf("declared here"));
1242812448 result_type = ira->codegen->builtin_types.entry_invalid;
1242912449 }
12430 } else {
12431 if (casted_init_value->value.special == ConstValSpecialStatic &&
12432 casted_init_value->value.type->id == ZigTypeIdFn &&
12433 casted_init_value->value.data.x_ptr.data.fn.fn_entry->fn_inline == FnInlineAlways)
12434 {
12435 var_class_requires_const = true;
12436 if (!var->src_is_const && !is_comptime_var) {
12437 ErrorMsg *msg = ir_add_error_node(ira, source_node,
12438 buf_sprintf("functions marked inline must be stored in const or comptime var"));
12439 AstNode *proto_node = casted_init_value->value.data.x_ptr.data.fn.fn_entry->proto_node;
12440 add_error_note(ira->codegen, msg, proto_node, buf_sprintf("declared here"));
12441 result_type = ira->codegen->builtin_types.entry_invalid;
12442 }
12443 }
1244412450 }
12451 break;
1244512452 }
1244612453
1244712454 if (var->value->type != nullptr && !is_comptime_var) {
......@@ -12912,10 +12919,15 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
1291212919 }
1291312920
1291412921 if (!comptime_arg) {
12915 if (type_requires_comptime(casted_arg->value.type)) {
12922 switch (type_requires_comptime(ira->codegen, casted_arg->value.type)) {
12923 case ReqCompTimeYes:
1291612924 ir_add_error(ira, casted_arg,
1291712925 buf_sprintf("parameter of type '%s' requires comptime", buf_ptr(&casted_arg->value.type->name)));
1291812926 return false;
12927 case ReqCompTimeInvalid:
12928 return false;
12929 case ReqCompTimeNo:
12930 break;
1291912931 }
1292012932
1292112933 casted_args[fn_type_id->param_count] = casted_arg;
......@@ -13388,12 +13400,15 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
1338813400 inst_fn_type_id.return_type = specified_return_type;
1338913401 }
1339013402
13391 if ((err = type_resolve(ira->codegen, specified_return_type, ResolveStatusZeroBitsKnown)))
13392 return ira->codegen->invalid_instruction;
13393
13394 if (type_requires_comptime(specified_return_type)) {
13403 switch (type_requires_comptime(ira->codegen, specified_return_type)) {
13404 case ReqCompTimeYes:
1339513405 // Throw out our work and call the function as if it were comptime.
13396 return ir_analyze_fn_call(ira, call_instruction, fn_entry, fn_type, fn_ref, first_arg_ptr, true, FnInlineAuto);
13406 return ir_analyze_fn_call(ira, call_instruction, fn_entry, fn_type, fn_ref, first_arg_ptr,
13407 true, FnInlineAuto);
13408 case ReqCompTimeInvalid:
13409 return ira->codegen->invalid_instruction;
13410 case ReqCompTimeNo:
13411 break;
1339713412 }
1339813413 }
1339913414 IrInstruction *async_allocator_inst = nullptr;
......@@ -14334,11 +14349,16 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1433414349
1433514350 } else {
1433614351 // runtime known element index
14337 if (type_requires_comptime(return_type)) {
14352 switch (type_requires_comptime(ira->codegen, return_type)) {
14353 case ReqCompTimeYes:
1433814354 ir_add_error(ira, elem_index,
1433914355 buf_sprintf("values of type '%s' must be comptime known, but index value is runtime known",
1434014356 buf_ptr(&return_type->data.pointer.child_type->name)));
1434114357 return ira->codegen->invalid_instruction;
14358 case ReqCompTimeInvalid:
14359 return ira->codegen->invalid_instruction;
14360 case ReqCompTimeNo:
14361 break;
1434214362 }
1434314363 if (ptr_align < abi_align) {
1434414364 if (elem_size >= ptr_align && elem_size % ptr_align == 0) {
......@@ -19390,7 +19410,6 @@ static IrInstruction *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
1939019410}
1939119411
1939219412static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstructionFnProto *instruction) {
19393 Error err;
1939419413 AstNode *proto_node = instruction->base.source_node;
1939519414 assert(proto_node->type == NodeTypeFnProto);
1939619415
......@@ -19429,11 +19448,8 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct
1942919448 if (type_is_invalid(param_type_value->value.type))
1943019449 return ira->codegen->invalid_instruction;
1943119450 ZigType *param_type = ir_resolve_type(ira, param_type_value);
19432 if (type_is_invalid(param_type))
19433 return ira->codegen->invalid_instruction;
19434 if ((err = type_resolve(ira->codegen, param_type, ResolveStatusZeroBitsKnown)))
19435 return ira->codegen->invalid_instruction;
19436 if (type_requires_comptime(param_type)) {
19451 switch (type_requires_comptime(ira->codegen, param_type)) {
19452 case ReqCompTimeYes:
1943719453 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
1943819454 ir_add_error(ira, param_type_value,
1943919455 buf_sprintf("parameter of type '%s' not allowed in function with calling convention '%s'",
......@@ -19443,6 +19459,10 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct
1944319459 param_info->type = param_type;
1944419460 fn_type_id.next_param_index += 1;
1944519461 return ir_const_type(ira, &instruction->base, get_generic_fn_type(ira->codegen, &fn_type_id));
19462 case ReqCompTimeInvalid:
19463 return ira->codegen->invalid_instruction;
19464 case ReqCompTimeNo:
19465 break;
1944619466 }
1944719467 if (!type_has_bits(param_type) && !calling_convention_allows_zig_types(fn_type_id.cc)) {
1944819468 ir_add_error(ira, param_type_value,
src/os.cpp+9-3
......@@ -193,14 +193,20 @@ void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename) {
193193 size_t len = buf_len(full_path);
194194 if (len != 0) {
195195 size_t last_index = len - 1;
196 if (os_is_sep(buf_ptr(full_path)[last_index])) {
196 char last_char = buf_ptr(full_path)[last_index];
197 if (os_is_sep(last_char)) {
198 if (last_index == 0) {
199 if (out_dirname) buf_init_from_mem(out_dirname, &last_char, 1);
200 if (out_basename) buf_init_from_str(out_basename, "");
201 return;
202 }
197203 last_index -= 1;
198204 }
199205 for (size_t i = last_index;;) {
200206 uint8_t c = buf_ptr(full_path)[i];
201207 if (os_is_sep(c)) {
202208 if (out_dirname) {
203 buf_init_from_mem(out_dirname, buf_ptr(full_path), i);
209 buf_init_from_mem(out_dirname, buf_ptr(full_path), (i == 0) ? 1 : i);
204210 }
205211 if (out_basename) {
206212 buf_init_from_mem(out_basename, buf_ptr(full_path) + i + 1, buf_len(full_path) - (i + 1));
......@@ -1990,7 +1996,7 @@ Error os_file_read(OsFile file, void *ptr, size_t *len) {
19901996 case EFAULT:
19911997 zig_unreachable();
19921998 case EISDIR:
1993 zig_unreachable();
1999 return ErrorIsDir;
19942000 default:
19952001 return ErrorFileSystem;
19962002 }
std/debug/index.zig+3-2
......@@ -282,8 +282,9 @@ fn printSourceAtAddressWindows(di: *DebugInfo, out_stream: var, relocated_addres
282282
283283 var coff_section: *coff.Section = undefined;
284284 const mod_index = for (di.sect_contribs) |sect_contrib| {
285 if (sect_contrib.Section >= di.coff.sections.len) continue;
286 coff_section = &di.coff.sections.toSlice()[sect_contrib.Section];
285 if (sect_contrib.Section > di.coff.sections.len) continue;
286 // Remember that SectionContribEntry.Section is 1-based.
287 coff_section = &di.coff.sections.toSlice()[sect_contrib.Section-1];
287288
288289 const vaddr_start = coff_section.header.virtual_address + sect_contrib.Offset;
289290 const vaddr_end = vaddr_start + sect_contrib.Size;
std/json.zig+17-10
......@@ -910,7 +910,7 @@ fn checkNext(p: *TokenStream, id: Token.Id) void {
910910 debug.assert(token.id == id);
911911}
912912
913test "token" {
913test "json.token" {
914914 const s =
915915 \\{
916916 \\ "Image": {
......@@ -980,7 +980,7 @@ pub fn validate(s: []const u8) bool {
980980 return p.complete;
981981}
982982
983test "json validate" {
983test "json.validate" {
984984 debug.assert(validate("{}"));
985985}
986986
......@@ -1188,7 +1188,7 @@ pub const Parser = struct {
11881188 }
11891189
11901190 var value = p.stack.pop();
1191 try p.pushToParent(value);
1191 try p.pushToParent(&value);
11921192 },
11931193 Token.Id.String => {
11941194 try p.stack.append(try p.parseString(allocator, token, input, i));
......@@ -1251,7 +1251,7 @@ pub const Parser = struct {
12511251 }
12521252
12531253 var value = p.stack.pop();
1254 try p.pushToParent(value);
1254 try p.pushToParent(&value);
12551255 },
12561256 Token.Id.ObjectBegin => {
12571257 try p.stack.append(Value{ .Object = ObjectMap.init(allocator) });
......@@ -1312,19 +1312,19 @@ pub const Parser = struct {
13121312 }
13131313 }
13141314
1315 fn pushToParent(p: *Parser, value: Value) !void {
1316 switch (p.stack.at(p.stack.len - 1)) {
1315 fn pushToParent(p: *Parser, value: *const Value) !void {
1316 switch (p.stack.toSlice()[p.stack.len - 1]) {
13171317 // Object Parent -> [ ..., object, <key>, value ]
13181318 Value.String => |key| {
13191319 _ = p.stack.pop();
13201320
13211321 var object = &p.stack.items[p.stack.len - 1].Object;
1322 _ = try object.put(key, value);
1322 _ = try object.put(key, value.*);
13231323 p.state = State.ObjectKey;
13241324 },
13251325 // Array Parent -> [ ..., <array>, value ]
13261326 Value.Array => |*array| {
1327 try array.append(value);
1327 try array.append(value.*);
13281328 p.state = State.ArrayValue;
13291329 },
13301330 else => {
......@@ -1348,7 +1348,7 @@ pub const Parser = struct {
13481348 }
13491349};
13501350
1351test "json parser dynamic" {
1351test "json.parser.dynamic" {
13521352 var p = Parser.init(debug.global_allocator, false);
13531353 defer p.deinit();
13541354
......@@ -1364,7 +1364,8 @@ test "json parser dynamic" {
13641364 \\ "Width": 100
13651365 \\ },
13661366 \\ "Animated" : false,
1367 \\ "IDs": [116, 943, 234, 38793]
1367 \\ "IDs": [116, 943, 234, 38793],
1368 \\ "ArrayOfObject": [{"n": "m"}]
13681369 \\ }
13691370 \\}
13701371 ;
......@@ -1387,4 +1388,10 @@ test "json parser dynamic" {
13871388
13881389 const animated = image.Object.get("Animated").?.value;
13891390 debug.assert(animated.Bool == false);
1391
1392 const array_of_object = image.Object.get("ArrayOfObject").?.value;
1393 debug.assert(array_of_object.Array.len == 1);
1394
1395 const obj0 = array_of_object.Array.at(0).Object.get("n").?.value;
1396 debug.assert(mem.eql(u8, obj0.String, "m"));
13901397}
std/json_test.zig+319-319
......@@ -21,7 +21,7 @@ fn any(comptime s: []const u8) void {
2121//
2222// Additional tests not part of test JSONTestSuite.
2323
24test "y_trailing_comma_after_empty" {
24test "json.test.y_trailing_comma_after_empty" {
2525 ok(
2626 \\{"1":[],"2":{},"3":"4"}
2727 );
......@@ -29,252 +29,252 @@ test "y_trailing_comma_after_empty" {
2929
3030////////////////////////////////////////////////////////////////////////////////////////////////////
3131
32test "y_array_arraysWithSpaces" {
32test "json.test.y_array_arraysWithSpaces" {
3333 ok(
3434 \\[[] ]
3535 );
3636}
3737
38test "y_array_empty" {
38test "json.test.y_array_empty" {
3939 ok(
4040 \\[]
4141 );
4242}
4343
44test "y_array_empty-string" {
44test "json.test.y_array_empty-string" {
4545 ok(
4646 \\[""]
4747 );
4848}
4949
50test "y_array_ending_with_newline" {
50test "json.test.y_array_ending_with_newline" {
5151 ok(
5252 \\["a"]
5353 );
5454}
5555
56test "y_array_false" {
56test "json.test.y_array_false" {
5757 ok(
5858 \\[false]
5959 );
6060}
6161
62test "y_array_heterogeneous" {
62test "json.test.y_array_heterogeneous" {
6363 ok(
6464 \\[null, 1, "1", {}]
6565 );
6666}
6767
68test "y_array_null" {
68test "json.test.y_array_null" {
6969 ok(
7070 \\[null]
7171 );
7272}
7373
74test "y_array_with_1_and_newline" {
74test "json.test.y_array_with_1_and_newline" {
7575 ok(
7676 \\[1
7777 \\]
7878 );
7979}
8080
81test "y_array_with_leading_space" {
81test "json.test.y_array_with_leading_space" {
8282 ok(
8383 \\ [1]
8484 );
8585}
8686
87test "y_array_with_several_null" {
87test "json.test.y_array_with_several_null" {
8888 ok(
8989 \\[1,null,null,null,2]
9090 );
9191}
9292
93test "y_array_with_trailing_space" {
93test "json.test.y_array_with_trailing_space" {
9494 ok("[2] ");
9595}
9696
97test "y_number_0e+1" {
97test "json.test.y_number_0e+1" {
9898 ok(
9999 \\[0e+1]
100100 );
101101}
102102
103test "y_number_0e1" {
103test "json.test.y_number_0e1" {
104104 ok(
105105 \\[0e1]
106106 );
107107}
108108
109test "y_number_after_space" {
109test "json.test.y_number_after_space" {
110110 ok(
111111 \\[ 4]
112112 );
113113}
114114
115test "y_number_double_close_to_zero" {
115test "json.test.y_number_double_close_to_zero" {
116116 ok(
117117 \\[-0.000000000000000000000000000000000000000000000000000000000000000000000000000001]
118118 );
119119}
120120
121test "y_number_int_with_exp" {
121test "json.test.y_number_int_with_exp" {
122122 ok(
123123 \\[20e1]
124124 );
125125}
126126
127test "y_number" {
127test "json.test.y_number" {
128128 ok(
129129 \\[123e65]
130130 );
131131}
132132
133test "y_number_minus_zero" {
133test "json.test.y_number_minus_zero" {
134134 ok(
135135 \\[-0]
136136 );
137137}
138138
139test "y_number_negative_int" {
139test "json.test.y_number_negative_int" {
140140 ok(
141141 \\[-123]
142142 );
143143}
144144
145test "y_number_negative_one" {
145test "json.test.y_number_negative_one" {
146146 ok(
147147 \\[-1]
148148 );
149149}
150150
151test "y_number_negative_zero" {
151test "json.test.y_number_negative_zero" {
152152 ok(
153153 \\[-0]
154154 );
155155}
156156
157test "y_number_real_capital_e" {
157test "json.test.y_number_real_capital_e" {
158158 ok(
159159 \\[1E22]
160160 );
161161}
162162
163test "y_number_real_capital_e_neg_exp" {
163test "json.test.y_number_real_capital_e_neg_exp" {
164164 ok(
165165 \\[1E-2]
166166 );
167167}
168168
169test "y_number_real_capital_e_pos_exp" {
169test "json.test.y_number_real_capital_e_pos_exp" {
170170 ok(
171171 \\[1E+2]
172172 );
173173}
174174
175test "y_number_real_exponent" {
175test "json.test.y_number_real_exponent" {
176176 ok(
177177 \\[123e45]
178178 );
179179}
180180
181test "y_number_real_fraction_exponent" {
181test "json.test.y_number_real_fraction_exponent" {
182182 ok(
183183 \\[123.456e78]
184184 );
185185}
186186
187test "y_number_real_neg_exp" {
187test "json.test.y_number_real_neg_exp" {
188188 ok(
189189 \\[1e-2]
190190 );
191191}
192192
193test "y_number_real_pos_exponent" {
193test "json.test.y_number_real_pos_exponent" {
194194 ok(
195195 \\[1e+2]
196196 );
197197}
198198
199test "y_number_simple_int" {
199test "json.test.y_number_simple_int" {
200200 ok(
201201 \\[123]
202202 );
203203}
204204
205test "y_number_simple_real" {
205test "json.test.y_number_simple_real" {
206206 ok(
207207 \\[123.456789]
208208 );
209209}
210210
211test "y_object_basic" {
211test "json.test.y_object_basic" {
212212 ok(
213213 \\{"asd":"sdf"}
214214 );
215215}
216216
217test "y_object_duplicated_key_and_value" {
217test "json.test.y_object_duplicated_key_and_value" {
218218 ok(
219219 \\{"a":"b","a":"b"}
220220 );
221221}
222222
223test "y_object_duplicated_key" {
223test "json.test.y_object_duplicated_key" {
224224 ok(
225225 \\{"a":"b","a":"c"}
226226 );
227227}
228228
229test "y_object_empty" {
229test "json.test.y_object_empty" {
230230 ok(
231231 \\{}
232232 );
233233}
234234
235test "y_object_empty_key" {
235test "json.test.y_object_empty_key" {
236236 ok(
237237 \\{"":0}
238238 );
239239}
240240
241test "y_object_escaped_null_in_key" {
241test "json.test.y_object_escaped_null_in_key" {
242242 ok(
243243 \\{"foo\u0000bar": 42}
244244 );
245245}
246246
247test "y_object_extreme_numbers" {
247test "json.test.y_object_extreme_numbers" {
248248 ok(
249249 \\{ "min": -1.0e+28, "max": 1.0e+28 }
250250 );
251251}
252252
253test "y_object" {
253test "json.test.y_object" {
254254 ok(
255255 \\{"asd":"sdf", "dfg":"fgh"}
256256 );
257257}
258258
259test "y_object_long_strings" {
259test "json.test.y_object_long_strings" {
260260 ok(
261261 \\{"x":[{"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}], "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
262262 );
263263}
264264
265test "y_object_simple" {
265test "json.test.y_object_simple" {
266266 ok(
267267 \\{"a":[]}
268268 );
269269}
270270
271test "y_object_string_unicode" {
271test "json.test.y_object_string_unicode" {
272272 ok(
273273 \\{"title":"\u041f\u043e\u043b\u0442\u043e\u0440\u0430 \u0417\u0435\u043c\u043b\u0435\u043a\u043e\u043f\u0430" }
274274 );
275275}
276276
277test "y_object_with_newlines" {
277test "json.test.y_object_with_newlines" {
278278 ok(
279279 \\{
280280 \\"a": "b"
......@@ -282,419 +282,419 @@ test "y_object_with_newlines" {
282282 );
283283}
284284
285test "y_string_1_2_3_bytes_UTF-8_sequences" {
285test "json.test.y_string_1_2_3_bytes_UTF-8_sequences" {
286286 ok(
287287 \\["\u0060\u012a\u12AB"]
288288 );
289289}
290290
291test "y_string_accepted_surrogate_pair" {
291test "json.test.y_string_accepted_surrogate_pair" {
292292 ok(
293293 \\["\uD801\udc37"]
294294 );
295295}
296296
297test "y_string_accepted_surrogate_pairs" {
297test "json.test.y_string_accepted_surrogate_pairs" {
298298 ok(
299299 \\["\ud83d\ude39\ud83d\udc8d"]
300300 );
301301}
302302
303test "y_string_allowed_escapes" {
303test "json.test.y_string_allowed_escapes" {
304304 ok(
305305 \\["\"\\\/\b\f\n\r\t"]
306306 );
307307}
308308
309test "y_string_backslash_and_u_escaped_zero" {
309test "json.test.y_string_backslash_and_u_escaped_zero" {
310310 ok(
311311 \\["\\u0000"]
312312 );
313313}
314314
315test "y_string_backslash_doublequotes" {
315test "json.test.y_string_backslash_doublequotes" {
316316 ok(
317317 \\["\""]
318318 );
319319}
320320
321test "y_string_comments" {
321test "json.test.y_string_comments" {
322322 ok(
323323 \\["a/*b*/c/*d//e"]
324324 );
325325}
326326
327test "y_string_double_escape_a" {
327test "json.test.y_string_double_escape_a" {
328328 ok(
329329 \\["\\a"]
330330 );
331331}
332332
333test "y_string_double_escape_n" {
333test "json.test.y_string_double_escape_n" {
334334 ok(
335335 \\["\\n"]
336336 );
337337}
338338
339test "y_string_escaped_control_character" {
339test "json.test.y_string_escaped_control_character" {
340340 ok(
341341 \\["\u0012"]
342342 );
343343}
344344
345test "y_string_escaped_noncharacter" {
345test "json.test.y_string_escaped_noncharacter" {
346346 ok(
347347 \\["\uFFFF"]
348348 );
349349}
350350
351test "y_string_in_array" {
351test "json.test.y_string_in_array" {
352352 ok(
353353 \\["asd"]
354354 );
355355}
356356
357test "y_string_in_array_with_leading_space" {
357test "json.test.y_string_in_array_with_leading_space" {
358358 ok(
359359 \\[ "asd"]
360360 );
361361}
362362
363test "y_string_last_surrogates_1_and_2" {
363test "json.test.y_string_last_surrogates_1_and_2" {
364364 ok(
365365 \\["\uDBFF\uDFFF"]
366366 );
367367}
368368
369test "y_string_nbsp_uescaped" {
369test "json.test.y_string_nbsp_uescaped" {
370370 ok(
371371 \\["new\u00A0line"]
372372 );
373373}
374374
375test "y_string_nonCharacterInUTF-8_U+10FFFF" {
375test "json.test.y_string_nonCharacterInUTF-8_U+10FFFF" {
376376 ok(
377377 \\["􏿿"]
378378 );
379379}
380380
381test "y_string_nonCharacterInUTF-8_U+FFFF" {
381test "json.test.y_string_nonCharacterInUTF-8_U+FFFF" {
382382 ok(
383383 \\["￿"]
384384 );
385385}
386386
387test "y_string_null_escape" {
387test "json.test.y_string_null_escape" {
388388 ok(
389389 \\["\u0000"]
390390 );
391391}
392392
393test "y_string_one-byte-utf-8" {
393test "json.test.y_string_one-byte-utf-8" {
394394 ok(
395395 \\["\u002c"]
396396 );
397397}
398398
399test "y_string_pi" {
399test "json.test.y_string_pi" {
400400 ok(
401401 \\["π"]
402402 );
403403}
404404
405test "y_string_reservedCharacterInUTF-8_U+1BFFF" {
405test "json.test.y_string_reservedCharacterInUTF-8_U+1BFFF" {
406406 ok(
407407 \\["𛿿"]
408408 );
409409}
410410
411test "y_string_simple_ascii" {
411test "json.test.y_string_simple_ascii" {
412412 ok(
413413 \\["asd "]
414414 );
415415}
416416
417test "y_string_space" {
417test "json.test.y_string_space" {
418418 ok(
419419 \\" "
420420 );
421421}
422422
423test "y_string_surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF" {
423test "json.test.y_string_surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF" {
424424 ok(
425425 \\["\uD834\uDd1e"]
426426 );
427427}
428428
429test "y_string_three-byte-utf-8" {
429test "json.test.y_string_three-byte-utf-8" {
430430 ok(
431431 \\["\u0821"]
432432 );
433433}
434434
435test "y_string_two-byte-utf-8" {
435test "json.test.y_string_two-byte-utf-8" {
436436 ok(
437437 \\["\u0123"]
438438 );
439439}
440440
441test "y_string_u+2028_line_sep" {
441test "json.test.y_string_u+2028_line_sep" {
442442 ok("[\"\xe2\x80\xa8\"]");
443443}
444444
445test "y_string_u+2029_par_sep" {
445test "json.test.y_string_u+2029_par_sep" {
446446 ok("[\"\xe2\x80\xa9\"]");
447447}
448448
449test "y_string_uescaped_newline" {
449test "json.test.y_string_uescaped_newline" {
450450 ok(
451451 \\["new\u000Aline"]
452452 );
453453}
454454
455test "y_string_uEscape" {
455test "json.test.y_string_uEscape" {
456456 ok(
457457 \\["\u0061\u30af\u30EA\u30b9"]
458458 );
459459}
460460
461test "y_string_unescaped_char_delete" {
461test "json.test.y_string_unescaped_char_delete" {
462462 ok("[\"\x7f\"]");
463463}
464464
465test "y_string_unicode_2" {
465test "json.test.y_string_unicode_2" {
466466 ok(
467467 \\["⍂㈴⍂"]
468468 );
469469}
470470
471test "y_string_unicodeEscapedBackslash" {
471test "json.test.y_string_unicodeEscapedBackslash" {
472472 ok(
473473 \\["\u005C"]
474474 );
475475}
476476
477test "y_string_unicode_escaped_double_quote" {
477test "json.test.y_string_unicode_escaped_double_quote" {
478478 ok(
479479 \\["\u0022"]
480480 );
481481}
482482
483test "y_string_unicode" {
483test "json.test.y_string_unicode" {
484484 ok(
485485 \\["\uA66D"]
486486 );
487487}
488488
489test "y_string_unicode_U+10FFFE_nonchar" {
489test "json.test.y_string_unicode_U+10FFFE_nonchar" {
490490 ok(
491491 \\["\uDBFF\uDFFE"]
492492 );
493493}
494494
495test "y_string_unicode_U+1FFFE_nonchar" {
495test "json.test.y_string_unicode_U+1FFFE_nonchar" {
496496 ok(
497497 \\["\uD83F\uDFFE"]
498498 );
499499}
500500
501test "y_string_unicode_U+200B_ZERO_WIDTH_SPACE" {
501test "json.test.y_string_unicode_U+200B_ZERO_WIDTH_SPACE" {
502502 ok(
503503 \\["\u200B"]
504504 );
505505}
506506
507test "y_string_unicode_U+2064_invisible_plus" {
507test "json.test.y_string_unicode_U+2064_invisible_plus" {
508508 ok(
509509 \\["\u2064"]
510510 );
511511}
512512
513test "y_string_unicode_U+FDD0_nonchar" {
513test "json.test.y_string_unicode_U+FDD0_nonchar" {
514514 ok(
515515 \\["\uFDD0"]
516516 );
517517}
518518
519test "y_string_unicode_U+FFFE_nonchar" {
519test "json.test.y_string_unicode_U+FFFE_nonchar" {
520520 ok(
521521 \\["\uFFFE"]
522522 );
523523}
524524
525test "y_string_utf8" {
525test "json.test.y_string_utf8" {
526526 ok(
527527 \\["€𝄞"]
528528 );
529529}
530530
531test "y_string_with_del_character" {
531test "json.test.y_string_with_del_character" {
532532 ok("[\"a\x7fa\"]");
533533}
534534
535test "y_structure_lonely_false" {
535test "json.test.y_structure_lonely_false" {
536536 ok(
537537 \\false
538538 );
539539}
540540
541test "y_structure_lonely_int" {
541test "json.test.y_structure_lonely_int" {
542542 ok(
543543 \\42
544544 );
545545}
546546
547test "y_structure_lonely_negative_real" {
547test "json.test.y_structure_lonely_negative_real" {
548548 ok(
549549 \\-0.1
550550 );
551551}
552552
553test "y_structure_lonely_null" {
553test "json.test.y_structure_lonely_null" {
554554 ok(
555555 \\null
556556 );
557557}
558558
559test "y_structure_lonely_string" {
559test "json.test.y_structure_lonely_string" {
560560 ok(
561561 \\"asd"
562562 );
563563}
564564
565test "y_structure_lonely_true" {
565test "json.test.y_structure_lonely_true" {
566566 ok(
567567 \\true
568568 );
569569}
570570
571test "y_structure_string_empty" {
571test "json.test.y_structure_string_empty" {
572572 ok(
573573 \\""
574574 );
575575}
576576
577test "y_structure_trailing_newline" {
577test "json.test.y_structure_trailing_newline" {
578578 ok(
579579 \\["a"]
580580 );
581581}
582582
583test "y_structure_true_in_array" {
583test "json.test.y_structure_true_in_array" {
584584 ok(
585585 \\[true]
586586 );
587587}
588588
589test "y_structure_whitespace_array" {
589test "json.test.y_structure_whitespace_array" {
590590 ok(" [] ");
591591}
592592
593593////////////////////////////////////////////////////////////////////////////////////////////////////
594594
595test "n_array_1_true_without_comma" {
595test "json.test.n_array_1_true_without_comma" {
596596 err(
597597 \\[1 true]
598598 );
599599}
600600
601test "n_array_a_invalid_utf8" {
601test "json.test.n_array_a_invalid_utf8" {
602602 err(
603603 \\[aå]
604604 );
605605}
606606
607test "n_array_colon_instead_of_comma" {
607test "json.test.n_array_colon_instead_of_comma" {
608608 err(
609609 \\["": 1]
610610 );
611611}
612612
613test "n_array_comma_after_close" {
613test "json.test.n_array_comma_after_close" {
614614 //err(
615615 // \\[""],
616616 //);
617617}
618618
619test "n_array_comma_and_number" {
619test "json.test.n_array_comma_and_number" {
620620 err(
621621 \\[,1]
622622 );
623623}
624624
625test "n_array_double_comma" {
625test "json.test.n_array_double_comma" {
626626 err(
627627 \\[1,,2]
628628 );
629629}
630630
631test "n_array_double_extra_comma" {
631test "json.test.n_array_double_extra_comma" {
632632 err(
633633 \\["x",,]
634634 );
635635}
636636
637test "n_array_extra_close" {
637test "json.test.n_array_extra_close" {
638638 err(
639639 \\["x"]]
640640 );
641641}
642642
643test "n_array_extra_comma" {
643test "json.test.n_array_extra_comma" {
644644 //err(
645645 // \\["",]
646646 //);
647647}
648648
649test "n_array_incomplete_invalid_value" {
649test "json.test.n_array_incomplete_invalid_value" {
650650 err(
651651 \\[x
652652 );
653653}
654654
655test "n_array_incomplete" {
655test "json.test.n_array_incomplete" {
656656 err(
657657 \\["x"
658658 );
659659}
660660
661test "n_array_inner_array_no_comma" {
661test "json.test.n_array_inner_array_no_comma" {
662662 err(
663663 \\[3[4]]
664664 );
665665}
666666
667test "n_array_invalid_utf8" {
667test "json.test.n_array_invalid_utf8" {
668668 err(
669669 \\[ÿ]
670670 );
671671}
672672
673test "n_array_items_separated_by_semicolon" {
673test "json.test.n_array_items_separated_by_semicolon" {
674674 err(
675675 \\[1:2]
676676 );
677677}
678678
679test "n_array_just_comma" {
679test "json.test.n_array_just_comma" {
680680 err(
681681 \\[,]
682682 );
683683}
684684
685test "n_array_just_minus" {
685test "json.test.n_array_just_minus" {
686686 err(
687687 \\[-]
688688 );
689689}
690690
691test "n_array_missing_value" {
691test "json.test.n_array_missing_value" {
692692 err(
693693 \\[ , ""]
694694 );
695695}
696696
697test "n_array_newlines_unclosed" {
697test "json.test.n_array_newlines_unclosed" {
698698 err(
699699 \\["a",
700700 \\4
......@@ -702,41 +702,41 @@ test "n_array_newlines_unclosed" {
702702 );
703703}
704704
705test "n_array_number_and_comma" {
705test "json.test.n_array_number_and_comma" {
706706 err(
707707 \\[1,]
708708 );
709709}
710710
711test "n_array_number_and_several_commas" {
711test "json.test.n_array_number_and_several_commas" {
712712 err(
713713 \\[1,,]
714714 );
715715}
716716
717test "n_array_spaces_vertical_tab_formfeed" {
717test "json.test.n_array_spaces_vertical_tab_formfeed" {
718718 err("[\"\x0aa\"\\f]");
719719}
720720
721test "n_array_star_inside" {
721test "json.test.n_array_star_inside" {
722722 err(
723723 \\[*]
724724 );
725725}
726726
727test "n_array_unclosed" {
727test "json.test.n_array_unclosed" {
728728 err(
729729 \\[""
730730 );
731731}
732732
733test "n_array_unclosed_trailing_comma" {
733test "json.test.n_array_unclosed_trailing_comma" {
734734 err(
735735 \\[1,
736736 );
737737}
738738
739test "n_array_unclosed_with_new_lines" {
739test "json.test.n_array_unclosed_with_new_lines" {
740740 err(
741741 \\[1,
742742 \\1
......@@ -744,956 +744,956 @@ test "n_array_unclosed_with_new_lines" {
744744 );
745745}
746746
747test "n_array_unclosed_with_object_inside" {
747test "json.test.n_array_unclosed_with_object_inside" {
748748 err(
749749 \\[{}
750750 );
751751}
752752
753test "n_incomplete_false" {
753test "json.test.n_incomplete_false" {
754754 err(
755755 \\[fals]
756756 );
757757}
758758
759test "n_incomplete_null" {
759test "json.test.n_incomplete_null" {
760760 err(
761761 \\[nul]
762762 );
763763}
764764
765test "n_incomplete_true" {
765test "json.test.n_incomplete_true" {
766766 err(
767767 \\[tru]
768768 );
769769}
770770
771test "n_multidigit_number_then_00" {
771test "json.test.n_multidigit_number_then_00" {
772772 err("123\x00");
773773}
774774
775test "n_number_0.1.2" {
775test "json.test.n_number_0.1.2" {
776776 err(
777777 \\[0.1.2]
778778 );
779779}
780780
781test "n_number_-01" {
781test "json.test.n_number_-01" {
782782 err(
783783 \\[-01]
784784 );
785785}
786786
787test "n_number_0.3e" {
787test "json.test.n_number_0.3e" {
788788 err(
789789 \\[0.3e]
790790 );
791791}
792792
793test "n_number_0.3e+" {
793test "json.test.n_number_0.3e+" {
794794 err(
795795 \\[0.3e+]
796796 );
797797}
798798
799test "n_number_0_capital_E" {
799test "json.test.n_number_0_capital_E" {
800800 err(
801801 \\[0E]
802802 );
803803}
804804
805test "n_number_0_capital_E+" {
805test "json.test.n_number_0_capital_E+" {
806806 err(
807807 \\[0E+]
808808 );
809809}
810810
811test "n_number_0.e1" {
811test "json.test.n_number_0.e1" {
812812 err(
813813 \\[0.e1]
814814 );
815815}
816816
817test "n_number_0e" {
817test "json.test.n_number_0e" {
818818 err(
819819 \\[0e]
820820 );
821821}
822822
823test "n_number_0e+" {
823test "json.test.n_number_0e+" {
824824 err(
825825 \\[0e+]
826826 );
827827}
828828
829test "n_number_1_000" {
829test "json.test.n_number_1_000" {
830830 err(
831831 \\[1 000.0]
832832 );
833833}
834834
835test "n_number_1.0e-" {
835test "json.test.n_number_1.0e-" {
836836 err(
837837 \\[1.0e-]
838838 );
839839}
840840
841test "n_number_1.0e" {
841test "json.test.n_number_1.0e" {
842842 err(
843843 \\[1.0e]
844844 );
845845}
846846
847test "n_number_1.0e+" {
847test "json.test.n_number_1.0e+" {
848848 err(
849849 \\[1.0e+]
850850 );
851851}
852852
853test "n_number_-1.0." {
853test "json.test.n_number_-1.0." {
854854 err(
855855 \\[-1.0.]
856856 );
857857}
858858
859test "n_number_1eE2" {
859test "json.test.n_number_1eE2" {
860860 err(
861861 \\[1eE2]
862862 );
863863}
864864
865test "n_number_.-1" {
865test "json.test.n_number_.-1" {
866866 err(
867867 \\[.-1]
868868 );
869869}
870870
871test "n_number_+1" {
871test "json.test.n_number_+1" {
872872 err(
873873 \\[+1]
874874 );
875875}
876876
877test "n_number_.2e-3" {
877test "json.test.n_number_.2e-3" {
878878 err(
879879 \\[.2e-3]
880880 );
881881}
882882
883test "n_number_2.e-3" {
883test "json.test.n_number_2.e-3" {
884884 err(
885885 \\[2.e-3]
886886 );
887887}
888888
889test "n_number_2.e+3" {
889test "json.test.n_number_2.e+3" {
890890 err(
891891 \\[2.e+3]
892892 );
893893}
894894
895test "n_number_2.e3" {
895test "json.test.n_number_2.e3" {
896896 err(
897897 \\[2.e3]
898898 );
899899}
900900
901test "n_number_-2." {
901test "json.test.n_number_-2." {
902902 err(
903903 \\[-2.]
904904 );
905905}
906906
907test "n_number_9.e+" {
907test "json.test.n_number_9.e+" {
908908 err(
909909 \\[9.e+]
910910 );
911911}
912912
913test "n_number_expression" {
913test "json.test.n_number_expression" {
914914 err(
915915 \\[1+2]
916916 );
917917}
918918
919test "n_number_hex_1_digit" {
919test "json.test.n_number_hex_1_digit" {
920920 err(
921921 \\[0x1]
922922 );
923923}
924924
925test "n_number_hex_2_digits" {
925test "json.test.n_number_hex_2_digits" {
926926 err(
927927 \\[0x42]
928928 );
929929}
930930
931test "n_number_infinity" {
931test "json.test.n_number_infinity" {
932932 err(
933933 \\[Infinity]
934934 );
935935}
936936
937test "n_number_+Inf" {
937test "json.test.n_number_+Inf" {
938938 err(
939939 \\[+Inf]
940940 );
941941}
942942
943test "n_number_Inf" {
943test "json.test.n_number_Inf" {
944944 err(
945945 \\[Inf]
946946 );
947947}
948948
949test "n_number_invalid+-" {
949test "json.test.n_number_invalid+-" {
950950 err(
951951 \\[0e+-1]
952952 );
953953}
954954
955test "n_number_invalid-negative-real" {
955test "json.test.n_number_invalid-negative-real" {
956956 err(
957957 \\[-123.123foo]
958958 );
959959}
960960
961test "n_number_invalid-utf-8-in-bigger-int" {
961test "json.test.n_number_invalid-utf-8-in-bigger-int" {
962962 err(
963963 \\[123å]
964964 );
965965}
966966
967test "n_number_invalid-utf-8-in-exponent" {
967test "json.test.n_number_invalid-utf-8-in-exponent" {
968968 err(
969969 \\[1e1å]
970970 );
971971}
972972
973test "n_number_invalid-utf-8-in-int" {
973test "json.test.n_number_invalid-utf-8-in-int" {
974974 err(
975975 \\[0å]
976976 );
977977}
978978
979test "n_number_++" {
979test "json.test.n_number_++" {
980980 err(
981981 \\[++1234]
982982 );
983983}
984984
985test "n_number_minus_infinity" {
985test "json.test.n_number_minus_infinity" {
986986 err(
987987 \\[-Infinity]
988988 );
989989}
990990
991test "n_number_minus_sign_with_trailing_garbage" {
991test "json.test.n_number_minus_sign_with_trailing_garbage" {
992992 err(
993993 \\[-foo]
994994 );
995995}
996996
997test "n_number_minus_space_1" {
997test "json.test.n_number_minus_space_1" {
998998 err(
999999 \\[- 1]
10001000 );
10011001}
10021002
1003test "n_number_-NaN" {
1003test "json.test.n_number_-NaN" {
10041004 err(
10051005 \\[-NaN]
10061006 );
10071007}
10081008
1009test "n_number_NaN" {
1009test "json.test.n_number_NaN" {
10101010 err(
10111011 \\[NaN]
10121012 );
10131013}
10141014
1015test "n_number_neg_int_starting_with_zero" {
1015test "json.test.n_number_neg_int_starting_with_zero" {
10161016 err(
10171017 \\[-012]
10181018 );
10191019}
10201020
1021test "n_number_neg_real_without_int_part" {
1021test "json.test.n_number_neg_real_without_int_part" {
10221022 err(
10231023 \\[-.123]
10241024 );
10251025}
10261026
1027test "n_number_neg_with_garbage_at_end" {
1027test "json.test.n_number_neg_with_garbage_at_end" {
10281028 err(
10291029 \\[-1x]
10301030 );
10311031}
10321032
1033test "n_number_real_garbage_after_e" {
1033test "json.test.n_number_real_garbage_after_e" {
10341034 err(
10351035 \\[1ea]
10361036 );
10371037}
10381038
1039test "n_number_real_with_invalid_utf8_after_e" {
1039test "json.test.n_number_real_with_invalid_utf8_after_e" {
10401040 err(
10411041 \\[1eå]
10421042 );
10431043}
10441044
1045test "n_number_real_without_fractional_part" {
1045test "json.test.n_number_real_without_fractional_part" {
10461046 err(
10471047 \\[1.]
10481048 );
10491049}
10501050
1051test "n_number_starting_with_dot" {
1051test "json.test.n_number_starting_with_dot" {
10521052 err(
10531053 \\[.123]
10541054 );
10551055}
10561056
1057test "n_number_U+FF11_fullwidth_digit_one" {
1057test "json.test.n_number_U+FF11_fullwidth_digit_one" {
10581058 err(
10591059 \\[1]
10601060 );
10611061}
10621062
1063test "n_number_with_alpha_char" {
1063test "json.test.n_number_with_alpha_char" {
10641064 err(
10651065 \\[1.8011670033376514H-308]
10661066 );
10671067}
10681068
1069test "n_number_with_alpha" {
1069test "json.test.n_number_with_alpha" {
10701070 err(
10711071 \\[1.2a-3]
10721072 );
10731073}
10741074
1075test "n_number_with_leading_zero" {
1075test "json.test.n_number_with_leading_zero" {
10761076 err(
10771077 \\[012]
10781078 );
10791079}
10801080
1081test "n_object_bad_value" {
1081test "json.test.n_object_bad_value" {
10821082 err(
10831083 \\["x", truth]
10841084 );
10851085}
10861086
1087test "n_object_bracket_key" {
1087test "json.test.n_object_bracket_key" {
10881088 err(
10891089 \\{[: "x"}
10901090 );
10911091}
10921092
1093test "n_object_comma_instead_of_colon" {
1093test "json.test.n_object_comma_instead_of_colon" {
10941094 err(
10951095 \\{"x", null}
10961096 );
10971097}
10981098
1099test "n_object_double_colon" {
1099test "json.test.n_object_double_colon" {
11001100 err(
11011101 \\{"x"::"b"}
11021102 );
11031103}
11041104
1105test "n_object_emoji" {
1105test "json.test.n_object_emoji" {
11061106 err(
11071107 \\{🇨🇭}
11081108 );
11091109}
11101110
1111test "n_object_garbage_at_end" {
1111test "json.test.n_object_garbage_at_end" {
11121112 err(
11131113 \\{"a":"a" 123}
11141114 );
11151115}
11161116
1117test "n_object_key_with_single_quotes" {
1117test "json.test.n_object_key_with_single_quotes" {
11181118 err(
11191119 \\{key: 'value'}
11201120 );
11211121}
11221122
1123test "n_object_lone_continuation_byte_in_key_and_trailing_comma" {
1123test "json.test.n_object_lone_continuation_byte_in_key_and_trailing_comma" {
11241124 err(
11251125 \\{"¹":"0",}
11261126 );
11271127}
11281128
1129test "n_object_missing_colon" {
1129test "json.test.n_object_missing_colon" {
11301130 err(
11311131 \\{"a" b}
11321132 );
11331133}
11341134
1135test "n_object_missing_key" {
1135test "json.test.n_object_missing_key" {
11361136 err(
11371137 \\{:"b"}
11381138 );
11391139}
11401140
1141test "n_object_missing_semicolon" {
1141test "json.test.n_object_missing_semicolon" {
11421142 err(
11431143 \\{"a" "b"}
11441144 );
11451145}
11461146
1147test "n_object_missing_value" {
1147test "json.test.n_object_missing_value" {
11481148 err(
11491149 \\{"a":
11501150 );
11511151}
11521152
1153test "n_object_no-colon" {
1153test "json.test.n_object_no-colon" {
11541154 err(
11551155 \\{"a"
11561156 );
11571157}
11581158
1159test "n_object_non_string_key_but_huge_number_instead" {
1159test "json.test.n_object_non_string_key_but_huge_number_instead" {
11601160 err(
11611161 \\{9999E9999:1}
11621162 );
11631163}
11641164
1165test "n_object_non_string_key" {
1165test "json.test.n_object_non_string_key" {
11661166 err(
11671167 \\{1:1}
11681168 );
11691169}
11701170
1171test "n_object_repeated_null_null" {
1171test "json.test.n_object_repeated_null_null" {
11721172 err(
11731173 \\{null:null,null:null}
11741174 );
11751175}
11761176
1177test "n_object_several_trailing_commas" {
1177test "json.test.n_object_several_trailing_commas" {
11781178 err(
11791179 \\{"id":0,,,,,}
11801180 );
11811181}
11821182
1183test "n_object_single_quote" {
1183test "json.test.n_object_single_quote" {
11841184 err(
11851185 \\{'a':0}
11861186 );
11871187}
11881188
1189test "n_object_trailing_comma" {
1189test "json.test.n_object_trailing_comma" {
11901190 err(
11911191 \\{"id":0,}
11921192 );
11931193}
11941194
1195test "n_object_trailing_comment" {
1195test "json.test.n_object_trailing_comment" {
11961196 err(
11971197 \\{"a":"b"}/**/
11981198 );
11991199}
12001200
1201test "n_object_trailing_comment_open" {
1201test "json.test.n_object_trailing_comment_open" {
12021202 err(
12031203 \\{"a":"b"}/**//
12041204 );
12051205}
12061206
1207test "n_object_trailing_comment_slash_open_incomplete" {
1207test "json.test.n_object_trailing_comment_slash_open_incomplete" {
12081208 err(
12091209 \\{"a":"b"}/
12101210 );
12111211}
12121212
1213test "n_object_trailing_comment_slash_open" {
1213test "json.test.n_object_trailing_comment_slash_open" {
12141214 err(
12151215 \\{"a":"b"}//
12161216 );
12171217}
12181218
1219test "n_object_two_commas_in_a_row" {
1219test "json.test.n_object_two_commas_in_a_row" {
12201220 err(
12211221 \\{"a":"b",,"c":"d"}
12221222 );
12231223}
12241224
1225test "n_object_unquoted_key" {
1225test "json.test.n_object_unquoted_key" {
12261226 err(
12271227 \\{a: "b"}
12281228 );
12291229}
12301230
1231test "n_object_unterminated-value" {
1231test "json.test.n_object_unterminated-value" {
12321232 err(
12331233 \\{"a":"a
12341234 );
12351235}
12361236
1237test "n_object_with_single_string" {
1237test "json.test.n_object_with_single_string" {
12381238 err(
12391239 \\{ "foo" : "bar", "a" }
12401240 );
12411241}
12421242
1243test "n_object_with_trailing_garbage" {
1243test "json.test.n_object_with_trailing_garbage" {
12441244 err(
12451245 \\{"a":"b"}#
12461246 );
12471247}
12481248
1249test "n_single_space" {
1249test "json.test.n_single_space" {
12501250 err(" ");
12511251}
12521252
1253test "n_string_1_surrogate_then_escape" {
1253test "json.test.n_string_1_surrogate_then_escape" {
12541254 err(
12551255 \\["\uD800\"]
12561256 );
12571257}
12581258
1259test "n_string_1_surrogate_then_escape_u1" {
1259test "json.test.n_string_1_surrogate_then_escape_u1" {
12601260 err(
12611261 \\["\uD800\u1"]
12621262 );
12631263}
12641264
1265test "n_string_1_surrogate_then_escape_u1x" {
1265test "json.test.n_string_1_surrogate_then_escape_u1x" {
12661266 err(
12671267 \\["\uD800\u1x"]
12681268 );
12691269}
12701270
1271test "n_string_1_surrogate_then_escape_u" {
1271test "json.test.n_string_1_surrogate_then_escape_u" {
12721272 err(
12731273 \\["\uD800\u"]
12741274 );
12751275}
12761276
1277test "n_string_accentuated_char_no_quotes" {
1277test "json.test.n_string_accentuated_char_no_quotes" {
12781278 err(
12791279 \\[é]
12801280 );
12811281}
12821282
1283test "n_string_backslash_00" {
1283test "json.test.n_string_backslash_00" {
12841284 err("[\"\x00\"]");
12851285}
12861286
1287test "n_string_escaped_backslash_bad" {
1287test "json.test.n_string_escaped_backslash_bad" {
12881288 err(
12891289 \\["\\\"]
12901290 );
12911291}
12921292
1293test "n_string_escaped_ctrl_char_tab" {
1293test "json.test.n_string_escaped_ctrl_char_tab" {
12941294 err("\x5b\x22\x5c\x09\x22\x5d");
12951295}
12961296
1297test "n_string_escaped_emoji" {
1297test "json.test.n_string_escaped_emoji" {
12981298 err("[\"\x5c\xc3\xb0\xc2\x9f\xc2\x8c\xc2\x80\"]");
12991299}
13001300
1301test "n_string_escape_x" {
1301test "json.test.n_string_escape_x" {
13021302 err(
13031303 \\["\x00"]
13041304 );
13051305}
13061306
1307test "n_string_incomplete_escaped_character" {
1307test "json.test.n_string_incomplete_escaped_character" {
13081308 err(
13091309 \\["\u00A"]
13101310 );
13111311}
13121312
1313test "n_string_incomplete_escape" {
1313test "json.test.n_string_incomplete_escape" {
13141314 err(
13151315 \\["\"]
13161316 );
13171317}
13181318
1319test "n_string_incomplete_surrogate_escape_invalid" {
1319test "json.test.n_string_incomplete_surrogate_escape_invalid" {
13201320 err(
13211321 \\["\uD800\uD800\x"]
13221322 );
13231323}
13241324
1325test "n_string_incomplete_surrogate" {
1325test "json.test.n_string_incomplete_surrogate" {
13261326 err(
13271327 \\["\uD834\uDd"]
13281328 );
13291329}
13301330
1331test "n_string_invalid_backslash_esc" {
1331test "json.test.n_string_invalid_backslash_esc" {
13321332 err(
13331333 \\["\a"]
13341334 );
13351335}
13361336
1337test "n_string_invalid_unicode_escape" {
1337test "json.test.n_string_invalid_unicode_escape" {
13381338 err(
13391339 \\["\uqqqq"]
13401340 );
13411341}
13421342
1343test "n_string_invalid_utf8_after_escape" {
1343test "json.test.n_string_invalid_utf8_after_escape" {
13441344 err("[\"\\\x75\xc3\xa5\"]");
13451345}
13461346
1347test "n_string_invalid-utf-8-in-escape" {
1347test "json.test.n_string_invalid-utf-8-in-escape" {
13481348 err(
13491349 \\["\uå"]
13501350 );
13511351}
13521352
1353test "n_string_leading_uescaped_thinspace" {
1353test "json.test.n_string_leading_uescaped_thinspace" {
13541354 err(
13551355 \\[\u0020"asd"]
13561356 );
13571357}
13581358
1359test "n_string_no_quotes_with_bad_escape" {
1359test "json.test.n_string_no_quotes_with_bad_escape" {
13601360 err(
13611361 \\[\n]
13621362 );
13631363}
13641364
1365test "n_string_single_doublequote" {
1365test "json.test.n_string_single_doublequote" {
13661366 err(
13671367 \\"
13681368 );
13691369}
13701370
1371test "n_string_single_quote" {
1371test "json.test.n_string_single_quote" {
13721372 err(
13731373 \\['single quote']
13741374 );
13751375}
13761376
1377test "n_string_single_string_no_double_quotes" {
1377test "json.test.n_string_single_string_no_double_quotes" {
13781378 err(
13791379 \\abc
13801380 );
13811381}
13821382
1383test "n_string_start_escape_unclosed" {
1383test "json.test.n_string_start_escape_unclosed" {
13841384 err(
13851385 \\["\
13861386 );
13871387}
13881388
1389test "n_string_unescaped_crtl_char" {
1389test "json.test.n_string_unescaped_crtl_char" {
13901390 err("[\"a\x00a\"]");
13911391}
13921392
1393test "n_string_unescaped_newline" {
1393test "json.test.n_string_unescaped_newline" {
13941394 err(
13951395 \\["new
13961396 \\line"]
13971397 );
13981398}
13991399
1400test "n_string_unescaped_tab" {
1400test "json.test.n_string_unescaped_tab" {
14011401 err("[\"\t\"]");
14021402}
14031403
1404test "n_string_unicode_CapitalU" {
1404test "json.test.n_string_unicode_CapitalU" {
14051405 err(
14061406 \\"\UA66D"
14071407 );
14081408}
14091409
1410test "n_string_with_trailing_garbage" {
1410test "json.test.n_string_with_trailing_garbage" {
14111411 err(
14121412 \\""x
14131413 );
14141414}
14151415
1416test "n_structure_100000_opening_arrays" {
1416test "json.test.n_structure_100000_opening_arrays" {
14171417 err("[" ** 100000);
14181418}
14191419
1420test "n_structure_angle_bracket_." {
1420test "json.test.n_structure_angle_bracket_." {
14211421 err(
14221422 \\<.>
14231423 );
14241424}
14251425
1426test "n_structure_angle_bracket_null" {
1426test "json.test.n_structure_angle_bracket_null" {
14271427 err(
14281428 \\[<null>]
14291429 );
14301430}
14311431
1432test "n_structure_array_trailing_garbage" {
1432test "json.test.n_structure_array_trailing_garbage" {
14331433 err(
14341434 \\[1]x
14351435 );
14361436}
14371437
1438test "n_structure_array_with_extra_array_close" {
1438test "json.test.n_structure_array_with_extra_array_close" {
14391439 err(
14401440 \\[1]]
14411441 );
14421442}
14431443
1444test "n_structure_array_with_unclosed_string" {
1444test "json.test.n_structure_array_with_unclosed_string" {
14451445 err(
14461446 \\["asd]
14471447 );
14481448}
14491449
1450test "n_structure_ascii-unicode-identifier" {
1450test "json.test.n_structure_ascii-unicode-identifier" {
14511451 err(
14521452 \\aå
14531453 );
14541454}
14551455
1456test "n_structure_capitalized_True" {
1456test "json.test.n_structure_capitalized_True" {
14571457 err(
14581458 \\[True]
14591459 );
14601460}
14611461
1462test "n_structure_close_unopened_array" {
1462test "json.test.n_structure_close_unopened_array" {
14631463 err(
14641464 \\1]
14651465 );
14661466}
14671467
1468test "n_structure_comma_instead_of_closing_brace" {
1468test "json.test.n_structure_comma_instead_of_closing_brace" {
14691469 err(
14701470 \\{"x": true,
14711471 );
14721472}
14731473
1474test "n_structure_double_array" {
1474test "json.test.n_structure_double_array" {
14751475 err(
14761476 \\[][]
14771477 );
14781478}
14791479
1480test "n_structure_end_array" {
1480test "json.test.n_structure_end_array" {
14811481 err(
14821482 \\]
14831483 );
14841484}
14851485
1486test "n_structure_incomplete_UTF8_BOM" {
1486test "json.test.n_structure_incomplete_UTF8_BOM" {
14871487 err(
14881488 \\ï»{}
14891489 );
14901490}
14911491
1492test "n_structure_lone-invalid-utf-8" {
1492test "json.test.n_structure_lone-invalid-utf-8" {
14931493 err(
14941494 \\å
14951495 );
14961496}
14971497
1498test "n_structure_lone-open-bracket" {
1498test "json.test.n_structure_lone-open-bracket" {
14991499 err(
15001500 \\[
15011501 );
15021502}
15031503
1504test "n_structure_no_data" {
1504test "json.test.n_structure_no_data" {
15051505 err(
15061506 \\
15071507 );
15081508}
15091509
1510test "n_structure_null-byte-outside-string" {
1510test "json.test.n_structure_null-byte-outside-string" {
15111511 err("[\x00]");
15121512}
15131513
1514test "n_structure_number_with_trailing_garbage" {
1514test "json.test.n_structure_number_with_trailing_garbage" {
15151515 err(
15161516 \\2@
15171517 );
15181518}
15191519
1520test "n_structure_object_followed_by_closing_object" {
1520test "json.test.n_structure_object_followed_by_closing_object" {
15211521 err(
15221522 \\{}}
15231523 );
15241524}
15251525
1526test "n_structure_object_unclosed_no_value" {
1526test "json.test.n_structure_object_unclosed_no_value" {
15271527 err(
15281528 \\{"":
15291529 );
15301530}
15311531
1532test "n_structure_object_with_comment" {
1532test "json.test.n_structure_object_with_comment" {
15331533 err(
15341534 \\{"a":/*comment*/"b"}
15351535 );
15361536}
15371537
1538test "n_structure_object_with_trailing_garbage" {
1538test "json.test.n_structure_object_with_trailing_garbage" {
15391539 err(
15401540 \\{"a": true} "x"
15411541 );
15421542}
15431543
1544test "n_structure_open_array_apostrophe" {
1544test "json.test.n_structure_open_array_apostrophe" {
15451545 err(
15461546 \\['
15471547 );
15481548}
15491549
1550test "n_structure_open_array_comma" {
1550test "json.test.n_structure_open_array_comma" {
15511551 err(
15521552 \\[,
15531553 );
15541554}
15551555
1556test "n_structure_open_array_object" {
1556test "json.test.n_structure_open_array_object" {
15571557 err("[{\"\":" ** 50000);
15581558}
15591559
1560test "n_structure_open_array_open_object" {
1560test "json.test.n_structure_open_array_open_object" {
15611561 err(
15621562 \\[{
15631563 );
15641564}
15651565
1566test "n_structure_open_array_open_string" {
1566test "json.test.n_structure_open_array_open_string" {
15671567 err(
15681568 \\["a
15691569 );
15701570}
15711571
1572test "n_structure_open_array_string" {
1572test "json.test.n_structure_open_array_string" {
15731573 err(
15741574 \\["a"
15751575 );
15761576}
15771577
1578test "n_structure_open_object_close_array" {
1578test "json.test.n_structure_open_object_close_array" {
15791579 err(
15801580 \\{]
15811581 );
15821582}
15831583
1584test "n_structure_open_object_comma" {
1584test "json.test.n_structure_open_object_comma" {
15851585 err(
15861586 \\{,
15871587 );
15881588}
15891589
1590test "n_structure_open_object" {
1590test "json.test.n_structure_open_object" {
15911591 err(
15921592 \\{
15931593 );
15941594}
15951595
1596test "n_structure_open_object_open_array" {
1596test "json.test.n_structure_open_object_open_array" {
15971597 err(
15981598 \\{[
15991599 );
16001600}
16011601
1602test "n_structure_open_object_open_string" {
1602test "json.test.n_structure_open_object_open_string" {
16031603 err(
16041604 \\{"a
16051605 );
16061606}
16071607
1608test "n_structure_open_object_string_with_apostrophes" {
1608test "json.test.n_structure_open_object_string_with_apostrophes" {
16091609 err(
16101610 \\{'a'
16111611 );
16121612}
16131613
1614test "n_structure_open_open" {
1614test "json.test.n_structure_open_open" {
16151615 err(
16161616 \\["\{["\{["\{["\{
16171617 );
16181618}
16191619
1620test "n_structure_single_eacute" {
1620test "json.test.n_structure_single_eacute" {
16211621 err(
16221622 \\é
16231623 );
16241624}
16251625
1626test "n_structure_single_star" {
1626test "json.test.n_structure_single_star" {
16271627 err(
16281628 \\*
16291629 );
16301630}
16311631
1632test "n_structure_trailing_#" {
1632test "json.test.n_structure_trailing_#" {
16331633 err(
16341634 \\{"a":"b"}#{}
16351635 );
16361636}
16371637
1638test "n_structure_U+2060_word_joined" {
1638test "json.test.n_structure_U+2060_word_joined" {
16391639 err(
16401640 \\[⁠]
16411641 );
16421642}
16431643
1644test "n_structure_uescaped_LF_before_string" {
1644test "json.test.n_structure_uescaped_LF_before_string" {
16451645 err(
16461646 \\[\u000A""]
16471647 );
16481648}
16491649
1650test "n_structure_unclosed_array" {
1650test "json.test.n_structure_unclosed_array" {
16511651 err(
16521652 \\[1
16531653 );
16541654}
16551655
1656test "n_structure_unclosed_array_partial_null" {
1656test "json.test.n_structure_unclosed_array_partial_null" {
16571657 err(
16581658 \\[ false, nul
16591659 );
16601660}
16611661
1662test "n_structure_unclosed_array_unfinished_false" {
1662test "json.test.n_structure_unclosed_array_unfinished_false" {
16631663 err(
16641664 \\[ true, fals
16651665 );
16661666}
16671667
1668test "n_structure_unclosed_array_unfinished_true" {
1668test "json.test.n_structure_unclosed_array_unfinished_true" {
16691669 err(
16701670 \\[ false, tru
16711671 );
16721672}
16731673
1674test "n_structure_unclosed_object" {
1674test "json.test.n_structure_unclosed_object" {
16751675 err(
16761676 \\{"asd":"asd"
16771677 );
16781678}
16791679
1680test "n_structure_unicode-identifier" {
1680test "json.test.n_structure_unicode-identifier" {
16811681 err(
16821682 \\Ã¥
16831683 );
16841684}
16851685
1686test "n_structure_UTF8_BOM_no_data" {
1686test "json.test.n_structure_UTF8_BOM_no_data" {
16871687 err(
16881688 \\
16891689 );
16901690}
16911691
1692test "n_structure_whitespace_formfeed" {
1692test "json.test.n_structure_whitespace_formfeed" {
16931693 err("[\x0c]");
16941694}
16951695
1696test "n_structure_whitespace_U+2060_word_joiner" {
1696test "json.test.n_structure_whitespace_U+2060_word_joiner" {
16971697 err(
16981698 \\[⁠]
16991699 );
......@@ -1701,203 +1701,203 @@ test "n_structure_whitespace_U+2060_word_joiner" {
17011701
17021702////////////////////////////////////////////////////////////////////////////////////////////////////
17031703
1704test "i_number_double_huge_neg_exp" {
1704test "json.test.i_number_double_huge_neg_exp" {
17051705 any(
17061706 \\[123.456e-789]
17071707 );
17081708}
17091709
1710test "i_number_huge_exp" {
1710test "json.test.i_number_huge_exp" {
17111711 any(
17121712 \\[0.4e00669999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999969999999006]
17131713 );
17141714}
17151715
1716test "i_number_neg_int_huge_exp" {
1716test "json.test.i_number_neg_int_huge_exp" {
17171717 any(
17181718 \\[-1e+9999]
17191719 );
17201720}
17211721
1722test "i_number_pos_double_huge_exp" {
1722test "json.test.i_number_pos_double_huge_exp" {
17231723 any(
17241724 \\[1.5e+9999]
17251725 );
17261726}
17271727
1728test "i_number_real_neg_overflow" {
1728test "json.test.i_number_real_neg_overflow" {
17291729 any(
17301730 \\[-123123e100000]
17311731 );
17321732}
17331733
1734test "i_number_real_pos_overflow" {
1734test "json.test.i_number_real_pos_overflow" {
17351735 any(
17361736 \\[123123e100000]
17371737 );
17381738}
17391739
1740test "i_number_real_underflow" {
1740test "json.test.i_number_real_underflow" {
17411741 any(
17421742 \\[123e-10000000]
17431743 );
17441744}
17451745
1746test "i_number_too_big_neg_int" {
1746test "json.test.i_number_too_big_neg_int" {
17471747 any(
17481748 \\[-123123123123123123123123123123]
17491749 );
17501750}
17511751
1752test "i_number_too_big_pos_int" {
1752test "json.test.i_number_too_big_pos_int" {
17531753 any(
17541754 \\[100000000000000000000]
17551755 );
17561756}
17571757
1758test "i_number_very_big_negative_int" {
1758test "json.test.i_number_very_big_negative_int" {
17591759 any(
17601760 \\[-237462374673276894279832749832423479823246327846]
17611761 );
17621762}
17631763
1764test "i_object_key_lone_2nd_surrogate" {
1764test "json.test.i_object_key_lone_2nd_surrogate" {
17651765 any(
17661766 \\{"\uDFAA":0}
17671767 );
17681768}
17691769
1770test "i_string_1st_surrogate_but_2nd_missing" {
1770test "json.test.i_string_1st_surrogate_but_2nd_missing" {
17711771 any(
17721772 \\["\uDADA"]
17731773 );
17741774}
17751775
1776test "i_string_1st_valid_surrogate_2nd_invalid" {
1776test "json.test.i_string_1st_valid_surrogate_2nd_invalid" {
17771777 any(
17781778 \\["\uD888\u1234"]
17791779 );
17801780}
17811781
1782test "i_string_incomplete_surrogate_and_escape_valid" {
1782test "json.test.i_string_incomplete_surrogate_and_escape_valid" {
17831783 any(
17841784 \\["\uD800\n"]
17851785 );
17861786}
17871787
1788test "i_string_incomplete_surrogate_pair" {
1788test "json.test.i_string_incomplete_surrogate_pair" {
17891789 any(
17901790 \\["\uDd1ea"]
17911791 );
17921792}
17931793
1794test "i_string_incomplete_surrogates_escape_valid" {
1794test "json.test.i_string_incomplete_surrogates_escape_valid" {
17951795 any(
17961796 \\["\uD800\uD800\n"]
17971797 );
17981798}
17991799
1800test "i_string_invalid_lonely_surrogate" {
1800test "json.test.i_string_invalid_lonely_surrogate" {
18011801 any(
18021802 \\["\ud800"]
18031803 );
18041804}
18051805
1806test "i_string_invalid_surrogate" {
1806test "json.test.i_string_invalid_surrogate" {
18071807 any(
18081808 \\["\ud800abc"]
18091809 );
18101810}
18111811
1812test "i_string_invalid_utf-8" {
1812test "json.test.i_string_invalid_utf-8" {
18131813 any(
18141814 \\["ÿ"]
18151815 );
18161816}
18171817
1818test "i_string_inverted_surrogates_U+1D11E" {
1818test "json.test.i_string_inverted_surrogates_U+1D11E" {
18191819 any(
18201820 \\["\uDd1e\uD834"]
18211821 );
18221822}
18231823
1824test "i_string_iso_latin_1" {
1824test "json.test.i_string_iso_latin_1" {
18251825 any(
18261826 \\["é"]
18271827 );
18281828}
18291829
1830test "i_string_lone_second_surrogate" {
1830test "json.test.i_string_lone_second_surrogate" {
18311831 any(
18321832 \\["\uDFAA"]
18331833 );
18341834}
18351835
1836test "i_string_lone_utf8_continuation_byte" {
1836test "json.test.i_string_lone_utf8_continuation_byte" {
18371837 any(
18381838 \\[""]
18391839 );
18401840}
18411841
1842test "i_string_not_in_unicode_range" {
1842test "json.test.i_string_not_in_unicode_range" {
18431843 any(
18441844 \\["ô¿¿¿"]
18451845 );
18461846}
18471847
1848test "i_string_overlong_sequence_2_bytes" {
1848test "json.test.i_string_overlong_sequence_2_bytes" {
18491849 any(
18501850 \\["À¯"]
18511851 );
18521852}
18531853
1854test "i_string_overlong_sequence_6_bytes" {
1854test "json.test.i_string_overlong_sequence_6_bytes" {
18551855 any(
18561856 \\["üƒ¿¿¿¿"]
18571857 );
18581858}
18591859
1860test "i_string_overlong_sequence_6_bytes_null" {
1860test "json.test.i_string_overlong_sequence_6_bytes_null" {
18611861 any(
18621862 \\["ü€€€€€"]
18631863 );
18641864}
18651865
1866test "i_string_truncated-utf-8" {
1866test "json.test.i_string_truncated-utf-8" {
18671867 any(
18681868 \\["àÿ"]
18691869 );
18701870}
18711871
1872test "i_string_utf16BE_no_BOM" {
1872test "json.test.i_string_utf16BE_no_BOM" {
18731873 any("\x00\x5b\x00\x22\x00\xc3\xa9\x00\x22\x00\x5d");
18741874}
18751875
1876test "i_string_utf16LE_no_BOM" {
1876test "json.test.i_string_utf16LE_no_BOM" {
18771877 any("\x5b\x00\x22\x00\xc3\xa9\x00\x22\x00\x5d\x00");
18781878}
18791879
1880test "i_string_UTF-16LE_with_BOM" {
1880test "json.test.i_string_UTF-16LE_with_BOM" {
18811881 any("\xc3\xbf\xc3\xbe\x5b\x00\x22\x00\xc3\xa9\x00\x22\x00\x5d\x00");
18821882}
18831883
1884test "i_string_UTF-8_invalid_sequence" {
1884test "json.test.i_string_UTF-8_invalid_sequence" {
18851885 any(
18861886 \\["日шú"]
18871887 );
18881888}
18891889
1890test "i_string_UTF8_surrogate_U+D800" {
1890test "json.test.i_string_UTF8_surrogate_U+D800" {
18911891 any(
18921892 \\["í €"]
18931893 );
18941894}
18951895
1896test "i_structure_500_nested_arrays" {
1896test "json.test.i_structure_500_nested_arrays" {
18971897 any(("[" ** 500) ++ ("]" ** 500));
18981898}
18991899
1900test "i_structure_UTF-8_BOM_empty_object" {
1900test "json.test.i_structure_UTF-8_BOM_empty_object" {
19011901 any(
19021902 \\{}
19031903 );
std/math/index.zig+63
......@@ -365,6 +365,69 @@ pub fn Log2Int(comptime T: type) type {
365365 return @IntType(false, count);
366366}
367367
368pub fn IntFittingRange(comptime from: comptime_int, comptime to: comptime_int) type {
369 assert(from <= to);
370 if (from == 0 and to == 0) {
371 return u0;
372 }
373 const is_signed = from < 0;
374 const largest_positive_integer = max(if (from<0) (-from)-1 else from, to); // two's complement
375 const base = log2(largest_positive_integer);
376 const upper = (1 << base) - 1;
377 var magnitude_bits = if (upper >= largest_positive_integer) base else base + 1;
378 if (is_signed) {
379 magnitude_bits += 1;
380 }
381 return @IntType(is_signed, magnitude_bits);
382}
383
384test "math.IntFittingRange" {
385 assert(IntFittingRange(0, 0) == u0);
386 assert(IntFittingRange(0, 1) == u1);
387 assert(IntFittingRange(0, 2) == u2);
388 assert(IntFittingRange(0, 3) == u2);
389 assert(IntFittingRange(0, 4) == u3);
390 assert(IntFittingRange(0, 7) == u3);
391 assert(IntFittingRange(0, 8) == u4);
392 assert(IntFittingRange(0, 9) == u4);
393 assert(IntFittingRange(0, 15) == u4);
394 assert(IntFittingRange(0, 16) == u5);
395 assert(IntFittingRange(0, 17) == u5);
396 assert(IntFittingRange(0, 4095) == u12);
397 assert(IntFittingRange(2000, 4095) == u12);
398 assert(IntFittingRange(0, 4096) == u13);
399 assert(IntFittingRange(2000, 4096) == u13);
400 assert(IntFittingRange(0, 4097) == u13);
401 assert(IntFittingRange(2000, 4097) == u13);
402 assert(IntFittingRange(0, 123456789123456798123456789) == u87);
403 assert(IntFittingRange(0, 123456789123456798123456789123456789123456798123456789) == u177);
404
405 assert(IntFittingRange(-1, -1) == i1);
406 assert(IntFittingRange(-1, 0) == i1);
407 assert(IntFittingRange(-1, 1) == i2);
408 assert(IntFittingRange(-2, -2) == i2);
409 assert(IntFittingRange(-2, -1) == i2);
410 assert(IntFittingRange(-2, 0) == i2);
411 assert(IntFittingRange(-2, 1) == i2);
412 assert(IntFittingRange(-2, 2) == i3);
413 assert(IntFittingRange(-1, 2) == i3);
414 assert(IntFittingRange(-1, 3) == i3);
415 assert(IntFittingRange(-1, 4) == i4);
416 assert(IntFittingRange(-1, 7) == i4);
417 assert(IntFittingRange(-1, 8) == i5);
418 assert(IntFittingRange(-1, 9) == i5);
419 assert(IntFittingRange(-1, 15) == i5);
420 assert(IntFittingRange(-1, 16) == i6);
421 assert(IntFittingRange(-1, 17) == i6);
422 assert(IntFittingRange(-1, 4095) == i13);
423 assert(IntFittingRange(-4096, 4095) == i13);
424 assert(IntFittingRange(-1, 4096) == i14);
425 assert(IntFittingRange(-4097, 4095) == i14);
426 assert(IntFittingRange(-1, 4097) == i14);
427 assert(IntFittingRange(-1, 123456789123456798123456789) == i88);
428 assert(IntFittingRange(-1, 123456789123456798123456789123456789123456798123456789) == i178);
429}
430
368431test "math overflow functions" {
369432 testOverflow();
370433 comptime testOverflow();
std/mem.zig+1-3
......@@ -546,9 +546,7 @@ pub fn writeIntLE(comptime T: type, buf: *[@sizeOf(T)]u8, value: T) void {
546546 buf[0] = bits;
547547 return;
548548 }
549 // FIXME: this should just be for (buf).
550 // See https://github.com/ziglang/zig/issues/1663
551 for (buf.*) |*b| {
549 for (buf) |*b| {
552550 b.* = @truncate(u8, bits);
553551 bits >>= 8;
554552 }
std/meta/index.zig+19
......@@ -76,6 +76,25 @@ test "std.meta.tagName" {
7676 debug.assert(mem.eql(u8, tagName(u2b), "D"));
7777}
7878
79pub fn stringToEnum(comptime T: type, str: []const u8) ?T {
80 inline for (@typeInfo(T).Enum.fields) |enumField| {
81 if (std.mem.eql(u8, str, enumField.name)) {
82 return @field(T, enumField.name);
83 }
84 }
85 return null;
86}
87
88test "std.meta.stringToEnum" {
89 const E1 = enum {
90 A,
91 B,
92 };
93 debug.assert(E1.A == stringToEnum(E1, "A").?);
94 debug.assert(E1.B == stringToEnum(E1, "B").?);
95 debug.assert(null == stringToEnum(E1, "C"));
96}
97
7998pub fn bitCount(comptime T: type) u32 {
8099 return switch (@typeInfo(T)) {
81100 TypeId.Int => |info| info.bits,
std/os/linux/index.zig+56-47
......@@ -703,7 +703,7 @@ pub fn dup2(old: i32, new: i32) usize {
703703}
704704
705705pub fn dup3(old: i32, new: i32, flags: u32) usize {
706 return syscall3(SYS_dup3, @intCast(usize, old), @intCast(usize, new), flags);
706 return syscall3(SYS_dup3, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)), flags);
707707}
708708
709709// TODO https://github.com/ziglang/zig/issues/265
......@@ -747,7 +747,7 @@ pub fn getcwd(buf: [*]u8, size: usize) usize {
747747}
748748
749749pub fn getdents64(fd: i32, dirp: [*]u8, count: usize) usize {
750 return syscall3(SYS_getdents64, @intCast(usize, fd), @ptrToInt(dirp), count);
750 return syscall3(SYS_getdents64, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count);
751751}
752752
753753pub fn inotify_init1(flags: u32) usize {
......@@ -755,16 +755,16 @@ pub fn inotify_init1(flags: u32) usize {
755755}
756756
757757pub fn inotify_add_watch(fd: i32, pathname: [*]const u8, mask: u32) usize {
758 return syscall3(SYS_inotify_add_watch, @intCast(usize, fd), @ptrToInt(pathname), mask);
758 return syscall3(SYS_inotify_add_watch, @bitCast(usize, isize(fd)), @ptrToInt(pathname), mask);
759759}
760760
761761pub fn inotify_rm_watch(fd: i32, wd: i32) usize {
762 return syscall2(SYS_inotify_rm_watch, @intCast(usize, fd), @intCast(usize, wd));
762 return syscall2(SYS_inotify_rm_watch, @bitCast(usize, isize(fd)), @bitCast(usize, isize(wd)));
763763}
764764
765765pub fn isatty(fd: i32) bool {
766766 var wsz: winsize = undefined;
767 return syscall3(SYS_ioctl, @intCast(usize, fd), TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
767 return syscall3(SYS_ioctl, @bitCast(usize, isize(fd)), TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
768768}
769769
770770// TODO https://github.com/ziglang/zig/issues/265
......@@ -774,7 +774,7 @@ pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usiz
774774
775775// TODO https://github.com/ziglang/zig/issues/265
776776pub fn readlinkat(dirfd: i32, noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize {
777 return syscall4(SYS_readlinkat, @intCast(usize, dirfd), @ptrToInt(path), @ptrToInt(buf_ptr), buf_len);
777 return syscall4(SYS_readlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(buf_ptr), buf_len);
778778}
779779
780780// TODO https://github.com/ziglang/zig/issues/265
......@@ -784,7 +784,7 @@ pub fn mkdir(path: [*]const u8, mode: u32) usize {
784784
785785// TODO https://github.com/ziglang/zig/issues/265
786786pub fn mkdirat(dirfd: i32, path: [*]const u8, mode: u32) usize {
787 return syscall3(SYS_mkdirat, @intCast(usize, dirfd), @ptrToInt(path), mode);
787 return syscall3(SYS_mkdirat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode);
788788}
789789
790790// TODO https://github.com/ziglang/zig/issues/265
......@@ -803,7 +803,7 @@ pub fn umount2(special: [*]const u8, flags: u32) usize {
803803}
804804
805805pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
806 return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @intCast(usize, fd), @bitCast(usize, offset));
806 return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset));
807807}
808808
809809pub fn munmap(address: usize, length: usize) usize {
......@@ -811,23 +811,23 @@ pub fn munmap(address: usize, length: usize) usize {
811811}
812812
813813pub fn read(fd: i32, buf: [*]u8, count: usize) usize {
814 return syscall3(SYS_read, @intCast(usize, fd), @ptrToInt(buf), count);
814 return syscall3(SYS_read, @bitCast(usize, isize(fd)), @ptrToInt(buf), count);
815815}
816816
817817pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: u64) usize {
818 return syscall4(SYS_preadv, @intCast(usize, fd), @ptrToInt(iov), count, offset);
818 return syscall4(SYS_preadv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset);
819819}
820820
821821pub fn readv(fd: i32, iov: [*]const iovec, count: usize) usize {
822 return syscall3(SYS_readv, @intCast(usize, fd), @ptrToInt(iov), count);
822 return syscall3(SYS_readv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count);
823823}
824824
825825pub fn writev(fd: i32, iov: [*]const iovec_const, count: usize) usize {
826 return syscall3(SYS_writev, @intCast(usize, fd), @ptrToInt(iov), count);
826 return syscall3(SYS_writev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count);
827827}
828828
829829pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) usize {
830 return syscall4(SYS_pwritev, @intCast(usize, fd), @ptrToInt(iov), count, offset);
830 return syscall4(SYS_pwritev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset);
831831}
832832
833833// TODO https://github.com/ziglang/zig/issues/265
......@@ -842,12 +842,12 @@ pub fn symlink(existing: [*]const u8, new: [*]const u8) usize {
842842
843843// TODO https://github.com/ziglang/zig/issues/265
844844pub fn symlinkat(existing: [*]const u8, newfd: i32, newpath: [*]const u8) usize {
845 return syscall3(SYS_symlinkat, @ptrToInt(existing), @intCast(usize, newfd), @ptrToInt(newpath));
845 return syscall3(SYS_symlinkat, @ptrToInt(existing), @bitCast(usize, isize(newfd)), @ptrToInt(newpath));
846846}
847847
848848// TODO https://github.com/ziglang/zig/issues/265
849849pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize {
850 return syscall4(SYS_pread, @intCast(usize, fd), @ptrToInt(buf), count, offset);
850 return syscall4(SYS_pread, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset);
851851}
852852
853853// TODO https://github.com/ziglang/zig/issues/265
......@@ -856,7 +856,7 @@ pub fn access(path: [*]const u8, mode: u32) usize {
856856}
857857
858858pub fn faccessat(dirfd: i32, path: [*]const u8, mode: u32) usize {
859 return syscall3(SYS_faccessat, @intCast(usize, dirfd), @ptrToInt(path), mode);
859 return syscall3(SYS_faccessat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode);
860860}
861861
862862pub fn pipe(fd: *[2]i32) usize {
......@@ -868,11 +868,11 @@ pub fn pipe2(fd: *[2]i32, flags: u32) usize {
868868}
869869
870870pub fn write(fd: i32, buf: [*]const u8, count: usize) usize {
871 return syscall3(SYS_write, @intCast(usize, fd), @ptrToInt(buf), count);
871 return syscall3(SYS_write, @bitCast(usize, isize(fd)), @ptrToInt(buf), count);
872872}
873873
874874pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize {
875 return syscall4(SYS_pwrite, @intCast(usize, fd), @ptrToInt(buf), count, offset);
875 return syscall4(SYS_pwrite, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset);
876876}
877877
878878// TODO https://github.com/ziglang/zig/issues/265
......@@ -882,7 +882,7 @@ pub fn rename(old: [*]const u8, new: [*]const u8) usize {
882882
883883// TODO https://github.com/ziglang/zig/issues/265
884884pub fn renameat2(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8, flags: u32) usize {
885 return syscall5(SYS_renameat2, @intCast(usize, oldfd), @ptrToInt(oldpath), @intCast(usize, newfd), @ptrToInt(newpath), flags);
885 return syscall5(SYS_renameat2, @bitCast(usize, isize(oldfd)), @ptrToInt(oldpath), @bitCast(usize, isize(newfd)), @ptrToInt(newpath), flags);
886886}
887887
888888// TODO https://github.com/ziglang/zig/issues/265
......@@ -897,7 +897,8 @@ pub fn create(path: [*]const u8, perm: usize) usize {
897897
898898// TODO https://github.com/ziglang/zig/issues/265
899899pub fn openat(dirfd: i32, path: [*]const u8, flags: u32, mode: usize) usize {
900 return syscall4(SYS_openat, @intCast(usize, dirfd), @ptrToInt(path), flags, mode);
900 // dirfd could be negative, for example AT_FDCWD is -100
901 return syscall4(SYS_openat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags, mode);
901902}
902903
903904/// See also `clone` (from the arch-specific include)
......@@ -911,11 +912,11 @@ pub fn clone2(flags: u32, child_stack_ptr: usize) usize {
911912}
912913
913914pub fn close(fd: i32) usize {
914 return syscall1(SYS_close, @intCast(usize, fd));
915 return syscall1(SYS_close, @bitCast(usize, isize(fd)));
915916}
916917
917918pub fn lseek(fd: i32, offset: isize, ref_pos: usize) usize {
918 return syscall3(SYS_lseek, @intCast(usize, fd), @bitCast(usize, offset), ref_pos);
919 return syscall3(SYS_lseek, @bitCast(usize, isize(fd)), @bitCast(usize, offset), ref_pos);
919920}
920921
921922pub fn exit(status: i32) noreturn {
......@@ -933,7 +934,7 @@ pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize {
933934}
934935
935936pub fn kill(pid: i32, sig: i32) usize {
936 return syscall2(SYS_kill, @bitCast(usize, isize(pid)), @intCast(usize, sig));
937 return syscall2(SYS_kill, @bitCast(usize, isize(pid)), @bitCast(usize, isize(sig)));
937938}
938939
939940// TODO https://github.com/ziglang/zig/issues/265
......@@ -943,7 +944,7 @@ pub fn unlink(path: [*]const u8) usize {
943944
944945// TODO https://github.com/ziglang/zig/issues/265
945946pub fn unlinkat(dirfd: i32, path: [*]const u8, flags: u32) usize {
946 return syscall3(SYS_unlinkat, @intCast(usize, dirfd), @ptrToInt(path), flags);
947 return syscall3(SYS_unlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags);
947948}
948949
949950pub fn waitpid(pid: i32, status: *i32, options: i32) usize {
......@@ -1120,8 +1121,8 @@ pub const empty_sigset = []usize{0} ** sigset_t.len;
11201121pub fn raise(sig: i32) usize {
11211122 var set: sigset_t = undefined;
11221123 blockAppSignals(&set);
1123 const tid = @intCast(i32, syscall0(SYS_gettid));
1124 const ret = syscall2(SYS_tkill, @intCast(usize, tid), @intCast(usize, sig));
1124 const tid = syscall0(SYS_gettid);
1125 const ret = syscall2(SYS_tkill, tid, @bitCast(usize, isize(sig)));
11251126 restoreSignals(&set);
11261127 return ret;
11271128}
......@@ -1189,11 +1190,11 @@ pub const iovec_const = extern struct {
11891190};
11901191
11911192pub fn getsockname(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
1192 return syscall3(SYS_getsockname, @intCast(usize, fd), @ptrToInt(addr), @ptrToInt(len));
1193 return syscall3(SYS_getsockname, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len));
11931194}
11941195
11951196pub fn getpeername(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
1196 return syscall3(SYS_getpeername, @intCast(usize, fd), @ptrToInt(addr), @ptrToInt(len));
1197 return syscall3(SYS_getpeername, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len));
11971198}
11981199
11991200pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize {
......@@ -1201,47 +1202,47 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize {
12011202}
12021203
12031204pub fn setsockopt(fd: i32, level: u32, optname: u32, optval: [*]const u8, optlen: socklen_t) usize {
1204 return syscall5(SYS_setsockopt, @intCast(usize, fd), level, optname, @ptrToInt(optval), @intCast(usize, optlen));
1205 return syscall5(SYS_setsockopt, @bitCast(usize, isize(fd)), level, optname, @ptrToInt(optval), @intCast(usize, optlen));
12051206}
12061207
12071208pub fn getsockopt(fd: i32, level: u32, optname: u32, noalias optval: [*]u8, noalias optlen: *socklen_t) usize {
1208 return syscall5(SYS_getsockopt, @intCast(usize, fd), level, optname, @ptrToInt(optval), @ptrToInt(optlen));
1209 return syscall5(SYS_getsockopt, @bitCast(usize, isize(fd)), level, optname, @ptrToInt(optval), @ptrToInt(optlen));
12091210}
12101211
12111212pub fn sendmsg(fd: i32, msg: *const msghdr, flags: u32) usize {
1212 return syscall3(SYS_sendmsg, @intCast(usize, fd), @ptrToInt(msg), flags);
1213 return syscall3(SYS_sendmsg, @bitCast(usize, isize(fd)), @ptrToInt(msg), flags);
12131214}
12141215
12151216pub fn connect(fd: i32, addr: *const c_void, len: socklen_t) usize {
1216 return syscall3(SYS_connect, @intCast(usize, fd), @ptrToInt(addr), len);
1217 return syscall3(SYS_connect, @bitCast(usize, isize(fd)), @ptrToInt(addr), len);
12171218}
12181219
12191220pub fn recvmsg(fd: i32, msg: *msghdr, flags: u32) usize {
1220 return syscall3(SYS_recvmsg, @intCast(usize, fd), @ptrToInt(msg), flags);
1221 return syscall3(SYS_recvmsg, @bitCast(usize, isize(fd)), @ptrToInt(msg), flags);
12211222}
12221223
12231224pub fn recvfrom(fd: i32, noalias buf: [*]u8, len: usize, flags: u32, noalias addr: ?*sockaddr, noalias alen: ?*socklen_t) usize {
1224 return syscall6(SYS_recvfrom, @intCast(usize, fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen));
1225 return syscall6(SYS_recvfrom, @bitCast(usize, isize(fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen));
12251226}
12261227
12271228pub fn shutdown(fd: i32, how: i32) usize {
1228 return syscall2(SYS_shutdown, @intCast(usize, fd), @intCast(usize, how));
1229 return syscall2(SYS_shutdown, @bitCast(usize, isize(fd)), @bitCast(usize, isize(how)));
12291230}
12301231
12311232pub fn bind(fd: i32, addr: *const sockaddr, len: socklen_t) usize {
1232 return syscall3(SYS_bind, @intCast(usize, fd), @ptrToInt(addr), @intCast(usize, len));
1233 return syscall3(SYS_bind, @bitCast(usize, isize(fd)), @ptrToInt(addr), @intCast(usize, len));
12331234}
12341235
12351236pub fn listen(fd: i32, backlog: u32) usize {
1236 return syscall2(SYS_listen, @intCast(usize, fd), backlog);
1237 return syscall2(SYS_listen, @bitCast(usize, isize(fd)), backlog);
12371238}
12381239
12391240pub fn sendto(fd: i32, buf: [*]const u8, len: usize, flags: u32, addr: ?*const sockaddr, alen: socklen_t) usize {
1240 return syscall6(SYS_sendto, @intCast(usize, fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @intCast(usize, alen));
1241 return syscall6(SYS_sendto, @bitCast(usize, isize(fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @intCast(usize, alen));
12411242}
12421243
12431244pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize {
1244 return syscall4(SYS_socketpair, @intCast(usize, domain), @intCast(usize, socket_type), @intCast(usize, protocol), @ptrToInt(*fd[0]));
1245 return syscall4(SYS_socketpair, @intCast(usize, domain), @intCast(usize, socket_type), @intCast(usize, protocol), @ptrToInt(&fd[0]));
12451246}
12461247
12471248pub fn accept(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
......@@ -1249,11 +1250,11 @@ pub fn accept(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
12491250}
12501251
12511252pub fn accept4(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t, flags: u32) usize {
1252 return syscall4(SYS_accept4, @intCast(usize, fd), @ptrToInt(addr), @ptrToInt(len), flags);
1253 return syscall4(SYS_accept4, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len), flags);
12531254}
12541255
12551256pub fn fstat(fd: i32, stat_buf: *Stat) usize {
1256 return syscall2(SYS_fstat, @intCast(usize, fd), @ptrToInt(stat_buf));
1257 return syscall2(SYS_fstat, @bitCast(usize, isize(fd)), @ptrToInt(stat_buf));
12571258}
12581259
12591260// TODO https://github.com/ziglang/zig/issues/265
......@@ -1268,7 +1269,7 @@ pub fn lstat(pathname: [*]const u8, statbuf: *Stat) usize {
12681269
12691270// TODO https://github.com/ziglang/zig/issues/265
12701271pub fn fstatat(dirfd: i32, path: [*]const u8, stat_buf: *Stat, flags: u32) usize {
1271 return syscall4(SYS_fstatat, @intCast(usize, dirfd), @ptrToInt(path), @ptrToInt(stat_buf), flags);
1272 return syscall4(SYS_fstatat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(stat_buf), flags);
12721273}
12731274
12741275// TODO https://github.com/ziglang/zig/issues/265
......@@ -1355,7 +1356,7 @@ pub fn epoll_create1(flags: usize) usize {
13551356}
13561357
13571358pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: *epoll_event) usize {
1358 return syscall4(SYS_epoll_ctl, @intCast(usize, epoll_fd), @intCast(usize, op), @intCast(usize, fd), @ptrToInt(ev));
1359 return syscall4(SYS_epoll_ctl, @bitCast(usize, isize(epoll_fd)), @intCast(usize, op), @bitCast(usize, isize(fd)), @ptrToInt(ev));
13591360}
13601361
13611362pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32) usize {
......@@ -1363,7 +1364,15 @@ pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout
13631364}
13641365
13651366pub fn epoll_pwait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32, sigmask: ?*sigset_t) usize {
1366 return syscall6(SYS_epoll_pwait, @intCast(usize, epoll_fd), @ptrToInt(events), @intCast(usize, maxevents), @intCast(usize, timeout), @ptrToInt(sigmask), @sizeOf(sigset_t));
1367 return syscall6(
1368 SYS_epoll_pwait,
1369 @bitCast(usize, isize(epoll_fd)),
1370 @ptrToInt(events),
1371 @intCast(usize, maxevents),
1372 @bitCast(usize, isize(timeout)),
1373 @ptrToInt(sigmask),
1374 @sizeOf(sigset_t),
1375 );
13671376}
13681377
13691378pub fn eventfd(count: u32, flags: u32) usize {
......@@ -1371,7 +1380,7 @@ pub fn eventfd(count: u32, flags: u32) usize {
13711380}
13721381
13731382pub fn timerfd_create(clockid: i32, flags: u32) usize {
1374 return syscall2(SYS_timerfd_create, @intCast(usize, clockid), flags);
1383 return syscall2(SYS_timerfd_create, @bitCast(usize, isize(clockid)), flags);
13751384}
13761385
13771386pub const itimerspec = extern struct {
......@@ -1380,11 +1389,11 @@ pub const itimerspec = extern struct {
13801389};
13811390
13821391pub fn timerfd_gettime(fd: i32, curr_value: *itimerspec) usize {
1383 return syscall2(SYS_timerfd_gettime, @intCast(usize, fd), @ptrToInt(curr_value));
1392 return syscall2(SYS_timerfd_gettime, @bitCast(usize, isize(fd)), @ptrToInt(curr_value));
13841393}
13851394
13861395pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const itimerspec, old_value: ?*itimerspec) usize {
1387 return syscall4(SYS_timerfd_settime, @intCast(usize, fd), flags, @ptrToInt(new_value), @ptrToInt(old_value));
1396 return syscall4(SYS_timerfd_settime, @bitCast(usize, isize(fd)), flags, @ptrToInt(new_value), @ptrToInt(old_value));
13881397}
13891398
13901399pub const _LINUX_CAPABILITY_VERSION_1 = 0x19980330;
std/os/path.zig+1-1
......@@ -1183,7 +1183,7 @@ pub fn realC(out_buffer: *[os.MAX_PATH_BYTES]u8, pathname: [*]const u8) RealErro
11831183 const fd = try os.posixOpenC(pathname, posix.O_PATH | posix.O_NONBLOCK | posix.O_CLOEXEC, 0);
11841184 defer os.close(fd);
11851185
1186 var buf: ["/proc/self/fd/-2147483648".len]u8 = undefined;
1186 var buf: ["/proc/self/fd/-2147483648\x00".len]u8 = undefined;
11871187 const proc_path = fmt.bufPrint(buf[0..], "/proc/self/fd/{}\x00", fd) catch unreachable;
11881188
11891189 return os.readLinkC(out_buffer, proc_path.ptr);
std/os/zen.zig+11-11
......@@ -12,20 +12,20 @@ pub const Message = struct {
1212 args: [5]usize,
1313 payload: ?[]const u8,
1414
15 pub fn from(mailbox_id: *const MailboxId) Message {
15 pub fn from(mailbox_id: MailboxId) Message {
1616 return Message{
1717 .sender = MailboxId.Undefined,
18 .receiver = mailbox_id.*,
18 .receiver = mailbox_id,
1919 .code = undefined,
2020 .args = undefined,
2121 .payload = null,
2222 };
2323 }
2424
25 pub fn to(mailbox_id: *const MailboxId, msg_code: usize, args: ...) Message {
25 pub fn to(mailbox_id: MailboxId, msg_code: usize, args: ...) Message {
2626 var message = Message{
2727 .sender = MailboxId.This,
28 .receiver = mailbox_id.*,
28 .receiver = mailbox_id,
2929 .code = msg_code,
3030 .args = undefined,
3131 .payload = null,
......@@ -40,14 +40,14 @@ pub const Message = struct {
4040 return message;
4141 }
4242
43 pub fn as(self: *const Message, sender: *const MailboxId) Message {
44 var message = self.*;
45 message.sender = sender.*;
43 pub fn as(self: Message, sender: MailboxId) Message {
44 var message = self;
45 message.sender = sender;
4646 return message;
4747 }
4848
49 pub fn withPayload(self: *const Message, payload: []const u8) Message {
50 var message = self.*;
49 pub fn withPayload(self: Message, payload: []const u8) Message {
50 var message = self;
5151 message.payload = payload;
5252 return message;
5353 }
......@@ -93,7 +93,7 @@ pub fn read(fd: i32, buf: [*]u8, count: usize) usize {
9393 STDIN_FILENO => {
9494 var i: usize = 0;
9595 while (i < count) : (i += 1) {
96 send(Message.to(Server.Keyboard, 0));
96 send(&Message.to(Server.Keyboard, 0));
9797
9898 // FIXME: we should be certain that we are receiving from Keyboard.
9999 var message = Message.from(MailboxId.This);
......@@ -111,7 +111,7 @@ pub fn read(fd: i32, buf: [*]u8, count: usize) usize {
111111pub fn write(fd: i32, buf: [*]const u8, count: usize) usize {
112112 switch (fd) {
113113 STDOUT_FILENO, STDERR_FILENO => {
114 send(Message.to(Server.Terminal, 1).withPayload(buf[0..count]));
114 send(&Message.to(Server.Terminal, 1).withPayload(buf[0..count]));
115115 },
116116 else => unreachable,
117117 }
std/pdb.zig+1
......@@ -34,6 +34,7 @@ pub const DbiStreamHeader = packed struct {
3434};
3535
3636pub const SectionContribEntry = packed struct {
37 /// COFF Section index, 1-based
3738 Section: u16,
3839 Padding1: [2]u8,
3940 Offset: u32,
std/rand/index.zig+155-40
......@@ -57,6 +57,18 @@ pub const Random = struct {
5757 return @bitCast(T, unsigned_result);
5858 }
5959
60 /// Constant-time implementation off ::uintLessThan.
61 /// The results of this function may be biased.
62 pub fn uintLessThanBiased(r: *Random, comptime T: type, less_than: T) T {
63 comptime assert(T.is_signed == false);
64 comptime assert(T.bit_count <= 64); // TODO: workaround: LLVM ERROR: Unsupported library call operation!
65 assert(0 < less_than);
66 if (T.bit_count <= 32) {
67 return @intCast(T, limitRangeBiased(u32, r.int(u32), less_than));
68 } else {
69 return @intCast(T, limitRangeBiased(u64, r.int(u64), less_than));
70 }
71 }
6072 /// Returns an evenly distributed random unsigned integer `0 <= i < less_than`.
6173 /// This function assumes that the underlying ::fillFn produces evenly distributed values.
6274 /// Within this assumption, the runtime of this function is exponentially distributed.
......@@ -64,29 +76,53 @@ pub const Random = struct {
6476 /// the runtime of this function would technically be unbounded.
6577 /// However, if ::fillFn is backed by any evenly distributed pseudo random number generator,
6678 /// this function is guaranteed to return.
67 /// If you need deterministic runtime bounds, consider instead using `r.int(T) % less_than`,
68 /// which will usually be biased toward smaller values.
79 /// If you need deterministic runtime bounds, use `::uintLessThanBiased`.
6980 pub fn uintLessThan(r: *Random, comptime T: type, less_than: T) T {
70 assert(T.is_signed == false);
81 comptime assert(T.is_signed == false);
82 comptime assert(T.bit_count <= 64); // TODO: workaround: LLVM ERROR: Unsupported library call operation!
7183 assert(0 < less_than);
72
73 const last_group_size_minus_one: T = maxInt(T) % less_than;
74 if (last_group_size_minus_one == less_than - 1) {
75 // less_than is a power of two.
76 assert(math.floorPowerOfTwo(T, less_than) == less_than);
77 // There is no retry zone. The optimal retry_zone_start would be maxInt(T) + 1.
78 return r.int(T) % less_than;
79 }
80 const retry_zone_start = maxInt(T) - last_group_size_minus_one;
81
82 while (true) {
83 const rand_val = r.int(T);
84 if (rand_val < retry_zone_start) {
85 return rand_val % less_than;
84 // Small is typically u32
85 const Small = @IntType(false, @divTrunc(T.bit_count + 31, 32) * 32);
86 // Large is typically u64
87 const Large = @IntType(false, Small.bit_count * 2);
88
89 // adapted from:
90 // http://www.pcg-random.org/posts/bounded-rands.html
91 // "Lemire's (with an extra tweak from me)"
92 var x: Small = r.int(Small);
93 var m: Large = Large(x) * Large(less_than);
94 var l: Small = @truncate(Small, m);
95 if (l < less_than) {
96 // TODO: workaround for https://github.com/ziglang/zig/issues/1770
97 // should be:
98 // var t: Small = -%less_than;
99 var t: Small = @bitCast(Small, -%@bitCast(@IntType(true, Small.bit_count), Small(less_than)));
100
101 if (t >= less_than) {
102 t -= less_than;
103 if (t >= less_than) {
104 t %= less_than;
105 }
106 }
107 while (l < t) {
108 x = r.int(Small);
109 m = Large(x) * Large(less_than);
110 l = @truncate(Small, m);
86111 }
87112 }
113 return @intCast(T, m >> Small.bit_count);
88114 }
89115
116 /// Constant-time implementation off ::uintAtMost.
117 /// The results of this function may be biased.
118 pub fn uintAtMostBiased(r: *Random, comptime T: type, at_most: T) T {
119 assert(T.is_signed == false);
120 if (at_most == maxInt(T)) {
121 // have the full range
122 return r.int(T);
123 }
124 return r.uintLessThanBiased(T, at_most + 1);
125 }
90126 /// Returns an evenly distributed random unsigned integer `0 <= i <= at_most`.
91127 /// See ::uintLessThan, which this function uses in most cases,
92128 /// for commentary on the runtime of this function.
......@@ -99,6 +135,22 @@ pub const Random = struct {
99135 return r.uintLessThan(T, at_most + 1);
100136 }
101137
138 /// Constant-time implementation off ::intRangeLessThan.
139 /// The results of this function may be biased.
140 pub fn intRangeLessThanBiased(r: *Random, comptime T: type, at_least: T, less_than: T) T {
141 assert(at_least < less_than);
142 if (T.is_signed) {
143 // Two's complement makes this math pretty easy.
144 const UnsignedT = @IntType(false, T.bit_count);
145 const lo = @bitCast(UnsignedT, at_least);
146 const hi = @bitCast(UnsignedT, less_than);
147 const result = lo +% r.uintLessThanBiased(UnsignedT, hi -% lo);
148 return @bitCast(T, result);
149 } else {
150 // The signed implementation would work fine, but we can use stricter arithmetic operators here.
151 return at_least + r.uintLessThanBiased(T, less_than - at_least);
152 }
153 }
102154 /// Returns an evenly distributed random integer `at_least <= i < less_than`.
103155 /// See ::uintLessThan, which this function uses in most cases,
104156 /// for commentary on the runtime of this function.
......@@ -117,6 +169,22 @@ pub const Random = struct {
117169 }
118170 }
119171
172 /// Constant-time implementation off ::intRangeAtMostBiased.
173 /// The results of this function may be biased.
174 pub fn intRangeAtMostBiased(r: *Random, comptime T: type, at_least: T, at_most: T) T {
175 assert(at_least <= at_most);
176 if (T.is_signed) {
177 // Two's complement makes this math pretty easy.
178 const UnsignedT = @IntType(false, T.bit_count);
179 const lo = @bitCast(UnsignedT, at_least);
180 const hi = @bitCast(UnsignedT, at_most);
181 const result = lo +% r.uintAtMostBiased(UnsignedT, hi -% lo);
182 return @bitCast(T, result);
183 } else {
184 // The signed implementation would work fine, but we can use stricter arithmetic operators here.
185 return at_least + r.uintAtMostBiased(T, at_most - at_least);
186 }
187 }
120188 /// Returns an evenly distributed random integer `at_least <= i <= at_most`.
121189 /// See ::uintLessThan, which this function uses in most cases,
122190 /// for commentary on the runtime of this function.
......@@ -135,15 +203,11 @@ pub const Random = struct {
135203 }
136204 }
137205
138 /// Return a random integer/boolean type.
139206 /// TODO: deprecated. use ::boolean or ::int instead.
140207 pub fn scalar(r: *Random, comptime T: type) T {
141 if (T == bool) return r.boolean();
142 return r.int(T);
208 return if (T == bool) r.boolean() else r.int(T);
143209 }
144210
145 /// Return a random integer with even distribution between `start`
146 /// inclusive and `end` exclusive. `start` must be less than `end`.
147211 /// TODO: deprecated. renamed to ::intRangeLessThan
148212 pub fn range(r: *Random, comptime T: type, start: T, end: T) T {
149213 return r.intRangeLessThan(T, start, end);
......@@ -206,6 +270,20 @@ pub const Random = struct {
206270 }
207271};
208272
273/// Convert a random integer 0 <= random_int <= maxValue(T),
274/// into an integer 0 <= result < less_than.
275/// This function introduces a minor bias.
276pub fn limitRangeBiased(comptime T: type, random_int: T, less_than: T) T {
277 comptime assert(T.is_signed == false);
278 const T2 = @IntType(false, T.bit_count * 2);
279
280 // adapted from:
281 // http://www.pcg-random.org/posts/bounded-rands.html
282 // "Integer Multiplication (Biased)"
283 var m: T2 = T2(random_int) * T2(less_than);
284 return @intCast(T, m >> T.bit_count);
285}
286
209287const SequentialPrng = struct {
210288 const Self = @This();
211289 random: Random,
......@@ -294,10 +372,19 @@ fn testRandomIntLessThan() void {
294372 var r = SequentialPrng.init();
295373 r.next_value = 0xff;
296374 assert(r.random.uintLessThan(u8, 4) == 3);
297 r.next_value = 0xff;
298 assert(r.random.uintLessThan(u8, 3) == 0);
375 assert(r.next_value == 0);
376 assert(r.random.uintLessThan(u8, 4) == 0);
299377 assert(r.next_value == 1);
300378
379 r.next_value = 0;
380 assert(r.random.uintLessThan(u64, 32) == 0);
381
382 // trigger the bias rejection code path
383 r.next_value = 0;
384 assert(r.random.uintLessThan(u8, 3) == 0);
385 // verify we incremented twice
386 assert(r.next_value == 2);
387
301388 r.next_value = 0xff;
302389 assert(r.random.intRangeLessThan(u8, 0, 0x80) == 0x7f);
303390 r.next_value = 0xff;
......@@ -310,17 +397,10 @@ fn testRandomIntLessThan() void {
310397 r.next_value = 0xff;
311398 assert(r.random.intRangeLessThan(i8, -0x80, 0) == -1);
312399
313 r.next_value = 0xff;
314 assert(r.random.intRangeLessThan(i64, -0x8000000000000000, 0) == -1);
315400 r.next_value = 0xff;
316401 assert(r.random.intRangeLessThan(i3, -4, 0) == -1);
317402 r.next_value = 0xff;
318403 assert(r.random.intRangeLessThan(i3, -2, 2) == 1);
319
320 // test retrying and eventually getting a good value
321 // start just out of bounds
322 r.next_value = 0x81;
323 assert(r.random.uintLessThan(u8, 0x81) == 0);
324404}
325405
326406test "Random intAtMost" {
......@@ -332,9 +412,14 @@ fn testRandomIntAtMost() void {
332412 var r = SequentialPrng.init();
333413 r.next_value = 0xff;
334414 assert(r.random.uintAtMost(u8, 3) == 3);
335 r.next_value = 0xff;
415 assert(r.next_value == 0);
416 assert(r.random.uintAtMost(u8, 3) == 0);
417
418 // trigger the bias rejection code path
419 r.next_value = 0;
336420 assert(r.random.uintAtMost(u8, 2) == 0);
337 assert(r.next_value == 1);
421 // verify we incremented twice
422 assert(r.next_value == 2);
338423
339424 r.next_value = 0xff;
340425 assert(r.random.intRangeAtMost(u8, 0, 0x7f) == 0x7f);
......@@ -348,17 +433,43 @@ fn testRandomIntAtMost() void {
348433 r.next_value = 0xff;
349434 assert(r.random.intRangeAtMost(i8, -0x80, -1) == -1);
350435
351 r.next_value = 0xff;
352 assert(r.random.intRangeAtMost(i64, -0x8000000000000000, -1) == -1);
353436 r.next_value = 0xff;
354437 assert(r.random.intRangeAtMost(i3, -4, -1) == -1);
355438 r.next_value = 0xff;
356439 assert(r.random.intRangeAtMost(i3, -2, 1) == 1);
357440
358 // test retrying and eventually getting a good value
359 // start just out of bounds
360 r.next_value = 0x81;
361 assert(r.random.uintAtMost(u8, 0x80) == 0);
441 assert(r.random.uintAtMost(u0, 0) == 0);
442}
443
444test "Random Biased" {
445 var r = DefaultPrng.init(0);
446 // Not thoroughly checking the logic here.
447 // Just want to execute all the paths with different types.
448
449 assert(r.random.uintLessThanBiased(u1, 1) == 0);
450 assert(r.random.uintLessThanBiased(u32, 10) < 10);
451 assert(r.random.uintLessThanBiased(u64, 20) < 20);
452
453 assert(r.random.uintAtMostBiased(u0, 0) == 0);
454 assert(r.random.uintAtMostBiased(u1, 0) <= 0);
455 assert(r.random.uintAtMostBiased(u32, 10) <= 10);
456 assert(r.random.uintAtMostBiased(u64, 20) <= 20);
457
458 assert(r.random.intRangeLessThanBiased(u1, 0, 1) == 0);
459 assert(r.random.intRangeLessThanBiased(i1, -1, 0) == -1);
460 assert(r.random.intRangeLessThanBiased(u32, 10, 20) >= 10);
461 assert(r.random.intRangeLessThanBiased(i32, 10, 20) >= 10);
462 assert(r.random.intRangeLessThanBiased(u64, 20, 40) >= 20);
463 assert(r.random.intRangeLessThanBiased(i64, 20, 40) >= 20);
464
465 // uncomment for broken module error:
466 //assert(r.random.intRangeAtMostBiased(u0, 0, 0) == 0);
467 assert(r.random.intRangeAtMostBiased(u1, 0, 1) >= 0);
468 assert(r.random.intRangeAtMostBiased(i1, -1, 0) >= -1);
469 assert(r.random.intRangeAtMostBiased(u32, 10, 20) >= 10);
470 assert(r.random.intRangeAtMostBiased(i32, 10, 20) >= 10);
471 assert(r.random.intRangeAtMostBiased(u64, 20, 40) >= 20);
472 assert(r.random.intRangeAtMostBiased(i64, 20, 40) >= 20);
362473}
363474
364475// Generator to extend 64-bit seed values into longer sequences.
......@@ -870,12 +981,16 @@ test "Random range" {
870981}
871982
872983fn testRange(r: *Random, start: i8, end: i8) void {
984 testRangeBias(r, start, end, true);
985 testRangeBias(r, start, end, false);
986}
987fn testRangeBias(r: *Random, start: i8, end: i8, biased: bool) void {
873988 const count = @intCast(usize, i32(end) - i32(start));
874989 var values_buffer = []bool{false} ** 0x100;
875990 const values = values_buffer[0..count];
876991 var i: usize = 0;
877992 while (i < count) {
878 const value: i32 = r.intRangeLessThan(i8, start, end);
993 const value: i32 = if (biased) r.intRangeLessThanBiased(i8, start, end) else r.intRangeLessThan(i8, start, end);
879994 const index = @intCast(usize, value - start);
880995 if (!values[index]) {
881996 i += 1;
test/runtime_safety.zig+10
......@@ -275,6 +275,16 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
275275 \\}
276276 );
277277
278 cases.addRuntimeSafety("signed integer not fitting in cast to unsigned integer - widening",
279 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
280 \\ @import("std").os.exit(126);
281 \\}
282 \\pub fn main() void {
283 \\ var value: c_short = -1;
284 \\ var casted = @intCast(u32, value);
285 \\}
286 );
287
278288 cases.addRuntimeSafety("unwrap error",
279289 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
280290 \\ if (@import("std").mem.eql(u8, message, "attempt to unwrap error: Whatever")) {