Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

== operator does not support subtyping #27949

Closed
daboross opened this issue Aug 23, 2015 · 6 comments · Fixed by #58728
Closed

== operator does not support subtyping #27949

daboross opened this issue Aug 23, 2015 · 6 comments · Fixed by #58728
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@daboross
Copy link
Contributor

Here's an example showing this behavior:

struct Input<'a> {
    foo: &'a u32
}

impl <'a> std::cmp::PartialEq<Input<'a>> for Input<'a> {
    fn eq(&self, other: &Input<'a>) -> bool {
        self.foo == other.foo
    }

    fn ne(&self, other: &Input<'a>) -> bool {
        self.foo != other.foo
    }
}


// same error if no lifetime parameters are used, but I'm using explicit lifetime parameters to try to show the actual error
fn bar<'a, 'b>(x: Input<'a>, y: Input<'b>) -> bool {
    x == y
}

(https://play.rust-lang.org/?gist=c7b5d06f19fd345eef91)

Errors with:

<anon>:17:5: 17:11 error: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
<anon>:17     x == y
              ^~~~~~
<anon>:16:1: 18:2 help: consider using an explicit lifetime parameter as shown: fn bar<'a>(x: Input<'a>, y: Input<'a>) -> bool
<anon>:16 fn bar(x: Input, y: Input) -> bool {
<anon>:17     x == y
<anon>:18 }
error: aborting due to previous error

Using impl <'a, 'b> std::cmp::PartialEq<Input<'b>> for Input<'a> works, but it is confusing why the below allows lifetime elision while the above doesn't. I would think it would make sense for the rust compiler to merge the minimum of two lifetimes ('a and 'b) automatically when using == if PartialEq is only implemented for one shared lifetime.

impl <'a, 'b> std::cmp::PartialEq<Input<'b>> for Input<'a> {
    fn eq(&self, other: &Input<'b>) -> bool {
        self.foo == other.foo
    }

    fn ne(&self, other: &Input<'b>) -> bool {
        self.foo != other.foo
    }
}
@daboross daboross changed the title #[derive(PartialEq)] on structs with references kills lifetime elision on functions using == Implementing PartialEq for a struct with a lifetime parameter using the same lifetime for T stops lifetime elision Aug 23, 2015
@eefriedman
Copy link
Contributor

(Ignore this; the comment was completely off-base.)

@steveklabnik
Copy link
Member

/cc @rust-lang/lang

@nikomatsakis nikomatsakis changed the title Implementing PartialEq for a struct with a lifetime parameter using the same lifetime for T stops lifetime elision == operator does not support subtyping Sep 4, 2015
@nikomatsakis
Copy link
Contributor

I've taken the liberty of renaming the issue to be more precise. The problem is that the == operator, as implemented now, performs a precise match on the type of the LHS and RHS, rather than permitting subtyping. This means that the two lifetimes must match exactly. This is why you get an error when your two references have distinct lifetimes. The compiler could fairly probably introduce subtyping; I think it probably should, since the intuition is that a == b is the same as PartialEq::eq(&a, &b), except that if you write the latter, you'll find the program type-checks: http://is.gd/36eVCQ

@steveklabnik
Copy link
Member

Triage: no change

@steveklabnik steveklabnik added T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed A-lang labels Mar 24, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jul 22, 2017
@Centril Centril added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. and removed C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Feb 18, 2019
@Centril
Copy link
Contributor

Centril commented Feb 18, 2019

The linked playground in the issue compiles fine now; all that is left to do is to add a compile-pass test for it.

jamwt added a commit to jamwt/rust that referenced this issue Feb 25, 2019
@jamwt
Copy link

jamwt commented Feb 25, 2019

As detailed in the test comment, this was specifically fixed by 1a7fb7d.

Centril added a commit to Centril/rust that referenced this issue Feb 25, 2019
Test that binop subtyping in rustc_typeck fixes rust-lang#27949
Centril added a commit to Centril/rust that referenced this issue Feb 25, 2019
Test that binop subtyping in rustc_typeck fixes rust-lang#27949
bors added a commit that referenced this issue Feb 25, 2019
Rollup of 10 pull requests

Successful merges:

 - #55632 (Deny the `overflowing_literals` lint for all editions)
 - #58687 (Reduce Miri Code Repetition like `(n << amt) >> amt`)
 - #58690 (Reduce a Code Repetition like `(n << amt) >> amt`)
 - #58718 (Apply docs convention: Replace # Unsafety with # Safety in docs)
 - #58719 (librustc_codegen_llvm: #![deny(elided_lifetimes_in_paths)])
 - #58720 (librustc_codegen_ssa: #![deny(elided_lifetimes_in_paths)])
 - #58722 (librustc_typeck: deny(elided_lifetimes_in_paths))
 - #58723 (librustc: deny(elided_lifetimes_in_paths))
 - #58725 (Test that binop subtyping in rustc_typeck fixes #27949)
 - #58727 (bootstrap: deny(rust_2018_idioms))

Failed merges:

r? @ghost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants