Skip to content

Commit 764ef92

Browse files
committed
Auto merge of #33742 - Manishearth:rollup, r=Manishearth
Rollup of 10 pull requests - Successful merges: #33353, #33611, #33696, #33698, #33705, #33708, #33712, #33720, #33721, #33730 - Failed merges:
2 parents 2fb6f8e + acd2c11 commit 764ef92

28 files changed

+221
-80
lines changed

src/doc/book/advanced-linking.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ the `link_args` attribute. This attribute is applied to `extern` blocks and
1212
specifies raw flags which need to get passed to the linker when producing an
1313
artifact. An example usage would be:
1414

15-
``` no_run
15+
```rust,no_run
1616
#![feature(link_args)]
1717
1818
#[link_args = "-foo -bar -baz"]
@@ -52,7 +52,7 @@ By default, all Rust programs on Linux will link to the system `libc` along with
5252
a number of other libraries. Let's look at an example on a 64-bit Linux machine
5353
with GCC and `glibc` (by far the most common `libc` on Linux):
5454

55-
``` text
55+
```text
5656
$ cat example.rs
5757
fn main() {}
5858
$ rustc example.rs

src/doc/book/closures.md

+48-1
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,53 @@ assert_eq!(3, answer);
319319
Now we take a trait object, a `&Fn`. And we have to make a reference
320320
to our closure when we pass it to `call_with_one`, so we use `&||`.
321321

322+
A quick note about closures that use explicit lifetimes. Sometimes you might have a closure
323+
that takes a reference like so:
324+
325+
```
326+
fn call_with_ref<F>(some_closure:F) -> i32
327+
where F: Fn(&i32) -> i32 {
328+
329+
let mut value = 0;
330+
some_closure(&value)
331+
}
332+
```
333+
334+
Normally you can specify the lifetime of the parameter to our closure. We
335+
could annotate it on the function declaration:
336+
337+
```ignore
338+
fn call_with_ref<'a, F>(some_closure:F) -> i32
339+
where F: Fn(&'a 32) -> i32 {
340+
```
341+
342+
However this presents a problem with in our case. When you specify the explict
343+
lifetime on a function it binds that lifetime to the *entire* scope of the function
344+
instead of just the invocation scope of our closure. This means that the borrow checker
345+
will see a mutable reference in the same lifetime as our immutable reference and fail
346+
to compile.
347+
348+
In order to say that we only need the lifetime to be valid for the invocation scope
349+
of the closure we can use Higher-Ranked Trait Bounds with the `for<...>` syntax:
350+
351+
```ignore
352+
fn call_with_ref<F>(some_closure:F) -> i32
353+
where F: for<'a> Fn(&'a 32) -> i32 {
354+
```
355+
356+
This lets the Rust compiler find the minimum lifetime to invoke our closure and
357+
satisfy the borrow checker's rules. Our function then compiles and excutes as we
358+
expect.
359+
360+
```
361+
fn call_with_ref<F>(some_closure:F) -> i32
362+
where F: for<'a> Fn(&'a i32) -> i32 {
363+
364+
let mut value = 0;
365+
some_closure(&value)
366+
}
367+
```
368+
322369
# Function pointers and closures
323370

324371
A function pointer is kind of like a closure that has no environment. As such,
@@ -344,7 +391,7 @@ assert_eq!(2, answer);
344391
In this example, we don’t strictly need the intermediate variable `f`,
345392
the name of the function works just fine too:
346393

347-
```ignore
394+
```rust,ignore
348395
let answer = call_with_one(&add_one);
349396
```
350397

src/doc/book/compiler-plugins.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Let's write a plugin
3737
[`roman_numerals.rs`](https://github.com/rust-lang/rust/tree/master/src/test/auxiliary/roman_numerals.rs)
3838
that implements Roman numeral integer literals.
3939

40-
```ignore
40+
```rust,ignore
4141
#![crate_type="dylib"]
4242
#![feature(plugin_registrar, rustc_private)]
4343
@@ -102,7 +102,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
102102

103103
Then we can use `rn!()` like any other macro:
104104

105-
```ignore
105+
```rust,ignore
106106
#![feature(plugin)]
107107
#![plugin(roman_numerals)]
108108
@@ -132,7 +132,7 @@ Some of the [macro debugging tips](macros.html#debugging-macro-code) are applica
132132
You can use `syntax::parse` to turn token trees into
133133
higher-level syntax elements like expressions:
134134

135-
```ignore
135+
```rust,ignore
136136
fn expand_foo(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree])
137137
-> Box<MacResult+'static> {
138138
@@ -169,7 +169,7 @@ infrastructure](../reference.html#lint-check-attributes) with additional checks
169169
code style, safety, etc. Now let's write a plugin [`lint_plugin_test.rs`](https://github.com/rust-lang/rust/blob/master/src/test/auxiliary/lint_plugin_test.rs)
170170
that warns about any item named `lintme`.
171171

172-
```ignore
172+
```rust,ignore
173173
#![feature(plugin_registrar)]
174174
#![feature(box_syntax, rustc_private)]
175175
@@ -211,7 +211,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
211211

212212
Then code like
213213

214-
```ignore
214+
```rust,ignore
215215
#![plugin(lint_plugin_test)]
216216
217217
fn lintme() { }

src/doc/book/concurrency.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ concurrency bugs.
165165
As an example, here is a Rust program that would have a data race in many
166166
languages. It will not compile:
167167

168-
```ignore
168+
```rust,ignore
169169
use std::thread;
170170
use std::time::Duration;
171171
@@ -204,7 +204,7 @@ Calling `clone()` on an `Rc<T>` will return a new owned reference and bump the
204204
internal reference count. We create one of these for each thread:
205205

206206

207-
```ignore
207+
```rust,ignore
208208
use std::thread;
209209
use std::time::Duration;
210210
use std::rc::Rc;
@@ -250,7 +250,7 @@ In essence, `Arc<T>` is a type that lets us share ownership of data _across
250250
threads_.
251251

252252

253-
```ignore
253+
```rust,ignore
254254
use std::thread;
255255
use std::sync::Arc;
256256
use std::time::Duration;
@@ -336,7 +336,7 @@ The lock "release" here is implicit; when the result of the lock (in this case,
336336
Note that [`lock`](../std/sync/struct.Mutex.html#method.lock) method of
337337
[`Mutex`](../std/sync/struct.Mutex.html) has this signature:
338338

339-
```ignore
339+
```rust,ignore
340340
fn lock(&self) -> LockResult<MutexGuard<T>>
341341
```
342342

src/doc/book/documentation.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ Here’s an example of documenting a macro:
362362
/// # }
363363
/// ```
364364
///
365-
/// ```should_panic
365+
/// ```rust,should_panic
366366
/// # #[macro_use] extern crate foo;
367367
/// # fn main() {
368368
/// panic_unless!(true == false, “I’m broken.”);
@@ -429,7 +429,7 @@ There are a few more annotations that are useful to help `rustdoc` do the right
429429
thing when testing your code:
430430

431431
```rust
432-
/// ```ignore
432+
/// ```rust,ignore
433433
/// fn foo() {
434434
/// ```
435435
# fn foo() {}
@@ -441,7 +441,7 @@ with `text` if it's not code, or using `#`s to get a working example that
441441
only shows the part you care about.
442442

443443
```rust
444-
/// ```should_panic
444+
/// ```rust,should_panic
445445
/// assert!(false);
446446
/// ```
447447
# fn foo() {}
@@ -451,7 +451,7 @@ only shows the part you care about.
451451
not actually pass as a test.
452452

453453
```rust
454-
/// ```no_run
454+
/// ```rust,no_run
455455
/// loop {
456456
/// println!("Hello, world");
457457
/// }
@@ -563,7 +563,7 @@ can be useful when changing some options, or when writing a macro.
563563

564564
`rustdoc` will show the documentation for a public re-export in both places:
565565

566-
```ignore
566+
```rust,ignore
567567
extern crate foo;
568568
569569
pub use foo::bar;
@@ -575,7 +575,7 @@ documentation in both places.
575575

576576
This behavior can be suppressed with `no_inline`:
577577

578-
```ignore
578+
```rust,ignore
579579
extern crate foo;
580580
581581
#[doc(no_inline)]

src/doc/book/ffi.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ and add `extern crate libc;` to your crate root.
2828
The following is a minimal example of calling a foreign function which will
2929
compile if snappy is installed:
3030

31-
```no_run
31+
```rust,no_run
3232
# #![feature(libc)]
3333
extern crate libc;
3434
use libc::size_t;
@@ -62,7 +62,7 @@ keeping the binding correct at runtime.
6262

6363
The `extern` block can be extended to cover the entire snappy API:
6464

65-
```no_run
65+
```rust,no_run
6666
# #![feature(libc)]
6767
extern crate libc;
6868
use libc::{c_int, size_t};
@@ -209,7 +209,7 @@ A basic example is:
209209

210210
Rust code:
211211

212-
```no_run
212+
```rust,no_run
213213
extern fn callback(a: i32) {
214214
println!("I'm called from C with value {0}", a);
215215
}
@@ -262,7 +262,7 @@ referenced Rust object.
262262
263263
Rust code:
264264
265-
```no_run
265+
```rust,no_run
266266
#[repr(C)]
267267
struct RustObject {
268268
a: i32,
@@ -406,7 +406,7 @@ Foreign APIs often export a global variable which could do something like track
406406
global state. In order to access these variables, you declare them in `extern`
407407
blocks with the `static` keyword:
408408

409-
```no_run
409+
```rust,no_run
410410
# #![feature(libc)]
411411
extern crate libc;
412412
@@ -425,7 +425,7 @@ Alternatively, you may need to alter global state provided by a foreign
425425
interface. To do this, statics can be declared with `mut` so we can mutate
426426
them.
427427

428-
```no_run
428+
```rust,no_run
429429
# #![feature(libc)]
430430
extern crate libc;
431431

src/doc/book/functions.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ x = y = 5
134134
In Rust, however, using `let` to introduce a binding is _not_ an expression. The
135135
following will produce a compile-time error:
136136

137-
```ignore
137+
```rust,ignore
138138
let x = (let y = 5); // expected identifier, found keyword `let`
139139
```
140140

@@ -283,7 +283,7 @@ stack backtrace:
283283

284284
A diverging function can be used as any type:
285285

286-
```should_panic
286+
```rust,should_panic
287287
# fn diverges() -> ! {
288288
# panic!("This function never returns!");
289289
# }

src/doc/book/inline-assembly.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ For extremely low-level manipulations and performance reasons, one
44
might wish to control the CPU directly. Rust supports using inline
55
assembly to do this via the `asm!` macro.
66

7-
```ignore
7+
```rust,ignore
88
asm!(assembly template
99
: output operands
1010
: input operands

src/doc/book/loops.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ for x in 0..10 {
7474

7575
In slightly more abstract terms,
7676

77-
```ignore
77+
```rust,ignore
7878
for var in expression {
7979
code
8080
}

src/doc/book/macros.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ macro_rules! vec {
7878

7979
Whoa, that’s a lot of new syntax! Let’s break it down.
8080

81-
```ignore
81+
```rust,ignore
8282
macro_rules! vec { ... }
8383
```
8484

@@ -92,7 +92,7 @@ syntax and serves to distinguish a macro from an ordinary function.
9292
The macro is defined through a series of rules, which are pattern-matching
9393
cases. Above, we had
9494

95-
```ignore
95+
```rust,ignore
9696
( $( $x:expr ),* ) => { ... };
9797
```
9898

@@ -112,7 +112,7 @@ separated by commas.
112112
Aside from the special matcher syntax, any Rust tokens that appear in a matcher
113113
must match exactly. For example,
114114

115-
```rust
115+
```rust,ignore
116116
macro_rules! foo {
117117
(x => $e:expr) => (println!("mode X: {}", $e));
118118
(y => $e:expr) => (println!("mode Y: {}", $e));
@@ -147,7 +147,7 @@ The right-hand side of a macro rule is ordinary Rust syntax, for the most part.
147147
But we can splice in bits of syntax captured by the matcher. From the original
148148
example:
149149

150-
```ignore
150+
```rust,ignore
151151
$(
152152
temp_vec.push($x);
153153
)*
@@ -165,7 +165,7 @@ within the repeated block.
165165
Another detail: the `vec!` macro has *two* pairs of braces on the right-hand
166166
side. They are often combined like so:
167167

168-
```ignore
168+
```rust,ignore
169169
macro_rules! foo {
170170
() => {{
171171
...

src/doc/book/mutability.md

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ fn foo(mut x: i32) {
5555
# }
5656
```
5757

58+
Note that here, the `x` is mutable, but not the `y`.
59+
5860
[pattern]: patterns.html
5961

6062
# Interior vs. Exterior Mutability

src/doc/book/operators-and-overloading.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn main() {
123123
For `HasArea` and `Square`, we declare a type parameter `T` and replace
124124
`f64` with it. The `impl` needs more involved modifications:
125125

126-
```ignore
126+
```rust,ignore
127127
impl<T> HasArea<T> for Square<T>
128128
where T: Mul<Output=T> + Copy { ... }
129129
```

src/doc/book/trait-objects.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ let y = TraitObject {
306306
Not every trait can be used to make a trait object. For example, vectors implement
307307
`Clone`, but if we try to make a trait object:
308308

309-
```ignore
309+
```rust,ignore
310310
let v = vec![1, 2, 3];
311311
let o = &v as &Clone;
312312
```

src/doc/book/traits.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ fn main() {
195195
`is_square()` needs to check that the sides are equal, so the sides must be of
196196
a type that implements the [`core::cmp::PartialEq`][PartialEq] trait:
197197

198-
```ignore
198+
```rust,ignore
199199
impl<T: PartialEq> Rectangle<T> { ... }
200200
```
201201

0 commit comments

Comments
 (0)