Zig Version
0.16.0-dev.3132+fd2718f82
Steps to Reproduce and Observed Behavior
Minimal repro:
comptime{consta:[]constu8="123";constb:?[]constu8=@ptrCast(a);_=b;}zig build-obj -fno-emit-bin repro.zig
Compiler crash context:
Analyzing 'repro.zig'
%2 = ptr_type(@u8_type, const, slice) node_offset:2:14 to :2:24
%3 = str("123")
%4 = as_node(%2, %3) node_offset:2:27 to :2:32
%5 = validate_const(%4) node_offset:2:27 to :2:32
%6 = ptr_type(@u8_type, const, slice) node_offset:3:15 to :3:25
%7 = optional_type(%6) node_offset:3:14 to :3:25
> %8 = ptr_cast(%7, %4) node_offset:3:28 to :3:39
%9 = as_node(%7, %8) node_offset:3:28 to :3:39
%10 = validate_const(%9) node_offset:3:28 to :3:39
%11 = ensure_result_non_error(%9) node_offset:4:9 to :4:10
%12 = break_inline(%1, @void_value)
For full context, use the command
C:\Projects\zig\stage4\bin\zig.exe ast-check -t repro.zig
thread 7648 panic: reached unreachable code
C:\Projects\zig\src\InternPool.zig:9947:17: 0x7ff7e56a062f in slicePtrType (zig_zcu.obj)
else => unreachable, // not a slice type
^
C:\Projects\zig\src\Type.zig:1368:54: 0x7ff7e56ebf92 in slicePtrFieldType (zig_zcu.obj)
return .fromInterned(zcu.intern_pool.slicePtrType(ty.toIntern()));
^
C:\Projects\zig\src\Sema.zig:21670:77: 0x7ff7e653dfe3 in ptrCastFull (zig_zcu.obj)
.ptr = (try pt.getCoerced(ptr_val, dest_ty.slicePtrFieldType(zcu))).toIntern(),
^
C:\Projects\zig\src\Sema.zig:21258:28: 0x7ff7e6704720 in zirPtrCast (zig_zcu.obj)
return sema.ptrCastFull(
^
C:\Projects\zig\src\Sema.zig:1299:65: 0x7ff7e640c3d2 in analyzeBodyInner (zig_zcu.obj)
.ptr_cast => try sema.zirPtrCast(block, inst),
^
C:\Projects\zig\src\Sema.zig:1072:30: 0x7ff7e6456362 in analyzeInlineBody (zig_zcu.obj)
if (sema.analyzeBodyInner(block, body)) {
^
C:\Projects\zig\src\Sema.zig:1105:39: 0x7ff7e68d7550 in resolveInlineBody (zig_zcu.obj)
return (try sema.analyzeInlineBody(block, body, break_target)) orelse .unreachable_value;
^
C:\Projects\zig\src\Zcu\PerThread.zig:1320:50: 0x7ff7e7a52878 in analyzeComptimeUnit (zig_zcu.obj)
const result_ref = try sema.resolveInlineBody(&block, value_body, inst_resolved.inst);
^
C:\Projects\zig\src\Zcu\PerThread.zig:1221:34: 0x7ff7e7a4f0bd in ensureComptimeUnitUpToDate (zig_zcu.obj)
return pt.analyzeComptimeUnit(cu_id) catch |err| switch (err) {
^
C:\Projects\zig\src\Zcu\PerThread.zig:329:63: 0x7ff7e62b46a1 in update (zig_zcu.obj)
.@"comptime" => |cu| pt.ensureComptimeUnitUpToDate(cu),
^
C:\Projects\zig\src\Compilation.zig:4522:22: 0x7ff7e5fb1a66 in performAllTheWork (zig_zcu.obj)
try pt.update(main_progress_node, &decl_work_timer);
^
C:\Projects\zig\src\Compilation.zig:3091:31: 0x7ff7e4f233dd in update (zig_zcu.obj)
try comp.performAllTheWork(main_progress_node, arena);
^
C:\Projects\zig\src\main.zig:4688:20: 0x7ff7e4edd86b in updateModule (zig_zcu.obj)
try comp.update(prog_node);
^
C:\Projects\zig\src\main.zig:3784:21: 0x7ff7e4811b79 in buildOutputType (zig_zcu.obj)
updateModule(comp, color, root_prog_node) catch |err| switch (err) {
^
C:\Projects\zig\src\main.zig:288:31: 0x7ff7e48446a9 in mainArgs (zig_zcu.obj)
return buildOutputType(gpa, arena, io, args, .{ .build = .Obj }, environ_map);
^
C:\Projects\zig\src\main.zig:235:20: 0x7ff7e47c1b25 in main (zig_zcu.obj)
return mainArgs(gpa, arena, io, args, &environ_map);
^
C:\Projects\zig\lib\std\start.zig:657:28: 0x7ff7e461115c in main (zig_zcu.obj)
return callMain(std.os.windows.peb().ProcessParameters.CommandLine.slice(), .global);
^
C:\Projects\zig\lib\libc\mingw\crt\crtexe.c:259:0: 0x7ff7e7efcf2b in __tmainCRTStartup (crt2.obj)
mainret = _tmain (argc, argv, envp);
C:\Projects\zig\lib\libc\mingw\crt\crtexe.c:179:0: 0x7ff7e7efcf8b in mainCRTStartup (crt2.obj)
ret = __tmainCRTStartup ();
???:?:?: 0x7ffe498c7373 in ??? (KERNEL32.DLL)
???:?:?: 0x7ffe49ddcc90 in ??? (ntdll.dll)
Likely introduced by #31403. This bug is blocking users from e.g. using fixed buffer allocators at comptime because std.mem.Allocator.remap() returns ?@TypeOf(allocation).
Expected Behavior
No crash.
### Zig Version
0.16.0-dev.3132+fd2718f82
### Steps to Reproduce and Observed Behavior
Minimal repro:
```zig
comptime {
const a: []const u8 = "123";
const b: ?[]const u8 = @ptrCast(a);
_ = b;
}
```
```sh
zig build-obj -fno-emit-bin repro.zig
```
```
Compiler crash context:
Analyzing 'repro.zig'
%2 = ptr_type(@u8_type, const, slice) node_offset:2:14 to :2:24
%3 = str("123")
%4 = as_node(%2, %3) node_offset:2:27 to :2:32
%5 = validate_const(%4) node_offset:2:27 to :2:32
%6 = ptr_type(@u8_type, const, slice) node_offset:3:15 to :3:25
%7 = optional_type(%6) node_offset:3:14 to :3:25
> %8 = ptr_cast(%7, %4) node_offset:3:28 to :3:39
%9 = as_node(%7, %8) node_offset:3:28 to :3:39
%10 = validate_const(%9) node_offset:3:28 to :3:39
%11 = ensure_result_non_error(%9) node_offset:4:9 to :4:10
%12 = break_inline(%1, @void_value)
For full context, use the command
C:\Projects\zig\stage4\bin\zig.exe ast-check -t repro.zig
thread 7648 panic: reached unreachable code
C:\Projects\zig\src\InternPool.zig:9947:17: 0x7ff7e56a062f in slicePtrType (zig_zcu.obj)
else => unreachable, // not a slice type
^
C:\Projects\zig\src\Type.zig:1368:54: 0x7ff7e56ebf92 in slicePtrFieldType (zig_zcu.obj)
return .fromInterned(zcu.intern_pool.slicePtrType(ty.toIntern()));
^
C:\Projects\zig\src\Sema.zig:21670:77: 0x7ff7e653dfe3 in ptrCastFull (zig_zcu.obj)
.ptr = (try pt.getCoerced(ptr_val, dest_ty.slicePtrFieldType(zcu))).toIntern(),
^
C:\Projects\zig\src\Sema.zig:21258:28: 0x7ff7e6704720 in zirPtrCast (zig_zcu.obj)
return sema.ptrCastFull(
^
C:\Projects\zig\src\Sema.zig:1299:65: 0x7ff7e640c3d2 in analyzeBodyInner (zig_zcu.obj)
.ptr_cast => try sema.zirPtrCast(block, inst),
^
C:\Projects\zig\src\Sema.zig:1072:30: 0x7ff7e6456362 in analyzeInlineBody (zig_zcu.obj)
if (sema.analyzeBodyInner(block, body)) {
^
C:\Projects\zig\src\Sema.zig:1105:39: 0x7ff7e68d7550 in resolveInlineBody (zig_zcu.obj)
return (try sema.analyzeInlineBody(block, body, break_target)) orelse .unreachable_value;
^
C:\Projects\zig\src\Zcu\PerThread.zig:1320:50: 0x7ff7e7a52878 in analyzeComptimeUnit (zig_zcu.obj)
const result_ref = try sema.resolveInlineBody(&block, value_body, inst_resolved.inst);
^
C:\Projects\zig\src\Zcu\PerThread.zig:1221:34: 0x7ff7e7a4f0bd in ensureComptimeUnitUpToDate (zig_zcu.obj)
return pt.analyzeComptimeUnit(cu_id) catch |err| switch (err) {
^
C:\Projects\zig\src\Zcu\PerThread.zig:329:63: 0x7ff7e62b46a1 in update (zig_zcu.obj)
.@"comptime" => |cu| pt.ensureComptimeUnitUpToDate(cu),
^
C:\Projects\zig\src\Compilation.zig:4522:22: 0x7ff7e5fb1a66 in performAllTheWork (zig_zcu.obj)
try pt.update(main_progress_node, &decl_work_timer);
^
C:\Projects\zig\src\Compilation.zig:3091:31: 0x7ff7e4f233dd in update (zig_zcu.obj)
try comp.performAllTheWork(main_progress_node, arena);
^
C:\Projects\zig\src\main.zig:4688:20: 0x7ff7e4edd86b in updateModule (zig_zcu.obj)
try comp.update(prog_node);
^
C:\Projects\zig\src\main.zig:3784:21: 0x7ff7e4811b79 in buildOutputType (zig_zcu.obj)
updateModule(comp, color, root_prog_node) catch |err| switch (err) {
^
C:\Projects\zig\src\main.zig:288:31: 0x7ff7e48446a9 in mainArgs (zig_zcu.obj)
return buildOutputType(gpa, arena, io, args, .{ .build = .Obj }, environ_map);
^
C:\Projects\zig\src\main.zig:235:20: 0x7ff7e47c1b25 in main (zig_zcu.obj)
return mainArgs(gpa, arena, io, args, &environ_map);
^
C:\Projects\zig\lib\std\start.zig:657:28: 0x7ff7e461115c in main (zig_zcu.obj)
return callMain(std.os.windows.peb().ProcessParameters.CommandLine.slice(), .global);
^
C:\Projects\zig\lib\libc\mingw\crt\crtexe.c:259:0: 0x7ff7e7efcf2b in __tmainCRTStartup (crt2.obj)
mainret = _tmain (argc, argv, envp);
C:\Projects\zig\lib\libc\mingw\crt\crtexe.c:179:0: 0x7ff7e7efcf8b in mainCRTStartup (crt2.obj)
ret = __tmainCRTStartup ();
???:?:?: 0x7ffe498c7373 in ??? (KERNEL32.DLL)
???:?:?: 0x7ffe49ddcc90 in ??? (ntdll.dll)
```
Likely introduced by #31403. This bug is blocking users from e.g. [using fixed buffer allocators at comptime](https://github.com/castholm/zig-examples/blob/d430f780edc01c6cc70b7d0d8d42edd77278d3d9/breakout/main.zig#L23-L27) because [`std.mem.Allocator.remap()`](https://codeberg.org/ziglang/zig/src/commit/fd2718f82ab70d99186f43845e4291515efb66a5/lib/std/mem/Allocator.zig#L354) returns `?@TypeOf(allocation)`.
### Expected Behavior
No crash.