Skip to content

Commit caa99d6

Browse files
committed
---
yaml --- r: 140904 b: refs/heads/try2 c: 77c98f0 h: refs/heads/master v: v3
1 parent 4622018 commit caa99d6

File tree

10 files changed

+145
-44
lines changed

10 files changed

+145
-44
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: af54d58891505a67aaab5365b957679b0c593eb7
8+
refs/heads/try2: 77c98f081509733af957af6ed7b0b277e5f57871
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libcore/iter.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ much easier to implement.
4343
#[cfg(not(stage0))] use cmp::Ord;
4444
#[cfg(not(stage0))] use option::{Option, Some, None};
4545
#[cfg(not(stage0))] use vec::OwnedVector;
46+
#[cfg(not(stage0))] use num::{One, Zero};
47+
#[cfg(not(stage0))] use ops::{Add, Mul};
4648

4749
#[cfg(stage0)]
4850
pub trait Times {
@@ -212,6 +214,81 @@ pub fn min<T: Ord>(iter: &fn(f: &fn(T) -> bool) -> bool) -> Option<T> {
212214
result
213215
}
214216

217+
/**
218+
* Reduce an iterator to an accumulated value.
219+
*
220+
* # Example:
221+
*
222+
* ~~~~
223+
* assert_eq!(fold(0i, |f| int::range(1, 5, f), |a, x| *a += x), 10);
224+
* ~~~~
225+
*/
226+
#[cfg(not(stage0))]
227+
#[inline]
228+
pub fn fold<T, U>(start: T, iter: &fn(f: &fn(U) -> bool) -> bool, f: &fn(&mut T, U)) -> T {
229+
let mut result = start;
230+
for iter |x| {
231+
f(&mut result, x);
232+
}
233+
result
234+
}
235+
236+
/**
237+
* Reduce an iterator to an accumulated value.
238+
*
239+
* `fold_ref` is usable in some generic functions where `fold` is too lenient to type-check, but it
240+
* forces the iterator to yield borrowed pointers.
241+
*
242+
* # Example:
243+
*
244+
* ~~~~
245+
* fn product<T: One + Mul<T, T>>(iter: &fn(f: &fn(&T) -> bool) -> bool) -> T {
246+
* fold_ref(One::one::<T>(), iter, |a, x| *a = a.mul(x))
247+
* }
248+
* ~~~~
249+
*/
250+
#[cfg(not(stage0))]
251+
#[inline]
252+
pub fn fold_ref<T, U>(start: T, iter: &fn(f: &fn(&U) -> bool) -> bool, f: &fn(&mut T, &U)) -> T {
253+
let mut result = start;
254+
for iter |x| {
255+
f(&mut result, x);
256+
}
257+
result
258+
}
259+
260+
/**
261+
* Return the sum of the items yielding by an iterator.
262+
*
263+
* # Example:
264+
*
265+
* ~~~~
266+
* let xs: ~[int] = ~[1, 2, 3, 4];
267+
* assert_eq!(do sum |f| { xs.each(f) }, 10);
268+
* ~~~~
269+
*/
270+
#[cfg(not(stage0))]
271+
#[inline(always)]
272+
pub fn sum<T: Zero + Add<T, T>>(iter: &fn(f: &fn(&T) -> bool) -> bool) -> T {
273+
fold_ref(Zero::zero::<T>(), iter, |a, x| *a = a.add(x))
274+
}
275+
276+
/**
277+
* Return the product of the items yielded by an iterator.
278+
*
279+
* # Example:
280+
*
281+
* ~~~~
282+
* let xs: ~[int] = ~[1, 2, 3, 4];
283+
* assert_eq!(do product |f| { xs.each(f) }, 24);
284+
* ~~~~
285+
*/
286+
#[cfg(not(stage0))]
287+
#[inline(always)]
288+
pub fn product<T: One + Mul<T, T>>(iter: &fn(f: &fn(&T) -> bool) -> bool) -> T {
289+
fold_ref(One::one::<T>(), iter, |a, x| *a = a.mul(x))
290+
}
291+
215292
#[cfg(test)]
216293
mod tests {
217294
use super::*;
@@ -254,4 +331,33 @@ mod tests {
254331
let xs = ~[8, 2, 3, 1, -5, 9, 11, 15];
255332
assert_eq!(min(|f| xs.each(f)).unwrap(), &-5);
256333
}
334+
335+
#[test]
336+
fn test_fold() {
337+
assert_eq!(fold(0i, |f| int::range(1, 5, f), |a, x| *a += x), 10);
338+
}
339+
340+
#[test]
341+
fn test_sum() {
342+
let xs: ~[int] = ~[1, 2, 3, 4];
343+
assert_eq!(do sum |f| { xs.each(f) }, 10);
344+
}
345+
346+
#[test]
347+
fn test_empty_sum() {
348+
let xs: ~[int] = ~[];
349+
assert_eq!(do sum |f| { xs.each(f) }, 0);
350+
}
351+
352+
#[test]
353+
fn test_product() {
354+
let xs: ~[int] = ~[1, 2, 3, 4];
355+
assert_eq!(do product |f| { xs.each(f) }, 24);
356+
}
357+
358+
#[test]
359+
fn test_empty_product() {
360+
let xs: ~[int] = ~[];
361+
assert_eq!(do product |f| { xs.each(f) }, 1);
362+
}
257363
}

branches/try2/src/librustc/metadata/csearch.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,13 @@ pub fn get_field_type(tcx: ty::ctxt, class_id: ast::def_id,
234234
}
235235
}
236236

237-
// Given a def_id for an impl or class, return the traits it implements,
238-
// or the empty vector if it's not for an impl or for a class that implements
239-
// traits
240-
pub fn get_impl_traits(tcx: ty::ctxt,
241-
def: ast::def_id) -> ~[@ty::TraitRef] {
237+
// Given a def_id for an impl, return the trait it implements,
238+
// if there is one.
239+
pub fn get_impl_trait(tcx: ty::ctxt,
240+
def: ast::def_id) -> Option<@ty::TraitRef> {
242241
let cstore = tcx.cstore;
243242
let cdata = cstore::get_crate_data(cstore, def.crate);
244-
decoder::get_impl_traits(cdata, def.node, tcx)
243+
decoder::get_impl_trait(cdata, def.node, tcx)
245244
}
246245

247246
pub fn get_impl_method(cstore: @mut cstore::CStore,

branches/try2/src/librustc/metadata/decoder.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,19 +415,20 @@ pub fn get_type_param_count(data: @~[u8], id: ast::node_id) -> uint {
415415
item_ty_param_count(lookup_item(id, data))
416416
}
417417

418-
pub fn get_impl_traits(cdata: cmd,
418+
pub fn get_impl_trait(cdata: cmd,
419419
id: ast::node_id,
420-
tcx: ty::ctxt) -> ~[@ty::TraitRef]
420+
tcx: ty::ctxt) -> Option<@ty::TraitRef>
421421
{
422422
let item_doc = lookup_item(id, cdata.data);
423-
let mut results = ~[];
423+
let mut result = None;
424424
for reader::tagged_docs(item_doc, tag_item_trait_ref) |tp| {
425425
let trait_ref =
426426
@parse_trait_ref_data(tp.data, cdata.cnum, tp.start, tcx,
427427
|_, did| translate_def_id(cdata, did));
428-
results.push(trait_ref);
428+
result = Some(trait_ref);
429+
break;
429430
};
430-
results
431+
result
431432
}
432433

433434
pub fn get_impl_method(intr: @ident_interner, cdata: cmd, id: ast::node_id,

branches/try2/src/librustc/middle/trans/base.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,8 +2228,7 @@ pub fn register_fn_fuller(ccx: @CrateContext,
22282228
mangle_exported_name(ccx, /*bad*/copy path, node_type)
22292229
};
22302230
2231-
// XXX: Bad copy.
2232-
let llfn: ValueRef = decl_fn(ccx.llmod, copy ps, cc, llfty);
2231+
let llfn: ValueRef = decl_fn(ccx.llmod, ps, cc, llfty);
22332232
ccx.item_symbols.insert(node_id, ps);
22342233
22352234
// FIXME #4404 android JNI hacks

branches/try2/src/librustc/middle/trans/datum.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ use middle::ty;
101101
use util::common::indenter;
102102
use util::ppaux::ty_to_str;
103103

104-
use core::container::Set; // XXX: this should not be necessary
105104
use core::to_bytes;
106105
use syntax::ast;
107106
use syntax::codemap::span;

branches/try2/src/librustc/middle/trans/meth.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,8 +795,11 @@ pub fn make_impl_vtable(ccx: @CrateContext,
795795
let _icx = ccx.insn_ctxt("impl::make_impl_vtable");
796796
let tcx = ccx.tcx;
797797

798-
// XXX: This should support multiple traits.
799-
let trt_id = ty::impl_trait_refs(tcx, impl_id)[0].def_id;
798+
let trt_id = match ty::impl_trait_ref(tcx, impl_id) {
799+
Some(t_id) => t_id.def_id,
800+
None => ccx.sess.bug("make_impl_vtable: don't know how to \
801+
make a vtable for a type impl!")
802+
};
800803

801804
let has_tps =
802805
!ty::lookup_item_type(ccx.tcx, impl_id).generics.type_param_defs.is_empty();

branches/try2/src/librustc/middle/ty.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3883,23 +3883,23 @@ pub fn trait_method_def_ids(cx: ctxt, id: ast::def_id) -> @~[def_id] {
38833883
|| @csearch::get_trait_method_def_ids(cx.cstore, id))
38843884
}
38853885

3886-
pub fn impl_trait_refs(cx: ctxt, id: ast::def_id) -> ~[@TraitRef] {
3886+
pub fn impl_trait_ref(cx: ctxt, id: ast::def_id) -> Option<@TraitRef> {
38873887
if id.crate == ast::local_crate {
3888-
debug!("(impl_traits) searching for trait impl %?", id);
3888+
debug!("(impl_trait_ref) searching for trait impl %?", id);
38893889
match cx.items.find(&id.node) {
38903890
Some(&ast_map::node_item(@ast::item {
38913891
node: ast::item_impl(_, opt_trait, _, _),
38923892
_},
38933893
_)) => {
38943894
match opt_trait {
3895-
Some(t) => ~[ty::node_id_to_trait_ref(cx, t.ref_id)],
3896-
None => ~[]
3895+
Some(t) => Some(ty::node_id_to_trait_ref(cx, t.ref_id)),
3896+
None => None
38973897
}
38983898
}
3899-
_ => ~[]
3899+
_ => None
39003900
}
39013901
} else {
3902-
csearch::get_impl_traits(cx, id)
3902+
csearch::get_impl_trait(cx, id)
39033903
}
39043904
}
39053905

branches/try2/src/librustc/middle/typeck/check/vtable.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -261,24 +261,14 @@ fn lookup_vtable(vcx: &VtableContext,
261261
}
262262
impls_seen.insert(im.did);
263263

264-
// ty::impl_traits gives us the list of all
265-
// traits that im implements. Again, usually
266-
// there's just one.
264+
// ty::impl_traits gives us the trait im implements,
265+
// if there is one (there's either zero or one).
267266
//
268-
// For example, if im represented the struct
269-
// in:
270-
//
271-
// struct foo : baz<int>, bar, quux { ... }
272-
//
273-
// then ty::impl_traits would return
274-
//
275-
// ~[baz<int>, bar, quux]
276-
//
277-
// For each of the traits foo implements, if
278-
// it's the same trait as trait_ref, we need to
267+
// If foo implements a trait t, and if t is the
268+
// same trait as trait_ref, we need to
279269
// unify it with trait_ref in order to get all
280270
// the ty vars sorted out.
281-
for ty::impl_trait_refs(tcx, im.did).each |&of_trait_ref|
271+
for ty::impl_trait_ref(tcx, im.did).each |&of_trait_ref|
282272
{
283273
if of_trait_ref.def_id != trait_ref.def_id { loop; }
284274

@@ -456,8 +446,12 @@ fn connect_trait_tps(vcx: &VtableContext,
456446
{
457447
let tcx = vcx.tcx();
458448

459-
// XXX: This should work for multiple traits.
460-
let impl_trait_ref = ty::impl_trait_refs(tcx, impl_did)[0];
449+
let impl_trait_ref = match ty::impl_trait_ref(tcx, impl_did) {
450+
Some(t) => t,
451+
None => vcx.tcx().sess.span_bug(location_info.span,
452+
"connect_trait_tps invoked on a type impl")
453+
};
454+
461455
let impl_trait_ref = (*impl_trait_ref).subst(tcx, impl_substs);
462456
relate_trait_refs(vcx, location_info, &impl_trait_ref, trait_ref);
463457
}

branches/try2/src/librustc/middle/typeck/coherence.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
use driver;
19-
use metadata::csearch::{each_path, get_impl_traits};
19+
use metadata::csearch::{each_path, get_impl_trait};
2020
use metadata::csearch::{get_impls_for_mod};
2121
use metadata::csearch;
2222
use metadata::cstore::{CStore, iter_crate_data};
@@ -898,13 +898,13 @@ pub impl CoherenceChecker {
898898

899899
let self_type = lookup_item_type(self.crate_context.tcx,
900900
implementation.did);
901-
let associated_traits = get_impl_traits(self.crate_context.tcx,
901+
let associated_traits = get_impl_trait(self.crate_context.tcx,
902902
implementation.did);
903903

904904
// Do a sanity check to make sure that inherent methods have base
905905
// types.
906906

907-
if associated_traits.len() == 0 {
907+
if associated_traits.is_none() {
908908
match get_base_type_def_id(self.inference_context,
909909
dummy_sp(),
910910
self_type.ty) {
@@ -940,7 +940,7 @@ pub impl CoherenceChecker {
940940
Some(base_type_def_id) => {
941941
// inherent methods apply to `impl Type` but not
942942
// `impl Trait for Type`:
943-
if associated_traits.len() == 0 {
943+
if associated_traits.is_none() {
944944
self.add_inherent_method(base_type_def_id,
945945
*implementation);
946946
}

0 commit comments

Comments
 (0)