Skip to content

LLVM verify error: comparing a &str parameter to a string literal emits icmp between ptr and str #293

Closed

Description

@ehartford

Summary

Comparing a value bound as &str against a string literal mis-codegens: LLVM rejects an icmp eq whose operands are ptr (the &str) and str (the literal value). with check passes; the failure is at codegen. Comparing an owned str works fine.

Environment

  • with v0.14.5, macOS arm64

Minimal repro

fn is_x(k: &str) -> bool:
    k == "x"

fn main:
    let r = is_x("x")
    print(f"r={r}")

Observed

with run:

LLVM verify error
Both operands to ICmp instruction are not of the same type!
  %9 = icmp eq ptr %4, %str %8
error: code generation failed

with check → ok.

Workaround

Compare an owned str instead of a &str: take the parameter by value (k: str), or compare a str field/local. str == "literal" codegens correctly.

Notes

The &str operand lowers to a bare ptr while the string literal lowers to the str aggregate value; equality should deref the &str (or otherwise compare consistently) before the icmp.

Activity

  1. ehartford commented on May 31, 2026

    @ehartford
    ContributorAuthor

    Fixed in 6bf8849. Sema now treats a &str as the str it points at for comparison (cmp_normalize_str_ref — only str refs; &i32 keeps pointer semantics), and codegen classifies a &str as a str and loads the str aggregate from the ref before compare_str_eq. &str == "lit", &str == &str, and &str != "lit" now compare by value. Regression test test/behavior/ref_str_compare.w; full build + fixpoint + test green. (Note: the symptom had evolved from the original LLVM verify error to a Sema rejection; both are fixed.)

  2. added 2 commits that reference this issue on Aug 17, 2026
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions