Skip to content

Commit 16e1f8c

Browse files
committed
---
yaml --- r: 236442 b: refs/heads/auto c: f4dc6c7 h: refs/heads/master v: v3
1 parent 24dfc97 commit 16e1f8c

File tree

8 files changed

+52
-36
lines changed

8 files changed

+52
-36
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
88
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
99
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1010
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
11-
refs/heads/auto: 29048c3a803d085eb814c4410f9258dde81af59d
11+
refs/heads/auto: f4dc6c785c243b7de47d2e40caafe401f7b28aad
1212
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1313
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336
1414
refs/tags/0.2: 1754d02027f2924bed83b0160ee340c7f41d5ea1

branches/auto/src/doc/trpl/closures.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,9 @@ fn factory() -> &(Fn(i32) -> i32) {
411411
```
412412

413413
Right. Because we have a reference, we need to give it a lifetime. But
414-
our `factory()` function takes no arguments, so elision doesn’t kick in
415-
here. What lifetime can we choose? `'static`:
414+
our `factory()` function takes no arguments, so
415+
[elision](lifetimes.html#lifetime-elision) doesn’t kick in here. Then what
416+
choices do we have? Try `'static`:
416417

417418
```rust,ignore
418419
fn factory() -> &'static (Fn(i32) -> i32) {
@@ -432,7 +433,7 @@ But we get another error:
432433
```text
433434
error: mismatched types:
434435
expected `&'static core::ops::Fn(i32) -> i32`,
435-
found `[closure <anon>:7:9: 7:20]`
436+
found `[closure@<anon>:7:9: 7:20]`
436437
(expected &-ptr,
437438
found closure) [E0308]
438439
|x| x + num
@@ -441,21 +442,17 @@ error: mismatched types:
441442
```
442443

443444
This error is letting us know that we don’t have a `&'static Fn(i32) -> i32`,
444-
we have a `[closure <anon>:7:9: 7:20]`. Wait, what?
445+
we have a `[closure@<anon>:7:9: 7:20]`. Wait, what?
445446

446447
Because each closure generates its own environment `struct` and implementation
447448
of `Fn` and friends, these types are anonymous. They exist just solely for
448-
this closure. So Rust shows them as `closure <anon>`, rather than some
449+
this closure. So Rust shows them as `closure@<anon>`, rather than some
449450
autogenerated name.
450451

451-
But why doesn’t our closure implement `&'static Fn`? Well, as we discussed before,
452-
closures borrow their environment. And in this case, our environment is based
453-
on a stack-allocated `5`, the `num` variable binding. So the borrow has a lifetime
454-
of the stack frame. So if we returned this closure, the function call would be
455-
over, the stack frame would go away, and our closure is capturing an environment
456-
of garbage memory!
457-
458-
So what to do? This _almost_ works:
452+
The error also points out that the return type is expected to be a reference,
453+
but what we are trying to return is not. Further, we cannot directly assign a
454+
`'static` lifetime to an object. So we'll take a different approach and return
455+
a "trait object" by `Box`ing up the `Fn`. This _almost_ works:
459456

460457
```rust,ignore
461458
fn factory() -> Box<Fn(i32) -> i32> {
@@ -471,7 +468,7 @@ assert_eq!(6, answer);
471468
# }
472469
```
473470

474-
We use a trait object, by `Box`ing up the `Fn`. There’s just one last problem:
471+
There’s just one last problem:
475472

476473
```text
477474
error: closure may outlive the current function, but it borrows `num`,
@@ -480,8 +477,12 @@ Box::new(|x| x + num)
480477
^~~~~~~~~~~
481478
```
482479

483-
We still have a reference to the parent stack frame. With one last fix, we can
484-
make this work:
480+
Well, as we discussed before, closures borrow their environment. And in this
481+
case, our environment is based on a stack-allocated `5`, the `num` variable
482+
binding. So the borrow has a lifetime of the stack frame. So if we returned
483+
this closure, the function call would be over, the stack frame would go away,
484+
and our closure is capturing an environment of garbage memory! With one last
485+
fix, we can make this work:
485486

486487
```rust
487488
fn factory() -> Box<Fn(i32) -> i32> {

branches/auto/src/libcore/mem.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,11 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
434434
/// While this does call the argument's implementation of `Drop`, it will not
435435
/// release any borrows, as borrows are based on lexical scope.
436436
///
437+
/// This effectively does nothing for
438+
/// [types which implement `Copy`](../../book/ownership.html#copy-types),
439+
/// e.g. integers. Such values are copied and _then_ moved into the function,
440+
/// so the value persists after this function call.
441+
///
437442
/// # Examples
438443
///
439444
/// Basic usage:
@@ -486,6 +491,21 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
486491
/// let borrow = x.borrow();
487492
/// println!("{}", *borrow);
488493
/// ```
494+
///
495+
/// Integers and other types implementing `Copy` are unaffected by `drop()`
496+
///
497+
/// ```
498+
/// #[derive(Copy, Clone)]
499+
/// struct Foo(u8);
500+
///
501+
/// let x = 1;
502+
/// let y = Foo(2);
503+
/// drop(x); // a copy of `x` is moved and dropped
504+
/// drop(y); // a copy of `y` is moved and dropped
505+
///
506+
/// println!("x: {}, y: {}", x, y.0); // still available
507+
/// ```
508+
///
489509
#[inline]
490510
#[stable(feature = "rust1", since = "1.0.0")]
491511
pub fn drop<T>(_x: T) { }

branches/auto/src/libcore/slice.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,15 +1410,15 @@ impl<'a, T> ExactSizeIterator for ChunksMut<'a, T> {}
14101410
// Free functions
14111411
//
14121412

1413-
/// Converts a pointer to A into a slice of length 1 (without copying).
1413+
/// Converts a reference to A into a slice of length 1 (without copying).
14141414
#[unstable(feature = "ref_slice", issue = "27774")]
14151415
pub fn ref_slice<A>(s: &A) -> &[A] {
14161416
unsafe {
14171417
from_raw_parts(s, 1)
14181418
}
14191419
}
14201420

1421-
/// Converts a pointer to A into a slice of length 1 (without copying).
1421+
/// Converts a reference to A into a slice of length 1 (without copying).
14221422
#[unstable(feature = "ref_slice", issue = "27774")]
14231423
pub fn mut_ref_slice<A>(s: &mut A) -> &mut [A] {
14241424
unsafe {

branches/auto/src/libcore/str/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ impl Utf8Error {
115115
/// Returns the index in the given string up to which valid UTF-8 was
116116
/// verified.
117117
///
118-
/// Starting at the index provided, but not necessarily at it precisely, an
119-
/// invalid UTF-8 encoding sequence was found.
118+
/// It is the maximum index such that `from_utf8(input[..index])`
119+
/// would return `Some(_)`.
120120
#[unstable(feature = "utf8_error", reason = "method just added",
121121
issue = "27734")]
122122
pub fn valid_up_to(&self) -> usize { self.valid_up_to }

branches/auto/src/librustc_trans/back/linker.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use back::archive;
1919
use metadata::csearch;
2020
use middle::dependency_format::Linkage;
2121
use session::Session;
22-
use session::config::DebugInfoLevel::{NoDebugInfo, LimitedDebugInfo, FullDebugInfo};
2322
use session::config::CrateTypeDylib;
2423
use session::config;
2524
use syntax::ast;
@@ -281,17 +280,9 @@ impl<'a> Linker for MsvcLinker<'a> {
281280
}
282281

283282
fn debuginfo(&mut self) {
284-
match self.sess.opts.debuginfo {
285-
NoDebugInfo => {
286-
// Do nothing if debuginfo is disabled
287-
},
288-
LimitedDebugInfo |
289-
FullDebugInfo => {
290-
// This will cause the Microsoft linker to generate a PDB file
291-
// from the CodeView line tables in the object files.
292-
self.cmd.arg("/DEBUG");
293-
}
294-
}
283+
// This will cause the Microsoft linker to generate a PDB file
284+
// from the CodeView line tables in the object files.
285+
self.cmd.arg("/DEBUG");
295286
}
296287

297288
fn whole_archives(&mut self) {

branches/auto/src/libstd/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ macro_rules! print {
9898
($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*)));
9999
}
100100

101-
/// Macro for printing to the standard output.
101+
/// Macro for printing to the standard output, with a newline.
102102
///
103103
/// Use the `format!` syntax to write data to the standard output.
104104
/// See `std::fmt` for more information.

branches/auto/src/test/run-make/output-type-permutations/Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ all:
55
$(call REMOVE_RLIBS,bar)
66
$(call REMOVE_DYLIBS,bar)
77
rm $(TMPDIR)/libbar.a
8-
rm -f $(TMPDIR)/bar.{exp,lib}
8+
rm -f $(TMPDIR)/bar.{exp,lib,pdb}
99
# Check that $(TMPDIR) is empty.
1010
[ "$$(ls -1 $(TMPDIR) | wc -l)" -eq "0" ]
1111

1212
$(RUSTC) foo.rs --crate-type=bin
1313
rm $(TMPDIR)/$(call BIN,bar)
14+
rm -f $(TMPDIR)/bar.pdb
1415
[ "$$(ls -1 $(TMPDIR) | wc -l)" -eq "0" ]
1516

1617
$(RUSTC) foo.rs --emit=asm,llvm-ir,llvm-bc,obj,link
@@ -19,6 +20,7 @@ all:
1920
rm $(TMPDIR)/bar.s
2021
rm $(TMPDIR)/bar.o
2122
rm $(TMPDIR)/$(call BIN,bar)
23+
rm -f $(TMPDIR)/bar.pdb
2224
[ "$$(ls -1 $(TMPDIR) | wc -l)" -eq "0" ]
2325

2426
$(RUSTC) foo.rs --emit=asm -o $(TMPDIR)/foo
@@ -39,6 +41,7 @@ all:
3941

4042
$(RUSTC) foo.rs --emit=link -o $(TMPDIR)/$(call BIN,foo)
4143
rm $(TMPDIR)/$(call BIN,foo)
44+
rm -f $(TMPDIR)/foo.pdb
4245
[ "$$(ls -1 $(TMPDIR) | wc -l)" -eq "0" ]
4346

4447
$(RUSTC) foo.rs --crate-type=rlib -o $(TMPDIR)/foo
@@ -47,7 +50,7 @@ all:
4750

4851
$(RUSTC) foo.rs --crate-type=dylib -o $(TMPDIR)/$(call BIN,foo)
4952
rm $(TMPDIR)/$(call BIN,foo)
50-
rm -f $(TMPDIR)/foo.{exp,lib}
53+
rm -f $(TMPDIR)/foo.{exp,lib,pdb}
5154
[ "$$(ls -1 $(TMPDIR) | wc -l)" -eq "0" ]
5255

5356
$(RUSTC) foo.rs --crate-type=staticlib -o $(TMPDIR)/foo
@@ -56,6 +59,7 @@ all:
5659

5760
$(RUSTC) foo.rs --crate-type=bin -o $(TMPDIR)/$(call BIN,foo)
5861
rm $(TMPDIR)/$(call BIN,foo)
62+
rm -f $(TMPDIR)/foo.pdb
5963
[ "$$(ls -1 $(TMPDIR) | wc -l)" -eq "0" ]
6064

6165
$(RUSTC) foo.rs --emit=asm,llvm-ir,llvm-bc,obj,link --crate-type=staticlib

0 commit comments

Comments
 (0)