Skip to content

Trait coercion may ask for a static bound #11612

@alexcrichton

Description

@alexcrichton
Member
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

Activity

nikomatsakis

nikomatsakis commented on Feb 10, 2014

@nikomatsakis
Contributor

cc me

gavinb

gavinb commented on May 14, 2014

@gavinb
Contributor

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

rustonaut commented on Jun 6, 2014

@rustonaut

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

luqmana commented on Jun 6, 2014

@luqmana
Member

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

alexcrichton

alexcrichton commented on Jun 13, 2014

@alexcrichton
MemberAuthor

Closed by #14682

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @alexcrichton@nikomatsakis@gavinb@luqmana@rustonaut

      Issue actions

        Trait coercion may ask for a static bound · Issue #11612 · rust-lang/rust