rust/src/rustc/middle/lint.rs

726 lines
23 KiB
Rust
Raw Normal View History

2012-09-04 13:54:36 -05:00
use driver::session;
use driver::session::session;
use middle::ty;
use syntax::{ast, ast_util, visit};
use syntax::attr;
use syntax::codemap::span;
use std::map::{map,hashmap,int_hash,hash_from_strs};
2012-09-04 18:04:10 -05:00
use std::smallintmap::{map,SmallIntMap};
2012-09-04 13:54:36 -05:00
use io::WriterUtil;
use util::ppaux::{ty_to_str};
use middle::pat_util::{pat_bindings};
use syntax::ast_util::{path_to_ident};
use syntax::print::pprust::{expr_to_str, mode_to_str, pat_to_str};
export lint, ctypes, unused_imports, while_true, path_statement, old_vecs;
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_
* want to enforce, but might reasonably want to permit as well, on a
* module-by-module basis. They contrast with static constraints enforced by
* other phases of the compiler, which are generally required to hold in order
* 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 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 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 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.
*/
enum lint {
2012-01-19 19:56:05 -06:00
ctypes,
unused_imports,
while_true,
path_statement,
implicit_copies,
unrecognized_lint,
non_implicitly_copyable_typarams,
vecs_implicitly_copyable,
deprecated_mode,
deprecated_pattern,
non_camel_case_types,
structural_records,
managed_heap_memory,
owned_heap_memory,
heap_memory,
// FIXME(#3266)--make liveness warnings lintable
// unused_variable,
// dead_assignment
}
2012-08-27 18:26:35 -05:00
impl lint : cmp::Eq {
pure fn eq(&&other: lint) -> bool {
(self as uint) == (other as uint)
}
pure fn ne(&&other: lint) -> bool { !self.eq(other) }
2012-08-27 18:26:35 -05:00
}
fn level_to_str(lv: level) -> ~str {
2012-08-06 14:34:08 -05:00
match lv {
2012-08-03 21:59:04 -05:00
allow => ~"allow",
warn => ~"warn",
deny => ~"deny",
forbid => ~"forbid"
}
}
enum level {
allow, warn, deny, forbid
}
2012-08-27 18:26:35 -05:00
impl level : cmp::Eq {
pure fn eq(&&other: level) -> bool {
(self as uint) == (other as uint)
}
pure fn ne(&&other: level) -> bool { !self.eq(other) }
2012-08-27 18:26:35 -05:00
}
type lint_spec = @{lint: lint,
desc: ~str,
default: level};
type lint_dict = hashmap<~str,lint_spec>;
/*
Pass names should not contain a '-', as the compiler normalizes
'-' to '_' in command-line flags
*/
fn get_lint_dict() -> lint_dict {
let v = ~[
(~"ctypes",
@{lint: ctypes,
desc: ~"proper use of core::libc types in foreign modules",
default: warn}),
(~"unused_imports",
@{lint: unused_imports,
desc: ~"imports that are never used",
default: allow}),
(~"while_true",
@{lint: while_true,
desc: ~"suggest using loop { } instead of while(true) { }",
default: warn}),
(~"path_statement",
@{lint: path_statement,
desc: ~"path statements with no effect",
default: warn}),
(~"unrecognized_lint",
@{lint: unrecognized_lint,
desc: ~"unrecognized lint attribute",
default: warn}),
(~"non_implicitly_copyable_typarams",
@{lint: non_implicitly_copyable_typarams,
desc: ~"passing non implicitly copyable types as copy type params",
default: warn}),
(~"vecs_implicitly_copyable",
@{lint: vecs_implicitly_copyable,
desc: ~"make vecs and strs not implicitly copyable \
(only checked at top level)",
default: warn}),
(~"implicit_copies",
@{lint: implicit_copies,
desc: ~"implicit copies of non implicitly copyable data",
default: warn}),
(~"deprecated_mode",
@{lint: deprecated_mode,
desc: ~"warn about deprecated uses of modes",
default: allow}),
(~"deprecated_pattern",
@{lint: deprecated_pattern,
desc: ~"warn about deprecated uses of pattern bindings",
default: allow}),
(~"non_camel_case_types",
@{lint: non_camel_case_types,
desc: ~"types, variants and traits must have camel case names",
default: allow}),
(~"managed_heap_memory",
@{lint: managed_heap_memory,
desc: ~"use of managed (@ type) heap memory",
default: allow}),
(~"owned_heap_memory",
@{lint: owned_heap_memory,
desc: ~"use of owned (~ type) heap memory",
default: allow}),
(~"heap_memory",
@{lint: heap_memory,
desc: ~"use of any (~ type or @ type) heap memory",
default: allow}),
(~"structural_records",
@{lint: structural_records,
desc: ~"use of any structural records",
default: allow}),
/* FIXME(#3266)--make liveness warnings lintable
(~"unused_variable",
@{lint: unused_variable,
desc: ~"detect variables which are not used in any way",
default: warn}),
(~"dead_assignment",
@{lint: dead_assignment,
desc: ~"detect assignments that will never be read",
default: warn}),
*/
];
hash_from_strs(v)
}
// This is a highly not-optimal set of data structure decisions.
2012-09-04 18:04:10 -05:00
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 lint settings
// to their settings; default_settings contains the settings for everything
// not in the map.
type lint_settings = {
default_settings: lint_modes,
settings_map: lint_mode_map
};
fn mk_lint_settings() -> lint_settings {
{default_settings: std::smallintmap::mk(),
settings_map: int_hash()}
}
fn get_lint_level(modes: lint_modes, lint: lint) -> level {
2012-08-06 14:34:08 -05:00
match modes.find(lint as uint) {
2012-08-20 14:23:37 -05:00
Some(c) => c,
None => allow
}
}
fn get_lint_settings_level(settings: lint_settings,
lint_mode: lint,
_expr_id: ast::node_id,
item_id: ast::node_id) -> level {
2012-08-06 14:34:08 -05:00
match settings.settings_map.find(item_id) {
2012-08-20 14:23:37 -05:00
Some(modes) => get_lint_level(modes, lint_mode),
None => get_lint_level(settings.default_settings, lint_mode)
}
}
// This is kind of unfortunate. It should be somewhere else, or we should use
// a persistent data structure...
fn clone_lint_modes(modes: lint_modes) -> lint_modes {
2012-09-04 18:04:10 -05:00
std::smallintmap::SmallIntMap_(@{v: copy modes.v})
}
type ctxt_ = {dict: lint_dict,
curr: lint_modes,
is_default: bool,
sess: session};
enum ctxt {
ctxt_(ctxt_)
}
2012-08-07 20:10:06 -05:00
impl ctxt {
fn get_level(lint: lint) -> level {
get_lint_level(self.curr, lint)
}
fn set_level(lint: lint, level: level) {
if level == allow {
self.curr.remove(lint as uint);
} else {
self.curr.insert(lint as uint, level);
}
}
fn span_lint(level: level, span: span, msg: ~str) {
self.sess.span_lint_level(level, span, msg);
}
/**
* Merge the lints specified by any lint attributes into the
* current lint context, call the provided function, then reset the
* lints in effect to their previous state.
*/
fn with_lint_attrs(attrs: ~[ast::attribute], f: fn(ctxt)) {
let mut new_ctxt = self;
let mut triples = ~[];
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| {
2012-08-06 14:34:08 -05:00
match meta.node {
2012-08-03 21:59:04 -05:00
ast::meta_list(_, metas) => {
for metas.each |meta| {
2012-08-06 14:34:08 -05:00
match meta.node {
2012-08-03 21:59:04 -05:00
ast::meta_word(lintname) => {
vec::push(triples, (meta, level, lintname));
}
2012-08-03 21:59:04 -05:00
_ => {
self.sess.span_err(
meta.span,
~"malformed lint attribute");
}
}
}
}
2012-08-03 21:59:04 -05:00
_ => {
self.sess.span_err(meta.span,
~"malformed lint attribute");
}
}
}
}
for triples.each |pair| {
let (meta, level, lintname) = pair;
2012-07-18 18:18:02 -05:00
match self.dict.find(lintname) {
2012-08-20 14:23:37 -05:00
None => {
self.span_lint(
new_ctxt.get_level(unrecognized_lint),
meta.span,
2012-08-22 19:24:52 -05:00
fmt!("unknown `%s` attribute: `%s`",
level_to_str(level), lintname));
}
2012-08-20 14:23:37 -05:00
Some(lint) => {
if new_ctxt.get_level(lint.lint) == forbid &&
level != forbid {
self.span_lint(
forbid,
meta.span,
2012-08-22 19:24:52 -05:00
fmt!("%s(%s) overruled by outer forbid(%s)",
level_to_str(level),
2012-08-22 19:24:52 -05:00
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,
2012-09-04 15:29:32 -05:00
.. *new_ctxt});
new_ctxt.set_level(lint.lint, level);
}
}
}
f(new_ctxt);
}
}
fn build_settings_item(i: @ast::item, &&cx: ctxt, v: visit::vt<ctxt>) {
do cx.with_lint_attrs(i.attrs) |cx| {
if !cx.is_default {
cx.sess.lint_settings.settings_map.insert(i.id, cx.curr);
}
visit::visit_item(i, cx, v);
}
}
fn build_settings_crate(sess: session::session, crate: @ast::crate) {
let cx = ctxt_({dict: get_lint_dict(),
curr: std::smallintmap::mk(),
is_default: true,
sess: sess});
// Install defaults.
2012-06-30 18:19:07 -05:00
for cx.dict.each |_k, spec| { cx.set_level(spec.lint, spec.default); }
// Install command-line options, overriding defaults.
2012-06-30 18:19:07 -05:00
for sess.opts.lint_opts.each |pair| {
let (lint,level) = pair;
cx.set_level(lint, level);
}
do cx.with_lint_attrs(crate.node.attrs) |cx| {
// Copy out the default settings
2012-06-30 18:19:07 -05:00
for cx.curr.each |k, v| {
sess.lint_settings.default_settings.insert(k, v);
}
2012-09-04 15:29:32 -05:00
let cx = ctxt_({is_default: true,.. *cx});
let visit = visit::mk_vt(@{
2012-09-04 15:29:32 -05:00
visit_item: build_settings_item,
.. *visit::default_visitor()
});
visit::visit_crate(*crate, cx, visit);
}
sess.abort_if_errors();
}
fn check_item(i: @ast::item, cx: ty::ctxt) {
check_item_ctypes(cx, i);
check_item_while_true(cx, i);
check_item_path_statement(cx, i);
check_item_non_camel_case_types(cx, i);
check_item_heap(cx, i);
check_item_structural_records(cx, i);
}
// Take a visitor, and modify it so that it will not proceed past subitems.
// This is used to make the simple visitors used for the lint passes
// not traverse into subitems, since that is handled by the outer
// lint visitor.
fn item_stopping_visitor<E>(v: visit::vt<E>) -> visit::vt<E> {
2012-09-04 15:29:32 -05:00
visit::mk_vt(@{visit_item: |_i, _e, _v| { },.. **v})
}
fn check_item_while_true(cx: ty::ctxt, it: @ast::item) {
let visit = item_stopping_visitor(visit::mk_simple_visitor(@{
visit_expr: fn@(e: @ast::expr) {
2012-08-06 14:34:08 -05:00
match e.node {
2012-08-03 21:59:04 -05:00
ast::expr_while(cond, _) => {
2012-08-06 14:34:08 -05:00
match cond.node {
2012-08-03 21:59:04 -05:00
ast::expr_lit(@{node: ast::lit_bool(true),_}) => {
cx.sess.span_lint(
while_true, e.id, it.id,
e.span,
~"denote infinite loops with loop { ... }");
}
2012-08-03 21:59:04 -05:00
_ => ()
}
}
2012-08-03 21:59:04 -05:00
_ => ()
}
2012-09-04 15:29:32 -05:00
},
.. *visit::default_simple_visitor()
}));
visit::visit_item(it, (), visit);
}
fn check_item_structural_records(cx: ty::ctxt, it: @ast::item) {
let visit = item_stopping_visitor(visit::mk_simple_visitor(@{
visit_expr: fn@(e: @ast::expr) {
match e.node {
ast::expr_rec(*) =>
cx.sess.span_lint(
structural_records, e.id, it.id,
e.span,
~"structural records are deprecated"),
_ => ()
}
},
.. *visit::default_simple_visitor()
}));
visit::visit_item(it, (), visit);
}
fn check_item_ctypes(cx: ty::ctxt, it: @ast::item) {
fn check_foreign_fn(cx: ty::ctxt, fn_id: ast::node_id,
decl: ast::fn_decl) {
2012-06-30 18:19:07 -05:00
let tys = vec::map(decl.inputs, |a| a.ty );
for vec::each(vec::append_one(tys, decl.output)) |ty| {
2012-08-06 14:34:08 -05:00
match ty.node {
2012-08-03 21:59:04 -05:00
ast::ty_path(_, id) => {
2012-08-06 14:34:08 -05:00
match cx.def_map.get(id) {
2012-08-03 21:59:04 -05:00
ast::def_prim_ty(ast::ty_int(ast::ty_i)) => {
cx.sess.span_lint(
ctypes, id, fn_id,
ty.span,
~"found rust type `int` in foreign module, while \
libc::c_int or libc::c_long should be used");
}
2012-08-03 21:59:04 -05:00
ast::def_prim_ty(ast::ty_uint(ast::ty_u)) => {
cx.sess.span_lint(
ctypes, id, fn_id,
ty.span,
~"found rust type `uint` in foreign module, while \
libc::c_uint or libc::c_ulong should be used");
}
2012-08-03 21:59:04 -05:00
_ => ()
}
}
2012-08-03 21:59:04 -05:00
_ => ()
}
}
}
2012-08-06 14:34:08 -05:00
match it.node {
ast::item_foreign_mod(nmod) if attr::foreign_abi(it.attrs) !=
2012-08-14 18:54:13 -05:00
either::Right(ast::foreign_abi_rust_intrinsic) => {
2012-06-30 18:19:07 -05:00
for nmod.items.each |ni| {
2012-08-06 14:34:08 -05:00
match ni.node {
2012-08-26 14:12:05 -05:00
ast::foreign_item_fn(decl, _, _) => {
check_foreign_fn(cx, it.id, decl);
}
ast::foreign_item_const(*) => {} // XXX: Not implemented.
}
}
}
2012-08-03 21:59:04 -05:00
_ => {/* nothing to do */ }
}
}
fn check_item_heap(cx: ty::ctxt, it: @ast::item) {
fn check_type_for_lint(cx: ty::ctxt, lint: lint,
node: ast::node_id,
item: ast::node_id,
span: span, ty: ty::t) {
if get_lint_settings_level(cx.sess.lint_settings,
lint, node, item) != allow {
let mut n_box = 0;
let mut n_uniq = 0;
ty::fold_ty(cx, ty, |t| {
match ty::get(t).struct {
ty::ty_box(_) => n_box += 1,
ty::ty_uniq(_) => n_uniq += 1,
_ => ()
};
t
});
if (n_uniq > 0 && lint != managed_heap_memory) {
let s = ty_to_str(cx, ty);
let m = ~"type uses owned (~ type) pointers: " + s;
cx.sess.span_lint(lint, node, item, span, m);
}
if (n_box > 0 && lint != owned_heap_memory) {
let s = ty_to_str(cx, ty);
let m = ~"type uses managed (@ type) pointers: " + s;
cx.sess.span_lint(lint, node, item, span, m);
}
}
}
fn check_type(cx: ty::ctxt,
node: ast::node_id,
item: ast::node_id,
span: span, ty: ty::t) {
for [managed_heap_memory,
owned_heap_memory,
heap_memory].each |lint| {
check_type_for_lint(cx, lint, node, item, span, ty);
}
}
match it.node {
ast::item_fn(*) |
ast::item_ty(*) |
ast::item_enum(*) |
ast::item_class(*) |
ast::item_trait(*) => check_type(cx, it.id, it.id, it.span,
ty::node_id_to_type(cx, it.id)),
_ => ()
}
let visit = item_stopping_visitor(visit::mk_simple_visitor(@{
visit_expr: fn@(e: @ast::expr) {
let ty = ty::expr_ty(cx, e);
check_type(cx, e.id, it.id, e.span, ty);
2012-09-04 15:29:32 -05:00
},
.. *visit::default_simple_visitor()
}));
visit::visit_item(it, (), visit);
}
fn check_item_path_statement(cx: ty::ctxt, it: @ast::item) {
let visit = item_stopping_visitor(visit::mk_simple_visitor(@{
visit_stmt: fn@(s: @ast::stmt) {
2012-08-06 14:34:08 -05:00
match s.node {
ast::stmt_semi(@{id: id,
callee_id: _,
2012-08-26 14:12:05 -05:00
node: ast::expr_path(_),
2012-08-03 21:59:04 -05:00
span: _}, _) => {
cx.sess.span_lint(
path_statement, id, it.id,
s.span,
~"path statement with no effect");
}
2012-08-03 21:59:04 -05:00
_ => ()
}
2012-09-04 15:29:32 -05:00
},
.. *visit::default_simple_visitor()
}));
visit::visit_item(it, (), visit);
}
fn check_item_non_camel_case_types(cx: ty::ctxt, it: @ast::item) {
2012-07-18 18:18:02 -05:00
fn is_camel_case(cx: ty::ctxt, ident: ast::ident) -> bool {
let ident = cx.sess.str_of(ident);
assert ident.is_not_empty();
2012-07-18 18:18:02 -05:00
let ident = ident_without_trailing_underscores(ident);
let ident = ident_without_leading_underscores(ident);
char::is_uppercase(str::char_at(ident, 0)) &&
!ident.contains_char('_')
}
fn ident_without_trailing_underscores(ident: ~str) -> ~str {
match str::rfind(ident, |c| c != '_') {
2012-08-20 14:23:37 -05:00
Some(idx) => (ident).slice(0, idx + 1),
None => { ident } // all underscores
}
}
fn ident_without_leading_underscores(ident: ~str) -> ~str {
match str::find(ident, |c| c != '_') {
2012-08-20 14:23:37 -05:00
Some(idx) => ident.slice(idx, ident.len()),
None => {
// all underscores
ident
}
}
}
fn check_case(cx: ty::ctxt, ident: ast::ident,
expr_id: ast::node_id, item_id: ast::node_id,
span: span) {
2012-07-18 18:18:02 -05:00
if !is_camel_case(cx, ident) {
cx.sess.span_lint(
non_camel_case_types, expr_id, item_id, span,
~"type, variant, or trait must be camel case");
}
}
2012-08-06 14:34:08 -05:00
match it.node {
ast::item_ty(*) | ast::item_class(*) |
ast::item_trait(*) => {
check_case(cx, it.ident, it.id, it.id, it.span)
}
ast::item_enum(enum_definition, _) => {
check_case(cx, it.ident, it.id, it.id, it.span);
for enum_definition.variants.each |variant| {
check_case(cx, variant.node.name,
variant.node.id, it.id, variant.span);
}
}
2012-08-03 21:59:04 -05:00
_ => ()
}
}
fn check_pat(tcx: ty::ctxt, pat: @ast::pat) {
2012-08-22 19:24:52 -05:00
debug!("lint check_pat pat=%s", pat_to_str(pat, tcx.sess.intr()));
do pat_bindings(tcx.def_map, pat) |binding_mode, id, span, path| {
match binding_mode {
ast::bind_by_ref(_) | ast::bind_by_value | ast::bind_by_move => {}
ast::bind_by_implicit_ref => {
let pat_ty = ty::node_id_to_type(tcx, id);
let kind = ty::type_kind(tcx, pat_ty);
if !ty::kind_is_safe_for_default_mode(kind) {
tcx.sess.span_lint(
deprecated_pattern, id, id,
span,
2012-08-22 19:24:52 -05:00
fmt!("binding `%s` should use ref or copy mode",
tcx.sess.str_of(path_to_ident(path))));
}
}
}
}
}
fn check_fn(tcx: ty::ctxt, fk: visit::fn_kind, decl: ast::fn_decl,
_body: ast::blk, span: span, id: ast::node_id) {
2012-08-22 19:24:52 -05:00
debug!("lint check_fn fk=%? id=%?", fk, id);
// don't complain about blocks, since they tend to get their modes
// specified from the outside
2012-08-06 14:34:08 -05:00
match fk {
2012-08-01 19:30:05 -05:00
visit::fk_fn_block(*) => { return; }
_ => {}
}
let fn_ty = ty::node_id_to_type(tcx, id);
2012-08-21 19:22:45 -05:00
match ty::get(fn_ty).struct {
2012-08-03 21:59:04 -05:00
ty::ty_fn(fn_ty) => {
let mut counter = 0;
2012-09-07 09:37:19 -05:00
do vec::iter2(fn_ty.sig.inputs, decl.inputs) |arg_ty, arg_ast| {
counter += 1;
2012-08-22 19:24:52 -05:00
debug!("arg %d, ty=%s, mode=%s",
counter,
ty_to_str(tcx, arg_ty.ty),
2012-08-22 19:24:52 -05:00
mode_to_str(arg_ast.mode));
2012-08-06 14:34:08 -05:00
match arg_ast.mode {
ast::expl(ast::by_copy) => {
/* always allow by-copy */
}
ast::expl(_) => {
tcx.sess.span_lint(
deprecated_mode, id, id,
span,
2012-08-22 19:24:52 -05:00
fmt!("argument %d uses an explicit mode", counter));
}
2012-08-03 21:59:04 -05:00
ast::infer(_) => {
let kind = ty::type_kind(tcx, arg_ty.ty);
if !ty::kind_is_safe_for_default_mode(kind) {
tcx.sess.span_lint(
deprecated_mode, id, id,
span,
2012-08-22 19:24:52 -05:00
fmt!("argument %d uses the default mode \
but shouldn't",
2012-08-22 19:24:52 -05:00
counter));
}
}
}
}
}
2012-08-21 19:22:45 -05:00
_ => tcx.sess.impossible_case(span, ~"check_fn: function has \
non-fn type")
}
}
fn check_crate(tcx: ty::ctxt, crate: @ast::crate) {
let v = visit::mk_simple_visitor(@{
visit_item: |it|
check_item(it, tcx),
visit_fn: |fk, decl, body, span, id|
check_fn(tcx, fk, decl, body, span, id),
visit_pat: |pat|
check_pat(tcx, pat),
2012-09-04 15:29:32 -05:00
.. *visit::default_simple_visitor()
});
visit::visit_crate(*crate, (), v);
tcx.sess.abort_if_errors();
}
//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//