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

Associated type fails to typecheck against a reference parameter to a function (but not a function pointer or closure) #79207

Closed
fleabitdev opened this issue Nov 19, 2020 · 2 comments
Labels
A-associated-items Area: Associated items such as associated types and consts. A-typesystem Area: The type system C-bug Category: This is a bug.

Comments

@fleabitdev
Copy link

Rust 1.47.0-stable and 1.50.0-nightly (2020-11-18 8256379832b5ecb7f71e), both tested on the playground.

trait Example {
    type Ty;
}

impl<'a> Example for &'a i32 {
    type Ty = &'a i32;
}

fn example<F: Fn(<&i32 as Example>::Ty)>(f: F) {  }

fn main() {
    example(&|i: &i32| println!("{}", *i));
    example(|i: &i32| println!("{}", *i));

    fn print_integer(i: &i32) { println!("{}", *i); }
    example(&print_integer);
    example(*&print_integer); //fails
    example(print_integer); //fails
}

The first three calls to example succeed, but the last two calls fail with this error:

error[E0631]: type mismatch in function arguments
  --> src/main.rs:18:13
   |
9  | fn example<F: Fn(<&i32 as Example>::Ty)>(f: F) {  }
   |               ------------------------- required by this bound in `example`
...
15 |     fn print_integer(i: &i32) { println!("{}", *i); }
   |     ------------------------- found signature of `for<'r> fn(&'r i32) -> _`
...
18 |     example(print_integer); //fails
   |   

#76956 may be related.

The error does not occur if...

  • The Fn(<&i32 as Example>::Ty) bound is replaced with Fn(&i32)
  • All occurrences of &i32 are replaced with i32
  • The example function is given a lifetime parameter which is otherwise ignored:
fn example<'a, F: Fn(<&'a i32 as Example>::Ty)>(f: F) {  }
  • The generic lifetime is replaced with a generic type:
trait Example {
    type Ty;
}

impl<T> Example for Vec<T> {
    type Ty = Vec<T>;
}

fn example<F: Fn(<Vec<u8> as Example>::Ty)>(f: F) {  }

fn main() {
    example(&|v: Vec<u8>| println!("{:?}", v));
    example(|v: Vec<u8>| println!("{:?}", v));

    fn print_bytes(v: Vec<u8>) { println!("{:?}", v); }
    example(&print_bytes);
    example(*&print_bytes);
    example(print_bytes);
}
@fleabitdev fleabitdev added the C-bug Category: This is a bug. label Nov 19, 2020
@camelid camelid added A-associated-items Area: Associated items such as associated types and consts. A-typesystem Area: The type system labels Nov 20, 2020
@camelid
Copy link
Member

camelid commented Nov 20, 2020

Note that this seems to have been an issue since 1.0.

bors added a commit to rust-lang-ci/rust that referenced this issue Aug 25, 2021
…komatsakis

Normalize projections under binders

Fixes rust-lang#70243
Fixes rust-lang#70120
Fixes rust-lang#62529
Fixes rust-lang#87219

Issues to followup on after (probably fixed, but no test added here):
rust-lang#76956
rust-lang#56556
rust-lang#79207
rust-lang#85636

r? `@nikomatsakis`
@jackh726
Copy link
Member

Fixed by #85499, closing due to other related tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items such as associated types and consts. A-typesystem Area: The type system C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants