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

Very bad error message for missing trait #3480

Closed
jesse99 opened this issue Sep 13, 2012 · 3 comments
Closed

Very bad error message for missing trait #3480

jesse99 opened this issue Sep 13, 2012 · 3 comments
Labels
A-traits Area: Trait system A-typesystem Area: The type system
Milestone

Comments

@jesse99
Copy link
Contributor

jesse99 commented Sep 13, 2012

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()
                                    ^~~~~~~~~~~~~~~~
@catamorphism
Copy link
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()

@jesse99
Copy link
Contributor Author

jesse99 commented Dec 7, 2012

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.

@ghost ghost assigned catamorphism Feb 21, 2013
@pcwalton
Copy link
Contributor

Closing.

bors pushed a commit to rust-lang-ci/rust that referenced this issue May 15, 2021
RalfJung pushed a commit to RalfJung/rust that referenced this issue Apr 20, 2024
directly call handle_alloc_error

Also test more codepaths. There's like 5 different things that can happen on allocation failure! Between `-Zoom`, `#[alloc_error_handler]`, and `set_alloc_error_hook`, we have 3 layers of behavior overrides. It's all a bit messy.

rust-lang#112331 seems intended to clean this up, but has not yet reached consensus.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-traits Area: Trait system A-typesystem Area: The type system
Projects
None yet
Development

No branches or pull requests

3 participants