Skip to content

Commit b77d60a

Browse files
committed
Auto merge of #25888 - steveklabnik:rollup, r=steveklabnik
- Successful merges: #25788, #25861, #25864, #25865, #25866, #25873, #25876, #25883, #25886 - Failed merges:
2 parents 996fb8d + ed19a6e commit b77d60a

File tree

15 files changed

+190
-36
lines changed

15 files changed

+190
-36
lines changed

src/doc/style/features/functions-and-methods/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ for any operation that is clearly associated with a particular
2020
type.
2121

2222
Methods have numerous advantages over functions:
23+
2324
* They do not need to be imported or qualified to be used: all you
2425
need is a value of the appropriate type.
2526
* Their invocation performs autoborrowing (including mutable borrows).

src/doc/style/features/functions-and-methods/input.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fn foo(a: u8) { ... }
159159
Note that
160160
[`ascii::Ascii`](http://static.rust-lang.org/doc/master/std/ascii/struct.Ascii.html)
161161
is a _wrapper_ around `u8` that guarantees the highest bit is zero; see
162-
[newtype patterns]() for more details on creating typesafe wrappers.
162+
[newtype patterns](../types/newtype.md) for more details on creating typesafe wrappers.
163163
164164
Static enforcement usually comes at little run-time cost: it pushes the
165165
costs to the boundaries (e.g. when a `u8` is first converted into an

src/doc/style/features/let.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Prefer
3434

3535
```rust
3636
let foo = match bar {
37-
Baz => 0,
37+
Baz => 0,
3838
Quux => 1
3939
};
4040
```
@@ -44,7 +44,7 @@ over
4444
```rust
4545
let foo;
4646
match bar {
47-
Baz => {
47+
Baz => {
4848
foo = 0;
4949
}
5050
Quux => {
@@ -61,8 +61,8 @@ conditional expression.
6161
Prefer
6262

6363
```rust
64-
s.iter().map(|x| x * 2)
65-
.collect::<Vec<_>>()
64+
let v = s.iter().map(|x| x * 2)
65+
.collect::<Vec<_>>();
6666
```
6767

6868
over

src/doc/style/ownership/builders.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ If `T` is such a data structure, consider introducing a `T` _builder_:
1616
value. When possible, choose a better name: e.g. `Command` is the builder for
1717
`Process`.
1818
2. The builder constructor should take as parameters only the data _required_ to
19-
to make a `T`.
19+
make a `T`.
2020
3. The builder should offer a suite of convenient methods for configuration,
2121
including setting up compound inputs (like slices) incrementally.
2222
These methods should return `self` to allow chaining.

src/doc/trpl/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ language would.
1515

1616
[rust]: http://rust-lang.org
1717

18-
“The Rust Programming Language” is split into seven sections. This introduction
18+
“The Rust Programming Language” is split into eight sections. This introduction
1919
is the first. After this:
2020

2121
* [Getting started][gs] - Set up your computer for Rust development.

src/doc/trpl/glossary.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ In the example above `x` and `y` have arity 2. `z` has arity 3.
1919

2020
When a compiler is compiling your program, it does a number of different
2121
things. One of the things that it does is turn the text of your program into an
22-
‘abstract syntax tree’, or‘AST’. This tree is a representation of the
22+
‘abstract syntax tree’, or ‘AST’. This tree is a representation of the
2323
structure of your program. For example, `2 + 3` can be turned into a tree:
2424

2525
```text

src/doc/trpl/lifetimes.md

+23-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,29 @@ x: &'a i32,
134134
# }
135135
```
136136

137-
uses it. So why do we need a lifetime here? We need to ensure that any
138-
reference to the contained `i32` does not outlive the containing `Foo`.
137+
uses it. So why do we need a lifetime here? We need to ensure that any reference
138+
to a `Foo` cannot outlive the reference to an `i32` it contains.
139+
140+
If you have multiple references, you can use the same lifetime multiple times:
141+
142+
```rust
143+
fn x_or_y<'a>(x: &'a str, y: &'a str) -> &'a str {
144+
# x
145+
# }
146+
```
147+
148+
This says that `x` and `y` both are alive for the same scope, and that the
149+
return value is also alive for that scope. If you wanted `x` and `y` to have
150+
different lifetimes, you can use multiple lifetime parameters:
151+
152+
```rust
153+
fn x_or_y<'a, 'b>(x: &'a str, y: &'b str) -> &'a str {
154+
# x
155+
# }
156+
```
157+
158+
In this example, `x` and `y` have different valid scopes, but the return value
159+
has the same lifetime as `x`.
139160

140161
## Thinking in scopes
141162

src/doc/trpl/method-syntax.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Functions are great, but if you want to call a bunch of them on some data, it
44
can be awkward. Consider this code:
55

66
```rust,ignore
7-
baz(bar(foo)));
7+
baz(bar(foo));
88
```
99

1010
We would read this left-to right, and so we see ‘baz bar foo’. But this isn’t the

src/doc/trpl/traits.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ fn bar<T, K>(x: T, y: K) where T: Clone, K: Clone + Debug {
285285

286286
fn main() {
287287
foo("Hello", "world");
288-
bar("Hello", "workd");
288+
bar("Hello", "world");
289289
}
290290
```
291291

src/libcore/mem.rs

+48-6
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,61 @@ pub use intrinsics::transmute;
5252
/// * `mpsc::{Sender, Receiver}` cycles (they use `Arc` internally)
5353
/// * Panicking destructors are likely to leak local resources
5454
///
55+
/// # When To Use
56+
///
57+
/// There's only a few reasons to use this function. They mainly come
58+
/// up in unsafe code or FFI code.
59+
///
60+
/// * You have an uninitialized value, perhaps for performance reasons, and
61+
/// need to prevent the destructor from running on it.
62+
/// * You have two copies of a value (like `std::mem::swap`), but need the
63+
/// destructor to only run once to prevent a double free.
64+
/// * Transferring resources across FFI boundries.
65+
///
5566
/// # Example
5667
///
57-
/// ```rust,no_run
68+
/// Leak some heap memory by never deallocating it.
69+
///
70+
/// ```rust
5871
/// use std::mem;
59-
/// use std::fs::File;
6072
///
61-
/// // Leak some heap memory by never deallocating it
6273
/// let heap_memory = Box::new(3);
6374
/// mem::forget(heap_memory);
75+
/// ```
76+
///
77+
/// Leak an I/O object, never closing the file.
78+
///
79+
/// ```rust,no_run
80+
/// use std::mem;
81+
/// use std::fs::File;
6482
///
65-
/// // Leak an I/O object, never closing the file
6683
/// let file = File::open("foo.txt").unwrap();
6784
/// mem::forget(file);
6885
/// ```
86+
///
87+
/// The swap function uses forget to good effect.
88+
///
89+
/// ```rust
90+
/// use std::mem;
91+
/// use std::ptr;
92+
///
93+
/// fn swap<T>(x: &mut T, y: &mut T) {
94+
/// unsafe {
95+
/// // Give ourselves some scratch space to work with
96+
/// let mut t: T = mem::uninitialized();
97+
///
98+
/// // Perform the swap, `&mut` pointers never alias
99+
/// ptr::copy_nonoverlapping(&*x, &mut t, 1);
100+
/// ptr::copy_nonoverlapping(&*y, x, 1);
101+
/// ptr::copy_nonoverlapping(&t, y, 1);
102+
///
103+
/// // y and t now point to the same thing, but we need to completely
104+
/// // forget `t` because we do not want to run the destructor for `T`
105+
/// // on its value, which is still owned somewhere outside this function.
106+
/// mem::forget(t);
107+
/// }
108+
/// }
109+
/// ```
69110
#[stable(feature = "rust1", since = "1.0.0")]
70111
pub fn forget<T>(t: T) {
71112
unsafe { intrinsics::forget(t) }
@@ -267,8 +308,9 @@ pub fn swap<T>(x: &mut T, y: &mut T) {
267308
ptr::copy_nonoverlapping(&*y, x, 1);
268309
ptr::copy_nonoverlapping(&t, y, 1);
269310

270-
// y and t now point to the same thing, but we need to completely forget `t`
271-
// because it's no longer relevant.
311+
// y and t now point to the same thing, but we need to completely
312+
// forget `t` because we do not want to run the destructor for `T`
313+
// on its value, which is still owned somewhere outside this function.
272314
forget(t);
273315
}
274316
}

src/librustc/diagnostics.rs

+54-6
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,13 @@ const Y: i32 = A;
196196
"##,
197197

198198
E0015: r##"
199-
The only function calls allowed in static or constant expressions are enum
200-
variant constructors or struct constructors (for unit or tuple structs). This
201-
is because Rust currently does not support compile-time function execution.
199+
The only functions that can be called in static or constant expressions are
200+
`const` functions. Rust currently does not support more general compile-time
201+
function execution.
202+
203+
See [RFC 911] for more details on the design of `const fn`s.
204+
205+
[RFC 911]: https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md
202206
"##,
203207

204208
E0018: r##"
@@ -842,6 +846,53 @@ struct Foo<T: 'static> {
842846
foo: &'static T
843847
}
844848
```
849+
"##,
850+
851+
E0378: r##"
852+
Method calls that aren't calls to inherent `const` methods are disallowed
853+
in statics, constants, and constant functions.
854+
855+
For example:
856+
857+
```
858+
const BAZ: i32 = Foo(25).bar(); // error, `bar` isn't `const`
859+
860+
struct Foo(i32);
861+
862+
impl Foo {
863+
const fn foo(&self) -> i32 {
864+
self.bar() // error, `bar` isn't `const`
865+
}
866+
867+
fn bar(&self) -> i32 { self.0 }
868+
}
869+
```
870+
871+
For more information about `const fn`'s, see [RFC 911].
872+
873+
[RFC 911]: https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md
874+
"##,
875+
876+
E0394: r##"
877+
From [RFC 246]:
878+
879+
> It is illegal for a static to reference another static by value. It is
880+
> required that all references be borrowed.
881+
882+
[RFC 246]: https://github.com/rust-lang/rfcs/pull/246
883+
"##,
884+
885+
E0397: r##"
886+
It is not allowed for a mutable static to allocate or have destructors. For
887+
example:
888+
889+
```
890+
// error: mutable statics are not allowed to have boxes
891+
static mut FOO: Option<Box<usize>> = None;
892+
893+
// error: mutable statics are not allowed to have destructors
894+
static mut BAR: Option<Vec<i32>> = None;
895+
```
845896
"##
846897

847898
}
@@ -891,9 +942,6 @@ register_diagnostics! {
891942
E0315, // cannot invoke closure outside of its lifetime
892943
E0316, // nested quantification of lifetimes
893944
E0370, // discriminant overflow
894-
E0378, // method calls limited to constant inherent methods
895-
E0394, // cannot refer to other statics by value, use the address-of
896-
// operator or a constant instead
897945
E0395, // pointer comparison in const-expr
898946
E0396 // pointer dereference in const-expr
899947
}

src/librustc/middle/check_const.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,13 @@ impl<'a, 'tcx> CheckCrateVisitor<'a, 'tcx> {
273273
let suffix = if tcontents.has_dtor() {
274274
"destructors"
275275
} else if tcontents.owns_owned() {
276-
"owned pointers"
276+
"boxes"
277277
} else {
278278
return
279279
};
280280

281-
self.tcx.sess.span_err(e.span, &format!("mutable statics are not allowed \
282-
to have {}", suffix));
281+
span_err!(self.tcx.sess, e.span, E0397,
282+
"mutable statics are not allowed to have {}", suffix);
283283
}
284284

285285
fn check_static_type(&self, e: &ast::Expr) {

src/librustc_typeck/diagnostics.rs

+48-4
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,31 @@ Reference:
170170
http://doc.rust-lang.org/reference.html#trait-objects
171171
"##,
172172

173+
E0040: r##"
174+
It is not allowed to manually call destructors in Rust. It is also not
175+
necessary to do this since `drop` is called automatically whenever a value goes
176+
out of scope.
177+
178+
Here's an example of this error:
179+
180+
```
181+
struct Foo {
182+
x: i32,
183+
}
184+
185+
impl Drop for Foo {
186+
fn drop(&mut self) {
187+
println!("kaboom");
188+
}
189+
}
190+
191+
fn main() {
192+
let mut x = Foo { x: -7 };
193+
x.drop(); // error: explicit use of destructor method
194+
}
195+
```
196+
"##,
197+
173198
E0046: r##"
174199
When trying to make some type implement a trait `Foo`, you must, at minimum,
175200
provide implementations for all of `Foo`'s required methods (meaning the
@@ -241,7 +266,7 @@ impl Foo for Bar {
241266
fn foo(x: i16) { }
242267
243268
// error, values differ in mutability
244-
fn foo(&mut self) { }
269+
fn bar(&mut self) { }
245270
}
246271
```
247272
"##,
@@ -542,6 +567,21 @@ enum Empty {}
542567
```
543568
"##,
544569

570+
E0087: r##"
571+
Too many type parameters were supplied for a function. For example:
572+
573+
```
574+
fn foo<T>() {}
575+
576+
fn main() {
577+
foo::<f64, bool>(); // error, expected 1 parameter, found 2 parameters
578+
}
579+
```
580+
581+
The number of supplied parameters much exactly match the number of defined type
582+
parameters.
583+
"##,
584+
545585
E0089: r##"
546586
Not enough type parameters were supplied for a function. For example:
547587
@@ -1098,6 +1138,13 @@ Trait2 { ... }`) does not work if the trait is not object-safe. Please see the
10981138
[RFC 255]: https://github.com/rust-lang/rfcs/pull/255
10991139
"##,
11001140

1141+
E0379: r##"
1142+
Trait methods cannot be declared `const` by design. For more information, see
1143+
[RFC 911].
1144+
1145+
[RFC 911]: https://github.com/rust-lang/rfcs/pull/911
1146+
"##,
1147+
11011148
E0380: r##"
11021149
Default impls are only allowed for traits with no methods or associated items.
11031150
For more information see the [opt-in builtin traits RFC](https://github.com/rust
@@ -1113,7 +1160,6 @@ register_diagnostics! {
11131160
E0034, // multiple applicable methods in scope
11141161
E0035, // does not take type parameters
11151162
E0036, // incorrect number of type parameters given for this method
1116-
E0040, // explicit use of destructor method
11171163
E0044, // foreign items may not have type parameters
11181164
E0045, // variadic function must have C calling convention
11191165
E0057, // method has an incompatible type for trait
@@ -1128,7 +1174,6 @@ register_diagnostics! {
11281174
E0077,
11291175
E0085,
11301176
E0086,
1131-
E0087,
11321177
E0088,
11331178
E0090,
11341179
E0091,
@@ -1235,7 +1280,6 @@ register_diagnostics! {
12351280
// between structures
12361281
E0377, // the trait `CoerceUnsized` may only be implemented for a coercion
12371282
// between structures with the same definition
1238-
E0379, // trait fns cannot be const
12391283
E0390, // only a single inherent implementation marked with
12401284
// `#[lang = \"{}\"]` is allowed for the `{}` primitive
12411285
E0391, // unsupported cyclic reference between types/traits detected

0 commit comments

Comments
 (0)