Skip to content

method-resolution: emit error for method calls with illegal Sized b…#22372

Merged
ChayimFriedman2 merged 1 commit into
rust-lang:masterfrom
MavenRain:fix-illegal-sized-bound
May 15, 2026
Merged

method-resolution: emit error for method calls with illegal Sized b…#22372
ChayimFriedman2 merged 1 commit into
rust-lang:masterfrom
MavenRain:fix-illegal-sized-bound

Conversation

@MavenRain

Copy link
Copy Markdown
Contributor

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 #22140.

  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>
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 15, 2026

@ChayimFriedman2 ChayimFriedman2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that this is not a regular trait bound check.

Thanks!

View changes since this review

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue May 15, 2026
Merged via the queue into rust-lang:master with commit 1a68212 May 15, 2026
18 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 15, 2026
@MavenRain MavenRain deleted the fix-illegal-sized-bound branch May 15, 2026 15:44
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.

3 participants