Skip to content

An inherent impl on a variant registers no methods #1920

Description

@gfx

impl V { … } on a local variant is accepted by the parser but its methods are never registered, so every call site fails with no method … found. struct, enum and flags all work; only variant is affected.

docs/spec.md § Inherent Impls constrains inherent impls by locality of the self type's head constructor, not by type kind, so a local variant should be allowed.

Reproduction

use { println } from "core:cli";

struct S { v: i32 }
enum E { A, B }
variant V { A(i32), B }
flags F { X, Y }

impl S { fn s(&self) -> i32 { return 1; } }
impl E { fn e(&self) -> i32 { return 2; } }
impl V { fn v(&self) -> i32 { return 3; } }
impl F { fn f(&self) -> i32 { return 4; } }

export fn run() with Stdout {
    println(`${S { v: 0 }.s()} ${E::A.e()} ${V::B.v()} ${F::X.f()}`);
}
error: no method 'v' found on type 'V'

Removing the V line leaves 1 2 4 — the other three kinds resolve.

Impact

A variant has to put its private helpers in free functions taking the value as a parameter, losing the self receiver and the namespacing. core:value's Value hit this: its Inspect alternate body could not become self.inspect_alternate(f) and stayed a free function.

Expected

Either the methods resolve as they do for the other three kinds, or the impl is rejected at its own span with a diagnostic saying a variant takes no inherent impl — and the spec says so. Silently registering nothing is the worst of the three.

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