Add more context to dyn-compatibility diagnostic for receiver-less associated functions - #160428
Add more context to dyn-compatibility diagnostic for receiver-less associated functions#160428xyz-harshal wants to merge 1 commit into
Conversation
…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
|
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 (
Why was this reviewer chosen?The reviewer was selected based on:
|
|
There was a problem hiding this comment.
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.
Fixes #159492
Expands the two suggestion messages for the "no
selfparameter"dyn-compatibility violation to explain why each fix works, per the issue.
Before:
help: consider turning
createinto a method by giving it a&selfargumenthelp: alternatively, consider constraining
createso it does not apply to trait objectsAfter:
help: consider turning
createinto a method by giving it a&selfargument, so that it is accessible through the trait object's vtablehelp: alternatively, consider constraining
createso it is explicitly marked as not applying to trait objectsOnly 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/uisuite passes(21643 passed, 0 failed). Note:
next-solvercompare-mode has pre-existing,unrelated failures on 3 tests in this directory, confirmed present on
mainwithout this diff.