Skip to content

Example in instantiating_binders.md does not compile #2397

Open
@smanilov

Description

@smanilov

I'm not sure it's supposed to compile, but this example:

fn foo<'a>(a: &'a u32) -> &'a u32 {
    a
}
fn bar<T>(a: fn(&u32) -> T) -> T {
    a(&10)
}

fn main() {
    let higher_ranked_fn_ptr = foo as for<'a> fn(&'a u32) -> &'a u32;
    let references_bound_vars = bar(higher_ranked_fn_ptr);
}

Gives me the following diagnostic:

error[E0308]: mismatched types
  --> src/main.rs:10:37
   |
10 |     let references_bound_vars = bar(higher_ranked_fn_ptr);
   |                                 --- ^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
   |                                 |
   |                                 arguments to this function are incorrect
   |
   = note: expected fn pointer `for<'a> fn(&'a _) -> _`
              found fn pointer `for<'a> fn(&'a _) -> &'a u32`
note: function defined here
  --> src/main.rs:4:4
   |
4  | fn bar<T>(a: fn(&u32) -> T) -> T {
   |    ^^^    ----------------

For more information about this error, try `rustc --explain E0308`.
error: could not compile `foo` (bin "foo") due to 1 previous error

Is the example supposed to compile? I don't grok the type system quite enough to answer this myself, sorry.

Page is here: https://rustc-dev-guide.rust-lang.org/ty_module/instantiating_binders.html

Metadata

Metadata

Assignees

Labels

A-examplesArea: examplesC-bugCategory: bugI-confusingIssue: info is confusing to readersT-compilerRelevant to compiler team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions