-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.Diagnostics: A structured suggestion resulting in incorrect code.L-refining_impl_traitLint group: refining_impl_traitLint group: refining_impl_traitT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
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
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.Diagnostics: A structured suggestion resulting in incorrect code.L-refining_impl_traitLint group: refining_impl_traitLint group: refining_impl_traitT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.