Suggest impl Trait for References to Bare Trait in Function Header by veera-sivarajan · Pull Request #127692 · rust-lang/rust (original) (raw)

Choose a reason for hiding this comment

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

I special cased &mut because the current suggestion for fn code() -> &mut Trait looks like:

7 | fn code() -> &'static mut Trait { | +++++++

7 | fn code() -> &mut impl Trait { | ++++

(Note: The 'static lifetime suggestion is from E0106 and it's complicated to fix that because it happens in rustc_resolve.)

I thought the current suggestion might be very confusing for someone new to Rust than this proposed suggestion:

error[E0782]: cannot return a mutable reference to a bare trait --> $DIR/reference-to-bare-trait-in-fn-inputs-and-outputs-issue-125139.rs:44:25 | LL | fn parrot() -> &mut Trait { | ^^^^^ | help: use impl Trait to return an opaque type, as long as you return a single underlying type | LL | fn parrot() -> impl Trait { | ~~~~~~~~~~

But I agree that special casing for lifetime is more complicated than necessary. Maybe I can keep the case 6 branch and remove the cases 4 and 5 branch?