Skip to content

Instantly share code, notes, and snippets.

@pnkfelix
Created December 19, 2018 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pnkfelix/759ae8412b56839f57aab5ae90d1ca1a to your computer and use it in GitHub Desktop.
Save pnkfelix/759ae8412b56839f57aab5ae90d1ca1a to your computer and use it in GitHub Desktop.
#![feature(nll, type_ascription)]
#![allow(dead_code, unused_mut)]
type Pair<T> = (T, T);
pub fn swap<T>(_: &mut T, _: &mut T) { unimplemented!() }
fn static_to_a_to_static_through_tyvar<'a>(_x: &'a u32, s: &'static u32) -> &'static u32 {
let local = 3;
let (mut y, mut _z): Pair<&u32> = (s, &local);
// let (mut y, mut _z): Pair<&u32> = (s, &local);
// I should be able to add the call to `swap` below at whim based
// on the above type annotation, which should coerce both `s` and
// `local` to `&'1 u32` (and then `'1` should be inferred to be a
// function-local region).
//
// Furthermore, since I should be able to add the below call to
// `swap`, it should also be legal for me to include the `unsafe`
// code block given below, which has the same end effect of
// swapping the two values. (And that *definitely* will break the
// return type.)
// swap(&mut y, &mut _z);
#[cfg(not_now)]
unsafe {
let p_y = &mut y as *mut _;
let p_z = &mut _z as *mut _;
let t = *p_y;
*p_y = *p_z;
*p_z = t;
}
// Likewise, the same rules that caused `y` and `_z` to have the
// same `&'1 u32` type should likewise cause a borrow-check error
// at this attempt to return a `&'static u32`.
y
}
fn main() {
static_to_a_to_static_through_tyvar(&3, &4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment