Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it an error to not declare used features #23598

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions src/compiletest/compiletest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#![feature(std_misc)]
#![feature(test)]
#![feature(path_ext)]
#![feature(str_char)]

#![deny(warnings)]

Expand Down
19 changes: 10 additions & 9 deletions src/compiletest/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ pub struct TestProps {
pub check_stdout: bool,
// Don't force a --crate-type=dylib flag on the command line
pub no_prefer_dynamic: bool,
// Don't run --pretty expanded when running pretty printing tests
pub no_pretty_expanded: bool,
// Run --pretty expanded when running pretty printing tests
pub pretty_expanded: bool,
// Which pretty mode are we testing with, default to 'normal'
pub pretty_mode: String,
// Only compare pretty output and don't try compiling
Expand All @@ -62,7 +62,7 @@ pub fn load_props(testfile: &Path) -> TestProps {
let mut force_host = false;
let mut check_stdout = false;
let mut no_prefer_dynamic = false;
let mut no_pretty_expanded = false;
let mut pretty_expanded = false;
let mut pretty_mode = None;
let mut pretty_compare_only = false;
let mut forbid_output = Vec::new();
Expand Down Expand Up @@ -96,8 +96,8 @@ pub fn load_props(testfile: &Path) -> TestProps {
no_prefer_dynamic = parse_no_prefer_dynamic(ln);
}

if !no_pretty_expanded {
no_pretty_expanded = parse_no_pretty_expanded(ln);
if !pretty_expanded {
pretty_expanded = parse_pretty_expanded(ln);
}

if pretty_mode.is_none() {
Expand Down Expand Up @@ -152,7 +152,7 @@ pub fn load_props(testfile: &Path) -> TestProps {
force_host: force_host,
check_stdout: check_stdout,
no_prefer_dynamic: no_prefer_dynamic,
no_pretty_expanded: no_pretty_expanded,
pretty_expanded: pretty_expanded,
pretty_mode: pretty_mode.unwrap_or("normal".to_string()),
pretty_compare_only: pretty_compare_only,
forbid_output: forbid_output,
Expand Down Expand Up @@ -295,8 +295,8 @@ fn parse_no_prefer_dynamic(line: &str) -> bool {
parse_name_directive(line, "no-prefer-dynamic")
}

fn parse_no_pretty_expanded(line: &str) -> bool {
parse_name_directive(line, "no-pretty-expanded")
fn parse_pretty_expanded(line: &str) -> bool {
parse_name_directive(line, "pretty-expanded")
}

fn parse_pretty_mode(line: &str) -> Option<String> {
Expand Down Expand Up @@ -340,7 +340,8 @@ fn parse_pp_exact(line: &str, testfile: &Path) -> Option<PathBuf> {
}

fn parse_name_directive(line: &str, directive: &str) -> bool {
line.contains(directive)
// This 'no-' rule is a quick hack to allow pretty-expanded and no-pretty-expanded to coexist
line.contains(directive) && !line.contains(&("no-".to_string() + directive))
}

pub fn parse_name_value_directive(line: &str, directive: &str)
Expand Down
2 changes: 1 addition & 1 deletion src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ fn run_pretty_test(config: &Config, props: &TestProps, testfile: &Path) {
if !proc_res.status.success() {
fatal_proc_rec("pretty-printed source does not typecheck", &proc_res);
}
if props.no_pretty_expanded { return }
if !props.pretty_expanded { return }

// additionally, run `--pretty expanded` and try to build it.
let proc_res = print_source(config, props, testfile, srcs[round].clone(), "expanded");
Expand Down
6 changes: 4 additions & 2 deletions src/doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,7 @@ may optionally begin with any number of `attributes` that apply to the
containing module. Attributes on the anonymous crate module define important
metadata that influences the behavior of the compiler.

```{.rust}
# #![allow(unused_attribute)]
```no_run
// Crate name
#![crate_name = "projx"]

Expand Down Expand Up @@ -1020,6 +1019,7 @@ Use declarations support a number of convenient shortcuts:
An example of `use` declarations:

```
# #![feature(core)]
use std::iter::range_step;
use std::option::Option::{Some, None};
use std::collections::hash_map::{self, HashMap};
Expand Down Expand Up @@ -1080,6 +1080,7 @@ declarations.
An example of what will and will not work for `use` items:

```
# #![feature(core)]
# #![allow(unused_imports)]
use foo::core::iter; // good: foo is at the root of the crate
use foo::baz::foobaz; // good: foo is at the root of the crate
Expand Down Expand Up @@ -1781,6 +1782,7 @@ functions, with the exception that they may not have a body and are instead
terminated by a semicolon.

```
# #![feature(libc)]
extern crate libc;
use libc::{c_char, FILE};

Expand Down
5 changes: 5 additions & 0 deletions src/doc/trpl/concurrency.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ When `guard` goes out of scope, it will block execution until the thread is
finished. If we didn't want this behaviour, we could use `thread::spawn()`:

```
# #![feature(old_io, std_misc)]
use std::thread;
use std::old_io::timer;
use std::time::Duration;
Expand Down Expand Up @@ -146,6 +147,7 @@ As an example, here is a Rust program that would have a data race in many
languages. It will not compile:

```ignore
# #![feature(old_io, std_misc)]
use std::thread;
use std::old_io::timer;
use std::time::Duration;
Expand Down Expand Up @@ -185,6 +187,7 @@ only one person at a time can mutate what's inside. For that, we can use the
but for a different reason:

```ignore
# #![feature(old_io, std_misc)]
use std::thread;
use std::old_io::timer;
use std::time::Duration;
Expand Down Expand Up @@ -229,6 +232,7 @@ guard across thread boundaries, which gives us our error.
We can use `Arc<T>` to fix this. Here's the working version:

```
# #![feature(old_io, std_misc)]
use std::sync::{Arc, Mutex};
use std::thread;
use std::old_io::timer;
Expand All @@ -254,6 +258,7 @@ handle is then moved into the new thread. Let's examine the body of the
thread more closely:

```
# #![feature(old_io, std_misc)]
# use std::sync::{Arc, Mutex};
# use std::thread;
# use std::old_io::timer;
Expand Down
19 changes: 11 additions & 8 deletions src/doc/trpl/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,17 @@ fn main() {
}
```

Here's the full algorithm:

1. Given a code block, if it does not contain `fn main()`, it is wrapped in
`fn main() { your_code }`
2. Given that result, if it contains no `extern crate` directives but it also
contains the name of the crate being tested, then `extern crate <name>` is
injected at the top.
3. Some common allow attributes are added for documentation examples at the top.
Here's the full algorithm rustdoc uses to postprocess examples:

1. Any leading `#![foo]` attributes are left intact as crate attributes.
2. Some common `allow` attributes are inserted, including
`unused_variables`, `unused_assignments`, `unused_mut`,
`unused_attributes`, and `dead_code`. Small examples often trigger
these lints.
3. If the example does not contain `extern crate`, then `extern crate
<mycrate>;` is inserted.
2. Finally, if the example does not contain `fn main`, the remainder of the
text is wrapped in `fn main() { your_code }`

Sometimes, this isn't enough, though. For example, all of these code samples
with `///` we've been talking about? The raw text:
Expand Down
8 changes: 8 additions & 0 deletions src/doc/trpl/ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The following is a minimal example of calling a foreign function which will
compile if snappy is installed:

```no_run
# #![feature(libc)]
extern crate libc;
use libc::size_t;

Expand Down Expand Up @@ -45,6 +46,7 @@ keeping the binding correct at runtime.
The `extern` block can be extended to cover the entire snappy API:

```no_run
# #![feature(libc)]
extern crate libc;
use libc::{c_int, size_t};

Expand Down Expand Up @@ -80,6 +82,7 @@ length is number of elements currently contained, and the capacity is the total
the allocated memory. The length is less than or equal to the capacity.

```
# #![feature(libc)]
# extern crate libc;
# use libc::{c_int, size_t};
# unsafe fn snappy_validate_compressed_buffer(_: *const u8, _: size_t) -> c_int { 0 }
Expand All @@ -104,6 +107,7 @@ required capacity to hold the compressed output. The vector can then be passed t
the true length after compression for setting the length.

```
# #![feature(libc)]
# extern crate libc;
# use libc::{size_t, c_int};
# unsafe fn snappy_compress(a: *const u8, b: size_t, c: *mut u8,
Expand All @@ -130,6 +134,7 @@ Decompression is similar, because snappy stores the uncompressed size as part of
format and `snappy_uncompressed_length` will retrieve the exact buffer size required.

```
# #![feature(libc)]
# extern crate libc;
# use libc::{size_t, c_int};
# unsafe fn snappy_uncompress(compressed: *const u8,
Expand Down Expand Up @@ -408,6 +413,7 @@ global state. In order to access these variables, you declare them in `extern`
blocks with the `static` keyword:

```no_run
# #![feature(libc)]
extern crate libc;

#[link(name = "readline")]
Expand All @@ -426,6 +432,7 @@ interface. To do this, statics can be declared with `mut` so we can mutate
them.

```no_run
# #![feature(libc)]
extern crate libc;

use std::ffi::CString;
Expand Down Expand Up @@ -458,6 +465,7 @@ calling foreign functions. Some foreign functions, most notably the Windows API,
conventions. Rust provides a way to tell the compiler which convention to use:

```
# #![feature(libc)]
extern crate libc;

#[cfg(all(target_os = "win32", target_arch = "x86"))]
Expand Down
2 changes: 2 additions & 0 deletions src/doc/trpl/iterators.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ These two basic iterators should serve you well. There are some more
advanced iterators, including ones that are infinite. Like `count`:

```rust
# #![feature(core)]
std::iter::count(1, 5);
```

Expand Down Expand Up @@ -294,6 +295,7 @@ has no side effect on the original iterator. Let's try it out with our infinite
iterator from before, `count()`:

```rust
# #![feature(core)]
for i in std::iter::count(1, 5).take(5) {
println!("{}", i);
}
Expand Down
3 changes: 3 additions & 0 deletions src/doc/trpl/method-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ the ability to use this *method call syntax* via the `impl` keyword.
Here's how it works:

```{rust}
# #![feature(core)]
struct Circle {
x: f64,
y: f64,
Expand Down Expand Up @@ -87,6 +88,7 @@ original example, `foo.bar().baz()`? This is called 'method chaining', and we
can do it by returning `self`.

```
# #![feature(core)]
struct Circle {
x: f64,
y: f64,
Expand Down Expand Up @@ -164,6 +166,7 @@ have method overloading, named arguments, or variable arguments. We employ
the builder pattern instead. It looks like this:

```
# #![feature(core)]
struct Circle {
x: f64,
y: f64,
Expand Down
1 change: 1 addition & 0 deletions src/doc/trpl/more-strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ Rust provides iterators for each of these situations:
Usually, the `graphemes()` method on `&str` is what you want:

```
# #![feature(unicode)]
let s = "u͔n͈̰̎i̙̮͚̦c͚̉o̼̩̰͗d͔̆̓ͥé";

for l in s.graphemes(true) {
Expand Down
4 changes: 3 additions & 1 deletion src/doc/trpl/standard-input.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ we haven't seen before. Here's a simple program that reads some input,
and then prints it back out:

```{rust,ignore}
fn main() {
corefn main() {
println!("Type something!");

let input = std::old_io::stdin().read_line().ok().expect("Failed to read line");
Expand All @@ -28,6 +28,7 @@ Since writing the fully qualified name all the time is annoying, we can use
the `use` statement to import it in:

```{rust}
# #![feature(old_io)]
use std::old_io::stdin;

stdin();
Expand All @@ -37,6 +38,7 @@ However, it's considered better practice to not import individual functions, but
to import the module, and only use one level of qualification:

```{rust}
# #![feature(old_io)]
use std::old_io;

old_io::stdin();
Expand Down
2 changes: 2 additions & 0 deletions src/doc/trpl/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,8 @@ is an opaque "black box" to the optimizer and so forces it to consider any
argument as used.

```rust
# #![feature(test)]

extern crate test;

# fn main() {
Expand Down
6 changes: 6 additions & 0 deletions src/doc/trpl/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Do you remember the `impl` keyword, used to call a function with method
syntax?

```{rust}
# #![feature(core)]
struct Circle {
x: f64,
y: f64,
Expand All @@ -21,6 +22,7 @@ Traits are similar, except that we define a trait with just the method
signature, then implement the trait for that struct. Like this:

```{rust}
# #![feature(core)]
struct Circle {
x: f64,
y: f64,
Expand Down Expand Up @@ -84,6 +86,7 @@ which implements `HasArea` will have an `.area()` method.
Here's an extended example of how this works:

```{rust}
# #![feature(core)]
trait HasArea {
fn area(&self) -> f64;
}
Expand Down Expand Up @@ -225,6 +228,7 @@ If we add a `use` line right above `main` and make the right things public,
everything is fine:

```{rust}
# #![feature(core)]
use shapes::HasArea;

mod shapes {
Expand Down Expand Up @@ -408,6 +412,7 @@ but instead, we found a floating-point variable. We need a different bound. `Flo
to the rescue:

```
# #![feature(std_misc)]
use std::num::Float;

fn inverse<T: Float>(x: T) -> Result<T, String> {
Expand All @@ -423,6 +428,7 @@ from the `Float` trait. Both `f32` and `f64` implement `Float`, so our function
works just fine:

```
# #![feature(std_misc)]
# use std::num::Float;
# fn inverse<T: Float>(x: T) -> Result<T, String> {
# if x == Float::zero() { return Err("x cannot be zero!".to_string()) }
Expand Down
Loading