You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
A trait's default method runs another impl's methods: A.shout() calls B's name #2115
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
MirLower decides nothing from a name's spelling (#2043); each impl ha…