Skip to content

Commit de11710

Browse files
committed
rollup merge of #19891: nikomatsakis/unique-fn-types-3
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
2 parents 459f3b2 + 763152b commit de11710

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+537
-255
lines changed

src/libcollections/btree/map.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,7 @@ impl<K, V> BTreeMap<K, V> {
12301230
#[unstable = "matches collection reform specification, waiting for dust to settle"]
12311231
pub fn keys<'a>(&'a self) -> Keys<'a, K, V> {
12321232
fn first<A, B>((a, _): (A, B)) -> A { a }
1233+
let first: fn((&'a K, &'a V)) -> &'a K = first; // coerce to fn pointer
12331234

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

12551257
Values { inner: self.iter().map(second) }
12561258
}

src/libcollections/btree/set.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ impl<T> BTreeSet<T> {
126126
#[unstable = "matches collection reform specification, waiting for dust to settle"]
127127
pub fn into_iter(self) -> IntoIter<T> {
128128
fn first<A, B>((a, _): (A, B)) -> A { a }
129+
let first: fn((T, ())) -> T = first; // coerce to fn pointer
129130

130131
IntoIter { iter: self.map.into_iter().map(first) }
131132
}

src/libcollections/vec_map.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ impl<V> VecMap<V> {
144144
#[unstable = "matches collection reform specification, waiting for dust to settle"]
145145
pub fn keys<'r>(&'r self) -> Keys<'r, V> {
146146
fn first<A, B>((a, _): (A, B)) -> A { a }
147+
let first: fn((uint, &'r V)) -> uint = first; // coerce to fn pointer
147148

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

157159
Values { iter: self.iter().map(second) }
158160
}
@@ -239,6 +241,7 @@ impl<V> VecMap<V> {
239241
fn filter<A>((i, v): (uint, Option<A>)) -> Option<(uint, A)> {
240242
v.map(|v| (i, v))
241243
}
244+
let filter: fn((uint, Option<V>)) -> Option<(uint, V)> = filter; // coerce to fn ptr
242245

243246
let values = replace(&mut self.v, vec!());
244247
IntoIter { iter: values.into_iter().enumerate().filter_map(filter) }

src/libcore/iter.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2612,6 +2612,9 @@ pub fn iterate<T, F>(seed: T, f: F) -> Iterate<T, F> where
26122612
val.clone()
26132613
}
26142614

2615+
// coerce to a fn pointer
2616+
let next: fn(&mut IterateState<T,F>) -> Option<T> = next;
2617+
26152618
Unfold::new((f, Some(seed), true), next)
26162619
}
26172620

src/libcore/str.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,7 @@ impl StrExt for str {
13101310
else { line }
13111311
}
13121312

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

src/librustc/metadata/decoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ pub fn get_enum_variants<'tcx>(intr: Rc<IdentInterner>, cdata: Cmd, id: ast::Nod
700700
item, tcx, cdata);
701701
let name = item_name(&*intr, item);
702702
let (ctor_ty, arg_tys, arg_names) = match ctor_ty.sty {
703-
ty::ty_bare_fn(ref f) =>
703+
ty::ty_bare_fn(_, ref f) =>
704704
(Some(ctor_ty), f.sig.0.inputs.clone(), None),
705705
_ => { // Nullary or struct enum variant.
706706
let mut arg_names = Vec::new();

src/librustc/metadata/tydecode.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,11 @@ fn parse_ty<'a, 'tcx>(st: &mut PState<'a, 'tcx>, conv: conv_did) -> Ty<'tcx> {
453453
return ty::mk_closure(st.tcx, parse_closure_ty(st, |x,y| conv(x,y)));
454454
}
455455
'F' => {
456-
return ty::mk_bare_fn(st.tcx, parse_bare_fn_ty(st, |x,y| conv(x,y)));
456+
let def_id = parse_def(st, NominalType, |x,y| conv(x,y));
457+
return ty::mk_bare_fn(st.tcx, Some(def_id), parse_bare_fn_ty(st, |x,y| conv(x,y)));
458+
}
459+
'G' => {
460+
return ty::mk_bare_fn(st.tcx, None, parse_bare_fn_ty(st, |x,y| conv(x,y)));
457461
}
458462
'#' => {
459463
let pos = parse_hex(st);

src/librustc/metadata/tyencode.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,13 @@ pub fn enc_ty<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>, t: Ty<'t
123123
mywrite!(w, "f");
124124
enc_closure_ty(w, cx, &**f);
125125
}
126-
ty::ty_bare_fn(ref f) => {
126+
ty::ty_bare_fn(Some(def_id), ref f) => {
127127
mywrite!(w, "F");
128+
mywrite!(w, "{}|", (cx.ds)(def_id));
129+
enc_bare_fn_ty(w, cx, f);
130+
}
131+
ty::ty_bare_fn(None, ref f) => {
132+
mywrite!(w, "G");
128133
enc_bare_fn_ty(w, cx, f);
129134
}
130135
ty::ty_infer(_) => {

src/librustc/middle/astencode.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,14 +1007,21 @@ impl<'a, 'tcx> rbml_writer_helpers<'tcx> for Encoder<'a> {
10071007

10081008
self.emit_enum("AutoAdjustment", |this| {
10091009
match *adj {
1010-
ty::AdjustAddEnv(store) => {
1011-
this.emit_enum_variant("AutoAddEnv", 0, 1, |this| {
1012-
this.emit_enum_variant_arg(0, |this| store.encode(this))
1010+
ty::AdjustAddEnv(def_id, store) => {
1011+
this.emit_enum_variant("AdjustAddEnv", 0, 2, |this| {
1012+
this.emit_enum_variant_arg(0, |this| def_id.encode(this));
1013+
this.emit_enum_variant_arg(1, |this| store.encode(this))
1014+
})
1015+
}
1016+
1017+
ty::AdjustReifyFnPointer(def_id) => {
1018+
this.emit_enum_variant("AdjustReifyFnPointer", 1, 2, |this| {
1019+
this.emit_enum_variant_arg(0, |this| def_id.encode(this))
10131020
})
10141021
}
10151022

10161023
ty::AdjustDerefRef(ref auto_deref_ref) => {
1017-
this.emit_enum_variant("AutoDerefRef", 1, 1, |this| {
1024+
this.emit_enum_variant("AdjustDerefRef", 2, 2, |this| {
10181025
this.emit_enum_variant_arg(0,
10191026
|this| Ok(this.emit_auto_deref_ref(ecx, auto_deref_ref)))
10201027
})
@@ -1648,12 +1655,20 @@ impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> {
16481655
this.read_enum_variant(&variants, |this, i| {
16491656
Ok(match i {
16501657
0 => {
1658+
let def_id: ast::DefId =
1659+
this.read_def_id(dcx);
16511660
let store: ty::TraitStore =
16521661
this.read_enum_variant_arg(0, |this| Decodable::decode(this)).unwrap();
16531662

1654-
ty::AdjustAddEnv(store.tr(dcx))
1663+
ty::AdjustAddEnv(def_id, store.tr(dcx))
16551664
}
16561665
1 => {
1666+
let def_id: ast::DefId =
1667+
this.read_def_id(dcx);
1668+
1669+
ty::AdjustReifyFnPointer(def_id)
1670+
}
1671+
2 => {
16571672
let auto_deref_ref: ty::AutoDerefRef =
16581673
this.read_enum_variant_arg(0,
16591674
|this| Ok(this.read_auto_deref_ref(dcx))).unwrap();

src/librustc/middle/check_const.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ fn check_expr(v: &mut CheckCrateVisitor, e: &ast::Expr) -> bool {
127127
ast::ExprCast(ref from, _) => {
128128
let toty = ty::expr_ty(v.tcx, e);
129129
let fromty = ty::expr_ty(v.tcx, &**from);
130-
if !ty::type_is_numeric(toty) && !ty::type_is_unsafe_ptr(toty) {
130+
let is_legal_cast =
131+
ty::type_is_numeric(toty) ||
132+
ty::type_is_unsafe_ptr(toty) ||
133+
(ty::type_is_bare_fn(toty) && ty::type_is_bare_fn_item(fromty));
134+
if !is_legal_cast {
131135
span_err!(v.tcx.sess, e.span, E0012,
132136
"can not cast to `{}` in a constant expression",
133137
ppaux::ty_to_string(v.tcx, toty));

src/librustc/middle/effect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ enum UnsafeContext {
3232

3333
fn type_is_unsafe_function(ty: Ty) -> bool {
3434
match ty.sty {
35-
ty::ty_bare_fn(ref f) => f.unsafety == ast::Unsafety::Unsafe,
35+
ty::ty_bare_fn(_, ref f) => f.unsafety == ast::Unsafety::Unsafe,
3636
ty::ty_closure(ref f) => f.unsafety == ast::Unsafety::Unsafe,
3737
_ => false,
3838
}

src/librustc/middle/expr_use_visitor.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -824,10 +824,12 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
824824
None => { }
825825
Some(adjustment) => {
826826
match *adjustment {
827-
ty::AdjustAddEnv(..) => {
828-
// Creating a closure consumes the input and stores it
829-
// into the resulting rvalue.
830-
debug!("walk_adjustment(AutoAddEnv)");
827+
ty::AdjustAddEnv(..) |
828+
ty::AdjustReifyFnPointer(..) => {
829+
// Creating a closure/fn-pointer consumes the
830+
// input and stores it into the resulting
831+
// rvalue.
832+
debug!("walk_adjustment(AutoAddEnv|AdjustReifyFnPointer)");
831833
let cmt_unadjusted =
832834
return_if_err!(self.mc.cat_expr_unadjusted(expr));
833835
self.delegate_consume(expr.id, expr.span, cmt_unadjusted);

src/librustc/middle/fast_reject.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub fn simplify_type(tcx: &ty::ctxt,
8383
ty::ty_closure(ref f) => {
8484
Some(FunctionSimplifiedType(f.sig.0.inputs.len()))
8585
}
86-
ty::ty_bare_fn(ref f) => {
86+
ty::ty_bare_fn(_, ref f) => {
8787
Some(FunctionSimplifiedType(f.sig.0.inputs.len()))
8888
}
8989
ty::ty_param(_) => {

0 commit comments

Comments
 (0)