Skip to content

Implementation of traits on functions ignored after function call #31730

@sophiajt

Description

@sophiajt

In this example, I implement a trait on generic functions. One with mutability and one without. When invoked manually, the proper trait gets called. Instead, if I try to peel off the function's unique tag using another function, the traits are no longer visible.

Checked against: rustc 1.8.0-nightly (57c357d 2016-02-16)

trait FooFn {
    fn foo(&self);
}

impl<T: Sized> FooFn for fn(&mut T)-> T {
    fn foo(&self) {
        println!("Generic (with mutable) match!");
    }
}


impl<T: Sized> FooFn for fn(T)->T {
    fn foo(&self) {
        println!("Generic match!");

    }    
}

fn simple_fn(x: i32) -> i32 { 1 }
fn simple_fn2(x: &mut i32) -> i32 { 2 }

fn strip<T, U>(x: fn(T)->U) -> fn(T)->U { x }

fn main() {
    //Works
    &(simple_fn as fn(i32)->i32).foo(); 
    //Also works
    &(simple_fn2 as fn(&mut i32)->i32).foo();

    //Works
    strip(simple_fn).foo();
    //Error: no method named `foo` found for type `fn(&mut i32) -> i32`
    strip(simple_fn2).foo();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions