Skip to content

Commit

Permalink
Update question 5 for universe transition
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jan 6, 2019
1 parent f59fa1c commit 6fd0e82
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
10 changes: 5 additions & 5 deletions docs/questions.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 24 additions & 13 deletions questions/005-trait-resolution-hrtb.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
Answer: 112
Difficulty: 2
Answer: error
Difficulty: 3

# Hint

If you are familiar with [higher-rank trait bound][hrtb] syntax, try desugaring
all the types in the impl signatures and types in `main` into their fully
explicit form.

[hrtb]: https://doc.rust-lang.org/nomicon/hrtb.html
The answer is different for Rust versions 1.0 through 1.32 vs 1.33+. The answer
accepted as correct here is the one for compilers 1.33+.

# Explanation

This is a rare example of a Rust program that *used to* compile. This code
compiles and runs successfully with every Rust version 1.0 through 1.32,
printing the output `112`. The reasoning on those compilers is as follows.

The first impl applies to function pointers of type `fn(T)` where `T` is any
single concrete type. The second impl applies to function pointers of
higher-ranked type `for<'a> fn(&'a T)` for some concrete type `T` that outlives
`'a`.
[higher-ranked] type `for<'a> fn(&'a T)` for some concrete type `T` that
outlives `'a`.

[higher-ranked]: https://doc.rust-lang.org/nomicon/hrtb.html

Inside of `main`, the compiler is going to use type inference to substitute all
occurrences of `_` in a type by some concrete type.

For the closure `a` we infer `_ = u8`, yielding the closure type `fn(u8)` taking
an argument of type `u8` and returning `()`.
For the function pointer `a` we infer `_ = u8`, yielding the function pointer
type `fn(u8)` taking an argument of type `u8` and returning `()`.

For `b` we infer `_ = &'x u8` for some concrete lifetime `'x` that will
ultimately feed into the borrow checker. The type of `b` is `fn(&'x u8)`.

And finally for `c` we infer `_ = u8`, yielding the higher-ranked closure type
`for<'a> fn(&'a u8)`.
And finally for `c` we infer `_ = u8`, yielding the higher-ranked function
pointer type `for<'a> fn(&'a u8)`.

Framed in this way, it follows that the trait method calls at the end of `main`
print `112`.

The compiler's reasoning changed in Rust version 1.33 as part of the ["universe
transition"] and this program no longer compiles. Under the new model the first
impl applies to all three function pointers. If the second impl didn't exist,
the program would compile and print `111`. But with both impls present these are
considered conflicting impls and the program fails to compile.

["universe transition"]: https://github.com/rust-lang/rust/issues/56105
21 changes: 12 additions & 9 deletions questions/005-trait-resolution-hrtb.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
trait Trait {
fn f(self);
fn p(self);
}

impl<T> Trait for fn(T) {
fn f(self) {
fn p(self) {
print!("1");
}
}

impl<T> Trait for fn(&T) {
fn f(self) {
fn p(self) {
print!("2");
}
}

fn f(_: u8) {}
fn g(_: &u8) {}

fn main() {
let a: fn(_) = |_: u8| {};
let b: fn(_) = |_: &u8| {};
let c: fn(&_) = |_: &u8| {};
a.f();
b.f();
c.f();
let a: fn(_) = f;
let b: fn(_) = g;
let c: fn(&_) = g;
a.p();
b.p();
c.p();
}

0 comments on commit 6fd0e82

Please sign in to comment.