From b345437c3c1f73cc5c97544f07ab327abe86a2ed Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 23 Jul 2015 22:19:12 -0700 Subject: [PATCH] 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 --- src/librustc/lint/context.rs | 17 ++++++++++++++++- src/librustc/session/config.rs | 12 ++++++++++++ src/test/compile-fail/bad-lint-cap.rs | 14 ++++++++++++++ src/test/compile-fail/bad-lint-cap2.rs | 17 +++++++++++++++++ src/test/compile-fail/bad-lint-cap3.rs | 20 ++++++++++++++++++++ src/test/run-pass/lint-cap.rs | 18 ++++++++++++++++++ 6 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 src/test/compile-fail/bad-lint-cap.rs create mode 100644 src/test/compile-fail/bad-lint-cap2.rs create mode 100644 src/test/compile-fail/bad-lint-cap3.rs create mode 100644 src/test/run-pass/lint-cap.rs diff --git a/src/librustc/lint/context.rs b/src/librustc/lint/context.rs index 6d29a1031c6..40ee023b7e8 100644 --- a/src/librustc/lint/context.rs +++ b/src/librustc/lint/context.rs @@ -34,6 +34,7 @@ use util::nodemap::FnvHashMap; use std::cell::RefCell; +use std::cmp; use std::mem; use syntax::ast_util::IdVisitingOperation; use syntax::attr::AttrMetaMethods; @@ -66,6 +67,9 @@ pub struct LintStore { /// Map of registered lint groups to what lints they expand to. The bool /// is true if the lint group was added by a plugin. lint_groups: FnvHashMap<&'static str, (Vec, bool)>, + + /// Maximum level a lint can be + lint_cap: Option, } /// The targed of the `by_name` map, which accounts for renaming/deprecation. @@ -94,7 +98,10 @@ fn get_level_source(&self, lint: LintId) -> LevelSource { } } - 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 { self.levels.remove(&lint); } else { @@ -109,6 +116,7 @@ pub fn new() -> LintStore { by_name: FnvHashMap(), levels: FnvHashMap(), lint_groups: FnvHashMap(), + lint_cap: None, } } @@ -227,6 +235,13 @@ pub fn process_command_line(&mut self, sess: &Session) { } } } + + 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); + } + } } } diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index c6ce3a22d9b..c14a0595d5a 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -84,6 +84,7 @@ pub struct Options { pub debug_assertions: bool, pub debuginfo: DebugInfoLevel, pub lint_opts: Vec<(String, lint::Level)>, + pub lint_cap: Option, pub describe_lints: bool, pub output_types: Vec, // This was mutable for rustpkg, which updates search paths based on the @@ -203,6 +204,7 @@ pub fn basic_options() -> Options { optimize: No, debuginfo: NoDebugInfo, lint_opts: Vec::new(), + lint_cap: None, describe_lints: false, output_types: Vec::new(), search_paths: SearchPaths::new(), @@ -792,6 +794,9 @@ pub fn rustc_short_optgroups() -> Vec { opt::multi("A", "allow", "Set lint allowed", "OPT"), opt::multi("D", "deny", "Set lint denied", "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::flag("V", "version", "Print version info and exit"), 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 parse_only = debugging_opts.parse_only; @@ -1023,6 +1034,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { optimize: opt_level, debuginfo: debuginfo, lint_opts: lint_opts, + lint_cap: lint_cap, describe_lints: describe_lints, output_types: output_types, search_paths: search_paths, diff --git a/src/test/compile-fail/bad-lint-cap.rs b/src/test/compile-fail/bad-lint-cap.rs new file mode 100644 index 00000000000..cb9c347af60 --- /dev/null +++ b/src/test/compile-fail/bad-lint-cap.rs @@ -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 or the MIT license +// , 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() {} diff --git a/src/test/compile-fail/bad-lint-cap2.rs b/src/test/compile-fail/bad-lint-cap2.rs new file mode 100644 index 00000000000..5a97d7b1a79 --- /dev/null +++ b/src/test/compile-fail/bad-lint-cap2.rs @@ -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 or the MIT license +// , 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() {} diff --git a/src/test/compile-fail/bad-lint-cap3.rs b/src/test/compile-fail/bad-lint-cap3.rs new file mode 100644 index 00000000000..e03ba6ecb64 --- /dev/null +++ b/src/test/compile-fail/bad-lint-cap3.rs @@ -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 or the MIT license +// , 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 + diff --git a/src/test/run-pass/lint-cap.rs b/src/test/run-pass/lint-cap.rs new file mode 100644 index 00000000000..003a99f746a --- /dev/null +++ b/src/test/run-pass/lint-cap.rs @@ -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 or the MIT license +// , 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() {} + -- GitLab