Skip to content

IndexPlace on a generic user type is not dispatched (bounded impl unseen, unbounded impl fails inference); IndexGet cannot yield a view #2560

Closed

Description

@ehartford

IndexPlace on a generic user type does not dispatch (spec §2.4, §11 "user types implementing IndexGet/IndexPlace").

pub type Box3[T] { items: List[T] }
impl[T: Copy] IndexPlace[isize, T] for Box3[T]:
    fn get(self: &Self, index: isize) -> T: self.items[index]
    mut fn set(index: isize, value: T): self.items[index] = value
fn main:
    var b = Box3 { items: [1, 2, 3] }
    b[1] = 9
    print(b[1])

gives type 'Box3[isize]' cannot be indexed: it is not a positional collection and implements no IndexPlace (the index type is also inferred as the type argument). Without the bound (impl[T] IndexPlace[isize, T] for Box3[T]) the read fails with cannot infer type parameter 'T' for this call; the argument's shape does not match the parameter type that mentions 'T'. A non-generic impl IndexPlace[i32, i32] for Grid works (behav_index_place_user.w).

lib/std/traits.w notes that a formal impl IndexPlace for List[T] waits on generic trait method bodies; the same gap blocks Deque[T] (std.collections.deque, Phase 3 of the stdlib sourcing plan), which offers get(i) -> &T until d[i] works. IndexGet returning a &T view (D27's element view for a user type) is the second half of the same item.

Expected: b[1] = 9; b[1] reads 9 through the impl; Deque[T] spells element access d[i].

Activity

  1. ehartford commented on Oct 24, 2026

    @ehartford
    ContributorAuthor

    Fixed in #2614 (main 23dd9db). Sema records the specialized get/set per subscript (record_index_place_contracts) and settles a bounded blanket impl against the instance (index_place_insts, IndexPlace or IndexGet); MirLower lowers the recorded contract, borrowing the receiver place for get(self: &Self), and a subscript as a place materializes a temp. IndexGet yielding a view works too: Deque and Stack now implement IndexGet[isize, &T] and spell d[i]. Fixture test/behavior/behav_index_place_generic.w (bounded and unbounded impls, two instantiations, compound store, call-argument subscript, IndexGet views); an IndexGet-only store is refused.

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