Skip to content

A default trait method's body is checked by a smaller copy of the function-body check: an implicit Ok tail is an ICE #3360

Closed

Description

@ehartford

Sema checks a default trait method's body with a smaller copy of the function-body check (check_trait_default_method_body_for_impl beside check_fn_body_with_sig_at). The copy lacks the tail rules, so a default method that relies on one is accepted by Sema and then refused by the MIR validator as a compiler bug.

Repro (§4.9, implicit Ok):

type Failed { code: i32 }

trait Sink:
    fn tag(self: &Self) -> i32
    fn checked(self: &Self, n: i32) -> Result[i32, Failed]:
        if n < 0: return Err(Failed { code: n })
        self.tag() + n

type S { t: i32 }
impl Sink for S:
    fn tag(self: &Self) -> i32: self.t

fn main:
    match S { t: 1 }.checked(3):
        Ok(v) => print(f"ok {v}")
        Err(e) => print(f"err {e.code}")

with run: internal compiler error: invalid MIR before codegen … use rvalue type is incompatible with assign destination … in S.checked. The same body as a method of impl S prints ok 4.

Exact line. SemaCheck.check_trait_default_method_body_for_impl: after check_expr_with_expected(body, ret_tid) it runs only check_body_explicit_value_results. check_fn_body_with_sig_at goes on to decide the implicit Ok (implicit_ok_tail_sigs.insert(sig_idx, 1), which MIR reads, D65), the T tail under Option[T] (§4.9a, D103), the contextual copy of a view tail (D111), the Result[Unit, E] fall-off, the missing-Default error, the -> Never check, numeric narrowing of the tail, and the returned-view / ephemeral / closure-environment origin checks. (A view of a local is still refused in a default method, by the block-level check: verified.)

Fix. One owner (D65): the tail and return checks of check_fn_body_with_sig_at become a function both callers use, as #3343's follow-up did for lowering (lower_fn_shape). Then add the default-method cases to the tail tests.

Activity

  1. ehartford commented on Nov 16, 2026

    @ehartford
    ContributorAuthor

    Merged in #3362: the implicit Ok and the Result[Unit, E] fall-off work in a default method (the repro above prints ok 4). The return rules are now shared functions (check_annotated_body_result, check_body_result, note_option_tail_demand).

    Still open here: a T tail under Option[T] in a default method is refused ("the function returns Option[i32], this value is i32"): the ordinary check sets the body's tail context (body_tail_block, infer_tail_node) before checking the body, and the default check does not. And the default body does not run check_body_result (returned-tail effects and origins), check_returned_field_move, note_scope_exit_drops or stamp_move_site_liveness. The fix for all of them is one body check with two callers.

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