Created
December 4, 2014 22:01
-
-
Save abonander/b1047d5ab238bd8468d4 to your computer and use it in GitHub Desktop.
Fooling the Borrowchecker with `Vec` and `HashMap`, pretty-printed. Source: https://github.com/rust-lang/rust/issues/19537
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(phase)] | |
#![no_std] | |
#![feature(globs)] | |
#[phase(plugin, link)] | |
extern crate "std" as std; | |
#[prelude_import] | |
use std::prelude::*; | |
use std::collections::HashMap; | |
struct Foo<'a> { | |
buf: Vec<String>, | |
map: HashMap<uint, &'a str>, | |
} | |
#[automatically_derived] | |
impl <'a> ::std::fmt::Show for Foo<'a> { | |
fn fmt(&self, __arg_0: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { | |
(match (*(self as &Foo<'a>) as Foo<'a>) { | |
Foo { buf: ref __self_0_0, map: ref __self_0_1 } => | |
(match (((&(((*(__self_0_0 as | |
&collections::vec::Vec<collections::string::String>) | |
as | |
collections::vec::Vec<collections::string::String>)) | |
as | |
collections::vec::Vec<collections::string::String>) | |
as | |
&collections::vec::Vec<collections::string::String>), | |
(&(((*(__self_0_1 as | |
&std::collections::hash::map::HashMap<uint, &str>) | |
as | |
std::collections::hash::map::HashMap<uint, &str>)) | |
as | |
std::collections::hash::map::HashMap<uint, &str>) | |
as | |
&std::collections::hash::map::HashMap<uint, &str>)) | |
as | |
(&collections::vec::Vec<collections::string::String>, &std::collections::hash::map::HashMap<uint, &str>)) | |
{ | |
(__arg0, __arg1) => { | |
#[inline] | |
#[allow(dead_code)] | |
static __STATIC_FMTSTR: &'static [&'static str] = | |
(&([("Foo { buf: " as &'static str), | |
(", map: " as &'static str), | |
(" }" as &'static str)] as [&'static str, ..3]) | |
as &'static [&'static str, ..3]); | |
let __args_vec = | |
(&([((::std::fmt::argument as | |
fn(fn(&collections::vec::Vec<collections::string::String>, &mut core::fmt::Formatter<'_>) -> core::result::Result<(), core::fmt::Error>, &'a collections::vec::Vec<collections::string::String>) -> core::fmt::Argument<'a>)((::std::fmt::Show::fmt | |
as | |
fn(&collections::vec::Vec<collections::string::String>, &mut core::fmt::Formatter<'_>) -> core::result::Result<(), core::fmt::Error>), | |
(__arg0 | |
as | |
&collections::vec::Vec<collections::string::String>)) | |
as core::fmt::Argument<'_>), | |
((::std::fmt::argument as | |
fn(fn(&std::collections::hash::map::HashMap<uint, &str>, &mut core::fmt::Formatter<'_>) -> core::result::Result<(), core::fmt::Error>, &'a std::collections::hash::map::HashMap<uint, &str>) -> core::fmt::Argument<'a>)((::std::fmt::Show::fmt | |
as | |
fn(&std::collections::hash::map::HashMap<uint, &str>, &mut core::fmt::Formatter<'_>) -> core::result::Result<(), core::fmt::Error>), | |
(__arg1 | |
as | |
&std::collections::hash::map::HashMap<uint, &str>)) | |
as core::fmt::Argument<'_>)] as | |
[core::fmt::Argument<'_>, ..2]) as | |
&[core::fmt::Argument<'_>, ..2]); | |
let __args = | |
(unsafe { | |
((::std::fmt::Arguments::new as | |
unsafe fn(&'static [&'static str], &'a [core::fmt::Argument<'a>]) -> core::fmt::Arguments<'a>)((__STATIC_FMTSTR | |
as | |
&'static [&'static str]), | |
(__args_vec | |
as | |
&[core::fmt::Argument<'_>, ..2])) | |
as core::fmt::Arguments<'_>) | |
} as core::fmt::Arguments<'_>); | |
((__arg_0 as | |
&mut core::fmt::Formatter<'_>).write_fmt((&(__args | |
as | |
core::fmt::Arguments<'_>) | |
as | |
&core::fmt::Arguments<'_>)) | |
as core::result::Result<(), core::fmt::Error>) | |
} | |
} as core::result::Result<(), core::fmt::Error>), | |
} as core::result::Result<(), core::fmt::Error>) | |
} | |
} | |
impl <'a> Foo<'a> { | |
fn new() -> Foo<'a> { | |
(Foo{buf: | |
((Vec::new as | |
fn() -> collections::vec::Vec<collections::string::String>)() | |
as collections::vec::Vec<collections::string::String>), | |
map: | |
((HashMap::new as | |
fn() -> std::collections::hash::map::HashMap<uint, &'a str>)() | |
as std::collections::hash::map::HashMap<uint, &'a str>),} | |
as Foo<'a>) | |
} | |
fn insert(&'a mut self, s: String) { | |
(((self as &'a mut Foo<'a>).buf as | |
collections::vec::Vec<collections::string::String>).push((s as | |
collections::string::String)) | |
as ()); | |
(match (((self as &'a mut Foo<'a>).buf as | |
collections::vec::Vec<collections::string::String>).last() | |
as core::option::Option<&'a collections::string::String>) { | |
None => { | |
static _FILE_LINE: (&'static str, uint) = | |
((("borrowchk.rs" as &'static str), (16u as uint)) as | |
(&'static str, uint)); | |
((::std::rt::begin_unwind as | |
fn(&'static str, &(&'static str, uint)) -> !)(("" as | |
&'static str), | |
(&(_FILE_LINE | |
as | |
(&'static str, uint)) | |
as | |
&(&'static str, uint))) | |
as ()) | |
} | |
Some(x) => { | |
(((self as &'a mut Foo<'a>).map as | |
std::collections::hash::map::HashMap<uint, &'a str>).insert(((x | |
as | |
&'a collections::string::String).len() | |
as | |
uint), | |
((x | |
as | |
&'a collections::string::String).as_slice() | |
as | |
&'a str)) | |
as core::option::Option<&'a str>); | |
} | |
} as ()) | |
} | |
fn do_bad_stuff(&mut self) { | |
(((self as &mut Foo<'a>).buf as | |
collections::vec::Vec<collections::string::String>).clear() as | |
()); | |
} | |
} | |
fn main() { | |
let mut foo = ((Foo::new as fn() -> Foo<'_>)() as Foo<'_>); | |
((foo as | |
Foo<'_>).insert((("bad" as &'static str).into_string() as | |
collections::string::String)) as ()); | |
((foo as | |
Foo<'_>).insert((("stuff" as &'static str).into_string() as | |
collections::string::String)) as ()); | |
(match (((&(foo as Foo<'_>) as &Foo<'_>),) as (&Foo<'_>,)) { | |
(__arg0,) => { | |
#[inline] | |
#[allow(dead_code)] | |
static __STATIC_FMTSTR: &'static [&'static str] = | |
(&([("" as &'static str)] as [&'static str, ..1]) as | |
&'static [&'static str, ..1]); | |
let __args_vec = | |
(&([((::std::fmt::argument as | |
fn(fn(&Foo<'_>, &mut core::fmt::Formatter<'_>) -> core::result::Result<(), core::fmt::Error>, &'a Foo<'_>) -> core::fmt::Argument<'a>)((::std::fmt::Show::fmt | |
as | |
fn(&Foo<'_>, &mut core::fmt::Formatter<'_>) -> core::result::Result<(), core::fmt::Error>), | |
(__arg0 | |
as | |
&Foo<'_>)) | |
as core::fmt::Argument<'_>)] as | |
[core::fmt::Argument<'_>, ..1]) as | |
&[core::fmt::Argument<'_>, ..1]); | |
let __args = | |
(unsafe { | |
((::std::fmt::Arguments::new as | |
unsafe fn(&'static [&'static str], &'a [core::fmt::Argument<'a>]) -> core::fmt::Arguments<'a>)((__STATIC_FMTSTR | |
as | |
&'static [&'static str]), | |
(__args_vec | |
as | |
&[core::fmt::Argument<'_>, ..1])) | |
as core::fmt::Arguments<'_>) | |
} as core::fmt::Arguments<'_>); | |
((::std::io::stdio::println_args as | |
fn(&core::fmt::Arguments<'_>))((&(__args as | |
core::fmt::Arguments<'_>) | |
as | |
&core::fmt::Arguments<'_>)) | |
as ()) | |
} | |
} as ()); | |
((foo as Foo<'_>).do_bad_stuff() as ()); | |
(match (((&(foo as Foo<'_>) as &Foo<'_>),) as (&Foo<'_>,)) { | |
(__arg0,) => { | |
#[inline] | |
#[allow(dead_code)] | |
static __STATIC_FMTSTR: &'static [&'static str] = | |
(&([("" as &'static str)] as [&'static str, ..1]) as | |
&'static [&'static str, ..1]); | |
let __args_vec = | |
(&([((::std::fmt::argument as | |
fn(fn(&Foo<'_>, &mut core::fmt::Formatter<'_>) -> core::result::Result<(), core::fmt::Error>, &'a Foo<'_>) -> core::fmt::Argument<'a>)((::std::fmt::Show::fmt | |
as | |
fn(&Foo<'_>, &mut core::fmt::Formatter<'_>) -> core::result::Result<(), core::fmt::Error>), | |
(__arg0 | |
as | |
&Foo<'_>)) | |
as core::fmt::Argument<'_>)] as | |
[core::fmt::Argument<'_>, ..1]) as | |
&[core::fmt::Argument<'_>, ..1]); | |
let __args = | |
(unsafe { | |
((::std::fmt::Arguments::new as | |
unsafe fn(&'static [&'static str], &'a [core::fmt::Argument<'a>]) -> core::fmt::Arguments<'a>)((__STATIC_FMTSTR | |
as | |
&'static [&'static str]), | |
(__args_vec | |
as | |
&[core::fmt::Argument<'_>, ..1])) | |
as core::fmt::Arguments<'_>) | |
} as core::fmt::Arguments<'_>); | |
((::std::io::stdio::println_args as | |
fn(&core::fmt::Arguments<'_>))((&(__args as | |
core::fmt::Arguments<'_>) | |
as | |
&core::fmt::Arguments<'_>)) | |
as ()) | |
} | |
} as ()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment