From 48b0473d331497107cb91a5cc6f4969b54efac41 Mon Sep 17 00:00:00 2001 From: Christopher Kendell Date: Thu, 3 Apr 2014 15:38:19 -0700 Subject: [PATCH 1/9] Removed references to managed boxes/pointers from the tutorial text. Code examples in relevant sections were similarly altered. --- src/doc/tutorial.md | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/src/doc/tutorial.md b/src/doc/tutorial.md index 713c41ddf7d8e..2ba15a4cd2536 100644 --- a/src/doc/tutorial.md +++ b/src/doc/tutorial.md @@ -1408,20 +1408,17 @@ struct Point { ~~~~ We can use this simple definition to allocate points in many different -ways. For example, in this code, each of these three local variables +ways. For example, in this code, each of these local variables contains a point, but allocated in a different location: ~~~ # struct Point { x: f64, y: f64 } let on_the_stack : Point = Point { x: 3.0, y: 4.0 }; -let managed_box : @Point = @Point { x: 5.0, y: 1.0 }; let owned_box : ~Point = ~Point { x: 7.0, y: 9.0 }; ~~~ Suppose we want to write a procedure that computes the distance -between any two points, no matter where they are stored. For example, -we might like to compute the distance between `on_the_stack` and -`managed_box`, or between `managed_box` and `owned_box`. One option is +between any two points, no matter where they are stored. One option is to define a function that takes two arguments of type point—that is, it takes the points by value. But this will cause the points to be copied when we call the function. For points, this is probably not so @@ -1442,11 +1439,9 @@ Now we can call `compute_distance()` in various ways: ~~~ # struct Point{ x: f64, y: f64 }; # let on_the_stack : Point = Point { x: 3.0, y: 4.0 }; -# let managed_box : @Point = @Point { x: 5.0, y: 1.0 }; # let owned_box : ~Point = ~Point { x: 7.0, y: 9.0 }; # fn compute_distance(p1: &Point, p2: &Point) -> f64 { 0.0 } -compute_distance(&on_the_stack, managed_box); -compute_distance(managed_box, owned_box); +compute_distance(&on_the_stack, owned_box); ~~~ Here the `&` operator is used to take the address of the variable @@ -1456,11 +1451,11 @@ reference. We also call this _borrowing_ the local variable `on_the_stack`, because we are creating an alias: that is, another route to the same data. -In the case of the boxes `managed_box` and `owned_box`, however, no +In the case of `owned_box`, however, no explicit action is necessary. The compiler will automatically convert -a box like `@point` or `~point` to a reference like +a box `~point` to a reference like `&point`. This is another form of borrowing; in this case, the -contents of the managed/owned box are being lent out. +contents of the owned box are being lent out. Whenever a value is borrowed, there are some limitations on what you can do with the original. For example, if the contents of a variable @@ -1497,11 +1492,10 @@ Rust uses the unary star operator (`*`) to access the contents of a box or pointer, similarly to C. ~~~ -let managed = @10; let owned = ~20; let borrowed = &30; -let sum = *managed + *owned + *borrowed; +let sum = *owned + *borrowed; ~~~ Dereferenced mutable pointers may appear on the left hand side of @@ -1509,14 +1503,13 @@ assignments. Such an assignment modifies the value that the pointer points to. ~~~ -let managed = @10; -let mut owned = ~20; +let mut owned = ~10; -let mut value = 30; +let mut value = 20; let borrowed = &mut value; *owned = *borrowed + 100; -*borrowed = *managed + 1000; +*borrowed = *owned + 1000; ~~~ Pointers have high operator precedence, but lower precedence than the @@ -1911,7 +1904,7 @@ to a reference. # fn draw_value(self) { /* ... */ } # } # let s = Circle(Point { x: 1.0, y: 2.0 }, 3.0); -// As with typical function arguments, managed and owned pointers +// As with typical function arguments, owned pointers // are automatically converted to references (@s).draw_reference(); @@ -2094,7 +2087,7 @@ and may not be overridden: * `Send` - Sendable types. Types are sendable -unless they contain managed boxes, managed closures, or references. +unless they contain managed closures or references. * `Share` - Types that are *threadsafe* These are types that are safe to be used across several threads with access to From 3e228ece7af0ba6fee3e79b82e72d2bb529115cd Mon Sep 17 00:00:00 2001 From: Christopher Kendell Date: Thu, 3 Apr 2014 15:48:32 -0700 Subject: [PATCH 2/9] Removed all references to managed closures. --- src/doc/tutorial.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/doc/tutorial.md b/src/doc/tutorial.md index 2ba15a4cd2536..1da3077fa72e8 100644 --- a/src/doc/tutorial.md +++ b/src/doc/tutorial.md @@ -1774,8 +1774,8 @@ pervasively in Rust code. Owned closures, written `proc`, hold on to things that can safely be sent between -processes. They copy the values they close over, much like managed -closures, but they also own them: that is, no other code can access +processes. They copy the values they close over, +but they also own them: that is, no other code can access them. Owned closures are used in concurrent code, particularly for spawning [tasks][tasks]. @@ -2087,7 +2087,7 @@ and may not be overridden: * `Send` - Sendable types. Types are sendable -unless they contain managed closures or references. +unless they contain references. * `Share` - Types that are *threadsafe* These are types that are safe to be used across several threads with access to From 4b8da97b70fb17918609a3d18a12878e95b773d4 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Fri, 28 Mar 2014 10:29:55 -0700 Subject: [PATCH 3/9] std: Remove `RefCell::get()` It's surprising that `RefCell::get()` is implicitly doing a clone on a value. This patch removes it and replaces all users with either `.borrow()` when we can autoderef, or `.borrow().clone()` when we cannot. --- src/librustc/front/test.rs | 4 +- src/librustc/metadata/encoder.rs | 4 +- src/librustc/middle/dead.rs | 2 +- src/librustc/middle/resolve.rs | 67 +++++++++++-------- src/librustc/middle/trans/base.rs | 8 +-- src/librustc/middle/trans/closure.rs | 2 +- src/librustc/middle/trans/glue.rs | 2 +- src/librustc/middle/trans/intrinsic.rs | 2 +- src/librustc/middle/trans/reflect.rs | 2 +- src/librustc/middle/typeck/check/mod.rs | 4 +- src/librustc/middle/typeck/check/regionck.rs | 6 +- .../middle/typeck/infer/error_reporting.rs | 2 +- src/librustc/middle/typeck/mod.rs | 2 +- src/libstd/cell.rs | 20 ++---- src/libstd/option.rs | 5 +- src/test/auxiliary/issue-2631-a.rs | 2 +- 16 files changed, 66 insertions(+), 68 deletions(-) diff --git a/src/librustc/front/test.rs b/src/librustc/front/test.rs index e39a4a9cc51f2..82e2e3147c93d 100644 --- a/src/librustc/front/test.rs +++ b/src/librustc/front/test.rs @@ -90,7 +90,7 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> { fn fold_item(&mut self, i: @ast::Item) -> SmallVector<@ast::Item> { self.cx.path.borrow_mut().push(i.ident); debug!("current path: {}", - ast_util::path_name_i(self.cx.path.get().as_slice())); + ast_util::path_name_i(self.cx.path.borrow().as_slice())); if is_test_fn(&self.cx, i) || is_bench_fn(&self.cx, i) { match i.node { @@ -104,7 +104,7 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> { debug!("this is a test function"); let test = Test { span: i.span, - path: self.cx.path.get(), + path: self.cx.path.borrow().clone(), bench: is_bench_fn(&self.cx, i), ignore: is_ignored(&self.cx, i), should_fail: should_fail(i) diff --git a/src/librustc/metadata/encoder.rs b/src/librustc/metadata/encoder.rs index ec6bb7de380f8..e453acac8109e 100644 --- a/src/librustc/metadata/encoder.rs +++ b/src/librustc/metadata/encoder.rs @@ -1345,7 +1345,7 @@ fn encode_info_for_items(ecx: &EncodeContext, } ebml_w.end_tag(); - return /*bad*/(*index).get(); + return /*bad*/index.borrow().clone(); } @@ -1365,7 +1365,7 @@ fn create_index( let mut buckets_frozen = Vec::new(); for bucket in buckets.iter() { - buckets_frozen.push(@/*bad*/(**bucket).get()); + buckets_frozen.push(@/*bad*/bucket.borrow().clone()); } return buckets_frozen; } diff --git a/src/librustc/middle/dead.rs b/src/librustc/middle/dead.rs index 1d23218583fc4..d0bf70ea1c2c1 100644 --- a/src/librustc/middle/dead.rs +++ b/src/librustc/middle/dead.rs @@ -270,7 +270,7 @@ fn create_and_seed_worklist(tcx: &ty::ctxt, } // Seed entry point - match tcx.sess.entry_fn.get() { + match *tcx.sess.entry_fn.borrow() { Some((id, _)) => worklist.push(id), None => () } diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index 3e1c1828b6c48..526ec66800a16 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -385,6 +385,10 @@ struct ImportResolution { type_id: Cell, } +fn get(cell: &RefCell) -> T { + cell.borrow().clone() +} + impl ImportResolution { fn new(id: NodeId, is_public: bool) -> ImportResolution { ImportResolution { @@ -400,8 +404,8 @@ impl ImportResolution { fn target_for_namespace(&self, namespace: Namespace) -> Option { match namespace { - TypeNS => return self.type_target.get(), - ValueNS => return self.value_target.get(), + TypeNS => return self.type_target.borrow().clone(), + ValueNS => return self.value_target.borrow().clone(), } } @@ -546,7 +550,8 @@ impl NameBindings { // Merges the module with the existing type def or creates a new one. let module_ = @Module::new(parent_link, def_id, kind, external, is_public); - match self.type_def.get() { + let type_def = self.type_def.borrow().clone(); + match type_def { None => { self.type_def.set(Some(TypeNsDef { is_public: is_public, @@ -574,7 +579,8 @@ impl NameBindings { external: bool, is_public: bool, _sp: Span) { - match self.type_def.get() { + let type_def = self.type_def.borrow().clone(); + match type_def { None => { let module = @Module::new(parent_link, def_id, kind, external, is_public); @@ -609,7 +615,8 @@ impl NameBindings { /// Records a type definition. fn define_type(&self, def: Def, sp: Span, is_public: bool) { // Merges the type with the existing type def or creates a new one. - match self.type_def.get() { + let type_def = self.type_def.borrow().clone(); + match type_def { None => { self.type_def.set(Some(TypeNsDef { module_def: None, @@ -662,17 +669,17 @@ impl NameBindings { fn defined_in_namespace(&self, namespace: Namespace) -> bool { match namespace { - TypeNS => return self.type_def.get().is_some(), - ValueNS => return self.value_def.get().is_some() + TypeNS => return self.type_def.borrow().is_some(), + ValueNS => return self.value_def.borrow().is_some() } } fn defined_in_public_namespace(&self, namespace: Namespace) -> bool { match namespace { - TypeNS => match self.type_def.get() { + TypeNS => match *self.type_def.borrow() { Some(def) => def.is_public, None => false }, - ValueNS => match self.value_def.get() { + ValueNS => match *self.value_def.borrow() { Some(def) => def.is_public, None => false } } @@ -681,7 +688,7 @@ impl NameBindings { fn def_for_namespace(&self, namespace: Namespace) -> Option { match namespace { TypeNS => { - match self.type_def.get() { + match *self.type_def.borrow() { None => None, Some(type_def) => { match type_def.type_def { @@ -702,7 +709,7 @@ impl NameBindings { } } ValueNS => { - match self.value_def.get() { + match *self.value_def.borrow() { None => None, Some(value_def) => Some(value_def.def) } @@ -714,13 +721,13 @@ impl NameBindings { if self.defined_in_namespace(namespace) { match namespace { TypeNS => { - match self.type_def.get() { + match *self.type_def.borrow() { None => None, Some(type_def) => type_def.type_span } } ValueNS => { - match self.value_def.get() { + match *self.value_def.borrow() { None => None, Some(value_def) => value_def.value_span } @@ -1620,7 +1627,8 @@ impl<'a> Resolver<'a> { match def { DefMod(def_id) | DefForeignMod(def_id) | DefStruct(def_id) | DefTy(def_id) => { - match child_name_bindings.type_def.get() { + let type_def = child_name_bindings.type_def.borrow().clone(); + match type_def { Some(TypeNsDef { module_def: Some(module_def), .. }) => { debug!("(building reduced graph for external crate) \ already created module"); @@ -1812,7 +1820,8 @@ impl<'a> Resolver<'a> { // Process the static methods. First, // create the module. let type_module; - match child_name_bindings.type_def.get() { + let type_def = child_name_bindings.type_def.borrow().clone(); + match type_def { Some(TypeNsDef { module_def: Some(module_def), .. @@ -2421,7 +2430,7 @@ impl<'a> Resolver<'a> { match type_result { BoundResult(target_module, name_bindings) => { debug!("(resolving single import) found type target: {:?}", - {name_bindings.type_def.get().unwrap().type_def}); + { name_bindings.type_def.borrow().clone().unwrap().type_def }); import_resolution.type_target.set( Some(Target::new(target_module, name_bindings))); import_resolution.type_id.set(directive.id); @@ -2433,8 +2442,8 @@ impl<'a> Resolver<'a> { } } - if import_resolution.value_target.get().is_none() && - import_resolution.type_target.get().is_none() { + if import_resolution.value_target.borrow().is_none() && + import_resolution.type_target.borrow().is_none() { let msg = format!("unresolved import: there is no \ `{}` in `{}`", token::get_ident(source), @@ -2452,7 +2461,7 @@ impl<'a> Resolver<'a> { // record what this import resolves to for later uses in documentation, // this may resolve to either a value or a type, but for documentation // purposes it's good enough to just favor one over the other. - let value_private = match import_resolution.value_target.get() { + let value_private = match *import_resolution.value_target.borrow() { Some(target) => { let def = target.bindings.def_for_namespace(ValueNS).unwrap(); self.def_map.borrow_mut().insert(directive.id, def); @@ -2463,7 +2472,7 @@ impl<'a> Resolver<'a> { // _exists is false. None => None, }; - let type_private = match import_resolution.type_target.get() { + let type_private = match *import_resolution.type_target.borrow() { Some(target) => { let def = target.bindings.def_for_namespace(TypeNS).unwrap(); self.def_map.borrow_mut().insert(directive.id, def); @@ -2513,7 +2522,7 @@ impl<'a> Resolver<'a> { for (ident, target_import_resolution) in import_resolutions.iter() { debug!("(resolving glob import) writing module resolution \ {:?} into `{}`", - target_import_resolution.type_target.get().is_none(), + target_import_resolution.type_target.borrow().is_none(), self.module_to_str(module_)); if !target_import_resolution.is_public.get() { @@ -2529,9 +2538,9 @@ impl<'a> Resolver<'a> { let new_import_resolution = @ImportResolution::new(id, is_public); new_import_resolution.value_target.set( - target_import_resolution.value_target.get()); + get(&target_import_resolution.value_target)); new_import_resolution.type_target.set( - target_import_resolution.type_target.get()); + get(&target_import_resolution.type_target)); import_resolutions.insert (*ident, new_import_resolution); @@ -2540,7 +2549,7 @@ impl<'a> Resolver<'a> { // Merge the two import resolutions at a finer-grained // level. - match target_import_resolution.value_target.get() { + match *target_import_resolution.value_target.borrow() { None => { // Continue. } @@ -2549,7 +2558,7 @@ impl<'a> Resolver<'a> { Some(value_target)); } } - match target_import_resolution.type_target.get() { + match *target_import_resolution.type_target.borrow() { None => { // Continue. } @@ -2692,7 +2701,7 @@ impl<'a> Resolver<'a> { Success((target, used_proxy)) => { // Check to see whether there are type bindings, and, if // so, whether there is a module within. - match target.bindings.type_def.get() { + match *target.bindings.type_def.borrow() { Some(type_def) => { match type_def.module_def { None => { @@ -3004,7 +3013,7 @@ impl<'a> Resolver<'a> { match resolve_result { Success((target, _)) => { let bindings = &*target.bindings; - match bindings.type_def.get() { + match *bindings.type_def.borrow() { Some(type_def) => { match type_def.module_def { None => { @@ -4526,8 +4535,8 @@ impl<'a> Resolver<'a> { debug!("(resolve bare identifier pattern) succeeded in \ finding {} at {:?}", token::get_ident(name), - target.bindings.value_def.get()); - match target.bindings.value_def.get() { + target.bindings.value_def.borrow()); + match *target.bindings.value_def.borrow() { None => { fail!("resolved name in the value namespace to a \ set of name bindings with no def?!"); diff --git a/src/librustc/middle/trans/base.rs b/src/librustc/middle/trans/base.rs index 09bbb95fdd04e..996de205b5a95 100644 --- a/src/librustc/middle/trans/base.rs +++ b/src/librustc/middle/trans/base.rs @@ -1128,7 +1128,7 @@ pub fn make_return_pointer(fcx: &FunctionContext, output_type: ty::t) llvm::LLVMGetParam(fcx.llfn, 0) } else { let lloutputtype = type_of::type_of(fcx.ccx, output_type); - let bcx = fcx.entry_bcx.get().unwrap(); + let bcx = fcx.entry_bcx.borrow().clone().unwrap(); Alloca(bcx, lloutputtype, "__make_return_pointer") } } @@ -1399,7 +1399,7 @@ pub fn trans_closure(ccx: &CrateContext, // Create the first basic block in the function and keep a handle on it to // pass to finish_fn later. - let bcx_top = fcx.entry_bcx.get().unwrap(); + let bcx_top = fcx.entry_bcx.borrow().clone().unwrap(); let mut bcx = bcx_top; let block_ty = node_id_type(bcx, body.id); @@ -1547,7 +1547,7 @@ fn trans_enum_variant_or_tuple_like_struct(ccx: &CrateContext, let arg_datums = create_datums_for_fn_args(&fcx, arg_tys.as_slice()); - let bcx = fcx.entry_bcx.get().unwrap(); + let bcx = fcx.entry_bcx.borrow().clone().unwrap(); if !type_is_zero_size(fcx.ccx, result_ty) { let repr = adt::represent_type(ccx, result_ty); @@ -1752,7 +1752,7 @@ pub fn register_fn_llvmty(ccx: &CrateContext, } pub fn is_entry_fn(sess: &Session, node_id: ast::NodeId) -> bool { - match sess.entry_fn.get() { + match *sess.entry_fn.borrow() { Some((entry_id, _)) => node_id == entry_id, None => false } diff --git a/src/librustc/middle/trans/closure.rs b/src/librustc/middle/trans/closure.rs index 1d2e1d0232c34..4671e21170e86 100644 --- a/src/librustc/middle/trans/closure.rs +++ b/src/librustc/middle/trans/closure.rs @@ -464,7 +464,7 @@ pub fn get_wrapper_for_bare_fn(ccx: &CrateContext, let arena = TypedArena::new(); let fcx = new_fn_ctxt(ccx, llfn, -1, true, f.sig.output, None, None, &arena); init_function(&fcx, true, f.sig.output, None); - let bcx = fcx.entry_bcx.get().unwrap(); + let bcx = fcx.entry_bcx.borrow().clone().unwrap(); let args = create_datums_for_fn_args(&fcx, ty::ty_fn_args(closure_ty) diff --git a/src/librustc/middle/trans/glue.rs b/src/librustc/middle/trans/glue.rs index da9d06b9a7526..87234ce368353 100644 --- a/src/librustc/middle/trans/glue.rs +++ b/src/librustc/middle/trans/glue.rs @@ -463,7 +463,7 @@ fn make_generic_glue(ccx: &CrateContext, // llfn is expected be declared to take a parameter of the appropriate // type, so we don't need to explicitly cast the function parameter. - let bcx = fcx.entry_bcx.get().unwrap(); + let bcx = fcx.entry_bcx.borrow().clone().unwrap(); let llrawptr0 = unsafe { llvm::LLVMGetParam(llfn, fcx.arg_pos(0) as c_uint) }; let bcx = helper(bcx, llrawptr0, t); finish_fn(&fcx, bcx); diff --git a/src/librustc/middle/trans/intrinsic.rs b/src/librustc/middle/trans/intrinsic.rs index 95c0b08d400ea..28a39718e7fb6 100644 --- a/src/librustc/middle/trans/intrinsic.rs +++ b/src/librustc/middle/trans/intrinsic.rs @@ -199,7 +199,7 @@ pub fn trans_intrinsic(ccx: &CrateContext, set_always_inline(fcx.llfn); - let mut bcx = fcx.entry_bcx.get().unwrap(); + let mut bcx = fcx.entry_bcx.borrow().clone().unwrap(); let first_real_arg = fcx.arg_pos(0u); let name = token::get_ident(item.ident); diff --git a/src/librustc/middle/trans/reflect.rs b/src/librustc/middle/trans/reflect.rs index e73ef639a0ba8..1adf5cf8afe5d 100644 --- a/src/librustc/middle/trans/reflect.rs +++ b/src/librustc/middle/trans/reflect.rs @@ -300,7 +300,7 @@ impl<'a> Reflector<'a> { // llvm::LLVMGetParam(llfdecl, fcx.arg_pos(0u) as c_uint) }; - let bcx = fcx.entry_bcx.get().unwrap(); + let bcx = fcx.entry_bcx.borrow().clone().unwrap(); let arg = BitCast(bcx, arg, llptrty); let ret = adt::trans_get_discr(bcx, repr, arg, Some(Type::i64(ccx))); Store(bcx, ret, fcx.llretptr.get().unwrap()); diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs index 20ab2b699c2b6..4273b26fe4ade 100644 --- a/src/librustc/middle/typeck/check/mod.rs +++ b/src/librustc/middle/typeck/check/mod.rs @@ -2209,8 +2209,8 @@ fn check_expr_with_unifier(fcx: &FnCtxt, fcx.write_ty(expr.id, fty); let (inherited_purity, id) = - ty::determine_inherited_purity((fcx.ps.get().purity, - fcx.ps.get().def), + ty::determine_inherited_purity((fcx.ps.borrow().purity, + fcx.ps.borrow().def), (purity, expr.id), sigil); diff --git a/src/librustc/middle/typeck/check/regionck.rs b/src/librustc/middle/typeck/check/regionck.rs index e67a83cd9269d..c8613fd70652e 100644 --- a/src/librustc/middle/typeck/check/regionck.rs +++ b/src/librustc/middle/typeck/check/regionck.rs @@ -398,7 +398,7 @@ fn visit_expr(rcx: &mut Rcx, expr: &ast::Expr) { expr.repr(rcx.fcx.tcx()), rcx.repeating_scope); let method_call = MethodCall::expr(expr.id); - let has_method_map = rcx.fcx.inh.method_map.get().contains_key(&method_call); + let has_method_map = rcx.fcx.inh.method_map.borrow().contains_key(&method_call); // Check any autoderefs or autorefs that appear. for &adjustment in rcx.fcx.inh.adjustments.borrow().find(&expr.id).iter() { @@ -498,7 +498,7 @@ fn visit_expr(rcx: &mut Rcx, expr: &ast::Expr) { ast::ExprUnary(ast::UnDeref, base) => { // For *a, the lifetime of a must enclose the deref let method_call = MethodCall::expr(expr.id); - let base_ty = match rcx.fcx.inh.method_map.get().find(&method_call) { + let base_ty = match rcx.fcx.inh.method_map.borrow().find(&method_call) { Some(method) => { constrain_call(rcx, None, expr, Some(base), [], true); ty::ty_fn_ret(method.ty) @@ -852,7 +852,7 @@ fn constrain_autoderefs(rcx: &mut Rcx, i, derefs); let method_call = MethodCall::autoderef(deref_expr.id, i as u32); - derefd_ty = match rcx.fcx.inh.method_map.get().find(&method_call) { + derefd_ty = match rcx.fcx.inh.method_map.borrow().find(&method_call) { Some(method) => { // Treat overloaded autoderefs as if an AutoRef adjustment // was applied on the base type, as that is always the case. diff --git a/src/librustc/middle/typeck/infer/error_reporting.rs b/src/librustc/middle/typeck/infer/error_reporting.rs index 83ca7ea7dfbd4..9c947fb3360e4 100644 --- a/src/librustc/middle/typeck/infer/error_reporting.rs +++ b/src/librustc/middle/typeck/infer/error_reporting.rs @@ -1286,6 +1286,6 @@ impl LifeGiver { } fn get_generated_lifetimes(&self) -> Vec { - self.generated.get() + self.generated.borrow().clone() } } diff --git a/src/librustc/middle/typeck/mod.rs b/src/librustc/middle/typeck/mod.rs index d635a9bdec41e..446d2ff055cca 100644 --- a/src/librustc/middle/typeck/mod.rs +++ b/src/librustc/middle/typeck/mod.rs @@ -431,7 +431,7 @@ fn check_start_fn_ty(ccx: &CrateCtxt, fn check_for_entry_fn(ccx: &CrateCtxt) { let tcx = ccx.tcx; if !tcx.sess.building_library.get() { - match tcx.sess.entry_fn.get() { + match *tcx.sess.entry_fn.borrow() { Some((id, sp)) => match tcx.sess.entry_type.get() { Some(session::EntryMain) => check_main_fn_ty(ccx, id, sp), Some(session::EntryStart) => check_start_fn_ty(ccx, id, sp), diff --git a/src/libstd/cell.rs b/src/libstd/cell.rs index eb114e895103a..ec6994728f706 100644 --- a/src/libstd/cell.rs +++ b/src/libstd/cell.rs @@ -176,21 +176,9 @@ impl RefCell { } } -impl RefCell { - /// Returns a copy of the contained value. - /// - /// # Failure - /// - /// Fails if the value is currently mutably borrowed. - #[inline] - pub fn get(&self) -> T { - (*self.borrow()).clone() - } -} - impl Clone for RefCell { fn clone(&self) -> RefCell { - RefCell::new(self.get()) + RefCell::new(self.borrow().clone()) } } @@ -216,7 +204,7 @@ impl<'b, T> Drop for Ref<'b, T> { impl<'b, T> Deref for Ref<'b, T> { #[inline] fn deref<'a>(&'a self) -> &'a T { - unsafe{ &*self.parent.value.get() } + unsafe { &*self.parent.value.get() } } } @@ -236,14 +224,14 @@ impl<'b, T> Drop for RefMut<'b, T> { impl<'b, T> Deref for RefMut<'b, T> { #[inline] fn deref<'a>(&'a self) -> &'a T { - unsafe{ &*self.parent.value.get() } + unsafe { &*self.parent.value.get() } } } impl<'b, T> DerefMut for RefMut<'b, T> { #[inline] fn deref_mut<'a>(&'a mut self) -> &'a mut T { - unsafe{ &mut *self.parent.value.get() } + unsafe { &mut *self.parent.value.get() } } } diff --git a/src/libstd/option.rs b/src/libstd/option.rs index be1c87ba7886c..ce2c1378fc1a2 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -651,7 +651,8 @@ mod tests { impl ::ops::Drop for R { fn drop(&mut self) { let ii = &*self.i; - ii.set(ii.get() + 1); + let i = ii.borrow().clone(); + ii.set(i + 1); } } @@ -667,7 +668,7 @@ mod tests { let opt = Some(x); let _y = opt.unwrap(); } - assert_eq!(i.get(), 1); + assert_eq!(*i.borrow(), 1); } #[test] diff --git a/src/test/auxiliary/issue-2631-a.rs b/src/test/auxiliary/issue-2631-a.rs index e7e0bf1bd60fa..e61510f2ef2d8 100644 --- a/src/test/auxiliary/issue-2631-a.rs +++ b/src/test/auxiliary/issue-2631-a.rs @@ -21,5 +21,5 @@ pub type header_map = HashMap<~str, @RefCell>>; // the unused ty param is necessary so this gets monomorphized pub fn request(req: &header_map) { - let _x = (**((**req.get(&~"METHOD")).clone()).get().get(0)).clone(); + let _x = (**((**req.get(&~"METHOD")).clone()).borrow().clone().get(0)).clone(); } From 400f409b1e3ffbe0adf82b1ac0b7ec7587b20e53 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Wed, 2 Apr 2014 06:55:33 -0700 Subject: [PATCH 4/9] std: Remove `RefCell::set()` --- src/librustc/driver/driver.rs | 4 +- src/librustc/middle/entry.rs | 6 +-- src/librustc/middle/resolve.rs | 58 ++++++++++++------------- src/librustc/middle/trans/base.rs | 2 +- src/librustc/middle/trans/common.rs | 2 +- src/librustc/middle/typeck/check/mod.rs | 2 +- src/libstd/cell.rs | 10 ----- src/libstd/option.rs | 2 +- src/test/run-pass/cycle-collection.rs | 2 +- src/test/run-pass/issue-980.rs | 4 +- 10 files changed, 39 insertions(+), 53 deletions(-) diff --git a/src/librustc/driver/driver.rs b/src/librustc/driver/driver.rs index d5dba02ed28f2..af5b3f8b0cdd3 100644 --- a/src/librustc/driver/driver.rs +++ b/src/librustc/driver/driver.rs @@ -212,9 +212,7 @@ pub fn phase_2_configure_and_expand(sess: &Session, let time_passes = sess.time_passes(); sess.building_library.set(session::building_library(&sess.opts, &krate)); - sess.crate_types.set(session::collect_crate_types(sess, - krate.attrs - .as_slice())); + *sess.crate_types.borrow_mut() = session::collect_crate_types(sess, krate.attrs.as_slice()); time(time_passes, "gated feature checking", (), |_| front::feature_gate::check_crate(sess, &krate)); diff --git a/src/librustc/middle/entry.rs b/src/librustc/middle/entry.rs index 7adfd6e0af03b..441a3a3672983 100644 --- a/src/librustc/middle/entry.rs +++ b/src/librustc/middle/entry.rs @@ -123,13 +123,13 @@ fn find_item(item: &Item, ctxt: &mut EntryContext) { fn configure_main(this: &mut EntryContext) { if this.start_fn.is_some() { - this.session.entry_fn.set(this.start_fn); + *this.session.entry_fn.borrow_mut() = this.start_fn; this.session.entry_type.set(Some(session::EntryStart)); } else if this.attr_main_fn.is_some() { - this.session.entry_fn.set(this.attr_main_fn); + *this.session.entry_fn.borrow_mut() = this.attr_main_fn; this.session.entry_type.set(Some(session::EntryMain)); } else if this.main_fn.is_some() { - this.session.entry_fn.set(this.main_fn); + *this.session.entry_fn.borrow_mut() = this.main_fn; this.session.entry_type.set(Some(session::EntryMain)); } else { if !this.session.building_library.get() { diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index 526ec66800a16..143b02f96d22f 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -553,20 +553,20 @@ impl NameBindings { let type_def = self.type_def.borrow().clone(); match type_def { None => { - self.type_def.set(Some(TypeNsDef { + *self.type_def.borrow_mut() = Some(TypeNsDef { is_public: is_public, module_def: Some(module_), type_def: None, type_span: Some(sp) - })); + }); } Some(type_def) => { - self.type_def.set(Some(TypeNsDef { + *self.type_def.borrow_mut() = Some(TypeNsDef { is_public: is_public, module_def: Some(module_), type_span: Some(sp), type_def: type_def.type_def - })); + }); } } } @@ -584,12 +584,12 @@ impl NameBindings { None => { let module = @Module::new(parent_link, def_id, kind, external, is_public); - self.type_def.set(Some(TypeNsDef { + *self.type_def.borrow_mut() = Some(TypeNsDef { is_public: is_public, module_def: Some(module), type_def: None, type_span: None, - })) + }); } Some(type_def) => { match type_def.module_def { @@ -599,12 +599,12 @@ impl NameBindings { kind, external, is_public); - self.type_def.set(Some(TypeNsDef { + *self.type_def.borrow_mut() = Some(TypeNsDef { is_public: is_public, module_def: Some(module), type_def: type_def.type_def, type_span: None, - })) + }); } Some(module_def) => module_def.kind.set(kind), } @@ -618,31 +618,31 @@ impl NameBindings { let type_def = self.type_def.borrow().clone(); match type_def { None => { - self.type_def.set(Some(TypeNsDef { + *self.type_def.borrow_mut() = Some(TypeNsDef { module_def: None, type_def: Some(def), type_span: Some(sp), is_public: is_public, - })); + }); } Some(type_def) => { - self.type_def.set(Some(TypeNsDef { + *self.type_def.borrow_mut() = Some(TypeNsDef { type_def: Some(def), type_span: Some(sp), module_def: type_def.module_def, is_public: is_public, - })); + }); } } } /// Records a value definition. fn define_value(&self, def: Def, sp: Span, is_public: bool) { - self.value_def.set(Some(ValueNsDef { + *self.value_def.borrow_mut() = Some(ValueNsDef { def: def, value_span: Some(sp), is_public: is_public, - })); + }); } /// Returns the module node if applicable. @@ -2417,8 +2417,8 @@ impl<'a> Resolver<'a> { match value_result { BoundResult(target_module, name_bindings) => { debug!("(resolving single import) found value target"); - import_resolution.value_target.set( - Some(Target::new(target_module, name_bindings))); + *import_resolution.value_target.borrow_mut() = + Some(Target::new(target_module, name_bindings)); import_resolution.value_id.set(directive.id); value_used_public = name_bindings.defined_in_public_namespace(ValueNS); } @@ -2431,8 +2431,8 @@ impl<'a> Resolver<'a> { BoundResult(target_module, name_bindings) => { debug!("(resolving single import) found type target: {:?}", { name_bindings.type_def.borrow().clone().unwrap().type_def }); - import_resolution.type_target.set( - Some(Target::new(target_module, name_bindings))); + *import_resolution.type_target.borrow_mut() = + Some(Target::new(target_module, name_bindings)); import_resolution.type_id.set(directive.id); type_used_public = name_bindings.defined_in_public_namespace(TypeNS); } @@ -2537,10 +2537,10 @@ impl<'a> Resolver<'a> { // Simple: just copy the old import resolution. let new_import_resolution = @ImportResolution::new(id, is_public); - new_import_resolution.value_target.set( - get(&target_import_resolution.value_target)); - new_import_resolution.type_target.set( - get(&target_import_resolution.type_target)); + *new_import_resolution.value_target.borrow_mut() = + get(&target_import_resolution.value_target); + *new_import_resolution.type_target.borrow_mut() = + get(&target_import_resolution.type_target); import_resolutions.insert (*ident, new_import_resolution); @@ -2554,8 +2554,7 @@ impl<'a> Resolver<'a> { // Continue. } Some(value_target) => { - dest_import_resolution.value_target.set( - Some(value_target)); + *dest_import_resolution.value_target.borrow_mut() = Some(value_target); } } match *target_import_resolution.type_target.borrow() { @@ -2563,8 +2562,7 @@ impl<'a> Resolver<'a> { // Continue. } Some(type_target) => { - dest_import_resolution.type_target.set( - Some(type_target)); + *dest_import_resolution.type_target.borrow_mut() = Some(type_target); } } dest_import_resolution.is_public.set(is_public); @@ -2636,14 +2634,14 @@ impl<'a> Resolver<'a> { // Merge the child item into the import resolution. if name_bindings.defined_in_public_namespace(ValueNS) { debug!("(resolving glob import) ... for value target"); - dest_import_resolution.value_target.set( - Some(Target::new(containing_module, name_bindings))); + *dest_import_resolution.value_target.borrow_mut() = + Some(Target::new(containing_module, name_bindings)); dest_import_resolution.value_id.set(id); } if name_bindings.defined_in_public_namespace(TypeNS) { debug!("(resolving glob import) ... for type target"); - dest_import_resolution.type_target.set( - Some(Target::new(containing_module, name_bindings))); + *dest_import_resolution.type_target.borrow_mut() = + Some(Target::new(containing_module, name_bindings)); dest_import_resolution.type_id.set(id); } dest_import_resolution.is_public.set(is_public); diff --git a/src/librustc/middle/trans/base.rs b/src/librustc/middle/trans/base.rs index 996de205b5a95..9657265e14038 100644 --- a/src/librustc/middle/trans/base.rs +++ b/src/librustc/middle/trans/base.rs @@ -1209,7 +1209,7 @@ pub fn init_function<'a>( param_substs: Option<@param_substs>) { let entry_bcx = fcx.new_temp_block("entry-block"); - fcx.entry_bcx.set(Some(entry_bcx)); + *fcx.entry_bcx.borrow_mut() = Some(entry_bcx); // Use a dummy instruction as the insertion point for all allocas. // This is later removed in FunctionContext::cleanup. diff --git a/src/librustc/middle/trans/common.rs b/src/librustc/middle/trans/common.rs index 97638f7e4690a..369807e98d7e0 100644 --- a/src/librustc/middle/trans/common.rs +++ b/src/librustc/middle/trans/common.rs @@ -322,7 +322,7 @@ impl<'a> FunctionContext<'a> { .unwrap()); } // Remove the cycle between fcx and bcx, so memory can be freed - self.entry_bcx.set(None); + *self.entry_bcx.borrow_mut() = None; } pub fn get_llreturn(&self) -> BasicBlockRef { diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs index 4273b26fe4ade..8f9b67f81e884 100644 --- a/src/librustc/middle/typeck/check/mod.rs +++ b/src/librustc/middle/typeck/check/mod.rs @@ -3333,7 +3333,7 @@ pub fn check_block_with_expected(fcx: &FnCtxt, }; }); - fcx.ps.set(prev); + *fcx.ps.borrow_mut() = prev; } pub fn check_const(ccx: &CrateCtxt, diff --git a/src/libstd/cell.rs b/src/libstd/cell.rs index ec6994728f706..40c6c3ebccf0b 100644 --- a/src/libstd/cell.rs +++ b/src/libstd/cell.rs @@ -164,16 +164,6 @@ impl RefCell { None => fail!("RefCell already borrowed") } } - - /// Sets the value, replacing what was there. - /// - /// # Failure - /// - /// Fails if the value is currently borrowed. - #[inline] - pub fn set(&self, value: T) { - *self.borrow_mut() = value; - } } impl Clone for RefCell { diff --git a/src/libstd/option.rs b/src/libstd/option.rs index ce2c1378fc1a2..ca1ea0169e66a 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -652,7 +652,7 @@ mod tests { fn drop(&mut self) { let ii = &*self.i; let i = ii.borrow().clone(); - ii.set(i + 1); + *ii.borrow_mut() = i + 1; } } diff --git a/src/test/run-pass/cycle-collection.rs b/src/test/run-pass/cycle-collection.rs index ca1e18eb87b97..c6f353136bada 100644 --- a/src/test/run-pass/cycle-collection.rs +++ b/src/test/run-pass/cycle-collection.rs @@ -19,7 +19,7 @@ enum taggy { fn f() { let a_box = @RefCell::new(nil); - a_box.set(cons(a_box)); + *a_box.borrow_mut() = cons(a_box); } pub fn main() { diff --git a/src/test/run-pass/issue-980.rs b/src/test/run-pass/issue-980.rs index f6dc4adcf9b5a..06f95bae5593b 100644 --- a/src/test/run-pass/issue-980.rs +++ b/src/test/run-pass/issue-980.rs @@ -23,7 +23,7 @@ struct Pointy { pub fn main() { let m = @RefCell::new(Pointy { x : no_pointy }); - m.set(Pointy { + *m.borrow_mut() = Pointy { x: yes_pointy(m) - }); + }; } From 3111a298a1a5ec8c8b3d223a9c73dd4142f0415b Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Tue, 1 Apr 2014 22:07:37 -0700 Subject: [PATCH 5/9] test: avoid infinite loop in out-of-stack.rs This fixes #13238. It avoids an infinite loop when compiling the tests with `-g`. Without this change, the debuginfo on `black_box` prevents the method from being inlined, which allows llvm to convert `silent_recurse` into a tail-call. This then loops forever instead of consuming all the stack like it is supposed to. This patch forces inlining `black_box`, which triggers the right error. --- src/test/run-pass/out-of-stack.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/run-pass/out-of-stack.rs b/src/test/run-pass/out-of-stack.rs index d1daa1e365eca..71fde86670322 100644 --- a/src/test/run-pass/out-of-stack.rs +++ b/src/test/run-pass/out-of-stack.rs @@ -17,6 +17,9 @@ use std::os; use std::str; // lifted from the test module +// Inlining to avoid llvm turning the recursive functions into tail calls, +// which doesn't consume stack. +#[inline(always)] pub fn black_box(dummy: T) { unsafe { asm!("" : : "r"(&dummy)) } } fn silent_recurse() { From bbc4c6fec0201b56699cea0df666205ec772bee0 Mon Sep 17 00:00:00 2001 From: Kevin Cantu Date: Sun, 16 Mar 2014 14:26:54 -0700 Subject: [PATCH 6/9] [std::cmp] add missing docs and provide an example --- src/libstd/cmp.rs | 108 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 91 insertions(+), 17 deletions(-) diff --git a/src/libstd/cmp.rs b/src/libstd/cmp.rs index 2fa8ff80d8613..6e62cd42e2cbe 100644 --- a/src/libstd/cmp.rs +++ b/src/libstd/cmp.rs @@ -10,17 +10,36 @@ /*! -The `Ord` and `Eq` comparison traits +Defines the `Ord` and `Eq` comparison traits. -This module contains the definition of both `Ord` and `Eq` which define -the common interfaces for doing comparison. Both are language items -that the compiler uses to implement the comparison operators. Rust code -may implement `Ord` to overload the `<`, `<=`, `>`, and `>=` operators, -and `Eq` to overload the `==` and `!=` operators. +This module defines both `Ord` and `Eq` traits which are used by the compiler +to implement comparison operators. +Rust programs may implement `Ord` to overload the `<`, `<=`, `>`, and `>=` operators, +and may implement `Eq` to overload the `==` and `!=` operators. -*/ +For example, to define a type with a customized definition for the Eq operators, +you could do the following: + +```rust +// Our type. +struct SketchyNum { + num : int +} + +// Our implementation of `Eq` to support `==` and `!=`. +impl Eq for SketchyNum { + // Our custom eq allows numbers which are near eachother to be equal! :D + fn eq(&self, other: &SketchyNum) -> bool { + (self.num - other.num).abs() < 5 + } +} + +// Now these binary operators will work when applied! +assert!(SketchyNum {num: 37} == SketchyNum {num: 34}); +assert!(SketchyNum {num: 25} != SketchyNum {num: 57}); +``` -#![allow(missing_doc)] +*/ /** * Trait for values that can be compared for equality and inequality. @@ -35,8 +54,10 @@ and `Eq` to overload the `==` and `!=` operators. */ #[lang="eq"] pub trait Eq { + /// This method tests for `self` and `other` values to be equal, and is used by `==`. fn eq(&self, other: &Self) -> bool; + /// This method tests for `!=`. #[inline] fn ne(&self, other: &Self) -> bool { !self.eq(other) } } @@ -55,6 +76,7 @@ pub trait TotalEq: Eq { fn assert_receiver_is_total_eq(&self) {} } +/// A macro which defines an implementation of TotalEq for a given type. macro_rules! totaleq_impl( ($t:ty) => { impl TotalEq for $t {} @@ -78,11 +100,29 @@ totaleq_impl!(uint) totaleq_impl!(char) +/// An ordering is, e.g, a result of a comparison between two values. #[deriving(Clone, Eq, Show)] -pub enum Ordering { Less = -1, Equal = 0, Greater = 1 } +pub enum Ordering { + /// An ordering where a compared value is less [than another]. + Less = -1, + /// An ordering where a compared value is equal [to another]. + Equal = 0, + /// An ordering where a compared value is greater [than another]. + Greater = 1 +} -/// Trait for types that form a total order +/// Trait for types that form a total order. pub trait TotalOrd: TotalEq + Ord { + /// This method returns an ordering between `self` and `other` values. + /// + /// By convention, `self.cmp(&other)` returns the ordering matching + /// the expression `self other` if true. For example: + /// + /// ``` + /// assert_eq!( 5u.cmp(&10), Less); // because 5 < 10 + /// assert_eq!(10u.cmp(&5), Greater); // because 10 > 5 + /// assert_eq!( 5u.cmp(&5), Equal); // because 5 == 5 + /// ``` fn cmp(&self, other: &Self) -> Ordering; } @@ -99,6 +139,7 @@ impl Ord for Ordering { fn lt(&self, other: &Ordering) -> bool { (*self as int) < (*other as int) } } +/// A macro which defines an implementation of TotalOrd for a given type. macro_rules! totalord_impl( ($t:ty) => { impl TotalOrd for $t { @@ -128,8 +169,11 @@ totalord_impl!(uint) totalord_impl!(char) /** -Return `o1` if it is not `Equal`, otherwise `o2`. Simulates the -lexical ordering on a type `(int, int)`. + * Combine orderings, lexically. + * + * For example for a type `(int, int)`, two comparisons could be done. + * If the first ordering is different, the first ordering is all that must be returned. + * If the first ordering is equal, then second ordering is returned. */ #[inline] pub fn lexical_ordering(o1: Ordering, o2: Ordering) -> Ordering { @@ -151,11 +195,18 @@ pub fn lexical_ordering(o1: Ordering, o2: Ordering) -> Ordering { */ #[lang="ord"] pub trait Ord: Eq { + /// This method tests less than (for `self` and `other`) and is used by the `<` operator. fn lt(&self, other: &Self) -> bool; + + /// This method tests less than or equal to (`<=`). #[inline] fn le(&self, other: &Self) -> bool { !other.lt(self) } + + /// This method tests greater than (`>`). #[inline] fn gt(&self, other: &Self) -> bool { other.lt(self) } + + /// This method tests greater than or equal to (`>=`). #[inline] fn ge(&self, other: &Self) -> bool { !self.lt(other) } } @@ -165,14 +216,17 @@ pub trait Ord: Eq { /// container types; e.g. it is often desirable to be able to use `&str` /// values to look up entries in a container with `~str` keys. pub trait Equiv { + /// Implement this function to decide equivalent values. fn equiv(&self, other: &T) -> bool; } +/// Compare and return the minimum of two values. #[inline] pub fn min(v1: T, v2: T) -> T { if v1 < v2 { v1 } else { v2 } } +/// Compare and return the maximum of two values. #[inline] pub fn max(v1: T, v2: T) -> T { if v1 > v2 { v1 } else { v2 } @@ -184,11 +238,11 @@ mod test { #[test] fn test_int_totalord() { - assert_eq!(5.cmp(&10), Less); - assert_eq!(10.cmp(&5), Greater); - assert_eq!(5.cmp(&5), Equal); - assert_eq!((-5).cmp(&12), Less); - assert_eq!(12.cmp(-5), Greater); + assert_eq!(5u.cmp(&10), Less); + assert_eq!(10u.cmp(&5), Greater); + assert_eq!(5u.cmp(&5), Equal); + assert_eq!((-5u).cmp(&12), Less); + assert_eq!(12u.cmp(-5), Greater); } #[test] @@ -210,4 +264,24 @@ mod test { t(Greater, o, Greater); } } + + #[test] + fn test_user_defined_eq() { + // Our type. + struct SketchyNum { + num : int + } + + // Our implementation of `Eq` to support `==` and `!=`. + impl Eq for SketchyNum { + // Our custom eq allows numbers which are near eachother to be equal! :D + fn eq(&self, other: &SketchyNum) -> bool { + (self.num - other.num).abs() < 5 + } + } + + // Now these binary operators will work when applied! + assert!(SketchyNum {num: 37} == SketchyNum {num: 34}); + assert!(SketchyNum {num: 25} != SketchyNum {num: 57}); + } } From c9e4e1c7c34f25f502af462ba91597a3b2132f24 Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Thu, 3 Apr 2014 23:03:01 -0700 Subject: [PATCH 7/9] rustdoc: Fix reporting of ignored tests librustdoc: instead of skipping ignored tests, pass them to libtest so it can report them as such. If a test is marked as `notrust`, however, it will not show up in the final report. --- src/librustdoc/html/markdown.rs | 13 +++++++------ src/librustdoc/test.rs | 6 +++--- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index ff2462cfb22ba..e6fb5629eaf31 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -268,24 +268,25 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) { extern fn block(_ob: *buf, text: *buf, lang: *buf, opaque: *libc::c_void) { unsafe { if text.is_null() { return } - let (should_fail, no_run, ignore) = if lang.is_null() { - (false, false, false) + let (should_fail, no_run, ignore, notrust) = if lang.is_null() { + (false, false, false, false) } else { slice::raw::buf_as_slice((*lang).data, (*lang).size as uint, |lang| { let s = str::from_utf8(lang).unwrap(); (s.contains("should_fail"), s.contains("no_run"), - s.contains("ignore") || s.contains("notrust")) + s.contains("ignore"), + s.contains("notrust")) }) }; - if ignore { return } + if notrust { return } slice::raw::buf_as_slice((*text).data, (*text).size as uint, |text| { let tests = &mut *(opaque as *mut ::test::Collector); let text = str::from_utf8(text).unwrap(); let mut lines = text.lines().map(|l| stripped_filtered_line(l).unwrap_or(l)); let text = lines.collect::<~[&str]>().connect("\n"); - tests.add_test(text, should_fail, no_run); + tests.add_test(text, should_fail, no_run, ignore); }) } } diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index 2138d210443f5..3ede325997d73 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -221,7 +221,7 @@ impl Collector { } } - pub fn add_test(&mut self, test: ~str, should_fail: bool, no_run: bool) { + pub fn add_test(&mut self, test: ~str, should_fail: bool, no_run: bool, should_ignore: bool) { let name = if self.use_headers { let s = self.current_header.as_ref().map(|s| s.as_slice()).unwrap_or(""); format!("{}_{}", s, self.cnt) @@ -236,7 +236,7 @@ impl Collector { self.tests.push(testing::TestDescAndFn { desc: testing::TestDesc { name: testing::DynTestName(name), - ignore: false, + ignore: should_ignore, should_fail: false, // compiler failures are test failures }, testfn: testing::DynTestFn(proc() { From 212532491999cf48390b7407d2db2a5176f4f0fe Mon Sep 17 00:00:00 2001 From: Christopher Kendell Date: Fri, 4 Apr 2014 14:03:40 -0700 Subject: [PATCH 8/9] Small change to example to make variable values more sensible. --- src/doc/tutorial.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/doc/tutorial.md b/src/doc/tutorial.md index 1da3077fa72e8..21282fea6f65b 100644 --- a/src/doc/tutorial.md +++ b/src/doc/tutorial.md @@ -1492,8 +1492,8 @@ Rust uses the unary star operator (`*`) to access the contents of a box or pointer, similarly to C. ~~~ -let owned = ~20; -let borrowed = &30; +let owned = ~10; +let borrowed = &20; let sum = *owned + *borrowed; ~~~ From d646ff8237a150ee51db6289a5a9c6b025538b03 Mon Sep 17 00:00:00 2001 From: Christopher Kendell Date: Fri, 4 Apr 2014 14:31:37 -0700 Subject: [PATCH 9/9] Removed all instance of `@` in code examples. --- src/doc/tutorial.md | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/doc/tutorial.md b/src/doc/tutorial.md index 21282fea6f65b..d0463ca17d3e0 100644 --- a/src/doc/tutorial.md +++ b/src/doc/tutorial.md @@ -1520,7 +1520,7 @@ can sometimes make code awkward and parenthesis-filled. # struct Point { x: f64, y: f64 } # enum Shape { Rectangle(Point, Point) } # impl Shape { fn area(&self) -> int { 0 } } -let start = @Point { x: 10.0, y: 20.0 }; +let start = ~Point { x: 10.0, y: 20.0 }; let end = ~Point { x: (*start).x + 100.0, y: (*start).y + 100.0 }; let rect = &Rectangle(*start, *end); let area = (*rect).area(); @@ -1534,7 +1534,7 @@ dot), so in most cases, explicitly dereferencing the receiver is not necessary. # struct Point { x: f64, y: f64 } # enum Shape { Rectangle(Point, Point) } # impl Shape { fn area(&self) -> int { 0 } } -let start = @Point { x: 10.0, y: 20.0 }; +let start = ~Point { x: 10.0, y: 20.0 }; let end = ~Point { x: start.x + 100.0, y: start.y + 100.0 }; let rect = &Rectangle(*start, *end); let area = rect.area(); @@ -1546,7 +1546,7 @@ something silly like ~~~ # struct Point { x: f64, y: f64 } -let point = &@~Point { x: 10.0, y: 20.0 }; +let point = &~Point { x: 10.0, y: 20.0 }; println!("{:f}", point.x); ~~~ @@ -1907,7 +1907,6 @@ to a reference. // As with typical function arguments, owned pointers // are automatically converted to references -(@s).draw_reference(); (~s).draw_reference(); // Unlike typical function arguments, the self value will @@ -1918,7 +1917,7 @@ s.draw_reference(); (& &s).draw_reference(); // ... and dereferenced and borrowed -(&@~s).draw_reference(); +(&~s).draw_reference(); ~~~ Implementations may also define standalone (sometimes called "static") @@ -2403,7 +2402,7 @@ that, like strings and vectors, objects have dynamic size and may only be referred to via one of the pointer types. Other pointer types work as well. Casts to traits may only be done with compatible pointers so, -for example, an `@Circle` may not be cast to an `~Drawable`. +for example, an `&Circle` may not be cast to an `~Drawable`. ~~~ # type Circle = int; type Rectangle = int; @@ -2506,8 +2505,8 @@ use std::f64::consts::PI; # impl Circle for CircleStruct { fn radius(&self) -> f64 { (self.area() / PI).sqrt() } } # impl Shape for CircleStruct { fn area(&self) -> f64 { PI * square(self.radius) } } -let concrete = @CircleStruct{center:Point{x:3.0,y:4.0},radius:5.0}; -let mycircle: @Circle = concrete as @Circle; +let concrete = ~CircleStruct{center:Point{x:3.0,y:4.0},radius:5.0}; +let mycircle: ~Circle = concrete as ~Circle; let nonsense = mycircle.radius() * mycircle.area(); ~~~