method-resolution: emit error for method calls with illegal Sized b…#22372
Merged
ChayimFriedman2 merged 1 commit intoMay 15, 2026
Merged
Conversation
Resolves a FIXME at hir-ty/src/method_resolution.rs:151 in lookup_method_including_private that previously allowed calls like `x.cant_call()` on a `&dyn Foo` receiver to type-check without diagnostic when `cant_call(&self) where Self: Sized`, even though the `Self: Sized` predicate cannot be satisfied by the trait object. The existing predicates_require_illegal_sized_bound check in confirm.rs already correctly identifies this case (it iterates elaborated predicates and looks for `Self: Sized` clauses with a `TyKind::Dynamic` self type). Adds an InferenceDiagnostic::MethodCallIllegalSizedBound variant, plumbs the cooked diagnostic through hir, and renders it as RustcHardError E0277. Tests: trait-object call with Sized-bounded method errors; trait-object call to dyn-safe method does not; concrete-type call to a Sized-bounded method does not. Part of rust-lang#22140. Signed-off-by: Onyeka Obi <softwareengineerasaservant@isurvivable.cv>
ChayimFriedman2
approved these changes
May 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves a FIXME at hir-ty/src/method_resolution.rs:151 in
lookup_method_including_private that previously allowed calls
like
x.cant_call()on a&dyn Fooreceiver to type-checkwithout diagnostic when
cant_call(&self) where Self: Sized,even though the
Self: Sizedpredicate cannot be satisfiedby the trait object.
The existing predicates_require_illegal_sized_bound check in
confirm.rs already correctly identifies this case (it iterates
elaborated predicates and looks for
Self: Sizedclauses witha
TyKind::Dynamicself type). Adds anInferenceDiagnostic::MethodCallIllegalSizedBound variant,
plumbs the cooked diagnostic through hir, and renders it as
RustcHardError E0277.
Tests: trait-object call with Sized-bounded method errors;
trait-object call to dyn-safe method does not; concrete-type
call to a Sized-bounded method does not.
Part of #22140.