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

Unable to infer trait's type parameter when using static method of trait #3902

Closed
burg opened this issue Nov 1, 2012 · 11 comments
Closed

Unable to infer trait's type parameter when using static method of trait #3902

burg opened this issue Nov 1, 2012 · 11 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system A-type-system Area: Type system E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. P-low Low priority

Comments

@burg
Copy link

burg commented Nov 1, 2012

The following should compile correctly AFAIK, but the type parameter isn't being properly inferred.

Updated Testcase

use std::io::println;

mod base {
    pub trait HasNew<T> {
        #[cfg(buggy)]
        fn new() -> T;
        #[cfg(fixed)]
        fn new() -> Self; // (see niko's first comment in ticket)
    }

    #[deriving(Show)]
    pub struct Foo {
        dummy: (),
    }

    impl HasNew<Foo> for Foo {
        fn new() -> Foo {
            Foo { dummy: () }
        }
    }

    pub struct Bar {
        dummy: (),
    }

    impl HasNew<Bar> for Bar {
        fn new() -> Bar {
            Bar { dummy: () }
        }
    }
}

fn main() {
    // 1. You cannot say this:
    // let f: base::Foo = base::HasNew<base::Foo>::new();

    // 2. There are discussions to provide some way to say
    // something like this (see Issue 6894):
    // let f: base::Foo = base::HasNew<base::Foo, for base::Foo>::new();

    // 3. But for now, one must rely on the type inference system
    // to feed the appropriate type via the expression context in which
    // the invocation of `new` occurs:
    let f: base::Foo = base::HasNew::new();
    println(format!("{}", f).as_slice());
}

Original Outdated Testcase

mod base {
    trait HasNew<T> {
        static pure fn new() -> T;
    }

    pub struct Foo {
        dummy: (),
    }

    pub impl Foo : HasNew<Foo> {
        static pure fn new() -> Foo {
            Foo { dummy: () }
        }
    }

    pub struct Bar {
        dummy: (),
    }

    pub impl Bar : HasNew<Bar> {
        static pure fn new() -> Bar {
            Bar { dummy: () }
        }
    }
}

fn main() {
    let f: base::Foo = base::new::<base::Foo, base::Foo>();
    debug!("%?", f);
}

Error

% rustc --cfg buggy /tmp/base.rs
/tmp/base.rs:42:23: 42:40 error: cannot determine a type for this bounded type parameter: unconstrained type
/tmp/base.rs:42     let f: base::Foo = base::HasNew::new();
                                       ^~~~~~~~~~~~~~~~~

@nikomatsakis
Copy link
Contributor

The problem is the error message. You should use -> self and not -> T. In this call, the parameter T is constrained but not self.

@catamorphism
Copy link
Contributor

Bumping to 0.7

@catamorphism
Copy link
Contributor

Seems not critical for 0.7. Nominating for milestone 5, production-ready.

@graydon
Copy link
Contributor

graydon commented May 9, 2013

accepted for production-ready milestone

@metajack
Copy link
Contributor

revisited for triage; nothing to add

@pnkfelix
Copy link
Member

pnkfelix commented Sep 4, 2013

visiting for triage, email from 2013-09-02.

Nothing to add beyond updating the test case's syntax (which I'm doing now).

@pnkfelix
Copy link
Member

pnkfelix commented Sep 4, 2013

(#8888 is related, although that is about explicitly providing the Self type parameter, while this ticket is about inferring it.)

@pnkfelix
Copy link
Member

assigning P-low.

@reem
Copy link
Contributor

reem commented Sep 3, 2014

Triage bump. Doesn't seem super-critical as this can currently be done. I have run in to some (I think) related issues with the limits of type inference while implementing rust-plugin.

@ghost
Copy link

ghost commented Nov 10, 2014

Updated test case passes now, e-needstest.

@ghost ghost added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Nov 10, 2014
@bors bors closed this as completed in 3725067 Dec 30, 2014
@nikomatsakis
Copy link
Contributor

Note that this was compiling but in an unreliable way. There are still no constraints on Self. Newer work on associated types makes this fail to compile (and I will move the test to compile-fail accordingly).

RalfJung pushed a commit to RalfJung/rust that referenced this issue Sep 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system A-type-system Area: Type system E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. P-low Low priority
Projects
None yet
Development

No branches or pull requests

7 participants