rustc: Add a --cap-lints flag to the compiler

This commit is an implementation of [RFC 1193][rfc] which adds the ability to
the compiler to cap the lint level for the entire compilation session. This flag
will ensure that no lints will go above this level, and Cargo will primarily use
this flag passing `--cap-lints allow` to all upstream dependencies.

[rfc]: https://github.com/rust-lang/rfcs/pull/1193

Closes #27259
This commit is contained in:
Alex Crichton 2015-07-23 22:19:12 -07:00
parent c85ba3e9cb
commit b345437c3c
6 changed files with 97 additions and 1 deletions

View File

@ -34,6 +34,7 @@ use lint::builtin;
use util::nodemap::FnvHashMap; use util::nodemap::FnvHashMap;
use std::cell::RefCell; use std::cell::RefCell;
use std::cmp;
use std::mem; use std::mem;
use syntax::ast_util::IdVisitingOperation; use syntax::ast_util::IdVisitingOperation;
use syntax::attr::AttrMetaMethods; use syntax::attr::AttrMetaMethods;
@ -66,6 +67,9 @@ pub struct LintStore {
/// Map of registered lint groups to what lints they expand to. The bool /// Map of registered lint groups to what lints they expand to. The bool
/// is true if the lint group was added by a plugin. /// is true if the lint group was added by a plugin.
lint_groups: FnvHashMap<&'static str, (Vec<LintId>, bool)>, lint_groups: FnvHashMap<&'static str, (Vec<LintId>, bool)>,
/// Maximum level a lint can be
lint_cap: Option<Level>,
} }
/// The targed of the `by_name` map, which accounts for renaming/deprecation. /// The targed of the `by_name` map, which accounts for renaming/deprecation.
@ -94,7 +98,10 @@ impl LintStore {
} }
} }
fn set_level(&mut self, lint: LintId, lvlsrc: LevelSource) { fn set_level(&mut self, lint: LintId, mut lvlsrc: LevelSource) {
if let Some(cap) = self.lint_cap {
lvlsrc.0 = cmp::min(lvlsrc.0, cap);
}
if lvlsrc.0 == Allow { if lvlsrc.0 == Allow {
self.levels.remove(&lint); self.levels.remove(&lint);
} else { } else {
@ -109,6 +116,7 @@ impl LintStore {
by_name: FnvHashMap(), by_name: FnvHashMap(),
levels: FnvHashMap(), levels: FnvHashMap(),
lint_groups: FnvHashMap(), lint_groups: FnvHashMap(),
lint_cap: None,
} }
} }
@ -227,6 +235,13 @@ impl LintStore {
} }
} }
} }
self.lint_cap = sess.opts.lint_cap;
if let Some(cap) = self.lint_cap {
for level in self.levels.iter_mut().map(|p| &mut (p.1).0) {
*level = cmp::min(*level, cap);
}
}
} }
} }

View File

@ -84,6 +84,7 @@ pub struct Options {
pub debug_assertions: bool, pub debug_assertions: bool,
pub debuginfo: DebugInfoLevel, pub debuginfo: DebugInfoLevel,
pub lint_opts: Vec<(String, lint::Level)>, pub lint_opts: Vec<(String, lint::Level)>,
pub lint_cap: Option<lint::Level>,
pub describe_lints: bool, pub describe_lints: bool,
pub output_types: Vec<OutputType>, pub output_types: Vec<OutputType>,
// This was mutable for rustpkg, which updates search paths based on the // This was mutable for rustpkg, which updates search paths based on the
@ -203,6 +204,7 @@ pub fn basic_options() -> Options {
optimize: No, optimize: No,
debuginfo: NoDebugInfo, debuginfo: NoDebugInfo,
lint_opts: Vec::new(), lint_opts: Vec::new(),
lint_cap: None,
describe_lints: false, describe_lints: false,
output_types: Vec::new(), output_types: Vec::new(),
search_paths: SearchPaths::new(), search_paths: SearchPaths::new(),
@ -792,6 +794,9 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
opt::multi("A", "allow", "Set lint allowed", "OPT"), opt::multi("A", "allow", "Set lint allowed", "OPT"),
opt::multi("D", "deny", "Set lint denied", "OPT"), opt::multi("D", "deny", "Set lint denied", "OPT"),
opt::multi("F", "forbid", "Set lint forbidden", "OPT"), opt::multi("F", "forbid", "Set lint forbidden", "OPT"),
opt::multi("", "cap-lints", "Set the most restrictive lint level. \
More restrictive lints are capped at this \
level", "LEVEL"),
opt::multi("C", "codegen", "Set a codegen option", "OPT[=VALUE]"), opt::multi("C", "codegen", "Set a codegen option", "OPT[=VALUE]"),
opt::flag("V", "version", "Print version info and exit"), opt::flag("V", "version", "Print version info and exit"),
opt::flag("v", "verbose", "Use verbose output"), opt::flag("v", "verbose", "Use verbose output"),
@ -860,6 +865,12 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
} }
} }
let lint_cap = matches.opt_str("cap-lints").map(|cap| {
lint::Level::from_str(&cap).unwrap_or_else(|| {
early_error(&format!("unknown lint level: `{}`", cap))
})
});
let debugging_opts = build_debugging_options(matches); let debugging_opts = build_debugging_options(matches);
let parse_only = debugging_opts.parse_only; let parse_only = debugging_opts.parse_only;
@ -1023,6 +1034,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
optimize: opt_level, optimize: opt_level,
debuginfo: debuginfo, debuginfo: debuginfo,
lint_opts: lint_opts, lint_opts: lint_opts,
lint_cap: lint_cap,
describe_lints: describe_lints, describe_lints: describe_lints,
output_types: output_types, output_types: output_types,
search_paths: search_paths, search_paths: search_paths,

View File

@ -0,0 +1,14 @@
// Copyright 2015 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.
// compile-flags: --cap-lints test
// error-pattern: unknown lint level: `test`
fn main() {}

View File

@ -0,0 +1,17 @@
// Copyright 2015 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.
// compile-flags: --cap-lints deny
#![deny(warnings)]
use std::option; //~ ERROR
fn main() {}

View File

@ -0,0 +1,20 @@
// Copyright 2015 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.
// compile-flags: --cap-lints warn
#![deny(warnings)]
#![feature(rustc_attrs)]
use std::option; //~ WARN
#[rustc_error]
fn main() {} //~ ERROR: compilation successful

View File

@ -0,0 +1,18 @@
// Copyright 2015 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.
// compile-flags: --cap-lints allow
#![deny(warnings)]
use std::option;
fn main() {}