Fix fallout from changes. In cases where stage0 compiler is needed, we

cannot use an `as` expression to coerce, so I used a one-off function
instead (this is a no-op in stage0, but in stage1+ it triggers
coercion from the fn pointer to the fn item type).
This commit is contained in:
Niko Matsakis 2014-11-26 05:52:16 -05:00
parent 211782fef5
commit 7f6177e646
19 changed files with 120 additions and 60 deletions

View File

@ -30,6 +30,7 @@ use iter::range;
use kinds::Sized; use kinds::Sized;
use mem; use mem;
use num::Int; use num::Int;
use ops::FnMut;
use option::Option; use option::Option;
use option::Option::{None, Some}; use option::Option::{None, Some};
use ops::{Fn, FnMut}; use ops::{Fn, FnMut};

View File

@ -65,17 +65,21 @@ const HOEDOWN_EXTENSIONS: libc::c_uint =
type hoedown_document = libc::c_void; // this is opaque to us type hoedown_document = libc::c_void; // this is opaque to us
type blockcodefn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
*const hoedown_buffer, *mut libc::c_void);
type headerfn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
libc::c_int, *mut libc::c_void);
#[repr(C)] #[repr(C)]
struct hoedown_renderer { struct hoedown_renderer {
opaque: *mut hoedown_html_renderer_state, opaque: *mut hoedown_html_renderer_state,
blockcode: Option<extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer, blockcode: Option<blockcodefn>,
*const hoedown_buffer, *mut libc::c_void)>,
blockquote: Option<extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer, blockquote: Option<extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
*mut libc::c_void)>, *mut libc::c_void)>,
blockhtml: Option<extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer, blockhtml: Option<extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
*mut libc::c_void)>, *mut libc::c_void)>,
header: Option<extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer, header: Option<headerfn>,
libc::c_int, *mut libc::c_void)>,
other: [libc::size_t, ..28], other: [libc::size_t, ..28],
} }
@ -281,8 +285,8 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
toc_builder: if print_toc {Some(TocBuilder::new())} else {None} toc_builder: if print_toc {Some(TocBuilder::new())} else {None}
}; };
(*(*renderer).opaque).opaque = &mut opaque as *mut _ as *mut libc::c_void; (*(*renderer).opaque).opaque = &mut opaque as *mut _ as *mut libc::c_void;
(*renderer).blockcode = Some(block); (*renderer).blockcode = Some(block as blockcodefn);
(*renderer).header = Some(header); (*renderer).header = Some(header as headerfn);
let document = hoedown_document_new(renderer, HOEDOWN_EXTENSIONS, 16); let document = hoedown_document_new(renderer, HOEDOWN_EXTENSIONS, 16);
hoedown_document_render(document, ob, s.as_ptr(), hoedown_document_render(document, ob, s.as_ptr(),
@ -354,8 +358,8 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
unsafe { unsafe {
let ob = hoedown_buffer_new(DEF_OUNIT); let ob = hoedown_buffer_new(DEF_OUNIT);
let renderer = hoedown_html_renderer_new(0, 0); let renderer = hoedown_html_renderer_new(0, 0);
(*renderer).blockcode = Some(block); (*renderer).blockcode = Some(block as blockcodefn);
(*renderer).header = Some(header); (*renderer).header = Some(header as headerfn);
(*(*renderer).opaque).opaque = tests as *mut _ as *mut libc::c_void; (*(*renderer).opaque).opaque = tests as *mut _ as *mut libc::c_void;
let document = hoedown_document_new(renderer, HOEDOWN_EXTENSIONS, 16); let document = hoedown_document_new(renderer, HOEDOWN_EXTENSIONS, 16);

View File

@ -829,8 +829,12 @@ impl Path {
let s = if self.has_nonsemantic_trailing_slash() { let s = if self.has_nonsemantic_trailing_slash() {
self.repr.slice_to(self.repr.len()-1) self.repr.slice_to(self.repr.len()-1)
} else { self.repr.as_slice() }; } else { self.repr.as_slice() };
let idx = s.rfind(if !prefix_is_verbatim(self.prefix) { is_sep } let sep_test: fn(char) -> bool = if !prefix_is_verbatim(self.prefix) {
else { is_sep_verbatim }); is_sep
} else {
is_sep_verbatim
};
let idx = s.rfind(sep_test);
let prefixlen = self.prefix_len(); let prefixlen = self.prefix_len();
self.sepidx = idx.and_then(|x| if x < prefixlen { None } else { Some(x) }); self.sepidx = idx.and_then(|x| if x < prefixlen { None } else { Some(x) });
} }
@ -1048,7 +1052,11 @@ fn parse_prefix<'a>(mut path: &'a str) -> Option<PathPrefix> {
// None result means the string didn't need normalizing // None result means the string didn't need normalizing
fn normalize_helper<'a>(s: &'a str, prefix: Option<PathPrefix>) -> (bool, Option<Vec<&'a str>>) { fn normalize_helper<'a>(s: &'a str, prefix: Option<PathPrefix>) -> (bool, Option<Vec<&'a str>>) {
let f = if !prefix_is_verbatim(prefix) { is_sep } else { is_sep_verbatim }; let f: fn(char) -> bool = if !prefix_is_verbatim(prefix) {
is_sep
} else {
is_sep_verbatim
};
let is_abs = s.len() > prefix_len(prefix) && f(s.char_at(prefix_len(prefix))); let is_abs = s.len() > prefix_len(prefix) && f(s.char_at(prefix_len(prefix)));
let s_ = s.slice_from(prefix_len(prefix)); let s_ = s.slice_from(prefix_len(prefix));
let s_ = if is_abs { s_.slice_from(1) } else { s_ }; let s_ = if is_abs { s_.slice_from(1) } else { s_ };

View File

@ -189,11 +189,12 @@ macro_rules! __thread_local_inner {
} }
}; };
#[cfg(not(any(target_os = "macos", target_os = "linux")))] #[cfg(all(stage0, not(any(target_os = "macos", target_os = "linux"))))]
const INIT: ::std::thread_local::KeyInner<$t> = { const INIT: ::std::thread_local::KeyInner<$t> = {
unsafe extern fn __destroy(ptr: *mut u8) { unsafe extern fn __destroy(ptr: *mut u8) {
::std::thread_local::destroy_value::<$t>(ptr); ::std::thread_local::destroy_value::<$t>(ptr);
} }
::std::thread_local::KeyInner { ::std::thread_local::KeyInner {
inner: ::std::cell::UnsafeCell { value: $init }, inner: ::std::cell::UnsafeCell { value: $init },
os: ::std::thread_local::OsStaticKey { os: ::std::thread_local::OsStaticKey {
@ -203,6 +204,21 @@ macro_rules! __thread_local_inner {
} }
}; };
#[cfg(all(not(stage0), not(any(target_os = "macos", target_os = "linux"))))]
const INIT: ::std::thread_local::KeyInner<$t> = {
unsafe extern fn __destroy(ptr: *mut u8) {
::std::thread_local::destroy_value::<$t>(ptr);
}
::std::thread_local::KeyInner {
inner: ::std::cell::UnsafeCell { value: $init },
os: ::std::thread_local::OsStaticKey {
inner: ::std::thread_local::OS_INIT_INNER,
dtor: ::std::option::Option::Some(__destroy as unsafe extern fn(*mut u8)),
},
}
};
INIT INIT
}); });
} }
@ -323,6 +339,12 @@ mod imp {
// *should* be the case that this loop always terminates because we // *should* be the case that this loop always terminates because we
// provide the guarantee that a TLS key cannot be set after it is // provide the guarantee that a TLS key cannot be set after it is
// flagged for destruction. // flagged for destruction.
#[cfg(not(stage0))]
static DTORS: os::StaticKey = os::StaticKey {
inner: os::INIT_INNER,
dtor: Some(run_dtors as unsafe extern "C" fn(*mut u8)),
};
#[cfg(stage0)]
static DTORS: os::StaticKey = os::StaticKey { static DTORS: os::StaticKey = os::StaticKey {
inner: os::INIT_INNER, inner: os::INIT_INNER,
dtor: Some(run_dtors), dtor: Some(run_dtors),

View File

@ -50,14 +50,16 @@ pub trait ItemDecorator {
push: |P<ast::Item>|); push: |P<ast::Item>|);
} }
impl ItemDecorator for fn(&mut ExtCtxt, Span, &ast::MetaItem, &ast::Item, |P<ast::Item>|) { impl<F> ItemDecorator for F
where F : Fn(&mut ExtCtxt, Span, &ast::MetaItem, &ast::Item, |P<ast::Item>|)
{
fn expand(&self, fn expand(&self,
ecx: &mut ExtCtxt, ecx: &mut ExtCtxt,
sp: Span, sp: Span,
meta_item: &ast::MetaItem, meta_item: &ast::MetaItem,
item: &ast::Item, item: &ast::Item,
push: |P<ast::Item>|) { push: |P<ast::Item>|) {
self.clone()(ecx, sp, meta_item, item, push) (*self)(ecx, sp, meta_item, item, push)
} }
} }
@ -70,14 +72,16 @@ pub trait ItemModifier {
-> P<ast::Item>; -> P<ast::Item>;
} }
impl ItemModifier for fn(&mut ExtCtxt, Span, &ast::MetaItem, P<ast::Item>) -> P<ast::Item> { impl<F> ItemModifier for F
where F : Fn(&mut ExtCtxt, Span, &ast::MetaItem, P<ast::Item>) -> P<ast::Item>
{
fn expand(&self, fn expand(&self,
ecx: &mut ExtCtxt, ecx: &mut ExtCtxt,
span: Span, span: Span,
meta_item: &ast::MetaItem, meta_item: &ast::MetaItem,
item: P<ast::Item>) item: P<ast::Item>)
-> P<ast::Item> { -> P<ast::Item> {
self.clone()(ecx, span, meta_item, item) (*self)(ecx, span, meta_item, item)
} }
} }
@ -93,13 +97,15 @@ pub trait TTMacroExpander {
pub type MacroExpanderFn = pub type MacroExpanderFn =
for<'cx> fn(&'cx mut ExtCtxt, Span, &[ast::TokenTree]) -> Box<MacResult+'cx>; for<'cx> fn(&'cx mut ExtCtxt, Span, &[ast::TokenTree]) -> Box<MacResult+'cx>;
impl TTMacroExpander for MacroExpanderFn { impl<F> TTMacroExpander for F
where F : for<'cx> Fn(&'cx mut ExtCtxt, Span, &[ast::TokenTree]) -> Box<MacResult+'cx>
{
fn expand<'cx>(&self, fn expand<'cx>(&self,
ecx: &'cx mut ExtCtxt, ecx: &'cx mut ExtCtxt,
span: Span, span: Span,
token_tree: &[ast::TokenTree]) token_tree: &[ast::TokenTree])
-> Box<MacResult+'cx> { -> Box<MacResult+'cx> {
self.clone()(ecx, span, token_tree) (*self)(ecx, span, token_tree)
} }
} }
@ -115,14 +121,18 @@ pub trait IdentMacroExpander {
pub type IdentMacroExpanderFn = pub type IdentMacroExpanderFn =
for<'cx> fn(&'cx mut ExtCtxt, Span, ast::Ident, Vec<ast::TokenTree>) -> Box<MacResult+'cx>; for<'cx> fn(&'cx mut ExtCtxt, Span, ast::Ident, Vec<ast::TokenTree>) -> Box<MacResult+'cx>;
impl IdentMacroExpander for IdentMacroExpanderFn { impl<F> IdentMacroExpander for F
where F : for<'cx> Fn(&'cx mut ExtCtxt, Span, ast::Ident,
Vec<ast::TokenTree>) -> Box<MacResult+'cx>
{
fn expand<'cx>(&self, fn expand<'cx>(&self,
cx: &'cx mut ExtCtxt, cx: &'cx mut ExtCtxt,
sp: Span, sp: Span,
ident: ast::Ident, ident: ast::Ident,
token_tree: Vec<ast::TokenTree> ) token_tree: Vec<ast::TokenTree> )
-> Box<MacResult+'cx> { -> Box<MacResult+'cx>
self.clone()(cx, sp, ident, token_tree) {
(*self)(cx, sp, ident, token_tree)
} }
} }

View File

@ -20,7 +20,7 @@ impl X {
} }
fn main() { fn main() {
let mut x = X(Either::Right(main)); let mut x = X(Either::Right(main as fn()));
(&mut x).with( (&mut x).with(
|opt| { //~ ERROR cannot borrow `x` as mutable more than once at a time |opt| { //~ ERROR cannot borrow `x` as mutable more than once at a time
match opt { match opt {

View File

@ -13,7 +13,7 @@ fn foo(_x: int) { }
fn main() { fn main() {
let v: u64 = 5; let v: u64 = 5;
let x = foo as extern "C" fn() -> int; let x = foo as extern "C" fn() -> int;
//~^ ERROR non-scalar cast //~^ ERROR mismatched types
let y = v as extern "Rust" fn(int) -> (int, int); let y = v as extern "Rust" fn(int) -> (int, int);
//~^ ERROR non-scalar cast //~^ ERROR non-scalar cast
y(x()); y(x());

View File

@ -8,12 +8,21 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
// Test that coercions from fn item types are ok, but not fn pointer
// types to closures/procs are not allowed.
fn foo() {} fn foo() {}
fn main() { fn fn_item_type() {
let f = foo; let f = foo;
let f_closure: || = f; let f_closure: || = f;
//~^ ERROR: cannot coerce non-statically resolved bare fn to closure
//~^^ HELP: consider embedding the function in a closure
} }
fn fn_pointer_type() {
let f = foo as fn();
let f_closure: || = f;
//~^ ERROR: mismatched types
}
fn main() { }

View File

@ -12,4 +12,4 @@ fn f(_: extern "Rust" fn()) {}
extern fn bar() {} extern fn bar() {}
fn main() { f(bar) } fn main() { f(bar) }
//~^ ERROR: expected `fn()`, found `extern "C" fn()` //~^ ERROR mismatched types

View File

@ -10,6 +10,6 @@
#[start] #[start]
fn start(argc: int, argv: *const *const u8, crate_map: *const u8) -> int { fn start(argc: int, argv: *const *const u8, crate_map: *const u8) -> int {
//~^ ERROR start function expects type: `fn(int, *const *const u8) -> int` //~^ ERROR incorrect number of function parameters
0 0
} }

View File

@ -15,7 +15,7 @@ fn a<'a, 'b:'a>(x: &mut &'a int, y: &mut &'b int) {
fn b<'a, 'b>(x: &mut &'a int, y: &mut &'b int) { fn b<'a, 'b>(x: &mut &'a int, y: &mut &'b int) {
// Illegal now because there is no `'b:'a` declaration. // Illegal now because there is no `'b:'a` declaration.
*x = *y; //~ ERROR mismatched types *x = *y; //~ ERROR cannot infer
} }
fn c<'a,'b>(x: &mut &'a int, y: &mut &'b int) { fn c<'a,'b>(x: &mut &'a int, y: &mut &'b int) {

View File

@ -12,10 +12,10 @@ fn ignore<T>(t: T) {}
fn nested<'x>(x: &'x int) { fn nested<'x>(x: &'x int) {
let y = 3; let y = 3;
let mut ay = &y; //~ ERROR cannot infer let mut ay = &y;
ignore::< for<'z>|&'z int|>(|z| { ignore::< for<'z>|&'z int|>(|z| {
ay = x; ay = x; //~ ERROR cannot infer
ay = &y; ay = &y;
ay = z; ay = z;
}); });

View File

@ -24,7 +24,7 @@ fn foo() -> Option<int> {
fn create() -> A<'static> { fn create() -> A<'static> {
A { A {
func: &foo, //~ ERROR borrowed value does not live long enough func: &foo, //~ ERROR mismatched types
} }
} }

View File

@ -9,9 +9,11 @@
// except according to those terms. // except according to those terms.
struct StateMachineIter<'a> { struct StateMachineIter<'a> {
statefn: &'a fn(&mut StateMachineIter<'a>) -> Option<&'static str> statefn: &'a StateMachineFunc<'a>
} }
type StateMachineFunc<'a> = fn(&mut StateMachineIter<'a>) -> Option<&'static str>;
impl<'a> Iterator<&'static str> for StateMachineIter<'a> { impl<'a> Iterator<&'static str> for StateMachineIter<'a> {
fn next(&mut self) -> Option<&'static str> { fn next(&mut self) -> Option<&'static str> {
return (*self.statefn)(self); return (*self.statefn)(self);
@ -19,19 +21,19 @@ impl<'a> Iterator<&'static str> for StateMachineIter<'a> {
} }
fn state1(self_: &mut StateMachineIter) -> Option<&'static str> { fn state1(self_: &mut StateMachineIter) -> Option<&'static str> {
self_.statefn = &state2; self_.statefn = &(state2 as StateMachineFunc);
//~^ ERROR borrowed value does not live long enough //~^ ERROR borrowed value does not live long enough
return Some("state1"); return Some("state1");
} }
fn state2(self_: &mut StateMachineIter) -> Option<(&'static str)> { fn state2(self_: &mut StateMachineIter) -> Option<(&'static str)> {
self_.statefn = &state3; self_.statefn = &(state3 as StateMachineFunc);
//~^ ERROR borrowed value does not live long enough //~^ ERROR borrowed value does not live long enough
return Some("state2"); return Some("state2");
} }
fn state3(self_: &mut StateMachineIter) -> Option<(&'static str)> { fn state3(self_: &mut StateMachineIter) -> Option<(&'static str)> {
self_.statefn = &finished; self_.statefn = &(finished as StateMachineFunc);
//~^ ERROR borrowed value does not live long enough //~^ ERROR borrowed value does not live long enough
return Some("state3"); return Some("state3");
} }
@ -42,7 +44,7 @@ fn finished(_: &mut StateMachineIter) -> Option<(&'static str)> {
fn state_iter() -> StateMachineIter<'static> { fn state_iter() -> StateMachineIter<'static> {
StateMachineIter { StateMachineIter {
statefn: &state1 //~ ERROR borrowed value does not live long enough statefn: &(state1 as StateMachineFunc) //~ ERROR borrowed value does not live long enough
} }
} }

View File

@ -50,20 +50,20 @@ pub fn bar() {
((::std::fmt::format as ((::std::fmt::format as
fn(&core::fmt::Arguments<'_>) -> collections::string::String)((&((::std::fmt::Arguments::new fn(&core::fmt::Arguments<'_>) -> collections::string::String {std::fmt::format})((&((::std::fmt::Arguments::new
as as
fn(&[&str], &[core::fmt::Argument<'_>]) -> core::fmt::Arguments<'_>)((__STATIC_FMTSTR fn(&[&str], &[core::fmt::Argument<'_>]) -> core::fmt::Arguments<'_> {core::fmt::Arguments<'a>::new})((__STATIC_FMTSTR
as as
&'static [&'static str]), &'static [&'static str]),
(&([] (&([]
as as
[core::fmt::Argument<'_>; 0]) [core::fmt::Argument<'_>; 0])
as as
&[core::fmt::Argument<'_>; 0])) &[core::fmt::Argument<'_>; 0]))
as as
core::fmt::Arguments<'_>) core::fmt::Arguments<'_>)
as as
&core::fmt::Arguments<'_>)) &core::fmt::Arguments<'_>))
as collections::string::String) as collections::string::String)
} }
} as collections::string::String); } as collections::string::String);
@ -78,7 +78,8 @@ pub fn id<T>(x: T) -> T { (x as T) }
pub fn use_id() { pub fn use_id() {
let _ = let _ =
((id::<[int; (3u as uint)]> as ((id::<[int; (3u as uint)]> as
fn([int; 3]) -> [int; 3])(([(1 as int), (2 as int), (3 as int)] fn([int; 3]) -> [int; 3] {id})(([(1 as int), (2 as int),
as [int; 3])) as [int; 3]); (3 as int)] as [int; 3])) as
[int; 3]);
} }
fn main() { } fn main() { }

View File

@ -18,6 +18,6 @@ struct S {
} }
pub fn main() { pub fn main() {
assert!(foopy == f); assert!(foopy as extern "C" fn() == f);
assert!(f == s.f); assert!(f == s.f);
} }

View File

@ -18,15 +18,17 @@ extern fn uintret() -> uint { 22 }
extern fn uintvoidret(_x: uint) {} extern fn uintvoidret(_x: uint) {}
extern fn uintuintuintuintret(x: uint, y: uint, z: uint) -> uint { x+y+z } extern fn uintuintuintuintret(x: uint, y: uint, z: uint) -> uint { x+y+z }
type uintuintuintuintret = extern fn(uint,uint,uint) -> uint;
pub fn main() { pub fn main() {
assert!(voidret1 == voidret1); assert!(voidret1 as extern fn() == voidret1 as extern fn());
assert!(voidret1 != voidret2); assert!(voidret1 as extern fn() != voidret2 as extern fn());
assert!(uintret == uintret); assert!(uintret as extern fn() -> uint == uintret as extern fn() -> uint);
assert!(uintvoidret == uintvoidret); assert!(uintvoidret as extern fn(uint) == uintvoidret as extern fn(uint));
assert!(uintuintuintuintret == uintuintuintuintret); assert!(uintuintuintuintret as uintuintuintuintret ==
uintuintuintuintret as uintuintuintuintret);
} }

View File

@ -12,5 +12,5 @@
pub fn main() { pub fn main() {
fn f() { fn f() {
}; };
let _: Box<fn()> = box f; let _: Box<fn()> = box() (f as fn());
} }

View File

@ -25,5 +25,6 @@ fn thing(a: int, b: int) -> int {
} }
fn main() { fn main() {
let thing: fn(int, int) -> int = thing; // coerce to fn type
bar(&thing); bar(&thing);
} }