Skip to content

Add more context to dyn-compatibility diagnostic for receiver-less associated functions - #160428

Open
xyz-harshal wants to merge 1 commit into
rust-lang:mainfrom
xyz-harshal:fix-dyn-compat-diagnostic-159492
Open

Add more context to dyn-compatibility diagnostic for receiver-less associated functions#160428
xyz-harshal wants to merge 1 commit into
rust-lang:mainfrom
xyz-harshal:fix-dyn-compat-diagnostic-159492

Conversation

@xyz-harshal

@xyz-harshal xyz-harshal commented Aug 3, 2026

Copy link
Copy Markdown

Fixes #159492

Expands the two suggestion messages for the "no self parameter"
dyn-compatibility violation to explain why each fix works, per the issue.

Before:
help: consider turning create into a method by giving it a &self argument
help: alternatively, consider constraining create so it does not apply to trait objects

After:
help: consider turning create into a method by giving it a &self argument, so that it is accessible through the trait object's vtable
help: alternatively, consider constraining create so it is explicitly marked as not applying to trait objects

Only the two strings in DynCompatibilityViolationSolution::add_to
(rustc_middle/src/traits/mod.rs) changed — no logic, applicability, or span changes.

Added a regression test using the issue's exact example
(tests/ui/dyn-compatibility/static-constructor-prevents-dyn-no-api-guidance.rs).

Verified against a locally-built rustc; full tests/ui suite passes
(21643 passed, 0 failed). Note: next-solver compare-mode has pre-existing,
unrelated failures on 3 tests in this directory, confirmed present on main
without this diff.

…sociated functions

Explains that a method without `self` cannot be dispatched through
a trait object's vtable, and that `where Self: Sized` explicitly
opts a method out of dyn-compatibility requirements.

Fixes rust-lang#159492
Copilot AI review requested due to automatic review settings August 3, 2026 10:28
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 3, 2026
@rustbot

rustbot commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @khyperia (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 75 candidates
  • Random selection from 19 candidates

@rustbot

rustbot commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR improves the dyn-compatibility diagnostic guidance for receiver-less associated functions by expanding the two existing “no self parameter” help suggestions to explain why each suggested fix works, and adds a regression test based on the reported issue example.

Changes:

  • Expand the “add &self” suggestion to explain vtable accessibility for trait objects.
  • Expand the “add where Self: Sized” suggestion to clarify it explicitly excludes the item from applying to trait objects.
  • Update UI stderr expectations across affected tests and add a dedicated regression test case.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated no comments.

Show a summary per file
File Description
compiler/rustc_middle/src/traits/mod.rs Updates the two help suggestion strings emitted for the “no self parameter” dyn-compatibility violation.
tests/ui/dyn-compatibility/static-constructor-prevents-dyn-no-api-guidance.rs Adds a regression test reproducing the issue’s minimal example.
tests/ui/dyn-compatibility/static-constructor-prevents-dyn-no-api-guidance.stderr New expected output validating the expanded help messages in a real-world scenario.
tests/ui/traits/object/safety.stderr Updates expected help text to match the expanded diagnostic strings.
tests/ui/traits/object/canonicalize-fresh-infer-vars-issue-103626.stderr Updates expected help text to match the expanded diagnostic strings.
tests/ui/traits/missing-for-type-in-impl.e2015.stderr Updates expected help text to match the expanded diagnostic strings.
tests/ui/traits/issue-72410.stderr Updates expected help text to match the expanded diagnostic strings.
tests/ui/suggestions/dyn-incompatible-trait-should-use-where-sized.stderr Updates expected help text to match the expanded diagnostic strings.
tests/ui/suggestions/dyn-incompatible-trait-should-use-self.stderr Updates expected help text to match the expanded diagnostic strings.
tests/ui/suggestions/dyn-incompatible-trait-should-use-self-2021.stderr Updates expected help text to match the expanded diagnostic strings.
tests/ui/statics/unsizing-wfcheck-issue-127299.stderr Updates expected help text to match the expanded diagnostic strings (both occurrences).
tests/ui/parallel-rustc/recursive-trait-fn-sig-issue-142064.stderr Updates expected help text to match the expanded diagnostic strings (both occurrences).
tests/ui/impl-trait/dyn-incompatible-trait-in-return-position-dyn-trait.stderr Updates expected help text to match the expanded diagnostic strings (all occurrences).
tests/ui/dyn-compatibility/taint-const-eval.stderr Updates expected help text to match the expanded diagnostic strings.
tests/ui/dyn-compatibility/spurious-dyn-compat-errors-58734.stderr Updates expected help text to match the expanded diagnostic strings.
tests/ui/dyn-compatibility/no-static.stderr Updates expected help text to match the expanded diagnostic strings (both occurrences).
tests/ui/dyn-compatibility/dyn-incompat-const-slice.stderr Updates expected help text to match the expanded diagnostic strings (both occurrences).
tests/ui/dyn-compatibility/avoid-ice-on-warning-3.old.stderr Updates expected help text to match the expanded diagnostic strings.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Be more pedagogic on why assoc fns without receiver aren't dyn-safe

4 participants