Merge pull request #1083 from Manishearth/allow-large-arrays
Don't warn when boxing large arrays r? @mcarton
This commit is contained in:
commit
aa75fda4c4
4 changed files with 49 additions and 6 deletions
|
@ -1,17 +1,21 @@
|
||||||
use rustc::hir::*;
|
use rustc::hir::*;
|
||||||
use rustc::hir::intravisit as visit;
|
use rustc::hir::intravisit as visit;
|
||||||
use rustc::hir::map::Node::{NodeExpr, NodeStmt};
|
use rustc::hir::map::Node::{NodeExpr, NodeStmt};
|
||||||
|
use rustc::infer::InferCtxt;
|
||||||
use rustc::lint::*;
|
use rustc::lint::*;
|
||||||
use rustc::middle::expr_use_visitor::*;
|
use rustc::middle::expr_use_visitor::*;
|
||||||
use rustc::middle::mem_categorization::{cmt, Categorization};
|
use rustc::middle::mem_categorization::{cmt, Categorization};
|
||||||
use rustc::ty::adjustment::AutoAdjustment;
|
use rustc::ty::adjustment::AutoAdjustment;
|
||||||
use rustc::ty;
|
use rustc::ty;
|
||||||
|
use rustc::ty::layout::TargetDataLayout;
|
||||||
use rustc::util::nodemap::NodeSet;
|
use rustc::util::nodemap::NodeSet;
|
||||||
use syntax::ast::NodeId;
|
use syntax::ast::NodeId;
|
||||||
use syntax::codemap::Span;
|
use syntax::codemap::Span;
|
||||||
use utils::span_lint;
|
use utils::span_lint;
|
||||||
|
|
||||||
pub struct Pass;
|
pub struct Pass {
|
||||||
|
pub too_large_for_stack: u64,
|
||||||
|
}
|
||||||
|
|
||||||
/// **What it does:** This lint checks for usage of `Box<T>` where an unboxed `T` would work fine.
|
/// **What it does:** This lint checks for usage of `Box<T>` where an unboxed `T` would work fine.
|
||||||
///
|
///
|
||||||
|
@ -39,9 +43,12 @@ fn is_non_trait_box(ty: ty::Ty) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct EscapeDelegate<'a, 'tcx: 'a> {
|
struct EscapeDelegate<'a, 'tcx: 'a+'gcx, 'gcx: 'a> {
|
||||||
tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
|
tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
|
||||||
set: NodeSet,
|
set: NodeSet,
|
||||||
|
infcx: &'a InferCtxt<'a, 'gcx, 'gcx>,
|
||||||
|
target: TargetDataLayout,
|
||||||
|
too_large_for_stack: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LintPass for Pass {
|
impl LintPass for Pass {
|
||||||
|
@ -55,9 +62,15 @@ impl LateLintPass for Pass {
|
||||||
let param_env = ty::ParameterEnvironment::for_item(cx.tcx, id);
|
let param_env = ty::ParameterEnvironment::for_item(cx.tcx, id);
|
||||||
|
|
||||||
let infcx = cx.tcx.borrowck_fake_infer_ctxt(param_env);
|
let infcx = cx.tcx.borrowck_fake_infer_ctxt(param_env);
|
||||||
|
|
||||||
|
// we store the infcx because it is expensive to recreate
|
||||||
|
// the context each time.
|
||||||
let mut v = EscapeDelegate {
|
let mut v = EscapeDelegate {
|
||||||
tcx: cx.tcx,
|
tcx: cx.tcx,
|
||||||
set: NodeSet(),
|
set: NodeSet(),
|
||||||
|
infcx: &infcx,
|
||||||
|
target: TargetDataLayout::parse(cx.sess()),
|
||||||
|
too_large_for_stack: self.too_large_for_stack,
|
||||||
};
|
};
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -74,7 +87,7 @@ impl LateLintPass for Pass {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'tcx: 'a> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
|
impl<'a, 'tcx: 'a+'gcx, 'gcx: 'a> Delegate<'tcx> for EscapeDelegate<'a, 'tcx, 'gcx> {
|
||||||
fn consume(&mut self, _: NodeId, _: Span, cmt: cmt<'tcx>, mode: ConsumeMode) {
|
fn consume(&mut self, _: NodeId, _: Span, cmt: cmt<'tcx>, mode: ConsumeMode) {
|
||||||
if let Categorization::Local(lid) = cmt.cat {
|
if let Categorization::Local(lid) = cmt.cat {
|
||||||
if self.set.contains(&lid) {
|
if self.set.contains(&lid) {
|
||||||
|
@ -93,7 +106,7 @@ impl<'a, 'tcx: 'a> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
|
||||||
if let Some(NodeExpr(..)) = map.find(map.get_parent_node(consume_pat.id)) {
|
if let Some(NodeExpr(..)) = map.find(map.get_parent_node(consume_pat.id)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if is_non_trait_box(cmt.ty) {
|
if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
|
||||||
self.set.insert(consume_pat.id);
|
self.set.insert(consume_pat.id);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
@ -104,7 +117,7 @@ impl<'a, 'tcx: 'a> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
|
||||||
if let DeclLocal(ref loc) = decl.node {
|
if let DeclLocal(ref loc) = decl.node {
|
||||||
if let Some(ref ex) = loc.init {
|
if let Some(ref ex) = loc.init {
|
||||||
if let ExprBox(..) = ex.node {
|
if let ExprBox(..) = ex.node {
|
||||||
if is_non_trait_box(cmt.ty) {
|
if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
|
||||||
// let x = box (...)
|
// let x = box (...)
|
||||||
self.set.insert(consume_pat.id);
|
self.set.insert(consume_pat.id);
|
||||||
}
|
}
|
||||||
|
@ -170,3 +183,21 @@ impl<'a, 'tcx: 'a> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
|
||||||
fn decl_without_init(&mut self, _: NodeId, _: Span) {}
|
fn decl_without_init(&mut self, _: NodeId, _: Span) {}
|
||||||
fn mutate(&mut self, _: NodeId, _: Span, _: cmt<'tcx>, _: MutateMode) {}
|
fn mutate(&mut self, _: NodeId, _: Span, _: cmt<'tcx>, _: MutateMode) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a, 'tcx: 'a+'gcx, 'gcx: 'a> EscapeDelegate<'a, 'tcx, 'gcx> {
|
||||||
|
fn is_large_box(&self, ty: ty::Ty<'gcx>) -> bool {
|
||||||
|
// Large types need to be boxed to avoid stack
|
||||||
|
// overflows.
|
||||||
|
match ty.sty {
|
||||||
|
ty::TyBox(ref inner) => {
|
||||||
|
if let Ok(layout) = inner.layout(self.infcx) {
|
||||||
|
let size = layout.size(&self.target);
|
||||||
|
size.bytes() > self.too_large_for_stack
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#![feature(slice_patterns)]
|
#![feature(slice_patterns)]
|
||||||
#![feature(stmt_expr_attributes)]
|
#![feature(stmt_expr_attributes)]
|
||||||
#![feature(type_macros)]
|
#![feature(type_macros)]
|
||||||
|
#![feature(iter_arith)]
|
||||||
|
|
||||||
#![allow(indexing_slicing, shadow_reuse, unknown_lints)]
|
#![allow(indexing_slicing, shadow_reuse, unknown_lints)]
|
||||||
|
|
||||||
|
@ -219,7 +220,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
|
||||||
reg.register_late_lint_pass(box temporary_assignment::Pass);
|
reg.register_late_lint_pass(box temporary_assignment::Pass);
|
||||||
reg.register_late_lint_pass(box transmute::Transmute);
|
reg.register_late_lint_pass(box transmute::Transmute);
|
||||||
reg.register_late_lint_pass(box cyclomatic_complexity::CyclomaticComplexity::new(conf.cyclomatic_complexity_threshold));
|
reg.register_late_lint_pass(box cyclomatic_complexity::CyclomaticComplexity::new(conf.cyclomatic_complexity_threshold));
|
||||||
reg.register_late_lint_pass(box escape::Pass);
|
reg.register_late_lint_pass(box escape::Pass{too_large_for_stack: conf.too_large_for_stack});
|
||||||
reg.register_early_lint_pass(box misc_early::MiscEarly);
|
reg.register_early_lint_pass(box misc_early::MiscEarly);
|
||||||
reg.register_late_lint_pass(box misc::UsedUnderscoreBinding);
|
reg.register_late_lint_pass(box misc::UsedUnderscoreBinding);
|
||||||
reg.register_late_lint_pass(box array_indexing::ArrayIndexing);
|
reg.register_late_lint_pass(box array_indexing::ArrayIndexing);
|
||||||
|
|
|
@ -158,6 +158,8 @@ define_Conf! {
|
||||||
("type-complexity-threshold", type_complexity_threshold, 250 => u64),
|
("type-complexity-threshold", type_complexity_threshold, 250 => u64),
|
||||||
/// Lint: MANY_SINGLE_CHAR_NAMES. The maximum number of single char bindings a scope may have
|
/// Lint: MANY_SINGLE_CHAR_NAMES. The maximum number of single char bindings a scope may have
|
||||||
("single-char-binding-names-threshold", max_single_char_names, 5 => u64),
|
("single-char-binding-names-threshold", max_single_char_names, 5 => u64),
|
||||||
|
/// Lint: BOXED_LOCAL. The maximum size of objects (in bytes) that will be linted. Larger objects are ok on the heap
|
||||||
|
("too-large-for-stack", too_large_for_stack, 200 => u64),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read the `toml` configuration file. The function will ignore “File not found” errors iif
|
/// Read the `toml` configuration file. The function will ignore “File not found” errors iif
|
||||||
|
|
|
@ -103,3 +103,12 @@ fn warn_match() {
|
||||||
ref y => ()
|
ref y => ()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn nowarn_large_array() {
|
||||||
|
// should not warn, is large array
|
||||||
|
// and should not be on stack
|
||||||
|
let x = box [1; 10000];
|
||||||
|
match &x { // not moved
|
||||||
|
ref y => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue