Implement the Fn trait for bare fn pointers in the compiler rather than doing it using hard-coded impls. This means that it works also for more complex fn types involving bound regions. Fixes #19126.

This commit is contained in:
Niko Matsakis 2014-12-01 09:23:40 -05:00
parent 207a508411
commit 39221a013f
15 changed files with 446 additions and 78 deletions

View file

@ -11,8 +11,6 @@
fn main() {
return
{ return () } //~ ERROR the type of this value must be known in this context
() //~^ ERROR the type of this value must be known in this context
//~^^ ERROR notation; the first type parameter for the function trait is neither a tuple nor unit
//~^^^ ERROR overloaded calls are experimental
()
;
}

View file

@ -17,6 +17,4 @@
fn main() {
(return)((),());
//~^ ERROR the type of this value must be known
//~^^ ERROR the type of this value must be known
//~^^^ ERROR cannot use call notation
}

View file

@ -0,0 +1,28 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Tests that unsafe extern fn pointers do not implement any Fn traits.
#![feature(unboxed_closures)]
use std::ops::{Fn,FnMut,FnOnce};
unsafe fn square(x: &int) -> int { (*x) * (*x) }
fn call_it<F:Fn(&int)->int>(_: &F, _: int) -> int { 0 }
fn call_it_mut<F:FnMut(&int)->int>(_: &mut F, _: int) -> int { 0 }
fn call_it_once<F:FnOnce(&int)->int>(_: F, _: int) -> int { 0 }
fn main() {
let x = call_it(&square, 22); //~ ERROR not implemented
let y = call_it_mut(&mut square, 22); //~ ERROR not implemented
let z = call_it_once(square, 22); //~ ERROR not implemented
}

View file

@ -0,0 +1,28 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Tests that unsafe extern fn pointers do not implement any Fn traits.
#![feature(unboxed_closures)]
use std::ops::{Fn,FnMut,FnOnce};
extern "C" fn square(x: &int) -> int { (*x) * (*x) }
fn call_it<F:Fn(&int)->int>(_: &F, _: int) -> int { 0 }
fn call_it_mut<F:FnMut(&int)->int>(_: &mut F, _: int) -> int { 0 }
fn call_it_once<F:FnOnce(&int)->int>(_: F, _: int) -> int { 0 }
fn main() {
let x = call_it(&square, 22); //~ ERROR not implemented
let y = call_it_mut(&mut square, 22); //~ ERROR not implemented
let z = call_it_once(square, 22); //~ ERROR not implemented
}

View file

@ -0,0 +1,29 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Tests that unsafe extern fn pointers do not implement any Fn traits.
#![feature(unboxed_closures)]
use std::ops::{Fn,FnMut,FnOnce};
unsafe fn square(x: int) -> int { x * x }
// note: argument type here is `int`, not `&int`
fn call_it<F:Fn(&int)->int>(_: &F, _: int) -> int { 0 }
fn call_it_mut<F:FnMut(&int)->int>(_: &mut F, _: int) -> int { 0 }
fn call_it_once<F:FnOnce(&int)->int>(_: F, _: int) -> int { 0 }
fn main() {
let x = call_it(&square, 22); //~ ERROR not implemented
let y = call_it_mut(&mut square, 22); //~ ERROR not implemented
let z = call_it_once(square, 22); //~ ERROR not implemented
}

View file

@ -0,0 +1,45 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Checks that higher-ranked extern fn pointers implement the full range of Fn traits.
#![feature(unboxed_closures)]
use std::ops::{Fn,FnMut,FnOnce};
fn square(x: &int) -> int { (*x) * (*x) }
fn call_it<F:Fn(&int)->int>(f: &F, x: int) -> int {
(*f)(&x)
}
fn call_it_boxed(f: &Fn(&int) -> int, x: int) -> int {
f.call((&x,))
}
fn call_it_mut<F:FnMut(&int)->int>(f: &mut F, x: int) -> int {
(*f)(&x)
}
fn call_it_once<F:FnOnce(&int)->int>(f: F, x: int) -> int {
f(&x)
}
fn main() {
let x = call_it(&square, 22);
let x1 = call_it_boxed(&square, 22);
let y = call_it_mut(&mut square, 22);
let z = call_it_once(square, 22);
assert_eq!(x, square(&22));
assert_eq!(x1, square(&22));
assert_eq!(y, square(&22));
assert_eq!(z, square(&22));
}

View file

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Checks that extern fn points implement the full range of Fn traits.
// Checks that extern fn pointers implement the full range of Fn traits.
#![feature(unboxed_closures)]
#![feature(unboxed_closures)]