Skip to content

A trait's default method runs another impl's methods: A.shout() calls B's name #2115

Closed

Description

@ehartford

Found while finishing #2043. On main (dad25d8), a default method that calls another trait method through self runs the method of whichever impl Sema checked last.

trait Named:
    fn name(self: &Self) -> str
    fn rank(self: &Self) -> i64
    fn shout(self: &Self) -> str: self.name() ++ "!"
    fn level(self: &Self) -> i64: self.rank()

type A { x: i64 }
type B { y: i64, z: i64 }
impl Named for A:
    fn name(self: &Self) -> str: "a"
    fn rank(self: &Self) -> i64: self.x
impl Named for B:
    fn name(self: &Self) -> str: "b"
    fn rank(self: &Self) -> i64: self.z

fn main:
    let a = A { x: 1 }
    let b = B { y: 9, z: 2 }
    print(f"{a.shout()} {b.shout()}")
    print(f"{a.level()} {b.level()}")

Expected a! b! and 1 2. Actual b! b! and 4372086800 2: a.level() runs B.rank on an A and reads past it (§9: safe code read memory it does not own).

A generic call with self in a default body (tag(self)) gets the last impl's instance the same way.

Cause. The trait's default body is one set of AST nodes, checked once per impl; everything Sema records about a body (comp_resolved, resolved_call_mono_syms, …) is keyed by node, so the last impl checked overwrites the others, and MirLower reads the record (D65).

Fix (on the stack with #2043). Each impl gets its own copy of each default body before the pool is frozen; Sema checks and codegen lowers the impl's copy. Test: test/behavior/behav_default_method_per_impl.w.

Activity

  1. added a commit that references this issue on Oct 5, 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