From 1ce09948975a148e09bf405f6a899e4fb0730ce6 Mon Sep 17 00:00:00 2001 From: frmdstryr Date: Mon, 26 Oct 2020 13:42:23 -0400 Subject: [PATCH 1/2] Fix @import of empty file --- src/stage1/analyze.cpp | 11 +++++++---- test/stage1/behavior/import.zig | 4 ++++ test/stage1/behavior/import/empty.zig | 0 3 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 test/stage1/behavior/import/empty.zig diff --git a/src/stage1/analyze.cpp b/src/stage1/analyze.cpp index 04c064efe3..24530307a2 100644 --- a/src/stage1/analyze.cpp +++ b/src/stage1/analyze.cpp @@ -1988,7 +1988,7 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc // behaviour when checking expected alignment with `@ptrToInt(fn_ptr)` // or similar. This commit proposes to make `align` expressions a // compile error when compiled to Wasm architecture. - // + // // Some references: // [1] [Mozilla: WebAssembly Tables](https://developer.mozilla.org/en-US/docs/WebAssembly/Understanding_the_text_format#WebAssembly_tables) // [2] [Sunfishcode's Wasm Ref Manual](https://github.com/sunfishcode/wasm-reference-manual/blob/master/WebAssembly.md#indirect-call) @@ -8052,10 +8052,13 @@ not_integer: } Error file_fetch(CodeGen *g, Buf *resolved_path, Buf *contents_buf) { - size_t len; + size_t len = 0xAA; const char *contents = stage2_fetch_file(&g->stage1, buf_ptr(resolved_path), buf_len(resolved_path), &len); - if (contents == nullptr) + if (len == 0) { + // File exists but is empty (otherwise it would be 0xAA) + } else if (contents == nullptr) { return ErrorFileNotFound; + } buf_init_from_mem(contents_buf, contents, len); return ErrorNone; } @@ -9044,7 +9047,7 @@ static void resolve_llvm_types_optional(CodeGen *g, ZigType *type, ResolveStatus 8 * child_type->abi_align, val_offset_in_bits, ZigLLVM_DIFlags_Zero, child_llvm_di_type); - di_element_types[maybe_null_index] = + di_element_types[maybe_null_index] = ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type), "maybe", di_file, line, 8*g->builtin_types.entry_bool->abi_size, diff --git a/test/stage1/behavior/import.zig b/test/stage1/behavior/import.zig index 2aa2ec4e60..30655554bf 100644 --- a/test/stage1/behavior/import.zig +++ b/test/stage1/behavior/import.zig @@ -16,3 +16,7 @@ test "import in non-toplevel scope" { }; expectEqual(@as(i32, 1234), S.foo()); } + +test "import empty file" { + const empty = @import("import/empty.zig"); +} diff --git a/test/stage1/behavior/import/empty.zig b/test/stage1/behavior/import/empty.zig new file mode 100644 index 0000000000..e69de29bb2 From 435c8ad703503e4dcc102b0907b45459d2693822 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 26 Oct 2020 16:06:14 -0700 Subject: [PATCH 2/2] non-hacky workaround for the empty file bug See #3328 --- src/stage1.zig | 2 ++ src/stage1/analyze.cpp | 7 ++----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/stage1.zig b/src/stage1.zig index d07a1c199c..2dabea91ad 100644 --- a/src/stage1.zig +++ b/src/stage1.zig @@ -428,5 +428,7 @@ export fn stage2_fetch_file( const max_file_size = std.math.maxInt(u32); const contents = comp.stage1_cache_manifest.addFilePostFetch(file_path, max_file_size) catch return null; result_len.* = contents.len; + // TODO https://github.com/ziglang/zig/issues/3328#issuecomment-716749475 + if (contents.len == 0) return @intToPtr(?[*]const u8, 0x1); return contents.ptr; } diff --git a/src/stage1/analyze.cpp b/src/stage1/analyze.cpp index 24530307a2..fd0de8e907 100644 --- a/src/stage1/analyze.cpp +++ b/src/stage1/analyze.cpp @@ -8052,13 +8052,10 @@ not_integer: } Error file_fetch(CodeGen *g, Buf *resolved_path, Buf *contents_buf) { - size_t len = 0xAA; + size_t len; const char *contents = stage2_fetch_file(&g->stage1, buf_ptr(resolved_path), buf_len(resolved_path), &len); - if (len == 0) { - // File exists but is empty (otherwise it would be 0xAA) - } else if (contents == nullptr) { + if (contents == nullptr) return ErrorFileNotFound; - } buf_init_from_mem(contents_buf, contents, len); return ErrorNone; }