Skip to content

Nullable c_import pointers can miscompile when checked against None and unwrapped #30

Closed

Description

@ehartford

Current behavior

Nullable C pointers imported through c_import still miscompile in normal control flow when stored in a local, compared against None, and then unwrapped.

Minimal repro:

use c_import("stdlib.h")

fn make_handle -> i64:
let raw_opt = malloc(8)
if raw_opt == None:
return 0
let raw = raw_opt.unwrap()
let _ = realloc(raw, 0)
1

fn main:
assert(make_handle() == 1)

Observed behavior

with run reports:
error: RK_AGGREGATE with unknown dest type fn=make_handle count=0
LLVM verify error: Both operands to ICmp instruction are not of the same type!
%14 = icmp eq ptr %12, i64 %13, !dbg !393

LLVM verify function: make_handle
error: code generation failed
error: run failed

Related repro

The same pattern in a Result-returning function also emits invalid LLVM:

use c_import("stdlib.h")
pub error E = Bad

fn allocish -> Result[i32, E]:
let raw_opt = malloc(8)
if raw_opt == None:
return Err(.Bad)
let raw = raw_opt.unwrap()
let _ = realloc(raw, 0)
Ok(1)

Expected behavior

A nullable C pointer should behave like a normal Option in local control flow. Comparing it against None and calling .unwrap() after the check should either work or be rejected with a front-end error, not emit invalid LLVM IR.

Repro

cat > /tmp/c_pointer_option_bug.w << 'EOF2'
use c_import("stdlib.h")

fn make_handle -> i64:
let raw_opt = malloc(8)
if raw_opt == None:
return 0
let raw = raw_opt.unwrap()
let _ = realloc(raw, 0)
1

fn main:
assert(make_handle() == 1)
EOF2
with run /tmp/c_pointer_option_bug.w

Activity

  1. added
    bug
    error-messages
    on Mar 20, 2026
  2. added a commit that references this issue on Jun 8, 2026
    c73fd48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugerror-messages

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions