Skip to content

A method declared for one instantiation (impl Box[str]) is callable on another (Box[isize]): safe code reads an integer as text #3508

Closed

Description

@ehartford

Found while moving List.join into the standard library (#3483): declared as impl List[str], it was accepted on a List[i32].

type Box[T] { v: T }

impl Box[str]:
    fn shout() -> str: self.v.to_upper()

fn main:
    let a = Box { v: "hi" }
    print(a.shout())
    let b = Box { v: 7 }
    print(b.shout())      // accepted; reads the integer as a str

Installed compiler (main at #3507): compiles with no error, prints HI and then an empty line. No unsafe anywhere.

Expected: b.shout() is refused: shout is declared for Box[str], and b is a Box[isize].

Why it matters: this is a memory-safety hole in safe code (the method body reads the receiver's payload at the wrong type), and it blocks declaring any built-in that exists for one element type only (List[str].join).

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