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

Generic type alias: unable to access static fn #5635

Closed
jeaye opened this issue Mar 30, 2013 · 5 comments
Closed

Generic type alias: unable to access static fn #5635

jeaye opened this issue Mar 30, 2013 · 5 comments
Labels
A-frontend Area: Compiler frontend (errors, parsing and HIR)

Comments

@jeaye
Copy link

jeaye commented Mar 30, 2013

It seems a generic type alias does not carry over static function. (incoming as of 03-29-13)

pub mod foo
{
  pub struct Foo<T>
  {
    x: T
  }
  impl<T> Foo<T>
  {
    pub fn new(t: T) -> Foo<T>
    { Foo { x: t } }
  }

  pub type Foof = Foo<float>;
}

fn main()
{
  let f = foo::Foof::new(42.0f);
  io::println("Good");
}
@ben0x539
Copy link
Contributor

Well, it seems a regular type alias doesn't either:

struct Foo;

impl Foo {
    pub fn f() {}
}

type Bar = Foo;

fn main() {
    Bar::f();
}

fails with

foo.rs:10:4: 10:10 error: unresolved name
foo.rs:10     Bar::f();
              ^~~~~~
foo.rs:10:4: 10:10 error: use of undeclared module `Bar`
foo.rs:10     Bar::f();
              ^~~~~~
foo.rs:10:4: 10:10 error: unresolved name: `Bar::f`.
foo.rs:10     Bar::f();
              ^~~~~~
error: aborting due to 3 previous errors

No idea what the intended behavior is there.

@brendanzab
Copy link
Member

I believe this is intended behavior. My work-around is to create a wrapper. Eg:

impl Bar {
    #[inline(always)]
    pub fn new(t: float) -> Bar { Foo::new(t) }
}

This could then be turned into a macro. I'm thinking something like this in my own library:

macro_rules! vec2_type(
    ($name:ident < $T:ty >) => (
        type $name = Vec2<$T>;

        impl $name {
            wrap_fn!(Vector2::new(x: $T ,y: $T) -> $name)
            wrap_fn!(Vector::from_value(v: $T) -> $name)
            wrap_fn!(NumericVector::identity() -> $name)
            wrap_fn!(NumericVector::zero() -> $name)
            wrap_fn!(NumericVector2::unit_x() -> $name)
            wrap_fn!(NumericVector2::unit_y() -> $name)

            wrap_fn!(dim() -> uint { 2 })
            wrap_fn!(size_of() -> uint { size_of::<$name>() })
        }
    )
)

pub vec2_type!(vec2<f32>)
pub vec2_type!(dvec2<f64>)
pub vec2_type!(ivec2<i32>)
pub vec2_type!(uvec2<u32>)

Unfortunately you then run into lots of unfixed macro issues, like #4375 and #4621. For now I've done them by hand.

Edit: I've now half-implemented the wrapper impl macro. I can't complete it because of #4375, but all in good time. :)

@bstrie
Copy link
Contributor

bstrie commented May 20, 2013

@bjz Do you happen to know why this intended?

@catamorphism
Copy link
Contributor

Type aliases don't work this way -- a type alias defines a name in the type namespace. The thing before ::new has to be a module name or a trait name. Closing.

@ben0x539
Copy link
Contributor

Couldn't/shouldn't a type alias also define a module in the module namespace when the type that is being aliased corresponds to a module containing static methods from an impl T {}?

flip1995 pushed a commit to flip1995/rust that referenced this issue May 27, 2020
…e, r=flip1995

option_option test case rust-lang#4298

Adds regression test case for rust-lang#4298.

The bug seems still present although rust Playground said otherwise.

changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-frontend Area: Compiler frontend (errors, parsing and HIR)
Projects
None yet
Development

No branches or pull requests

5 participants