Skip to content

Commit

Permalink
Auto merge of #51369 - Mark-Simulacrum:rollup, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
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:
  • Loading branch information
bors committed Jun 5, 2018
2 parents 90f34b5 + d011150 commit 4a9c58c
Show file tree
Hide file tree
Showing 33 changed files with 141 additions and 48 deletions.
47 changes: 34 additions & 13 deletions src/doc/rustdoc/src/documentation-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,17 @@ from your example, but are important to make the tests work. Consider
an example block that looks like this:

```text
/// Some documentation.
# fn foo() {}
/// /// Some documentation.
/// # fn foo() {} // this function will be hidden
/// println!("Hello, World!");
```

It will render like this:

```rust
/// Some documentation.
# fn foo() {}
println!("Hello, World!");
```

Yes, that's right: you can add lines that start with `# `, and they will
Expand Down Expand Up @@ -168,37 +170,56 @@ By repeating all parts of the example, you can ensure that your example still
compiles, while only showing the parts that are relevant to that part of your
explanation.

Another case where the use of `#` is handy is when you want to ignore
error handling. Lets say you want the following,

## Using `?` in doc tests

When writing an example, it is rarely useful to include a complete error
handling, as it would add significant amounts of boilerplate code. Instead, you
may want the following:

```ignore
/// ```
/// use std::io;
/// let mut input = String::new();
/// io::stdin().read_line(&mut input)?;
/// ```
```
The problem is that `?` returns a `Result<T, E>` and test functions
don't return anything so this will give a mismatched types error.
The problem is that `?` returns a `Result<T, E>` and test functions don't
return anything, so this will give a mismatched types error.
You can get around this limitation by manually adding a `main` that returns
`Result<T, E>`, because `Result<T, E>` implements the `Termination` trait:
```ignore
/// A doc test using ?
///
/// ```
/// use std::io;
/// # fn foo() -> io::Result<()> {
///
/// fn main() -> io::Result<()> {
/// let mut input = String::new();
/// io::stdin().read_line(&mut input)?;
/// Ok(())
/// }
/// ```
```
Together with the `# ` from the section above, you arrive at a solution that
appears to the reader as the initial idea but works with doc tests:
```ignore
/// ```
/// use std::io;
/// # fn main() -> io::Result<()> {
/// let mut input = String::new();
/// io::stdin().read_line(&mut input)?;
/// # Ok(())
/// # }
/// ```
# fn foo() {}
```
You can get around this by wrapping the code in a function. This catches
and swallows the `Result<T, E>` when running tests on the docs. This
pattern appears regularly in the standard library.
### Documenting macros
## Documenting macros
Here’s an example of documenting a macro:
Expand Down
10 changes: 0 additions & 10 deletions src/librustc_mir/transform/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,6 @@ impl<'b, 'a, 'tcx:'b> ConstPropagator<'b, 'a, 'tcx> {
) -> Option<Const<'tcx>> {
let span = source_info.span;
match *rvalue {
// No need to overwrite an already evaluated constant
Rvalue::Use(Operand::Constant(box Constant {
literal: Literal::Value {
value: &ty::Const {
val: ConstVal::Value(_),
..
},
},
..
})) => None,
// This branch exists for the sanity type check
Rvalue::Use(Operand::Constant(ref c)) => {
assert_eq!(c.ty, place_ty);
Expand Down
5 changes: 4 additions & 1 deletion src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,10 @@ pub fn run_core(search_paths: SearchPaths,

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

let name = ::rustc_codegen_utils::link::find_crate_name(Some(&sess), &krate.attrs, &input);
let name = match crate_name {
Some(ref crate_name) => crate_name.clone(),
None => ::rustc_codegen_utils::link::find_crate_name(Some(&sess), &krate.attrs, &input),
};

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

Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/redox/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl Instant {

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

Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/unix/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ mod inner {

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

Expand Down
1 change: 1 addition & 0 deletions src/test/codegen/abi-main-signature-16bit-c-int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// ignore-powerpc64
// ignore-s390x
// ignore-sparc
// ignore-sparc64
// ignore-wasm32
// ignore-x86
// ignore-x86_64
Expand Down
1 change: 1 addition & 0 deletions src/test/codegen/fastcall-inreg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
// ignore-r600
// ignore-amdgcn
// ignore-sparc
// ignore-sparc64
// ignore-sparcv9
// ignore-sparcel
// ignore-s390x
Expand Down
2 changes: 2 additions & 0 deletions src/test/codegen/repr-transparent-aggregates-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// ignore-asmjs
// ignore-mips64
// ignore-s390x
// ignore-sparc
// ignore-sparc64
// ignore-wasm
// ignore-x86
// ignore-x86_64
Expand Down
2 changes: 2 additions & 0 deletions src/test/codegen/stack-probes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// ignore-mips64
// ignore-powerpc
// ignore-s390x
// ignore-sparc
// ignore-sparc64
// ignore-wasm
// ignore-emscripten
// ignore-windows
Expand Down
2 changes: 2 additions & 0 deletions src/test/codegen/x86_mmx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// ignore-emscripten
// ignore-mips
// ignore-mips64
// ignore-sparc
// ignore-sparc64
// compile-flags: -O

#![feature(repr_simd)]
Expand Down
1 change: 1 addition & 0 deletions src/test/compile-fail/asm-bad-clobber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// ignore-emscripten
// ignore-powerpc
// ignore-sparc
// ignore-sparc64
// ignore-mips
// ignore-mips64

Expand Down
1 change: 1 addition & 0 deletions src/test/compile-fail/asm-in-bad-modifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ignore-emscripten
// ignore-powerpc
// ignore-sparc
// ignore-sparc64
// ignore-mips
// ignore-mips64

Expand Down
1 change: 1 addition & 0 deletions src/test/compile-fail/asm-misplaced-option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// ignore-emscripten
// ignore-powerpc
// ignore-sparc
// ignore-sparc64
// ignore-mips
// ignore-mips64

Expand Down
1 change: 1 addition & 0 deletions src/test/compile-fail/asm-out-no-modifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ignore-emscripten
// ignore-powerpc
// ignore-sparc
// ignore-sparc64
// ignore-mips
// ignore-mips64

Expand Down
1 change: 1 addition & 0 deletions src/test/compile-fail/asm-out-read-uninit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ignore-emscripten
// ignore-powerpc
// ignore-sparc
// ignore-sparc64
// ignore-mips
// ignore-mips64

Expand Down
1 change: 1 addition & 0 deletions src/test/compile-fail/borrowck/borrowck-asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ignore-emscripten
// ignore-powerpc
// ignore-sparc
// ignore-sparc64

// revisions: ast mir
//[mir]compile-flags: -Z borrowck=mir
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/const-err-early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub const C: u8 = 200u8 * 4; //~ ERROR const_err
//~^ ERROR this constant cannot be used
pub const D: u8 = 42u8 - (42u8 + 1); //~ ERROR const_err
//~^ ERROR this constant cannot be used
pub const E: u8 = [5u8][1];
//~^ ERROR const_err
pub const E: u8 = [5u8][1]; //~ ERROR const_err
//~| ERROR this constant cannot be used

fn main() {
let _a = A;
Expand Down
1 change: 1 addition & 0 deletions src/test/compile-fail/const-err2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fn main() {
let d = 42u8 - (42u8 + 1);
//~^ ERROR const_err
let _e = [5u8][1];
//~^ ERROR const_err
black_box(a);
black_box(b);
black_box(c);
Expand Down
1 change: 1 addition & 0 deletions src/test/compile-fail/const-err3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fn main() {
let d = 42u8 - (42u8 + 1);
//~^ ERROR const_err
let _e = [5u8][1];
//~^ ERROR const_err
black_box(b);
black_box(c);
black_box(d);
Expand Down
1 change: 1 addition & 0 deletions src/test/run-fail/mir_indexing_oob_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

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

#[allow(const_err)]
fn test() -> u32 {
C[10]
}
Expand Down
1 change: 1 addition & 0 deletions src/test/run-fail/mir_indexing_oob_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

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

#[allow(const_err)]
fn test() -> u8 {
C[10]
}
Expand Down
1 change: 1 addition & 0 deletions src/test/run-fail/mir_indexing_oob_3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

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

#[allow(const_err)]
fn mir() -> u8 {
C[10]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Test case from #39963.

#![feature(nll)]

#[derive(Clone)]
struct Foo(Option<Box<Foo>>, Option<Box<Foo>>);

fn test(f: &mut Foo) {
match *f {
Foo(Some(ref mut left), Some(ref mut right)) => match **left {
Foo(Some(ref mut left), Some(ref mut right)) => panic!(),
_ => panic!(),
},
_ => panic!(),
}
}

fn main() {
}
2 changes: 2 additions & 0 deletions src/test/run-pass/stack-probes-lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// ignore-mips64
// ignore-powerpc
// ignore-s390x
// ignore-sparc
// ignore-sparc64
// ignore-wasm
// ignore-cloudabi no processes
// ignore-emscripten no processes
Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/stack-probes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// ignore-mips64
// ignore-powerpc
// ignore-s390x
// ignore-sparc
// ignore-sparc64
// ignore-wasm
// ignore-cloudabi no processes
// ignore-emscripten no processes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

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

fn main() {}
pub fn foo() {}
1 change: 1 addition & 0 deletions src/test/ui/asm-out-assign-imm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ignore-emscripten
// ignore-powerpc
// ignore-sparc
// ignore-sparc64
// ignore-mips

#![feature(asm)]
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/asm-out-assign-imm.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0384]: cannot assign twice to immutable variable `x`
--> $DIR/asm-out-assign-imm.rs:30:9
--> $DIR/asm-out-assign-imm.rs:31:9
|
LL | x = 1;
| ----- first assignment to `x`
Expand Down
9 changes: 0 additions & 9 deletions src/test/ui/const-eval/index_out_of_bound.stderr

This file was deleted.

17 changes: 17 additions & 0 deletions src/test/ui/const-eval/index_out_of_bounds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

static FOO: i32 = [][0];
//~^ ERROR E0080

fn main() {
let array = [std::env::args().len()];
array[1]; //~ ERROR index out of bounds
}
17 changes: 17 additions & 0 deletions src/test/ui/const-eval/index_out_of_bounds.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0080]: constant evaluation error
--> $DIR/index_out_of_bounds.rs:11:19
|
LL | static FOO: i32 = [][0];
| ^^^^^ index out of bounds: the len is 0 but the index is 0

error: index out of bounds: the len is 1 but the index is 1
--> $DIR/index_out_of_bounds.rs:16:5
|
LL | array[1]; //~ ERROR index out of bounds
| ^^^^^^^^
|
= note: #[deny(const_err)] on by default

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0080`.
Loading

0 comments on commit 4a9c58c

Please sign in to comment.