Skip to content

Enhance compiler error for constraining builtin traits #93623

Closed
@Pzixel

Description

@Pzixel

Given the following code:

fn foo<T>(t: Option<T>) {
    let _ = t == t;
}

It gives a nice error:

error[E0369]: binary operation `==` cannot be applied to type `Option<T>`
 --> src/main.rs:6:15
  |
6 |     let _ = t == t;
  |             - ^^ - Option<T>
  |             |
  |             Option<T>
  |
help: consider restricting type parameter `T`
  |
5 | fn duplicate<T: std::cmp::PartialEq>(t: Option<T>) {
  |               +++++++++++++++++++++

OTOH it doesn't work for say Copy:

fn duplicate<T>(t: Option<T>) -> (Option<T>, Option<T>) {
    (t, t)
}

which gives a somewhat unhelpful message:

error[E0382]: use of moved value: `t`
 --> src/main.rs:6:9
  |
5 | fn duplicate<T>(t: Option<T>) -> (Option<T>, Option<T>) {
  |                 - move occurs because `t` has type `Option<T>`, which does not implement the `Copy` trait
6 |     (t, t)
  |      -  ^ value used here after move
  |      |
  |      value moved here

It would make sense to have this suggestion appearing here as well, e.g.

help: consider restricting type parameter `T`
  |
5 | fn duplicate<T: Copy>(t: Option<T>) {
  |               ++++++

Activity

WaffleLapkin

WaffleLapkin commented on Feb 3, 2022

@WaffleLapkin
Member

@rustbot label +A-diagnostics +A-suggestion-diagnostics +C-enhancement

added
A-diagnosticsArea: Messages for errors, warnings, and lints
A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
on Feb 3, 2022
Gordon01

Gordon01 commented on Feb 3, 2022

@Gordon01

Maybe also mention about .clone() possibility?

WaffleLapkin

WaffleLapkin commented on Feb 3, 2022

@WaffleLapkin
Member

@rustbot claim

added a commit that references this issue on Mar 1, 2022

Rollup merge of rust-lang#94375 - WaffleLapkin:copy-suggestion, r=est…

38b1f1d
added 3 commits that reference this issue on Mar 2, 2022

Rollup merge of rust-lang#94375 - WaffleLapkin:copy-suggestion, r=est…

dcadcef

Rollup merge of rust-lang#94375 - WaffleLapkin:copy-suggestion, r=est…

0faba1b

Rollup merge of rust-lang#94375 - WaffleLapkin:copy-suggestion, r=est…

e78acbd
added 3 commits that reference this issue on Mar 2, 2022

Rollup merge of rust-lang#94375 - WaffleLapkin:copy-suggestion, r=est…

4a6506a

Rollup merge of rust-lang#94375 - WaffleLapkin:copy-suggestion, r=est…

f5569b7

Rollup merge of rust-lang#94375 - WaffleLapkin:copy-suggestion, r=est…

0c40ca0

4 remaining items

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

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @Gordon01@Pzixel@WaffleLapkin@rustbot

    Issue actions

      Enhance compiler error for constraining builtin traits · Issue #93623 · rust-lang/rust