Skip to content

Commit ae8a3c9

Browse files
committed
Auto merge of #26182 - Manishearth:rollup, r=Manishearth
- Successful merges: #26142, #26143, #26145, #26146, #26164, #26174 - Failed merges:
2 parents d8a9570 + c8519c9 commit ae8a3c9

32 files changed

+121
-53
lines changed

src/doc/trpl/enums.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,45 @@ equality yet, but we’ll find out in the [`traits`][traits] section.
6464
[match]: match.html
6565
[if-let]: if-let.html
6666
[traits]: traits.html
67+
68+
# Constructors as functions
69+
70+
An enum’s constructors can also be used like functions. For example:
71+
72+
```rust
73+
# enum Message {
74+
# Write(String),
75+
# }
76+
let m = Message::Write("Hello, world".to_string());
77+
```
78+
79+
Is the same as
80+
81+
```rust
82+
# enum Message {
83+
# Write(String),
84+
# }
85+
fn foo(x: String) -> Message {
86+
Message::Write(x)
87+
}
88+
89+
let x = foo("Hello, world".to_string());
90+
```
91+
92+
This is not immediately useful to us, but when we get to
93+
[`closures`][closures], we’ll talk about passing functions as arguments to
94+
other functions. For example, with [`iterators`][iterators], we can do this
95+
to convert a vector of `String`s into a vector of `Message::Write`s:
96+
97+
```rust
98+
# enum Message {
99+
# Write(String),
100+
# }
101+
102+
let v = vec!["Hello".to_string(), "World".to_string()];
103+
104+
let v1: Vec<Message> = v.into_iter().map(Message::Write).collect();
105+
```
106+
107+
[closures]: closures.html
108+
[iterators]: iterators.html

src/doc/trpl/ffi.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ However it is often desired that the callback is targeted to a special
238238
Rust object. This could be the object that represents the wrapper for the
239239
respective C object.
240240
241-
This can be achieved by passing an unsafe pointer to the object down to the
241+
This can be achieved by passing an raw pointer to the object down to the
242242
C library. The C library can then include the pointer to the Rust object in
243243
the notification. This will allow the callback to unsafely access the
244244
referenced Rust object.
@@ -370,7 +370,7 @@ On OSX, frameworks behave with the same semantics as a dynamic library.
370370
371371
# Unsafe blocks
372372
373-
Some operations, like dereferencing unsafe pointers or calling functions that have been marked
373+
Some operations, like dereferencing raw pointers or calling functions that have been marked
374374
unsafe are only allowed inside unsafe blocks. Unsafe blocks isolate unsafety and are a promise to
375375
the compiler that the unsafety does not leak out of the block.
376376

src/doc/trpl/functions.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ an expression, and a `let` can only begin a statement, not an expression.
144144
Note that assigning to an already-bound variable (e.g. `y = 5`) is still an
145145
expression, although its value is not particularly useful. Unlike other
146146
languages where an assignment evaluates to the assigned value (e.g. `5` in the
147-
previous example), in Rust the value of an assignment is an empty tuple `()`:
147+
previous example), in Rust the value of an assignment is an empty tuple `()`
148+
because the assigned value can have [just one owner](ownership.html), and any
149+
other returned value would be too surprising:
148150

149151
```rust
150152
let mut y = 5;

src/doc/trpl/raw-pointers.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ println!("raw points at {}", *raw);
5252
It gives this error:
5353

5454
```text
55-
error: dereference of unsafe pointer requires unsafe function or block [E0133]
56-
println!("raw points at{}", *raw);
57-
^~~~
55+
error: dereference of raw pointer requires unsafe function or block [E0133]
56+
println!("raw points at {}", *raw);
57+
^~~~
5858
```
5959

6060
When you dereference a raw pointer, you’re taking responsibility that it’s not

src/doc/trpl/strings.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,30 @@ let dog = hachiko.chars().nth(1); // kinda like hachiko[1]
117117

118118
This emphasizes that we have to go through the whole list of `chars`.
119119

120+
## Slicing
121+
122+
You can get a slice of a string with slicing syntax:
123+
124+
```rust
125+
let dog = "hachiko";
126+
let hachi = &dog[0..5];
127+
```
128+
129+
But note that these are _byte_ offsets, not _character_ offsets. So
130+
this will fail at runtime:
131+
132+
```rust,should_panic
133+
let dog = "忠犬ハチ公";
134+
let hachi = &dog[0..2];
135+
```
136+
137+
with this error:
138+
139+
```text
140+
thread '<main>' panicked at 'index 0 and/or 2 in `忠犬ハチ公` do not lie on
141+
character boundary'
142+
```
143+
120144
## Concatenation
121145

122146
If you have a `String`, you can concatenate a `&str` to the end of it:

src/liballoc/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
//!
2323
//! ## Boxed values
2424
//!
25-
//! The [`Box`](boxed/index.html) type is the core owned pointer type in Rust.
26-
//! There can only be one owner of a `Box`, and the owner can decide to mutate
27-
//! the contents, which live on the heap.
25+
//! The [`Box`](boxed/index.html) type is a smart pointer type. There can
26+
//! only be one owner of a `Box`, and the owner can decide to mutate the
27+
//! contents, which live on the heap.
2828
//!
2929
//! This type can be sent among threads efficiently as the size of a `Box` value
3030
//! is the same as that of a pointer. Tree-like data structures are often built

src/libcollections/slice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ impl<T> [T] {
370370
core_slice::SliceExt::get_unchecked_mut(self, index)
371371
}
372372

373-
/// Returns an unsafe pointer to the slice's buffer
373+
/// Returns an raw pointer to the slice's buffer
374374
///
375375
/// The caller must ensure that the slice outlives the pointer this
376376
/// function returns, or else it will end up pointing to garbage.

src/libcollections/str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ impl str {
525525
core_str::StrExt::as_bytes(&self[..])
526526
}
527527

528-
/// Returns an unsafe pointer to the `&str`'s buffer.
528+
/// Returns a raw pointer to the `&str`'s buffer.
529529
///
530530
/// The caller must ensure that the string outlives this pointer, and
531531
/// that it is not

src/libcollections/vec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,9 +1213,9 @@ impl<T: PartialEq> Vec<T> {
12131213
// Duplicate, advance r. End of vec. Truncate to w.
12141214

12151215
let ln = self.len();
1216-
if ln < 1 { return; }
1216+
if ln <= 1 { return; }
12171217

1218-
// Avoid bounds checks by using unsafe pointers.
1218+
// Avoid bounds checks by using raw pointers.
12191219
let p = self.as_mut_ptr();
12201220
let mut r: usize = 1;
12211221
let mut w: usize = 1;

src/libcore/intrinsics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use marker::Sized;
4646

4747
extern "rust-intrinsic" {
4848

49-
// NB: These intrinsics take unsafe pointers because they mutate aliased
49+
// NB: These intrinsics take raw pointers because they mutate aliased
5050
// memory, which is not valid for either `&` or `&mut`.
5151

5252
pub fn atomic_cxchg<T>(dst: *mut T, old: T, src: T) -> T;

src/libcore/marker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ macro_rules! impls{
357357
/// struct is dropped, it may in turn drop one or more instances of
358358
/// the type `T`, though that may not be apparent from the other
359359
/// structure of the type itself. This is commonly necessary if the
360-
/// structure is using an unsafe pointer like `*mut T` whose referent
360+
/// structure is using a raw pointer like `*mut T` whose referent
361361
/// may be dropped when the type is dropped, as a `*mut T` is
362362
/// otherwise not treated as owned.
363363
///

src/libcore/ptr.rs

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

1111
// FIXME: talk about offset, copy_memory, copy_nonoverlapping_memory
1212

13-
//! Operations on unsafe pointers, `*const T`, and `*mut T`.
13+
//! Operations on raw pointers, `*const T`, and `*mut T`.
1414
//!
15-
//! Working with unsafe pointers in Rust is uncommon,
15+
//! Working with raw pointers in Rust is uncommon,
1616
//! typically limited to a few patterns.
1717
//!
1818
//! Use the `null` function to create null pointers, and the `is_null` method
1919
//! of the `*const T` type to check for null. The `*const T` type also defines
2020
//! the `offset` method, for pointer math.
2121
//!
22-
//! # Common ways to create unsafe pointers
22+
//! # Common ways to create raw pointers
2323
//!
2424
//! ## 1. Coerce a reference (`&T`) or mutable reference (`&mut T`).
2525
//!
@@ -86,7 +86,7 @@
8686
//!
8787
//! Usually you wouldn't literally use `malloc` and `free` from Rust,
8888
//! but C APIs hand out a lot of pointers generally, so are a common source
89-
//! of unsafe pointers in Rust.
89+
//! of raw pointers in Rust.
9090
9191
#![stable(feature = "rust1", since = "1.0.0")]
9292
#![doc(primitive = "pointer")]

src/librustc/middle/check_const.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
//
1414
// - For each *mutable* static item, it checks that its **type**:
1515
// - doesn't have a destructor
16-
// - doesn't own an owned pointer
16+
// - doesn't own a box
1717
//
1818
// - For each *immutable* static item, it checks that its **value**:
19-
// - doesn't own owned, managed pointers
19+
// - doesn't own a box
2020
// - doesn't contain a struct literal or a call to an enum variant / struct constructor where
2121
// - the type of the struct/enum has a dtor
2222
//

src/librustc/middle/effect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EffectCheckVisitor<'a, 'tcx> {
162162
debug!("effect: unary case, base type is {}",
163163
ppaux::ty_to_string(self.tcx, base_type));
164164
if let ty::ty_ptr(_) = base_type.sty {
165-
self.require_unsafe(expr.span, "dereference of unsafe pointer")
165+
self.require_unsafe(expr.span, "dereference of raw pointer")
166166
}
167167
}
168168
ast::ExprAssign(ref base, _) | ast::ExprAssignOp(_, ref base, _) => {

src/librustc/middle/mem_categorization.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,7 @@ pub enum AliasableReason {
14101410

14111411
impl<'tcx> cmt_<'tcx> {
14121412
pub fn guarantor(&self) -> cmt<'tcx> {
1413-
//! Returns `self` after stripping away any owned pointer derefs or
1413+
//! Returns `self` after stripping away any derefs or
14141414
//! interior content. The return value is basically the `cmt` which
14151415
//! determines how long the value in `self` remains live.
14161416
@@ -1546,7 +1546,7 @@ impl<'tcx> cmt_<'tcx> {
15461546
format!("`Box` content")
15471547
}
15481548
UnsafePtr(..) => {
1549-
format!("dereference of unsafe pointer")
1549+
format!("dereference of raw pointer")
15501550
}
15511551
BorrowedPtr(..) => {
15521552
format!("borrowed content")

src/librustc/middle/ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3646,7 +3646,7 @@ impl TypeContents {
36463646
*self & TC::ReachesAll)
36473647
}
36483648

3649-
/// Includes only those bits that still apply when indirected through an unsafe pointer (`*`)
3649+
/// Includes only those bits that still apply when indirected through a raw pointer (`*`)
36503650
pub fn unsafe_pointer(&self) -> TypeContents {
36513651
*self & TC::ReachesAll
36523652
}

src/librustc_borrowck/borrowck/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ overwrite (or freeze) `(*x).f`, and thus invalidate the reference
170170
that was created. In general it holds that when a path is
171171
lent, restrictions are issued for all the owning prefixes of that
172172
path. In this case, the path `*x` owns the path `(*x).f` and,
173-
because `x` is an owned pointer, the path `x` owns the path `*x`.
173+
because `x` has ownership, the path `x` owns the path `*x`.
174174
Therefore, borrowing `(*x).f` yields restrictions on both
175175
`*x` and `x`.
176176

@@ -286,7 +286,7 @@ MUTABILITY(X, imm) // M-Var-Imm
286286

287287
### Checking mutability of owned content
288288

289-
Fields and owned pointers inherit their mutability from
289+
Fields and boxes inherit their mutability from
290290
their base expressions, so both of their rules basically
291291
delegate the check to the base expression `LV`:
292292

@@ -387,7 +387,7 @@ LIFETIME(X, LT, MQ) // L-Local
387387

388388
### Checking lifetime for owned content
389389

390-
The lifetime of a field or owned pointer is the same as the lifetime
390+
The lifetime of a field or box is the same as the lifetime
391391
of its owner:
392392

393393
```text
@@ -466,10 +466,10 @@ origin of inherited mutability.
466466

467467
Because the mutability of owned referents is inherited, restricting an
468468
owned referent is similar to restricting a field, in that it implies
469-
restrictions on the pointer. However, owned pointers have an important
469+
restrictions on the pointer. However, boxes have an important
470470
twist: if the owner `LV` is mutated, that causes the owned referent
471471
`*LV` to be freed! So whenever an owned referent `*LV` is borrowed, we
472-
must prevent the owned pointer `LV` from being mutated, which means
472+
must prevent the box `LV` from being mutated, which means
473473
that we always add `MUTATE` and `CLAIM` to the restriction set imposed
474474
on `LV`:
475475

@@ -648,7 +648,7 @@ fn main() {
648648
```
649649

650650
Clause (2) propagates the restrictions on the referent to the pointer
651-
itself. This is the same as with an owned pointer, though the
651+
itself. This is the same as with an box, though the
652652
reasoning is mildly different. The basic goal in all cases is to
653653
prevent the user from establishing another route to the same data. To
654654
see what I mean, let's examine various cases of what can go wrong and

src/librustc_borrowck/borrowck/gather_loans/gather_moves.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ fn gather_move<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
137137
move_info.id, move_info.kind);
138138
}
139139
None => {
140-
// move from rvalue or unsafe pointer, hence ok
140+
// move from rvalue or raw pointer, hence ok
141141
}
142142
}
143143
}

src/librustc_borrowck/borrowck/gather_loans/restrictions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<'a, 'tcx> RestrictionsContext<'a, 'tcx> {
110110
mc::Unique => {
111111
// R-Deref-Send-Pointer
112112
//
113-
// When we borrow the interior of an owned pointer, we
113+
// When we borrow the interior of a box, we
114114
// cannot permit the base to be mutated, because that
115115
// would cause the unique pointer to be freed.
116116
//
@@ -145,7 +145,7 @@ impl<'a, 'tcx> RestrictionsContext<'a, 'tcx> {
145145
}
146146
}
147147
}
148-
// Borrowck is not relevant for unsafe pointers
148+
// Borrowck is not relevant for raw pointers
149149
mc::UnsafePtr(..) => Safe
150150
}
151151
}

src/librustc_typeck/check/cast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ impl<'tcx> CastCheck<'tcx> {
341341
// Due to the limitations of LLVM global constants,
342342
// region pointers end up pointing at copies of
343343
// vector elements instead of the original values.
344-
// To allow unsafe pointers to work correctly, we
345-
// need to special-case obtaining an unsafe pointer
344+
// To allow raw pointers to work correctly, we
345+
// need to special-case obtaining a raw pointer
346346
// from a region pointer to a vector.
347347

348348
// this will report a type mismatch if needed

src/librustc_typeck/check/regionck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
//! the borrow itself (L2). What do I mean by "guaranteed" by a
7777
//! borrowed pointer? I mean any data that is reached by first
7878
//! dereferencing a borrowed pointer and then either traversing
79-
//! interior offsets or owned pointers. We say that the guarantor
79+
//! interior offsets or boxes. We say that the guarantor
8080
//! of such data it the region of the borrowed pointer that was
8181
//! traversed. This is essentially the same as the ownership
8282
//! relation, except that a borrowed pointer never owns its

src/libstd/sys/common/thread_local.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub struct StaticKey {
102102
/// type is entirely safe to use.
103103
///
104104
/// Implementations will likely, however, contain unsafe code as this type only
105-
/// operates on `*mut u8`, an unsafe pointer.
105+
/// operates on `*mut u8`, a raw pointer.
106106
///
107107
/// # Examples
108108
///

src/libsyntax/parse/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4215,7 +4215,7 @@ impl<'a> Parser<'a> {
42154215
};
42164216
if self.is_self_ident() {
42174217
let span = self.span;
4218-
self.span_err(span, "cannot pass self by unsafe pointer");
4218+
self.span_err(span, "cannot pass self by raw pointer");
42194219
try!(self.bump());
42204220
}
42214221
// error case, making bogus self ident:

src/test/compile-fail/borrowck-move-from-unsafe-ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
fn foo(x: *const Box<isize>) -> Box<isize> {
13-
let y = *x; //~ ERROR dereference of unsafe pointer requires unsafe function or block
13+
let y = *x; //~ ERROR dereference of raw pointer requires unsafe function or block
1414
return y;
1515
}
1616

src/test/compile-fail/issue-20801.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ pub fn main() {
4040
//~^ ERROR cannot move out of borrowed content
4141

4242
let c = unsafe { *mut_ptr() };
43-
//~^ ERROR cannot move out of dereference of unsafe pointer
43+
//~^ ERROR cannot move out of dereference of raw pointer
4444

4545
let d = unsafe { *const_ptr() };
46-
//~^ ERROR cannot move out of dereference of unsafe pointer
46+
//~^ ERROR cannot move out of dereference of raw pointer
4747
}

0 commit comments

Comments
 (0)