Skip to content

Inconsistent Self behavior with generic structs #69306

Closed

Description

Summary from @pnkfelix : Self used as a tuple-struct constructor function should use the fully type-substituted form from the impl block, but does not.

Thus, today this code is accepted, but it should not be.

struct _Bar<T>(T);

impl<T> _Bar<T> {
    fn _map1<U>(x: U) -> _Bar<U> {
        Self(x)
    }
}

(Fixing this would be a breaking-change, so we should take care in how we deploy it.)

Original bug report follows:


I tried this code:

struct _Foo<T> {
    x: T
}

impl<T> _Foo<T> {
    fn _map<U>(x: U) -> _Foo<U> {
        Self { x }
    }
}

struct _Bar<T>(T);

impl<T> _Bar<T> {
    fn _map<U>(x: U) -> _Bar<U> {
        Self(x)
    }
}

In nightly rustc, _Bar compiles while _Foo yields a type mismatch error:

error[E0308]: mismatched types
  --> src/main.rs:10:16
   |
8  | impl<T> _Foo<T> {
   |      - expected type parameter
9  |     fn _map<U>(x: U) -> _Foo<U> {
   |             - found type parameter
10 |         Self { x }
   |                ^ expected type parameter `T`, found type parameter `U`
   |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

C-bugCategory: This is a bug.P-highHigh priorityT-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