Skip to content

miscompilation when writing through pointer to optional slice arg #14783

@andrewrk

Description

@andrewrk

zig version: 0.11.0-dev.1844+75ff34db9

const std = @import("std");
const testing = std.testing;

test "write through pointer to optional slice arg" {
    var foo: ?[]const u8 = null;
    try bar(&foo);
    try testing.expectEqualStrings(foo.?, "ok");
}

fn bar(foo: *?[]const u8) !void {
    foo.* = try baz();
}

fn baz() ![]const u8 {
    return "ok";
}
$ stage4/bin/zig test test.zig 
LLVM Emit Object... zig: /home/andy/Downloads/llvm-project-15/llvm/include/llvm/Support/Casting.h:578: decltype(auto) llvm::cast(From*) [with To = llvm::ValueAsMetadata; From = const llvm::Metadata]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
Aborted (core dumped)

In this example it triggers an LLVM assertion. In the actual usage that I have, it is a silent miscompilation that leaves foo incorrectly null.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.miscompilationThe compiler reports success but produces semantically incorrect code.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions