Created
December 20, 2018 13:10
-
-
Save pnkfelix/8dbe7ab864cbc6ac65164af06abe66be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(nll, type_ascription)] | |
#![allow(dead_code, unused_mut)] | |
type Pair<T> = (T, T); | |
pub fn swap<T>(_: &mut T, _: &mut T) { unimplemented!() } | |
// This case should pass rustc's checks. | |
fn ensure_interning_not_on_types_alone<'a>(_x: &'a u32, s: &'static u32) -> &'static u32 { | |
// This one should definitely pass: the type of `&local` should | |
// not have any influence on the type of `y` here. | |
let local = 3; | |
let (mut y, _): Pair<&u32> = (s, s); | |
let (_, _): Pair<&u32> = (s, &local); | |
y | |
} | |
macro_rules! one_type_two_lets { | |
(type $t:ty; let $p1:pat = $e1:expr; let $p2:pat = $e2:expr;) => { | |
let $p1: $t = $e1; | |
let $p2: $t = $e2; | |
} | |
} | |
// This case should pass rustc's checks. | |
fn ensure_interning_not_based_on_spans<'a>(_x: &'a u32, s: &'static u32) -> &'static u32 { | |
// Since ensure_interning_not_on_types_alone passes, this variant | |
// should also pass. | |
let local = 3; | |
one_type_two_lets!{ | |
type Pair<&u32>; | |
let (mut y, _) = (s, s); | |
let (_, _) = (s, &local); | |
} | |
y | |
} | |
// This case should fail rustc's checks. | |
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); | |
// 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 constraint_is_only_on_wildcard<'a>(_x: &'a u32, s: &'static u32) -> &'static u32 { | |
let local = 3; | |
let _: Pair<&u32> = (s, &local); | |
s | |
} | |
fn constraint_is_half_on_wildcard<'a>(_x: &'a u32, s: &'static u32) -> &'static u32 { | |
let local = 3; | |
let (y, _): Pair<&u32> = (s, &local); | |
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