Skip to content

Commit 4a9c58c

Browse files
committed
Auto merge of #51369 - Mark-Simulacrum:rollup, r=Mark-Simulacrum
Rollup of 7 pull requests Successful merges: - #50852 (Add doc comment to hiding portions of code example) - #51183 (Update rustdoc book to suggest using Termination trait instead of hidden ‘foo’ function) - #51255 (Fix confusing error message for sub_instant) - #51256 (Fix crate-name option in rustdoc) - #51308 (Check array indices in constant propagation) - #51343 (test: Ignore some problematic tests on sparc and sparc64) - #51358 (Tests that #39963 is fixed on MIR borrowck) Failed merges:
2 parents 90f34b5 + d011150 commit 4a9c58c

33 files changed

+141
-48
lines changed

src/doc/rustdoc/src/documentation-tests.md

+34-13
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,17 @@ from your example, but are important to make the tests work. Consider
7979
an example block that looks like this:
8080

8181
```text
82-
/// Some documentation.
83-
# fn foo() {}
82+
/// /// Some documentation.
83+
/// # fn foo() {} // this function will be hidden
84+
/// println!("Hello, World!");
8485
```
8586

8687
It will render like this:
8788

8889
```rust
8990
/// Some documentation.
9091
# fn foo() {}
92+
println!("Hello, World!");
9193
```
9294

9395
Yes, that's right: you can add lines that start with `# `, and they will
@@ -168,37 +170,56 @@ By repeating all parts of the example, you can ensure that your example still
168170
compiles, while only showing the parts that are relevant to that part of your
169171
explanation.
170172
171-
Another case where the use of `#` is handy is when you want to ignore
172-
error handling. Lets say you want the following,
173+
174+
## Using `?` in doc tests
175+
176+
When writing an example, it is rarely useful to include a complete error
177+
handling, as it would add significant amounts of boilerplate code. Instead, you
178+
may want the following:
173179
174180
```ignore
181+
/// ```
175182
/// use std::io;
176183
/// let mut input = String::new();
177184
/// io::stdin().read_line(&mut input)?;
185+
/// ```
178186
```
179187

180-
The problem is that `?` returns a `Result<T, E>` and test functions
181-
don't return anything so this will give a mismatched types error.
188+
The problem is that `?` returns a `Result<T, E>` and test functions don't
189+
return anything, so this will give a mismatched types error.
190+
191+
You can get around this limitation by manually adding a `main` that returns
192+
`Result<T, E>`, because `Result<T, E>` implements the `Termination` trait:
182193

183194
```ignore
184195
/// A doc test using ?
185196
///
186197
/// ```
187198
/// use std::io;
188-
/// # fn foo() -> io::Result<()> {
199+
///
200+
/// fn main() -> io::Result<()> {
201+
/// let mut input = String::new();
202+
/// io::stdin().read_line(&mut input)?;
203+
/// Ok(())
204+
/// }
205+
/// ```
206+
```
207+
208+
Together with the `# ` from the section above, you arrive at a solution that
209+
appears to the reader as the initial idea but works with doc tests:
210+
211+
```ignore
212+
/// ```
213+
/// use std::io;
214+
/// # fn main() -> io::Result<()> {
189215
/// let mut input = String::new();
190216
/// io::stdin().read_line(&mut input)?;
191217
/// # Ok(())
192218
/// # }
193219
/// ```
194-
# fn foo() {}
195220
```
196221

197-
You can get around this by wrapping the code in a function. This catches
198-
and swallows the `Result<T, E>` when running tests on the docs. This
199-
pattern appears regularly in the standard library.
200-
201-
### Documenting macros
222+
## Documenting macros
202223

203224
Here’s an example of documenting a macro:
204225

src/librustc_mir/transform/const_prop.rs

-10
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,6 @@ impl<'b, 'a, 'tcx:'b> ConstPropagator<'b, 'a, 'tcx> {
240240
) -> Option<Const<'tcx>> {
241241
let span = source_info.span;
242242
match *rvalue {
243-
// No need to overwrite an already evaluated constant
244-
Rvalue::Use(Operand::Constant(box Constant {
245-
literal: Literal::Value {
246-
value: &ty::Const {
247-
val: ConstVal::Value(_),
248-
..
249-
},
250-
},
251-
..
252-
})) => None,
253243
// This branch exists for the sanity type check
254244
Rvalue::Use(Operand::Constant(ref c)) => {
255245
assert_eq!(c.ty, place_ty);

src/librustdoc/core.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,10 @@ pub fn run_core(search_paths: SearchPaths,
230230

231231
let krate = panictry!(driver::phase_1_parse_input(control, &sess, &input));
232232

233-
let name = ::rustc_codegen_utils::link::find_crate_name(Some(&sess), &krate.attrs, &input);
233+
let name = match crate_name {
234+
Some(ref crate_name) => crate_name.clone(),
235+
None => ::rustc_codegen_utils::link::find_crate_name(Some(&sess), &krate.attrs, &input),
236+
};
234237

235238
let mut crate_loader = CrateLoader::new(&sess, &cstore, &name);
236239

src/libstd/sys/redox/time.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl Instant {
144144

145145
pub fn sub_instant(&self, other: &Instant) -> Duration {
146146
self.t.sub_timespec(&other.t).unwrap_or_else(|_| {
147-
panic!("other was less than the current instant")
147+
panic!("specified instant was later than self")
148148
})
149149
}
150150

src/libstd/sys/unix/time.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ mod inner {
289289

290290
pub fn sub_instant(&self, other: &Instant) -> Duration {
291291
self.t.sub_timespec(&other.t).unwrap_or_else(|_| {
292-
panic!("other was less than the current instant")
292+
panic!("specified instant was later than self")
293293
})
294294
}
295295

src/test/codegen/abi-main-signature-16bit-c-int.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
// ignore-powerpc64
2323
// ignore-s390x
2424
// ignore-sparc
25+
// ignore-sparc64
2526
// ignore-wasm32
2627
// ignore-x86
2728
// ignore-x86_64

src/test/codegen/fastcall-inreg.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
// ignore-r600
3030
// ignore-amdgcn
3131
// ignore-sparc
32+
// ignore-sparc64
3233
// ignore-sparcv9
3334
// ignore-sparcel
3435
// ignore-s390x

src/test/codegen/repr-transparent-aggregates-2.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// ignore-asmjs
1515
// ignore-mips64
1616
// ignore-s390x
17+
// ignore-sparc
18+
// ignore-sparc64
1719
// ignore-wasm
1820
// ignore-x86
1921
// ignore-x86_64

src/test/codegen/stack-probes.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// ignore-mips64
1515
// ignore-powerpc
1616
// ignore-s390x
17+
// ignore-sparc
18+
// ignore-sparc64
1719
// ignore-wasm
1820
// ignore-emscripten
1921
// ignore-windows

src/test/codegen/x86_mmx.rs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// ignore-emscripten
1414
// ignore-mips
1515
// ignore-mips64
16+
// ignore-sparc
17+
// ignore-sparc64
1618
// compile-flags: -O
1719

1820
#![feature(repr_simd)]

src/test/compile-fail/asm-bad-clobber.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// ignore-emscripten
1616
// ignore-powerpc
1717
// ignore-sparc
18+
// ignore-sparc64
1819
// ignore-mips
1920
// ignore-mips64
2021

src/test/compile-fail/asm-in-bad-modifier.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// ignore-emscripten
1313
// ignore-powerpc
1414
// ignore-sparc
15+
// ignore-sparc64
1516
// ignore-mips
1617
// ignore-mips64
1718

src/test/compile-fail/asm-misplaced-option.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// ignore-emscripten
1616
// ignore-powerpc
1717
// ignore-sparc
18+
// ignore-sparc64
1819
// ignore-mips
1920
// ignore-mips64
2021

src/test/compile-fail/asm-out-no-modifier.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// ignore-emscripten
1313
// ignore-powerpc
1414
// ignore-sparc
15+
// ignore-sparc64
1516
// ignore-mips
1617
// ignore-mips64
1718

src/test/compile-fail/asm-out-read-uninit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// ignore-emscripten
1313
// ignore-powerpc
1414
// ignore-sparc
15+
// ignore-sparc64
1516
// ignore-mips
1617
// ignore-mips64
1718

src/test/compile-fail/borrowck/borrowck-asm.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// ignore-emscripten
1313
// ignore-powerpc
1414
// ignore-sparc
15+
// ignore-sparc64
1516

1617
// revisions: ast mir
1718
//[mir]compile-flags: -Z borrowck=mir

src/test/compile-fail/const-err-early.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ pub const C: u8 = 200u8 * 4; //~ ERROR const_err
1919
//~^ ERROR this constant cannot be used
2020
pub const D: u8 = 42u8 - (42u8 + 1); //~ ERROR const_err
2121
//~^ ERROR this constant cannot be used
22-
pub const E: u8 = [5u8][1];
23-
//~^ ERROR const_err
22+
pub const E: u8 = [5u8][1]; //~ ERROR const_err
23+
//~| ERROR this constant cannot be used
2424

2525
fn main() {
2626
let _a = A;

src/test/compile-fail/const-err2.rs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ fn main() {
3131
let d = 42u8 - (42u8 + 1);
3232
//~^ ERROR const_err
3333
let _e = [5u8][1];
34+
//~^ ERROR const_err
3435
black_box(a);
3536
black_box(b);
3637
black_box(c);

src/test/compile-fail/const-err3.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ fn main() {
2323
let d = 42u8 - (42u8 + 1);
2424
//~^ ERROR const_err
2525
let _e = [5u8][1];
26+
//~^ ERROR const_err
2627
black_box(b);
2728
black_box(c);
2829
black_box(d);

src/test/run-fail/mir_indexing_oob_1.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
const C: [u32; 5] = [0; 5];
1414

15+
#[allow(const_err)]
1516
fn test() -> u32 {
1617
C[10]
1718
}

src/test/run-fail/mir_indexing_oob_2.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
const C: &'static [u8; 5] = b"hello";
1414

15+
#[allow(const_err)]
1516
fn test() -> u8 {
1617
C[10]
1718
}

src/test/run-fail/mir_indexing_oob_3.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
const C: &'static [u8; 5] = b"hello";
1414

15+
#[allow(const_err)]
1516
fn mir() -> u8 {
1617
C[10]
1718
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test case from #39963.
12+
13+
#![feature(nll)]
14+
15+
#[derive(Clone)]
16+
struct Foo(Option<Box<Foo>>, Option<Box<Foo>>);
17+
18+
fn test(f: &mut Foo) {
19+
match *f {
20+
Foo(Some(ref mut left), Some(ref mut right)) => match **left {
21+
Foo(Some(ref mut left), Some(ref mut right)) => panic!(),
22+
_ => panic!(),
23+
},
24+
_ => panic!(),
25+
}
26+
}
27+
28+
fn main() {
29+
}

src/test/run-pass/stack-probes-lto.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// ignore-mips64
1515
// ignore-powerpc
1616
// ignore-s390x
17+
// ignore-sparc
18+
// ignore-sparc64
1719
// ignore-wasm
1820
// ignore-cloudabi no processes
1921
// ignore-emscripten no processes

src/test/run-pass/stack-probes.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// ignore-mips64
1515
// ignore-powerpc
1616
// ignore-s390x
17+
// ignore-sparc
18+
// ignore-sparc64
1719
// ignore-wasm
1820
// ignore-cloudabi no processes
1921
// ignore-emscripten no processes

src/test/ui/const-eval/index_out_of_bound.rs renamed to src/test/rustdoc/invalid.crate.name.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
static FOO: i32 = [][0];
12-
//~^ ERROR E0080
11+
// compile-flags: --crate-name foo
1312

14-
fn main() {}
13+
pub fn foo() {}

src/test/ui/asm-out-assign-imm.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// ignore-emscripten
1313
// ignore-powerpc
1414
// ignore-sparc
15+
// ignore-sparc64
1516
// ignore-mips
1617

1718
#![feature(asm)]

src/test/ui/asm-out-assign-imm.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0384]: cannot assign twice to immutable variable `x`
2-
--> $DIR/asm-out-assign-imm.rs:30:9
2+
--> $DIR/asm-out-assign-imm.rs:31:9
33
|
44
LL | x = 1;
55
| ----- first assignment to `x`

src/test/ui/const-eval/index_out_of_bound.stderr

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
static FOO: i32 = [][0];
12+
//~^ ERROR E0080
13+
14+
fn main() {
15+
let array = [std::env::args().len()];
16+
array[1]; //~ ERROR index out of bounds
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0080]: constant evaluation error
2+
--> $DIR/index_out_of_bounds.rs:11:19
3+
|
4+
LL | static FOO: i32 = [][0];
5+
| ^^^^^ index out of bounds: the len is 0 but the index is 0
6+
7+
error: index out of bounds: the len is 1 but the index is 1
8+
--> $DIR/index_out_of_bounds.rs:16:5
9+
|
10+
LL | array[1]; //~ ERROR index out of bounds
11+
| ^^^^^^^^
12+
|
13+
= note: #[deny(const_err)] on by default
14+
15+
error: aborting due to 2 previous errors
16+
17+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)