Skip to content

Commit 832e5a0

Browse files
committed
Auto merge of #27453 - Manishearth:rollup, r=Manishearth
- Successful merges: #26982, #27305, #27419, #27423, #27426 - Failed merges:
2 parents f50518e + ead9a6d commit 832e5a0

File tree

4 files changed

+114
-28
lines changed

4 files changed

+114
-28
lines changed

src/doc/tarpl/borrow-splitting.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,27 @@ However borrowck doesn't understand arrays or slices in any way, so this doesn't
2727
work:
2828

2929
```rust,ignore
30-
let x = [1, 2, 3];
30+
let mut x = [1, 2, 3];
3131
let a = &mut x[0];
3232
let b = &mut x[1];
3333
println!("{} {}", a, b);
3434
```
3535

3636
```text
37-
<anon>:3:18: 3:22 error: cannot borrow immutable indexed content `x[..]` as mutable
38-
<anon>:3 let a = &mut x[0];
39-
^~~~
40-
<anon>:4:18: 4:22 error: cannot borrow immutable indexed content `x[..]` as mutable
41-
<anon>:4 let b = &mut x[1];
42-
^~~~
37+
<anon>:4:14: 4:18 error: cannot borrow `x[..]` as mutable more than once at a time
38+
<anon>:4 let b = &mut x[1];
39+
^~~~
40+
<anon>:3:14: 3:18 note: previous borrow of `x[..]` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `x[..]` until the borrow ends
41+
<anon>:3 let a = &mut x[0];
42+
^~~~
43+
<anon>:6:2: 6:2 note: previous borrow ends here
44+
<anon>:1 fn main() {
45+
<anon>:2 let mut x = [1, 2, 3];
46+
<anon>:3 let a = &mut x[0];
47+
<anon>:4 let b = &mut x[1];
48+
<anon>:5 println!("{} {}", a, b);
49+
<anon>:6 }
50+
^
4351
error: aborting due to 2 previous errors
4452
```
4553

src/doc/tarpl/send-and-sync.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ multiply alias a location in memory while mutating it. Unless these types use
55
synchronization to manage this access, they are absolutely not thread safe. Rust
66
captures this with through the `Send` and `Sync` traits.
77

8-
* A type is Send if it is safe to send it to another thread. A type is Sync if
9-
* it is safe to share between threads (`&T` is Send).
8+
* A type is Send if it is safe to send it to another thread.
9+
* A type is Sync if it is safe to share between threads (`&T` is Send).
1010

1111
Send and Sync are fundamental to Rust's concurrency story. As such, a
1212
substantial amount of special tooling exists to make them work right. First and
@@ -24,9 +24,9 @@ pretty much all types you'll ever interact with are Send and Sync.
2424

2525
Major exceptions include:
2626

27-
* raw pointers are neither Send nor Sync (because they have no safety guards)
28-
* `UnsafeCell` isn't Sync (and therefore `Cell` and `RefCell` aren't) `Rc` isn't
29-
* Send or Sync (because the refcount is shared and unsynchronized)
27+
* raw pointers are neither Send nor Sync (because they have no safety guards).
28+
* `UnsafeCell` isn't Sync (and therefore `Cell` and `RefCell` aren't).
29+
* `Rc` isn't Send or Sync (because the refcount is shared and unsynchronized).
3030

3131
`Rc` and `UnsafeCell` are very fundamentally not thread-safe: they enable
3232
unsynchronized shared mutable state. However raw pointers are, strictly
@@ -70,7 +70,7 @@ possible cause trouble by being incorrectly Send or Sync.
7070
Most uses of raw pointers should be encapsulated behind a sufficient abstraction
7171
that Send and Sync can be derived. For instance all of Rust's standard
7272
collections are Send and Sync (when they contain Send and Sync types) in spite
73-
of their pervasive use raw pointers to manage allocations and complex ownership.
73+
of their pervasive use of raw pointers to manage allocations and complex ownership.
7474
Similarly, most iterators into these collections are Send and Sync because they
7575
largely behave like an `&` or `&mut` into the collection.
7676

src/librustc_typeck/diagnostics.rs

+92-14
Original file line numberDiff line numberDiff line change
@@ -1227,16 +1227,22 @@ impl Bytes { ... } // error, same as above
12271227
"##,
12281228

12291229
E0117: r##"
1230-
You got this error because because you tried to implement a foreign
1231-
trait for a foreign type (with maybe a foreign type parameter). Erroneous
1232-
code example:
1230+
This error indicates a violation of one of Rust's orphan rules for trait
1231+
implementations. The rule prohibits any implementation of a foreign trait (a
1232+
trait defined in another crate) where
1233+
1234+
- the type that is implementing the trait is foreign
1235+
- all of the parameters being passed to the trait (if there are any) are also
1236+
foreign.
1237+
1238+
Here's one example of this error:
12331239
12341240
```
12351241
impl Drop for u32 {}
12361242
```
12371243
1238-
The type, trait or the type parameter (or all of them) has to be defined
1239-
in your crate. Example:
1244+
To avoid this kind of error, ensure that at least one local type is referenced
1245+
by the `impl`:
12401246
12411247
```
12421248
pub struct Foo; // you define your type in your crate
@@ -1245,21 +1251,29 @@ impl Drop for Foo { // and you can implement the trait on it!
12451251
// code of trait implementation here
12461252
}
12471253
1248-
trait Bar { // or define your trait in your crate
1249-
fn get(&self) -> usize;
1250-
}
1251-
1252-
impl Bar for u32 { // and then you implement it on a foreign type
1253-
fn get(&self) -> usize { 0 }
1254-
}
1255-
12561254
impl From<Foo> for i32 { // or you use a type from your crate as
12571255
// a type parameter
12581256
fn from(i: Foo) -> i32 {
12591257
0
12601258
}
12611259
}
12621260
```
1261+
1262+
Alternatively, define a trait locally and implement that instead:
1263+
1264+
```
1265+
trait Bar {
1266+
fn get(&self) -> usize;
1267+
}
1268+
1269+
impl Bar for u32 {
1270+
fn get(&self) -> usize { 0 }
1271+
}
1272+
```
1273+
1274+
For information on the design of the orphan rules, see [RFC 1023].
1275+
1276+
[RFC 1023]: https://github.com/rust-lang/rfcs/pull/1023
12631277
"##,
12641278

12651279
E0119: r##"
@@ -1889,6 +1903,71 @@ impl MyTrait for Foo {
18891903
```
18901904
"##,
18911905

1906+
E0210: r##"
1907+
This error indicates a violation of one of Rust's orphan rules for trait
1908+
implementations. The rule concerns the use of type parameters in an
1909+
implementation of a foreign trait (a trait defined in another crate), and
1910+
states that type parameters must be "covered" by a local type. To understand
1911+
what this means, it is perhaps easiest to consider a few examples.
1912+
1913+
If `ForeignTrait` is a trait defined in some external crate `foo`, then the
1914+
following trait `impl` is an error:
1915+
1916+
```
1917+
extern crate foo;
1918+
use foo::ForeignTrait;
1919+
1920+
impl<T> ForeignTrait for T { ... } // error
1921+
```
1922+
1923+
To work around this, it can be covered with a local type, `MyType`:
1924+
1925+
```
1926+
struct MyType<T>(T);
1927+
impl<T> ForeignTrait for MyType<T> { ... } // Ok
1928+
```
1929+
1930+
For another example of an error, suppose there's another trait defined in `foo`
1931+
named `ForeignTrait2` that takes two type parameters. Then this `impl` results
1932+
in the same rule violation:
1933+
1934+
```
1935+
struct MyType2;
1936+
impl<T> ForeignTrait2<T, MyType<T>> for MyType2 { ... } // error
1937+
```
1938+
1939+
The reason for this is that there are two appearances of type parameter `T` in
1940+
the `impl` header, both as parameters for `ForeignTrait2`. The first appearance
1941+
is uncovered, and so runs afoul of the orphan rule.
1942+
1943+
Consider one more example:
1944+
1945+
```
1946+
impl<T> ForeignTrait2<MyType<T>, T> for MyType2 { ... } // Ok
1947+
```
1948+
1949+
This only differs from the previous `impl` in that the parameters `T` and
1950+
`MyType<T>` for `ForeignTrait2` have been swapped. This example does *not*
1951+
violate the orphan rule; it is permitted.
1952+
1953+
To see why that last example was allowed, you need to understand the general
1954+
rule. Unfortunately this rule is a bit tricky to state. Consider an `impl`:
1955+
1956+
```
1957+
impl<P1, ..., Pm> ForeignTrait<T1, ..., Tn> for T0 { ... }
1958+
```
1959+
1960+
where `P1, ..., Pm` are the type parameters of the `impl` and `T0, ..., Tn`
1961+
are types. One of the types `T0, ..., Tn` must be a local type (this is another
1962+
orphan rule, see the explanation for E0117). Let `i` be the smallest integer
1963+
such that `Ti` is a local type. Then no type parameter can appear in any of the
1964+
`Tj` for `j < i`.
1965+
1966+
For information on the design of the orphan rules, see [RFC 1023].
1967+
1968+
[RFC 1023]: https://github.com/rust-lang/rfcs/pull/1023
1969+
"##,
1970+
18921971
E0211: r##"
18931972
You used an intrinsic function which doesn't correspond to its
18941973
definition. Erroneous code example:
@@ -2335,7 +2414,6 @@ register_diagnostics! {
23352414
// and only one is supported
23362415
E0208,
23372416
E0209, // builtin traits can only be implemented on structs or enums
2338-
E0210, // type parameter is not constrained by any local type
23392417
E0212, // cannot extract an associated type from a higher-ranked trait bound
23402418
E0213, // associated types are not accepted in this context
23412419
E0214, // parenthesized parameters may only be used with a trait

src/libstd/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//! for the [broader Rust ecosystem](https://crates.io). It offers
1616
//! core types (e.g. [`Vec`](vec/index.html)
1717
//! and [`Option`](option/index.html)), library-defined [operations on
18-
//! language primitives](#primitive) (e.g. [`u32`](u32/index.html) and
18+
//! language primitives](#primitives) (e.g. [`u32`](u32/index.html) and
1919
//! [`str`](str/index.html)), [standard macros](#macros),
2020
//! [I/O](io/index.html) and [multithreading](thread/index.html), among
2121
//! [many other lovely

0 commit comments

Comments
 (0)