Nomenclature fixes in the lint checker. Fewer double-negatives.

New style is allow(foo), warn(foo), deny(foo) and forbid(foo),
mirrored by -A foo, -W foo, -D foo and -F foo on command line.

These replace -W no-foo, -W foo, -W err-foo, respectively.

Forbid is new, and means "deny, and you can't override it".
This commit is contained in:
Graydon Hoare 2012-07-26 17:08:21 -07:00
parent c60a6b93fb
commit dbbaa50290
20 changed files with 225 additions and 140 deletions

View File

@ -7,7 +7,6 @@
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:
#[link(name = "cargo",
@ -19,7 +18,15 @@
#[no_core];
#[warn(no_non_implicitly_copyable_typarams,no_vecs_not_implicitly_copyable)];
// NB: transitional for stage0:
#[allow(unrecognized_lint)];
#[warn(no_unrecognized_warning)];
#[warn(no_non_implicitly_copyable_typarams,
no_vecs_not_implicitly_copyable)];
// The new version:
#[allow(vecs_implicitly_copyable,
non_implicitly_copyable_typarams)];
use core(vers = "0.3");
use std(vers = "0.3");

View File

@ -2,7 +2,12 @@
#[no_core];
// NB: transitional for stage0:
#[allow(unrecognized_lint)];
#[warn(no_unrecognized_warning)];
#[warn(no_vecs_not_implicitly_copyable)];
// The new version:
#[allow(vecs_implicitly_copyable)];
use core(vers = "0.3");
use std(vers = "0.3");

View File

@ -4,7 +4,12 @@
#[no_core];
// NB: transitional for stage0:
#[allow(unrecognized_lint)];
#[warn(no_unrecognized_warning)];
#[warn(no_vecs_not_implicitly_copyable)];
// The new version:
#[allow(vecs_implicitly_copyable)];
use core(vers = "0.3");
use std(vers = "0.3");
@ -17,5 +22,4 @@ import core::*;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:

View File

@ -31,7 +31,12 @@
// Don't link to core. We are core.
#[no_core];
// NB: transitional for stage0:
#[allow(unrecognized_lint)];
#[warn(no_unrecognized_warning)];
#[warn(no_vecs_not_implicitly_copyable)];
// The new version:
#[allow(vecs_implicitly_copyable)];
export int, i8, i16, i32, i64;
export uint, u8, u16, u32, u64;

View File

@ -10,7 +10,12 @@
#[no_core];
// NB: transitional for stage0:
#[allow(unrecognized_lint)];
#[warn(no_unrecognized_warning)];
#[warn(no_vecs_not_implicitly_copyable)];
// The new version:
#[allow(vecs_implicitly_copyable)];
use core(vers = "0.3");
import core::*;

View File

@ -328,7 +328,9 @@ fn get_concurrency() -> uint {
else { threads * sched_overcommit }
}
// NB: transitional duplication here.
#[warn(no_non_implicitly_copyable_typarams)]
#[allow(non_implicitly_copyable_typarams)]
fn filter_tests(opts: test_opts,
tests: ~[test_desc]) -> ~[test_desc] {
let mut filtered = copy tests;

View File

@ -8,7 +8,12 @@
#[no_core];
// NB: transitional for stage0:
#[allow(unrecognized_lint)];
#[warn(no_unrecognized_warning)];
#[warn(no_vecs_not_implicitly_copyable)];
// The new version:
#[allow(vecs_implicitly_copyable)];
use core(vers = "0.3");
use std(vers = "0.3");

View File

@ -158,7 +158,7 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg,
crate = time(time_passes, ~"core injection", ||
front::core_inject::maybe_inject_libcore_ref(sess, crate));
time(time_passes, ~"building warning settings table", ||
time(time_passes, ~"building lint settings table", ||
lint::build_settings_crate(sess, crate));
let ast_map = time(time_passes, ~"ast indexing", ||
@ -418,17 +418,28 @@ fn build_session_options(match: getopts::match,
let parse_only = opt_present(match, ~"parse-only");
let no_trans = opt_present(match, ~"no-trans");
let lint_flags = vec::append(getopts::opt_strs(match, ~"W"),
getopts::opt_strs(match, ~"warn"));
let lint_levels = [lint::allow, lint::warn,
lint::deny, lint::forbid];
let mut lint_opts = ~[];
let lint_dict = lint::get_lint_dict();
let lint_opts = do vec::map(lint_flags) |flag| {
alt lint::lookup_lint(lint_dict, flag) {
(flag, none) {
early_error(demitter, #fmt("unknown warning: %s", flag))
}
(_, some(x)) { x }
for lint_levels.each |level| {
let level_name = lint::level_to_str(level);
let level_short = level_name.substr(0,1).to_upper();
let flags = vec::append(getopts::opt_strs(match, level_short),
getopts::opt_strs(match, level_name));
for flags.each |lint_name| {
let lint_name = str::replace(lint_name, ~"-", ~"_");
alt lint_dict.find(lint_name) {
none {
early_error(demitter, #fmt("unknown %s flag: %s",
level_name, lint_name));
}
some(lint) {
vec::push(lint_opts, (lint.lint, level));
}
}
}
};
}
let mut debugging_opts = 0u;
let debug_flags = getopts::opt_strs(match, ~"Z");
@ -540,7 +551,7 @@ fn build_session_(sopts: @session::options,
sopts.maybe_sysroot,
sopts.target_triple,
sopts.addl_lib_search_paths);
let warning_settings = lint::mk_warning_settings();
let lint_settings = lint::mk_lint_settings();
session_(@{targ_cfg: target_cfg,
opts: sopts,
cstore: cstore,
@ -553,7 +564,7 @@ fn build_session_(sopts: @session::options,
filesearch: filesearch,
mut building_library: false,
working_dir: os::getcwd(),
warning_settings: warning_settings})
lint_settings: lint_settings})
}
fn parse_pretty(sess: session, &&name: ~str) -> pp_mode {
@ -582,6 +593,9 @@ fn opts() -> ~[getopts::opt] {
optopt(~"sysroot"), optopt(~"target"),
optmulti(~"W"), optmulti(~"warn"),
optmulti(~"A"), optmulti(~"allow"),
optmulti(~"D"), optmulti(~"deny"),
optmulti(~"F"), optmulti(~"forbid"),
optmulti(~"Z"),

View File

@ -1,5 +1,10 @@
#[no_core];
// NB: transitional for stage0:
#[allow(unrecognized_lint)];
#[warn(no_unrecognized_warning)];
#[warn(no_vecs_not_implicitly_copyable)];
// The new version:
#[allow(vecs_implicitly_copyable)];
use core(vers = "0.3");
use std(vers = "0.3");
@ -66,10 +71,12 @@ Options:
(see http://sources.redhat.com/autobook/autobook/
autobook_17.html for detail)
-W <foo> enable warning <foo>
-W no-<foo> disable warning <foo>
-W err-<foo> enable warning <foo> as an error
-W help Print available warnings and default settings
-(W|A|D|F) help Print available 'lint' checks and default settings
-W <foo> warn about <foo> by default
-A <foo> allow <foo> by default
-D <foo> deny <foo> by default
-F <foo> forbid <foo> (deny, and deny all overrides)
-Z help list internal options for debugging rustc
@ -84,7 +91,7 @@ fn describe_warnings() {
fn padded(max: uint, s: ~str) -> ~str {
str::from_bytes(vec::from_elem(max - s.len(), ' ' as u8)) + s
}
io::println(#fmt("\nAvailable warnings:\n"));
io::println(#fmt("\nAvailable lint checks:\n"));
io::println(#fmt(" %s %7.7s %s",
padded(max_key, ~"name"), ~"default", ~"meaning"));
io::println(#fmt(" %s %7.7s %s\n",
@ -93,9 +100,12 @@ fn describe_warnings() {
let k = str::replace(k, ~"_", ~"-");
io::println(#fmt(" %s %7.7s %s",
padded(max_key, k),
alt v.default { lint::warn { ~"warn" }
lint::error { ~"error" }
lint::ignore { ~"ignore" } },
alt v.default {
lint::allow { ~"allow" }
lint::warn { ~"warn" }
lint::deny { ~"deny" }
lint::forbid { ~"forbid" }
},
v.desc));
}
io::println(~"");

View File

@ -101,7 +101,7 @@ type session_ = {targ_cfg: @config,
filesearch: filesearch::filesearch,
mut building_library: bool,
working_dir: ~str,
warning_settings: lint::warning_settings};
lint_settings: lint::lint_settings};
enum session {
session_(@session_)
@ -153,16 +153,18 @@ impl session for session {
fn span_lint_level(level: lint::level,
sp: span, msg: ~str) {
alt level {
lint::ignore { }
lint::allow { }
lint::warn { self.span_warn(sp, msg); }
lint::error { self.span_err(sp, msg); }
lint::deny | lint::forbid {
self.span_err(sp, msg);
}
}
}
fn span_lint(lint_mode: lint::lint,
expr_id: ast::node_id, item_id: ast::node_id,
span: span, msg: ~str) {
let level = lint::get_warning_settings_level(
self.warning_settings, lint_mode, expr_id, item_id);
let level = lint::get_lint_settings_level(
self.lint_settings, lint_mode, expr_id, item_id);
self.span_lint_level(level, span, msg);
}
fn next_node_id() -> ast::node_id {

View File

@ -10,13 +10,13 @@ import io::writer_util;
import util::ppaux::{ty_to_str};
import syntax::print::pprust::{expr_to_str, mode_to_str};
export lint, ctypes, unused_imports, while_true, path_statement, old_vecs;
export unrecognized_warning, non_implicitly_copyable_typarams;
export vecs_not_implicitly_copyable, implicit_copies;
export level, ignore, warn, error;
export lookup_lint, lint_dict, get_lint_dict;
export get_warning_level, get_warning_settings_level;
export check_crate, build_settings_crate, mk_warning_settings;
export warning_settings;
export unrecognized_lint, non_implicitly_copyable_typarams;
export vecs_implicitly_copyable, implicit_copies;
export level, allow, warn, deny, forbid;
export lint_dict, get_lint_dict, level_to_str;
export get_lint_level, get_lint_settings_level;
export check_crate, build_settings_crate, mk_lint_settings;
export lint_settings;
/**
* A 'lint' check is a kind of miscellaneous constraint that a user _might_
@ -26,14 +26,14 @@ export warning_settings;
* to compile the program at all.
*
* We also build up a table containing information about lint settings, in
* order to allow other passes to take advantage of the warning attribute
* order to allow other passes to take advantage of the lint attribute
* infrastructure. To save space, the table is keyed by the id of /items/, not
* of every expression. When an item has the default settings, the entry will
* be omitted. If we start allowing warn attributes on expressions, we will
* be omitted. If we start allowing lint attributes on expressions, we will
* start having entries for expressions that do not share their enclosing
* items settings.
*
* This module then, exports two passes: one that populates the warning
* This module then, exports two passes: one that populates the lint
* settings table in the session and is run early in the compile process, and
* one that does a variety of lint checks, and is run late in the compile
* process.
@ -45,9 +45,9 @@ enum lint {
while_true,
path_statement,
implicit_copies,
unrecognized_warning,
unrecognized_lint,
non_implicitly_copyable_typarams,
vecs_not_implicitly_copyable,
vecs_implicitly_copyable,
deprecated_mode,
}
@ -60,15 +60,24 @@ fn int_to_lint(i: int) -> lint {
2 { while_true }
3 { path_statement }
4 { implicit_copies }
5 { unrecognized_warning }
5 { unrecognized_lint }
6 { non_implicitly_copyable_typarams }
7 { vecs_not_implicitly_copyable }
7 { vecs_implicitly_copyable }
8 { deprecated_mode }
}
}
fn level_to_str(lv: level) -> ~str {
alt lv {
allow { ~"allow" }
warn { ~"warn" }
deny { ~"deny" }
forbid { ~"forbid" }
}
}
enum level {
ignore, warn, error
allow, warn, deny, forbid
}
type lint_spec = @{lint: lint,
@ -91,7 +100,7 @@ fn get_lint_dict() -> lint_dict {
(~"unused_imports",
@{lint: unused_imports,
desc: ~"imports that are never used",
default: ignore}),
default: allow}),
(~"while_true",
@{lint: while_true,
@ -103,9 +112,9 @@ fn get_lint_dict() -> lint_dict {
desc: ~"path statements with no effect",
default: warn}),
(~"unrecognized_warning",
@{lint: unrecognized_warning,
desc: ~"unrecognized warning attribute",
(~"unrecognized_lint",
@{lint: unrecognized_lint,
desc: ~"unrecognized lint attribute",
default: warn}),
(~"non_implicitly_copyable_typarams",
@ -113,10 +122,10 @@ fn get_lint_dict() -> lint_dict {
desc: ~"passing non implicitly copyable types as copy type params",
default: warn}),
(~"vecs_not_implicitly_copyable",
@{lint: vecs_not_implicitly_copyable,
desc: ~"make vecs and strs not implicitly copyable\
(`err` is ignored; only checked at top level",
(~"vecs_implicitly_copyable",
@{lint: vecs_implicitly_copyable,
desc: ~"make vecs and strs not implicitly copyable \
(only checked at top level)",
default: warn}),
(~"implicit_copies",
@ -127,7 +136,7 @@ fn get_lint_dict() -> lint_dict {
(~"deprecated_mode",
@{lint: deprecated_mode,
desc: ~"warn about deprecated uses of modes",
default: ignore})
default: allow})
];
hash_from_strs(v)
}
@ -136,33 +145,33 @@ fn get_lint_dict() -> lint_dict {
type lint_modes = smallintmap<level>;
type lint_mode_map = hashmap<ast::node_id, lint_modes>;
// settings_map maps node ids of items with non-default warning settings
// settings_map maps node ids of items with non-default lint settings
// to their settings; default_settings contains the settings for everything
// not in the map.
type warning_settings = {
type lint_settings = {
default_settings: lint_modes,
settings_map: lint_mode_map
};
fn mk_warning_settings() -> warning_settings {
fn mk_lint_settings() -> lint_settings {
{default_settings: std::smallintmap::mk(),
settings_map: int_hash()}
}
fn get_warning_level(modes: lint_modes, lint: lint) -> level {
fn get_lint_level(modes: lint_modes, lint: lint) -> level {
alt modes.find(lint as uint) {
some(c) { c }
none { ignore }
none { allow }
}
}
fn get_warning_settings_level(settings: warning_settings,
fn get_lint_settings_level(settings: lint_settings,
lint_mode: lint,
_expr_id: ast::node_id,
item_id: ast::node_id) -> level {
alt settings.settings_map.find(item_id) {
some(modes) { get_warning_level(modes, lint_mode) }
none { get_warning_level(settings.default_settings, lint_mode) }
some(modes) { get_lint_level(modes, lint_mode) }
none { get_lint_level(settings.default_settings, lint_mode) }
}
}
@ -183,11 +192,11 @@ enum ctxt {
impl methods for ctxt {
fn get_level(lint: lint) -> level {
get_warning_level(self.curr, lint)
get_lint_level(self.curr, lint)
}
fn set_level(lint: lint, level: level) {
if level == ignore {
if level == allow {
self.curr.remove(lint as uint);
} else {
self.curr.insert(lint as uint, level);
@ -199,82 +208,88 @@ impl methods for ctxt {
}
/**
* Merge the warnings specified by any `warn(...)` attributes into the
* Merge the lints specified by any lint attributes into the
* current lint context, call the provided function, then reset the
* warnings in effect to their previous state.
* lints in effect to their previous state.
*/
fn with_warn_attrs(attrs: ~[ast::attribute], f: fn(ctxt)) {
fn with_lint_attrs(attrs: ~[ast::attribute], f: fn(ctxt)) {
let mut new_ctxt = self;
let mut triples = ~[];
let metas = attr::attr_metas(
attr::find_attrs_by_name(attrs, ~"warn"));
for metas.each |meta| {
alt meta.node {
ast::meta_list(_, metas) {
for metas.each |meta| {
alt meta.node {
ast::meta_word(lintname) {
alt lookup_lint(self.dict, *lintname) {
(name, none) {
self.span_lint(
self.get_level(unrecognized_warning),
meta.span,
#fmt("unknown warning: `%s`", name));
for [allow, warn, deny, forbid].each |level| {
let level_name = level_to_str(level);
let metas =
attr::attr_metas(attr::find_attrs_by_name(attrs,
level_name));
for metas.each |meta| {
alt meta.node {
ast::meta_list(_, metas) {
for metas.each |meta| {
alt meta.node {
ast::meta_word(lintname) {
vec::push(triples, (meta, level, lintname));
}
(_, some((lint, new_level))) {
// we do multiple unneeded copies of the map
// if many attributes are set, but this shouldn't
// actually be a problem...
new_ctxt =
ctxt_({is_default: false,
curr: clone_lint_modes(new_ctxt.curr)
with *new_ctxt});
new_ctxt.set_level(lint, new_level);
_ {
self.sess.span_err(
meta.span,
~"malformed lint attribute");
}
}
}
_ {
self.sess.span_err(
meta.span,
~"malformed warning attribute");
}
}
}
_ {
self.sess.span_err(meta.span,
~"malformed lint attribute");
}
}
}
_ {
self.sess.span_err(meta.span,
~"malformed warning attribute");
}
}
}
for triples.each |pair| {
let (meta, level, lintname) = pair;
alt self.dict.find(*lintname) {
none {
self.span_lint(
new_ctxt.get_level(unrecognized_lint),
meta.span,
#fmt("unknown `%s` attribute: `%s`",
level_to_str(level), *lintname));
}
some(lint) {
if new_ctxt.get_level(lint.lint) == forbid &&
level != forbid {
self.span_lint(
forbid,
meta.span,
#fmt("%s(%s) overruled by outer forbid(%s)",
level_to_str(level),
*lintname, *lintname));
}
// we do multiple unneeded copies of the
// map if many attributes are set, but
// this shouldn't actually be a problem...
let c = clone_lint_modes(new_ctxt.curr);
new_ctxt =
ctxt_({is_default: false,
curr: c,
with *new_ctxt});
new_ctxt.set_level(lint.lint, level);
}
}
}
f(new_ctxt);
}
}
fn lookup_lint(dict: lint_dict, s: ~str)
-> (~str, option<(lint, level)>) {
let s = str::replace(s, ~"-", ~"_");
let (name, level) = if s.starts_with(~"no_") {
(s.substr(3u, s.len() - 3u), ignore)
} else if s.starts_with(~"err_") {
(s.substr(4u, s.len() - 4u), error)
} else {
(s, warn)
};
(name,
alt dict.find(name) {
none { none }
some(spec) { some((spec.lint, level)) }
})
}
fn build_settings_item(i: @ast::item, &&cx: ctxt, v: visit::vt<ctxt>) {
do cx.with_warn_attrs(i.attrs) |cx| {
do cx.with_lint_attrs(i.attrs) |cx| {
if !cx.is_default {
cx.sess.warning_settings.settings_map.insert(i.id, cx.curr);
cx.sess.lint_settings.settings_map.insert(i.id, cx.curr);
}
visit::visit_item(i, cx, v);
}
@ -296,10 +311,10 @@ fn build_settings_crate(sess: session::session, crate: @ast::crate) {
cx.set_level(lint, level);
}
do cx.with_warn_attrs(crate.node.attrs) |cx| {
do cx.with_lint_attrs(crate.node.attrs) |cx| {
// Copy out the default settings
for cx.curr.each |k, v| {
sess.warning_settings.default_settings.insert(k, v);
sess.lint_settings.default_settings.insert(k, v);
}
let cx = ctxt_({is_default: true with *cx});

View File

@ -3,7 +3,7 @@ import metadata::csearch::{each_path, get_impls_for_mod};
import metadata::csearch::{get_method_names_if_trait, lookup_defs};
import metadata::cstore::find_use_stmt_cnum;
import metadata::decoder::{def_like, dl_def, dl_field, dl_impl};
import middle::lint::{error, ignore, level, unused_imports, warn};
import middle::lint::{deny, allow, forbid, level, unused_imports, warn};
import syntax::ast::{_mod, arm, blk, bound_const, bound_copy, bound_trait};
import syntax::ast::{bound_owned};
import syntax::ast::{bound_send, capture_clause, class_ctor, class_dtor};
@ -447,7 +447,7 @@ fn unused_import_lint_level(session: session) -> level {
ret lint_level;
}
}
ret ignore;
ret allow;
}
// Records the definitions (at most one for each namespace) that a name is
@ -4363,7 +4363,7 @@ class Resolver {
//
fn check_for_unused_imports_if_necessary() {
if self.unused_import_lint_level == ignore {
if self.unused_import_lint_level == allow {
ret;
}
@ -4418,14 +4418,14 @@ class Resolver {
self.session.span_warn(import_resolution.span,
~"unused import");
}
error {
self.session.span_err(import_resolution.span,
~"unused import");
deny | forbid {
self.session.span_err(import_resolution.span,
~"unused import");
}
ignore {
self.session.span_bug(import_resolution.span,
~"shouldn't be here if lint \
pass is ignored");
allow {
self.session.span_bug(import_resolution.span,
~"shouldn't be here if lint \
is allowed");
}
}
}

View File

@ -11,8 +11,8 @@ import syntax::codemap::span;
import metadata::csearch;
import util::ppaux::region_to_str;
import util::ppaux::vstore_to_str;
import middle::lint::{get_warning_level, vecs_not_implicitly_copyable,
ignore};
import middle::lint;
import middle::lint::{get_lint_level, allow};
import syntax::ast::*;
import syntax::print::pprust::*;
import util::ppaux::{ty_to_str, tys_to_str};
@ -560,8 +560,8 @@ fn mk_ctxt(s: session::session,
option::map_default(k.o_def_id, 0u, ast_util::hash_def)
}, |&&a, &&b| a == b);
let vecs_implicitly_copyable =
get_warning_level(s.warning_settings.default_settings,
vecs_not_implicitly_copyable) == ignore;
get_lint_level(s.lint_settings.default_settings,
lint::vecs_implicitly_copyable) == allow;
@{diag: s.diagnostic(),
interner: interner,
mut next_id: 0u,

View File

@ -11,7 +11,12 @@
#[no_core];
// NB: transitional for stage0:
#[allow(unrecognized_lint)];
#[warn(no_unrecognized_warning)];
#[warn(no_vecs_not_implicitly_copyable)];
// The new version:
#[allow(vecs_implicitly_copyable)];
use core(vers = "0.3");
use std(vers = "0.3");

View File

@ -11,7 +11,13 @@
#[no_core];
// NB: transitional for stage0:
#[allow(unrecognized_lint)];
#[warn(no_unrecognized_warning)];
#[warn(no_vecs_not_implicitly_copyable)];
// The new version:
#[allow(vecs_implicitly_copyable,
non_implicitly_copyable_typarams)];
use core(vers = "0.3");
use std(vers = "0.3");

View File

@ -1,4 +1,4 @@
// compile-flags: -W err-while-true
// compile-flags: -D while-true
fn main() {
let mut i = 0;
while true { //~ ERROR denote infinite loops with loop

View File

@ -1,5 +1,5 @@
// error-pattern:found rust type
#[warn(err_ctypes)];
#[deny(ctypes)];
#[nolink]
extern mod libc {

View File

@ -1,4 +1,4 @@
// compile-flags:-W err-ctypes
// compile-flags:-D ctypes
// error-pattern:found rust type
#[nolink]
extern mod libc {

View File

@ -1,4 +1,4 @@
// compile-flags: -W err-path-statement
// compile-flags: -D path-statement
fn main() {
let x = 10;

View File

@ -1,6 +1,6 @@
// compile-flags:-W err-ctypes
// compile-flags:-D ctypes
#[warn(no_ctypes)];
#[allow(ctypes)];
#[nolink]
extern mod libc {