zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit fcedc35551cc6b14756499414e47c33004de3be4 (tree)
parent 83a59c4d077ead78a8c0ccecf8d4f7970309bf76
Author: Andrew Kelley <superjoe30@gmail.com>
Date:   Thu, 14 Apr 2016 10:39:03 -0700

fix crash with generic function and implicit cast

Diffstat:
Msrc/ast_render.cpp | 8++++++--
Msrc/parser.cpp | 6++++--
Mtest/run_tests.cpp | 2+-
Mtest/self_hosted.zig | 11+++++++++++
4 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/src/ast_render.cpp b/src/ast_render.cpp @@ -326,9 +326,9 @@ static void render_node(AstRender *ar, AstNode *node) { AstNode *statement = node->data.block.statements.at(i); print_indent(ar); render_node(ar, statement); + fprintf(ar->f, ";\n"); } ar->indent -= ar->indent_size; - fprintf(ar->f, "\n"); print_indent(ar); fprintf(ar->f, "}"); break; @@ -438,7 +438,11 @@ static void render_node(AstRender *ar, AstNode *node) { fprintf(ar->f, ")"); break; case NodeTypeArrayAccessExpr: - zig_panic("TODO"); + render_node(ar, node->data.array_access_expr.array_ref_expr); + fprintf(ar->f, "["); + render_node(ar, node->data.array_access_expr.subscript); + fprintf(ar->f, "]"); + break; case NodeTypeSliceExpr: zig_panic("TODO"); case NodeTypeFieldAccessExpr: diff --git a/src/parser.cpp b/src/parser.cpp @@ -2927,6 +2927,7 @@ static void clone_subtree_list(ZigList<AstNode *> *dest, ZigList<AstNode *> *src dest->resize(src->length); for (int i = 0; i < src->length; i += 1) { dest->at(i) = ast_clone_subtree(src->at(i), next_node_index); + dest->at(i)->parent_field = &dest->at(i); } } @@ -2958,11 +2959,12 @@ AstNode *ast_clone_subtree(AstNode *old_node, uint32_t *next_node_index) { memcpy(new_node, old_node, sizeof(AstNode)); new_node->create_index = *next_node_index; *next_node_index += 1; + new_node->parent_field = nullptr; switch (new_node->type) { case NodeTypeRoot: - clone_subtree_list(&new_node->data.root.top_level_decls, &old_node->data.root.top_level_decls, - next_node_index); + clone_subtree_list(&new_node->data.root.top_level_decls, + &old_node->data.root.top_level_decls, next_node_index); break; case NodeTypeFnProto: clone_subtree_tld(&new_node->data.fn_proto.top_level_decl, &old_node->data.fn_proto.top_level_decl, diff --git a/test/run_tests.cpp b/test/run_tests.cpp @@ -1667,7 +1667,7 @@ extern void (*fn_ptr)(void); )SOURCE", 2, "pub extern var fn_ptr: ?extern fn();", R"SOURCE(pub inline fn foo() { - (??fn_ptr)() + (??fn_ptr)(); })SOURCE"); diff --git a/test/self_hosted.zig b/test/self_hosted.zig @@ -975,3 +975,14 @@ pub fn vec3(x: f32, y: f32, z: f32) -> Vec3 { .data = []f32 { x, y, z, }, } } + + +#attribute("test") +fn generic_fn_with_implicit_cast() { + assert(get_first_byte(u8)([]u8 {13}) == 13); + assert(get_first_byte(u16)([]u16 {0, 13}) == 0); +} +fn get_byte(ptr: ?&u8) -> u8 {*??ptr} +fn get_first_byte(T: type)(mem: []T) -> u8 { + get_byte((&u8)(&mem[0])) +}