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

Trait coercion may ask for a static bound #11612

Closed
alexcrichton opened this issue Jan 17, 2014 · 5 comments

Comments

@alexcrichton
Copy link
Member

commented Jan 17, 2014

trait A {}                    
struct B<'a, T> { f: &'a T }  
impl<'a, T> A for B<'a, T> {} 

fn foo(_: &A) {}              

fn bar<T>(b: &B<T>) { foo(b) }

fn main() {}                  

Compiles with:

foo.rs:7:27: 7:28 error: value may contain references; add `'static` bound
foo.rs:7 fn bar<T>(b: &B<T>) { foo(b) }
                                   ^
error: aborting due to previous error

If I change it to foo(b as &A) the code compiles just fine. Perhaps a coercion bug?

cc @pcwalton, @luqmana

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

commented Feb 10, 2014

cc me

@gavinb

This comment has been minimized.

Copy link
Contributor

commented May 14, 2014

Just had this same problem, in a slightly different situation. In my failing code, the trait is templated, and the struct is empty. It's just for a function wrapper. Same error, and casting it explicitly to the trait type works. If it makes a difference, there are type bounds on T in the impll but none in the trait.

I can try to reduce the code to something simpler and standalone to post if that would help.

@rustonaut

This comment has been minimized.

Copy link

commented Jun 6, 2014

I have the same problem, but with Box insted of & (and a slightly different setup)

trait A<X:Eq> { 
    fn foo(&self, _: X) -> bool; 
}

struct E<T> { f: T } 
impl<T:Eq> A<T> for E<T> { 
    fn foo(&self, r: T) -> bool { self.f == r } 
}

struct B<T>;
impl<T:Eq> B<T> {
    fn check(&self, _ : Box<A<T>>) {}
    fn bar(&self,r:T) {
        let matcher = box E { f: r };
        self.check(matcher);
    }   
}

fn main(){}

won't compile unless I change self.check(matcher); to self.check(matcher as Box<A<T>>);
it fails with the same error message:

is11612.rs:16:20: 16:27 error: value may contain references; add `'static` bound to `T`
is11612.rs:16         self.check(matcher);
@luqmana

This comment has been minimized.

Copy link
Member

commented Jun 6, 2014

@Naicode Yep, that's the same issue. I have a pull which should hopefully land sometime today to fix it.

@alexcrichton

This comment has been minimized.

Copy link
Member Author

commented Jun 13, 2014

Closed by #14682

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.