Skip to content

Very bad error message for missing trait #3480

Closed

Description

@jesse99

The following code is missing a cmp::Eq type constraint:

// rustc --lib equals.rs
type IMap<K: Copy, V: Copy> = ~[(K, V)];

trait ImmutableMap<K: Copy, V: Copy>
{
    pure fn contains_key(key: K) -> bool;
}

impl<K: Copy, V: Copy> IMap<K, V> : ImmutableMap<K, V>
{
    pure fn contains_key(key: K) -> bool
    {
        vec::find(self, |e| {e.first() == key}).is_some()
    }
}

But the error is extremely confusing (and doesn't get any better if you split the code apart into multiple lines and add type annotations):

equals.rs:13:36: 13:39 error: mismatched types: expected `&const <V41>` but found `'a` (expected &-ptr but found type parameter)
equals.rs:13        vec::find(self, |e| {e.first() == key}).is_some()
                                                 ^~~
equals.rs:13:23: 13:39 error: cannot determine a type for this bounded type parameter: unconstrained type
equals.rs:13        vec::find(self, |e| {e.first() == key}).is_some()
                                    ^~~~~~~~~~~~~~~~

Activity

  1. catamorphism commented on Dec 7, 2012

    @catamorphism
    Contributor

    This is slightly better ( with d2ad028 ) but I still don't understand why this is the error:

    ../src/test/run-pass/issue-3480.rs:13:29: 13:45 error: binary operation == cannot be applied to type `'a`
    ../src/test/run-pass/issue-3480.rs:13         vec::find(self, |e| {e.first() == key}).is_some()
                                                                       ^~~~~~~~~~~~~~~~
    ../src/test/run-pass/issue-3480.rs:13:28: 13:46 error: mismatched types: expected `bool` but found `'a` (expected bool but found type parameter)
    ../src/test/run-pass/issue-3480.rs:13         vec::find(self, |e| {e.first() == key}).is_some()
    
  2. jesse99 commented on Dec 7, 2012

    @jesse99
    ContributorAuthor

    That's quite a bit better: at least it points you to the operator having problems. My understanding is that it is an error because arbitrary generic types cannot be equated. You need to constrain them with the Eq trait which the code above does not do.

  3. pcwalton commented on Mar 23, 2013

    @pcwalton
    Contributor

    Closing.

  4. added a commit that references this issue on May 15, 2021
  5. added a commit that references this issue on Apr 20, 2024
  6. added a commit that references this issue on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions