Code `&impl A + B`, which cannot compile, may be suggested by the lint `refining_impl_trait`
Open
Nobody has claimed this yet.
A-diagnostics
A-lints
D-invalid-suggestion
L-refining_impl_trait
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
pub trait Foo {
fn bar(&self) -> &(impl BarA + BarB);
}
pub trait BarA {}
impl<T> BarA for T {}
pub trait BarB {}
impl<T> BarB for T {}
pub struct Fool;
impl Foo for Fool {
fn bar(&self) -> &Fool {
&Fool
}
}
Current output
warning: impl trait in impl method signature does not match trait method signature
--> src\lib.rs:14:22
|
2 | fn bar(&self) -> &(impl BarA + BarB);
| ------------------- return type from trait method defined here
...
14 | fn bar(&self) -> &Fool {
| ^^^^^
|
= note: add `#[allow(refining_impl_trait)]` if it is intended for this to be part of the public API of this crate
= note: we are soliciting feedback, see issue #121718 <https://github.com/rust-lang/rust/issues/121718> for more information
= note: `#[warn(refining_impl_trait_reachable)]` on by default
help: replace the return type so that it matches the trait
|
14 - fn bar(&self) -> &Fool {
14 + fn bar(&self) -> &impl BarA + BarB {
|
Desired output
warning: impl trait in impl method signature does not match trait method signature
--> src\lib.rs:14:22
|
2 | fn bar(&self) -> &(impl BarA + BarB);
| ------------------- return type from trait method defined here
...
14 | fn bar(&self) -> &Fool {
| ^^^^^
|
= note: add `#[allow(refining_impl_trait)]` if it is intended for this to be part of the public API of this crate
= note: we are soliciting feedback, see issue #121718 <https://github.com/rust-lang/rust/issues/121718> for more information
= note: `#[warn(refining_impl_trait_reachable)]` on by default
help: replace the return type so that it matches the trait
|
14 - fn bar(&self) -> &Fool {
14 + fn bar(&self) -> &(impl BarA + BarB) {
|
Rationale and extra context
After applying the fix suggested in the current output, a compilation will give an error:
error: ambiguous `+` in a type
--> src\lib.rs:14:23
|
14 | fn bar(&self) -> &impl BarA + BarB {
| ^^^^^^^^^^^^^^^^
|
help: try adding parentheses
|
14 | fn bar(&self) -> &(impl BarA + BarB) {
| + +
Users can follow the hint to add missing parentheses. It is still an improvement to avoid the invalid suggestion though.
Other cases
Rust Version
rustc 1.88.0 (6b00bc388 2025-06-23)
binary: rustc
commit-hash: 6b00bc3880198600130e1cf62b8f8a93494488cc
commit-date: 2025-06-23
host: x86_64-pc-windows-gnu
release: 1.88.0
LLVM version: 20.1.5
Anything else?
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the refining_impl_trait lint with the Rust code and compiler version shown in the issue. Trace the lint's help suggestion generation, update it to preserve parentheses around impl BarA + BarB, and verify that the suggested code compiles without the ambiguous-+ error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100