Skip to content

Commit

Permalink
Format components/script_plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
chansuke committed Sep 12, 2018
1 parent 29ba510 commit 3324b5b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 49 deletions.
2 changes: 0 additions & 2 deletions components/script_plugins/lib.rs
Expand Up @@ -13,8 +13,6 @@
//! - `#[dom_struct]` : Implies #[derive(JSTraceable, DenyPublicFields)]`, and `#[must_root]`.
//! Use this for structs that correspond to a DOM type



#![deny(unsafe_code)]
#![feature(macro_at_most_once_rep)]
#![feature(plugin)]
Expand Down
118 changes: 75 additions & 43 deletions components/script_plugins/unrooted_must_root.rs
Expand Up @@ -9,8 +9,11 @@ use rustc::ty;
use syntax::{ast, source_map, symbol::Ident};
use utils::{match_def_path, in_derive_expn};

declare_lint!(UNROOTED_MUST_ROOT, Deny,
"Warn and report usage of unrooted jsmanaged objects");
declare_lint!(
UNROOTED_MUST_ROOT,
Deny,
"Warn and report usage of unrooted jsmanaged objects"
);

/// Lint for ensuring safe usage of unrooted pointers
///
Expand Down Expand Up @@ -48,15 +51,24 @@ fn is_unrooted_ty(cx: &LateContext, ty: &ty::TyS, in_new_function: bool) -> bool
false
} else if cx.tcx.has_attr(did.did, "allow_unrooted_interior") {
false
} else if match_def_path(cx, did.did, &["core", "cell", "Ref"])
|| match_def_path(cx, did.did, &["core", "cell", "RefMut"])
|| match_def_path(cx, did.did, &["core", "slice", "Iter"])
|| match_def_path(cx, did.did, &["core", "slice", "IterMut"])
|| match_def_path(cx, did.did, &["std", "collections", "hash", "map", "Entry"])
|| match_def_path(cx, did.did, &["std", "collections", "hash", "map", "OccupiedEntry"])
|| match_def_path(cx, did.did, &["std", "collections", "hash", "map", "VacantEntry"])
|| match_def_path(cx, did.did, &["std", "collections", "hash", "map", "Iter"])
|| match_def_path(cx, did.did, &["std", "collections", "hash", "set", "Iter"]) {
} else if match_def_path(cx, did.did, &["core", "cell", "Ref"]) ||
match_def_path(cx, did.did, &["core", "cell", "RefMut"]) ||
match_def_path(cx, did.did, &["core", "slice", "Iter"]) ||
match_def_path(cx, did.did, &["core", "slice", "IterMut"]) ||
match_def_path(cx, did.did, &["std", "collections", "hash", "map", "Entry"]) ||
match_def_path(
cx,
did.did,
&["std", "collections", "hash", "map", "OccupiedEntry"],
) ||
match_def_path(
cx,
did.did,
&["std", "collections", "hash", "map", "VacantEntry"],
) ||
match_def_path(cx, did.did, &["std", "collections", "hash", "map", "Iter"]) ||
match_def_path(cx, did.did, &["std", "collections", "hash", "set", "Iter"])
{
// Structures which are semantically similar to an &ptr.
false
} else if did.is_box() && in_new_function {
Expand All @@ -66,10 +78,10 @@ fn is_unrooted_ty(cx: &LateContext, ty: &ty::TyS, in_new_function: bool) -> bool
true
}
},
ty::Ref(..) => false, // don't recurse down &ptrs
ty::Ref(..) => false, // don't recurse down &ptrs
ty::RawPtr(..) => false, // don't recurse down *ptrs
ty::FnDef(..) | ty::FnPtr(_) => false,
_ => true
_ => true,
}
});
ret
Expand All @@ -83,12 +95,14 @@ impl LintPass for UnrootedPass {

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnrootedPass {
/// All structs containing #[must_root] types must be #[must_root] themselves
fn check_struct_def(&mut self,
cx: &LateContext,
def: &hir::VariantData,
_n: ast::Name,
_gen: &hir::Generics,
id: ast::NodeId) {
fn check_struct_def(
&mut self,
cx: &LateContext,
def: &hir::VariantData,
_n: ast::Name,
_gen: &hir::Generics,
id: ast::NodeId,
) {
let item = match cx.tcx.hir.get(id) {
hir::Node::Item(item) => item,
_ => cx.tcx.hir.expect_item(cx.tcx.hir.get_parent(id)),
Expand All @@ -107,35 +121,45 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnrootedPass {
/// All enums containing #[must_root] types must be #[must_root] themselves
fn check_variant(&mut self, cx: &LateContext, var: &hir::Variant, _gen: &hir::Generics) {
let ref map = cx.tcx.hir;
if map.expect_item(map.get_parent(var.node.data.id())).attrs.iter().all(|a| !a.check_name("must_root")) {
if map
.expect_item(map.get_parent(var.node.data.id()))
.attrs
.iter()
.all(|a| !a.check_name("must_root"))
{
match var.node.data {
hir::VariantData::Tuple(ref fields, _) => {
for ref field in fields {
let def_id = cx.tcx.hir.local_def_id(field.id);
if is_unrooted_ty(cx, cx.tcx.type_of(def_id), false) {
cx.span_lint(UNROOTED_MUST_ROOT, field.ty.span,
"Type must be rooted, use #[must_root] on \
the enum definition to propagate")
cx.span_lint(
UNROOTED_MUST_ROOT,
field.ty.span,
"Type must be rooted, use #[must_root] on \
the enum definition to propagate",
)
}
}
}
_ => () // Struct variants already caught by check_struct_def
},
_ => (), // Struct variants already caught by check_struct_def
}
}
}
/// Function arguments that are #[must_root] types are not allowed
fn check_fn(&mut self,
cx: &LateContext<'a, 'tcx>,
kind: visit::FnKind,
decl: &'tcx hir::FnDecl,
body: &'tcx hir::Body,
span: source_map::Span,
id: ast::NodeId) {
fn check_fn(
&mut self,
cx: &LateContext<'a, 'tcx>,
kind: visit::FnKind,
decl: &'tcx hir::FnDecl,
body: &'tcx hir::Body,
span: source_map::Span,
id: ast::NodeId,
) {
let in_new_function = match kind {
visit::FnKind::ItemFn(n, _, _, _, _) |
visit::FnKind::Method(Ident { name: n, .. }, _, _, _) => {
&*n.as_str() == "new" || n.as_str().starts_with("new_")
}
},
visit::FnKind::Closure(_) => return,
};

Expand All @@ -151,7 +175,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnrootedPass {

if !in_new_function {
if is_unrooted_ty(cx, sig.output().skip_binder(), false) {
cx.span_lint(UNROOTED_MUST_ROOT, decl.output.span(), "Type must be rooted")
cx.span_lint(
UNROOTED_MUST_ROOT,
decl.output.span(),
"Type must be rooted",
)
}
}
}
Expand All @@ -176,9 +204,11 @@ impl<'a, 'b, 'tcx> visit::Visitor<'tcx> for FnDefVisitor<'a, 'b, 'tcx> {
fn require_rooted(cx: &LateContext, in_new_function: bool, subexpr: &hir::Expr) {
let ty = cx.tables.expr_ty(&subexpr);
if is_unrooted_ty(cx, ty, in_new_function) {
cx.span_lint(UNROOTED_MUST_ROOT,
subexpr.span,
&format!("Expression of type {:?} must be rooted", ty))
cx.span_lint(
UNROOTED_MUST_ROOT,
subexpr.span,
&format!("Expression of type {:?} must be rooted", ty),
)
}
}

Expand All @@ -198,7 +228,7 @@ impl<'a, 'b, 'tcx> visit::Visitor<'tcx> for FnDefVisitor<'a, 'b, 'tcx> {
// }
_ => {
// TODO(pcwalton): Check generics with a whitelist of allowed generics.
}
},
}

visit::walk_expr(self, expr);
Expand All @@ -216,12 +246,14 @@ impl<'a, 'b, 'tcx> visit::Visitor<'tcx> for FnDefVisitor<'a, 'b, 'tcx> {
hir::PatKind::Binding(hir::BindingAnnotation::Mutable, _, _, _) => {
let ty = cx.tables.pat_ty(pat);
if is_unrooted_ty(cx, ty, self.in_new_function) {
cx.span_lint(UNROOTED_MUST_ROOT,
pat.span,
&format!("Expression of type {:?} must be rooted", ty))
cx.span_lint(
UNROOTED_MUST_ROOT,
pat.span,
&format!("Expression of type {:?} must be rooted", ty),
)
}
}
_ => {}
},
_ => {},
}

visit::walk_pat(self, pat);
Expand Down
9 changes: 5 additions & 4 deletions components/script_plugins/utils.rs
Expand Up @@ -22,10 +22,11 @@ pub fn match_def_path(cx: &LateContext, def_id: DefId, path: &[&str]) -> bool {
return false;
}

other.into_iter()
.map(|e| e.data)
.zip(path)
.all(|(nm, p)| &*nm.as_interned_str().as_str() == *p)
other
.into_iter()
.map(|e| e.data)
.zip(path)
.all(|(nm, p)| &*nm.as_interned_str().as_str() == *p)
}

pub fn in_derive_expn(span: Span) -> bool {
Expand Down

0 comments on commit 3324b5b

Please sign in to comment.