Skip to content

A default trait method that calls the type's other static methods (Self.a(x)) emits invalid LLVM: an extra receiver argument #3335

Closed

Description

@ehartford
trait P:
    fn a(x: &str) -> Option[Self]
    fn why(x: &str) -> str: ""
    fn must(x: &str, name: &str, line: i32) -> Self:
        let got = Self.a(x)
        if got.is_none(): panic(f"cannot parse {name} from \"{x}\"{Self.why(x)}\n  at {line}")
        got.unwrap()
impl P for i32:
    fn a(x: &str) -> Option[i32]: if x == "1": Some(1) else: None
    fn why(x: &str) -> str: ": overflow"
type C { v: i32 }
impl P for C:
    fn a(x: &str) -> Option[C]: Some(C { v: 9 })

fn main:
    print(i32.must("1", "i32", 3))
    print(C.must("x", "C", 4).v)
Incorrect number of arguments passed to called function!
  %6 = call { i32, [1 x i32] } @i32.P.a(%str zeroinitializer, %str %.fca.1.insert)
===== INVALID LLVM FUNCTION i32.must =====

Inside the default body, Self.a(x) is lowered with a receiver slot the static method does not have.

With a match in the default body instead of the let/if:

    fn must(x: &str, name: &str, line: i32) -> Self:
        match Self.a(x):
            Some(v) => v
            None => panic("no")

Sema reports internal error: conflicting contextual join decisions for one expression (sig -1: first expected i32 [3] final i32 [3], now expected C [310] final C [310]): the join decision is recorded per tree node, and the one default body is checked once per implementing type.

Both on the installed compiler (v0.16.0.8). Found while building D203/D204; that work uses a generic free function instead.

Activity

  1. ehartford commented on Nov 14, 2026

    @ehartford
    ContributorAuthor

    Findings (installed compiler, main 4c525fb).

    The join conflict, exact line. lldb, breakpoint Sema.resolve_contextual_join: both impls reach it with the same report_node (20151), called from check_trait_default_method_body_for_impl (SemaCheck.w:6751) under collect_declarations. Each impl is meant to have its own copy of a default body (AstPool.clone_default_method_bodies), but that pass runs in Frontend.w after the comptime phase; the first Sema pass checks the trait's one shared body for every impl. The join decision is keyed (current_fn_sig_idx, node) and current_fn_sig_idx is not set while a default body is checked (the message prints sig -1), so the second impl's decision meets the first's. Fix: check the default body under the impl method's own signature (current_fn_sig_idx = sig_idx, as current_effect_body already is, #1827).

    The invalid LLVM, narrowed. One impl is enough (impl P for C alone, let got = Self.a(x); got.unwrap()): call @C.P.a(%str zeroinitializer, %str …), two arguments to a one-parameter function. a is declared in the trait with no receiver use and is called as a static (C.a(x)) everywhere else; inside the default body Self.a(x) is lowered as an instance call with a receiver slot. The default method does not appear in --dump-mir.

    A tool that stayed silent. with analyze dl.w audit:resolution says violations=0 ok on the program that emits the invalid call.

    Related, to settle first. §9.5 says a fn inside an impl is an instance method, and §11 speaks of a trait's static method (fn one() -> Self). In an ordinary impl, fn a(x: &str) -> Option[Self] then Self.a(x) in a sibling is refused with "return type does not implement Default" at the let, which is the wrong message whatever the rule is. Which trait methods are static, and how a default body calls one, needs the spec read against the implementation before the lowering is changed.

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