Skip to content

Codegen: r as str with r: &Name (a distinct str) casts the reference, not the string — prints garbage #1782

Closed

Description

@ehartford

Spec

§4.5 (distinct types: zero-cost wrappers), §3.7 (a cast through a reference reads what it names).

Repro

type Name = distinct str
fn show(n: &Name): print(n as str)
fn main:
    let s = "abc".clone()
    let n = s as Name
    show(n)

Actual

Base stage1 (76cf0bf) and batch2-ownership's stage1: prints a garbage byte, debug-alloc: leak count=0.

Expected

abc.

Cause (reading, not yet in lldb)

MirLower.lower_cast reads through a reference only when the pointee is str, an array or a slice (r as []u8 with r: &str, §3.7). A &Name whose pointee is a distinct str takes the other path and casts the reference value (a pointer) to the string header.

Related, same shape: let n = s as Name lowers as _6 = cast(move _1 as ty239) with no reset, and both _1 and _5 get scope-exit drops; the distinct type's drop glue is what decides whether that is a double free. Found while fixing #1719 (a cast reads its source); not changed there.

Activity

  1. added 3 commits that reference this issue on Sep 28, 2026
    fcaac0e
    89d2d17
    527f627
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