Skip to content

A method declared for one instantiation is refused on another; it was accepted and read the receiver at the wrong type (#3508) - #3509

Merged
ehartford merged 1 commit into
mainfrom
impl-instantiation-3508
Nov 21, 2026
Merged

ehartford merged 1 commit into
mainfrom
impl-instantiation-3508

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Closes #3508.

The defect. Methods are found by the type's name. A method declared in impl Box[str] was therefore accepted on a Box[isize], and its body read the receiver's payload at the wrong type: an integer as text, in safe code with no unsafe.

The fix. Where a method call has resolved its method (beside the visibility check), check_method_declared_for compares the receiver with the instantiation the method's impl names. When they differ the call is refused:

error: method 'shout' is declared for `Box[str]`, and this is a `Box[isize]`
  = help: `shout` exists for that one instantiation; declare it in an `impl[T]` block to have it for every one

An impl[T] block, and an impl whose target names a bare type parameter, are unaffected.

Routes tried against the wrong instantiation, all refused: a direct call, the method as a value (Box[isize].shout, Box[str].shout applied to the wrong value), the type-qualified call, a call on an indexed element, a pipeline stage.

Tests: err_3508_method_of_another_instantiation (shown failing on the installed compiler first) and behav_3508_method_of_its_own_instantiation (a call, through a borrow, as a method value, and an impl[T] method on another instantiation).

No spec words: §9 already says a method belongs to the type its impl names.

Battery green; :release-checks green.

… accepted and read the receiver at the wrong type (#3508)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A method declared for one instantiation (impl Box[str]) is callable on another (Box[isize]): safe code reads an integer as text

1 participant