Skip to content

Commit 69f27c8

Browse files
committed
Auto merge of #28665 - steveklabnik:rollup, r=steveklabnik
- Successful merges: #28319, #28588, #28637, #28652, #28654, #28655 - Failed merges: #28621
2 parents e7a7388 + f4dc6c7 commit 69f27c8

File tree

6 files changed

+45
-24
lines changed

6 files changed

+45
-24
lines changed

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> {

src/doc/trpl/lifetimes.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,9 @@ fn frob<'a, 'b>(s: &'a str, t: &'b str) -> &str; // Expanded: Output lifetime is
349349
fn get_mut(&mut self) -> &mut T; // elided
350350
fn get_mut<'a>(&'a mut self) -> &'a mut T; // expanded
351351
352-
fn args<T:ToCStr>(&mut self, args: &[T]) -> &mut Command // elided
353-
fn args<'a, 'b, T:ToCStr>(&'a mut self, args: &'b [T]) -> &'a mut Command // expanded
352+
fn args<T:ToCStr>(&mut self, args: &[T]) -> &mut Command; // elided
353+
fn args<'a, 'b, T:ToCStr>(&'a mut self, args: &'b [T]) -> &'a mut Command; // expanded
354354
355355
fn new(buf: &mut [u8]) -> BufWriter; // elided
356-
fn new<'a>(buf: &'a mut [u8]) -> BufWriter<'a> // expanded
356+
fn new<'a>(buf: &'a mut [u8]) -> BufWriter<'a>; // expanded
357357
```

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) { }

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 {

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 }

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.

0 commit comments

Comments
 (0)