Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compiler crash while building obj file #2680

Closed
DutchGhost opened this issue Jun 14, 2019 · 1 comment
Closed

compiler crash while building obj file #2680

DutchGhost opened this issue Jun 14, 2019 · 1 comment
Labels
bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@DutchGhost
Copy link

The following code causes the compiler to crash when zig build-obj <file.zig>:

const std = @import("std");
const mem = std.mem;
const heap = std.heap;
const heapAllocator = heap.DirectAllocator;
const Allocator = mem.Allocator;

const allocator = struct {
  internal_allocator: heapAllocator,

  pub fn init() allocator {
    return allocator {
      .internal_allocator = heapAllocator.init()
    };
  }

  pub fn allocate(self: *@This(), size: usize, alignment: usize) error{OutOfMemory}![]u8 {
    var slice = [0]u8 {};
    var alloc: *Allocator = &self.internal_allocator.allocator;
    return alloc.reallocFn(alloc, &slice, 0, size, @intCast(u29, alignment));
  }
};


export fn allocate(size: usize, alignment: usize) *u8 {
  var alloc = allocator.init();
  
  if(alloc.allocate(size, alignment)) |slice| {
    return &slice[0];
  } else |err| {
    return @intToPtr(*u8, 1);
  }
}

gdb backtrace:

Assertion failed. This is a bug in the Zig compiler.
???:?:?: 0x8704029 in ??? (???)


???:?:?: 0x86c8494 in ??? (???)



Program received signal SIGABRT, Aborted.
__restore_sigs (set=set@entry=0x7ffffffecd40) at ./arch/x86_64/syscall_arch.h:40
40      ./arch/x86_64/syscall_arch.h: No such file or directory.
(gdb) backtrace
#0  __restore_sigs (set=set@entry=0x7ffffffecd40) at ./arch/x86_64/syscall_arch.h:40
#1  0x000000000c735c4d in raise (sig=sig@entry=6) at src/signal/raise.c:11
#2  0x000000000c730aaa in abort () at src/exit/abort.c:13
#3  0x0000000008706299 in std.os.abort () at /deps/zig/std/os.zig:200
#4  0x0000000008703e96 in std.debug.panicExtra () at /deps/zig/std/debug.zig:192
#5  0x0000000008703e38 in std.special.panic (msg=...) at /deps/zig/std/special/panic.zig:27
#6  0x000000000870fabc in std.debug.writeCurrentStackTrace () at /deps/zig/std/debug.zig:268
#7  0x0000000008706915 in std.debug.dumpCurrentStackTrace (start_addr=...) at /deps/zig/std/debug.zig:89
#8  0x0000000008703f38 in std.debug.panicExtra () at /deps/zig/std/debug.zig:199
#9  0x0000000008703e38 in std.special.panic (msg=...) at /deps/zig/std/special/panic.zig:27
#10 0x000000000870402a in stage2_panic (ptr=0xc7651c8 "Assertion failed. This is a bug in the Zig compiler.")
    at stage1.zig:16
#11 0x00000000086c8495 in assert(bool) [clone .part.0] ()
#12 0x00000000086c84ac in assert(bool) ()
#13 0x0000000008644849 in ir_render_cast(CodeGen*, IrExecutable*, IrInstructionCast*) [clone .isra.178] ()
#14 0x000000000864ad7b in ir_render_instruction(CodeGen*, IrExecutable*, IrInstruction*) [clone .isra.206] ()
#15 0x000000000864cab5 in do_code_gen(CodeGen*) ()
#16 0x00000000086500d7 in codegen_build_and_link(CodeGen*) ()
#17 0x000000000857925a in main ()
(gdb)
@andrewrk andrewrk added this to the 0.5.0 milestone Jun 16, 2019
@andrewrk andrewrk added the bug Observed behavior contradicts documented or intended behavior label Jun 16, 2019
@daurnimator
Copy link
Collaborator

daurnimator commented Sep 2, 2019

Example as given now fails with:

$ ./zig build-obj ../2680.zig 
/home/daurnimator/src/zig/2680.zig:4:27: error: 'DirectAllocator' is private
const heapAllocator = heap.DirectAllocator;
                          ^
/home/daurnimator/src/zig/build/lib/zig/std/heap.zig:42:1: note: declared here
const DirectAllocator = struct {
^
/home/daurnimator/src/zig/2680.zig:8:23: note: referenced here
  internal_allocator: heapAllocator,
                      ^
/home/daurnimator/src/zig/2680.zig:25:24: note: referenced here
  var alloc = allocator.init();
                       ^

Which looks like the expected behaviour.

This was likely a dupe of #2692

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

3 participants