Skip to content

Commit

Permalink
rollup merge of rust-lang#19891: nikomatsakis/unique-fn-types-3
Browse files Browse the repository at this point in the history
Conflicts:
	src/libcore/str.rs
	src/librustc_trans/trans/closure.rs
	src/librustc_typeck/collect.rs
	src/libstd/path/posix.rs
	src/libstd/path/windows.rs
  • Loading branch information
alexcrichton committed Dec 22, 2014
2 parents 459f3b2 + 763152b commit de11710
Show file tree
Hide file tree
Showing 73 changed files with 537 additions and 255 deletions.
2 changes: 2 additions & 0 deletions src/libcollections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,7 @@ impl<K, V> BTreeMap<K, V> {
#[unstable = "matches collection reform specification, waiting for dust to settle"]
pub fn keys<'a>(&'a self) -> Keys<'a, K, V> {
fn first<A, B>((a, _): (A, B)) -> A { a }
let first: fn((&'a K, &'a V)) -> &'a K = first; // coerce to fn pointer

Keys { inner: self.iter().map(first) }
}
Expand All @@ -1251,6 +1252,7 @@ impl<K, V> BTreeMap<K, V> {
#[unstable = "matches collection reform specification, waiting for dust to settle"]
pub fn values<'a>(&'a self) -> Values<'a, K, V> {
fn second<A, B>((_, b): (A, B)) -> B { b }
let second: fn((&'a K, &'a V)) -> &'a V = second; // coerce to fn pointer

Values { inner: self.iter().map(second) }
}
Expand Down
1 change: 1 addition & 0 deletions src/libcollections/btree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl<T> BTreeSet<T> {
#[unstable = "matches collection reform specification, waiting for dust to settle"]
pub fn into_iter(self) -> IntoIter<T> {
fn first<A, B>((a, _): (A, B)) -> A { a }
let first: fn((T, ())) -> T = first; // coerce to fn pointer

IntoIter { iter: self.map.into_iter().map(first) }
}
Expand Down
3 changes: 3 additions & 0 deletions src/libcollections/vec_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ impl<V> VecMap<V> {
#[unstable = "matches collection reform specification, waiting for dust to settle"]
pub fn keys<'r>(&'r self) -> Keys<'r, V> {
fn first<A, B>((a, _): (A, B)) -> A { a }
let first: fn((uint, &'r V)) -> uint = first; // coerce to fn pointer

Keys { iter: self.iter().map(first) }
}
Expand All @@ -153,6 +154,7 @@ impl<V> VecMap<V> {
#[unstable = "matches collection reform specification, waiting for dust to settle"]
pub fn values<'r>(&'r self) -> Values<'r, V> {
fn second<A, B>((_, b): (A, B)) -> B { b }
let second: fn((uint, &'r V)) -> &'r V = second; // coerce to fn pointer

Values { iter: self.iter().map(second) }
}
Expand Down Expand Up @@ -239,6 +241,7 @@ impl<V> VecMap<V> {
fn filter<A>((i, v): (uint, Option<A>)) -> Option<(uint, A)> {
v.map(|v| (i, v))
}
let filter: fn((uint, Option<V>)) -> Option<(uint, V)> = filter; // coerce to fn ptr

let values = replace(&mut self.v, vec!());
IntoIter { iter: values.into_iter().enumerate().filter_map(filter) }
Expand Down
3 changes: 3 additions & 0 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2612,6 +2612,9 @@ pub fn iterate<T, F>(seed: T, f: F) -> Iterate<T, F> where
val.clone()
}

// coerce to a fn pointer
let next: fn(&mut IterateState<T,F>) -> Option<T> = next;

Unfold::new((f, Some(seed), true), next)
}

Expand Down
1 change: 1 addition & 0 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,7 @@ impl StrExt for str {
else { line }
}

let f: fn(&str) -> &str = f; // coerce to fn pointer
LinesAny { inner: self.lines().map(f) }
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ pub fn get_enum_variants<'tcx>(intr: Rc<IdentInterner>, cdata: Cmd, id: ast::Nod
item, tcx, cdata);
let name = item_name(&*intr, item);
let (ctor_ty, arg_tys, arg_names) = match ctor_ty.sty {
ty::ty_bare_fn(ref f) =>
ty::ty_bare_fn(_, ref f) =>
(Some(ctor_ty), f.sig.0.inputs.clone(), None),
_ => { // Nullary or struct enum variant.
let mut arg_names = Vec::new();
Expand Down
6 changes: 5 additions & 1 deletion src/librustc/metadata/tydecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,11 @@ fn parse_ty<'a, 'tcx>(st: &mut PState<'a, 'tcx>, conv: conv_did) -> Ty<'tcx> {
return ty::mk_closure(st.tcx, parse_closure_ty(st, |x,y| conv(x,y)));
}
'F' => {
return ty::mk_bare_fn(st.tcx, parse_bare_fn_ty(st, |x,y| conv(x,y)));
let def_id = parse_def(st, NominalType, |x,y| conv(x,y));
return ty::mk_bare_fn(st.tcx, Some(def_id), parse_bare_fn_ty(st, |x,y| conv(x,y)));
}
'G' => {
return ty::mk_bare_fn(st.tcx, None, parse_bare_fn_ty(st, |x,y| conv(x,y)));
}
'#' => {
let pos = parse_hex(st);
Expand Down
7 changes: 6 additions & 1 deletion src/librustc/metadata/tyencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,13 @@ pub fn enc_ty<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>, t: Ty<'t
mywrite!(w, "f");
enc_closure_ty(w, cx, &**f);
}
ty::ty_bare_fn(ref f) => {
ty::ty_bare_fn(Some(def_id), ref f) => {
mywrite!(w, "F");
mywrite!(w, "{}|", (cx.ds)(def_id));
enc_bare_fn_ty(w, cx, f);
}
ty::ty_bare_fn(None, ref f) => {
mywrite!(w, "G");
enc_bare_fn_ty(w, cx, f);
}
ty::ty_infer(_) => {
Expand Down
25 changes: 20 additions & 5 deletions src/librustc/middle/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,14 +1007,21 @@ impl<'a, 'tcx> rbml_writer_helpers<'tcx> for Encoder<'a> {

self.emit_enum("AutoAdjustment", |this| {
match *adj {
ty::AdjustAddEnv(store) => {
this.emit_enum_variant("AutoAddEnv", 0, 1, |this| {
this.emit_enum_variant_arg(0, |this| store.encode(this))
ty::AdjustAddEnv(def_id, store) => {
this.emit_enum_variant("AdjustAddEnv", 0, 2, |this| {
this.emit_enum_variant_arg(0, |this| def_id.encode(this));
this.emit_enum_variant_arg(1, |this| store.encode(this))
})
}

ty::AdjustReifyFnPointer(def_id) => {
this.emit_enum_variant("AdjustReifyFnPointer", 1, 2, |this| {
this.emit_enum_variant_arg(0, |this| def_id.encode(this))
})
}

ty::AdjustDerefRef(ref auto_deref_ref) => {
this.emit_enum_variant("AutoDerefRef", 1, 1, |this| {
this.emit_enum_variant("AdjustDerefRef", 2, 2, |this| {
this.emit_enum_variant_arg(0,
|this| Ok(this.emit_auto_deref_ref(ecx, auto_deref_ref)))
})
Expand Down Expand Up @@ -1648,12 +1655,20 @@ impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> {
this.read_enum_variant(&variants, |this, i| {
Ok(match i {
0 => {
let def_id: ast::DefId =
this.read_def_id(dcx);
let store: ty::TraitStore =
this.read_enum_variant_arg(0, |this| Decodable::decode(this)).unwrap();

ty::AdjustAddEnv(store.tr(dcx))
ty::AdjustAddEnv(def_id, store.tr(dcx))
}
1 => {
let def_id: ast::DefId =
this.read_def_id(dcx);

ty::AdjustReifyFnPointer(def_id)
}
2 => {
let auto_deref_ref: ty::AutoDerefRef =
this.read_enum_variant_arg(0,
|this| Ok(this.read_auto_deref_ref(dcx))).unwrap();
Expand Down
6 changes: 5 additions & 1 deletion src/librustc/middle/check_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ fn check_expr(v: &mut CheckCrateVisitor, e: &ast::Expr) -> bool {
ast::ExprCast(ref from, _) => {
let toty = ty::expr_ty(v.tcx, e);
let fromty = ty::expr_ty(v.tcx, &**from);
if !ty::type_is_numeric(toty) && !ty::type_is_unsafe_ptr(toty) {
let is_legal_cast =
ty::type_is_numeric(toty) ||
ty::type_is_unsafe_ptr(toty) ||
(ty::type_is_bare_fn(toty) && ty::type_is_bare_fn_item(fromty));
if !is_legal_cast {
span_err!(v.tcx.sess, e.span, E0012,
"can not cast to `{}` in a constant expression",
ppaux::ty_to_string(v.tcx, toty));
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ enum UnsafeContext {

fn type_is_unsafe_function(ty: Ty) -> bool {
match ty.sty {
ty::ty_bare_fn(ref f) => f.unsafety == ast::Unsafety::Unsafe,
ty::ty_bare_fn(_, ref f) => f.unsafety == ast::Unsafety::Unsafe,
ty::ty_closure(ref f) => f.unsafety == ast::Unsafety::Unsafe,
_ => false,
}
Expand Down
10 changes: 6 additions & 4 deletions src/librustc/middle/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,10 +824,12 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
None => { }
Some(adjustment) => {
match *adjustment {
ty::AdjustAddEnv(..) => {
// Creating a closure consumes the input and stores it
// into the resulting rvalue.
debug!("walk_adjustment(AutoAddEnv)");
ty::AdjustAddEnv(..) |
ty::AdjustReifyFnPointer(..) => {
// Creating a closure/fn-pointer consumes the
// input and stores it into the resulting
// rvalue.
debug!("walk_adjustment(AutoAddEnv|AdjustReifyFnPointer)");
let cmt_unadjusted =
return_if_err!(self.mc.cat_expr_unadjusted(expr));
self.delegate_consume(expr.id, expr.span, cmt_unadjusted);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/fast_reject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn simplify_type(tcx: &ty::ctxt,
ty::ty_closure(ref f) => {
Some(FunctionSimplifiedType(f.sig.0.inputs.len()))
}
ty::ty_bare_fn(ref f) => {
ty::ty_bare_fn(_, ref f) => {
Some(FunctionSimplifiedType(f.sig.0.inputs.len()))
}
ty::ty_param(_) => {
Expand Down
Loading

0 comments on commit de11710

Please sign in to comment.