mirror of
https://github.com/servo/servo.git
synced 2025-07-07 23:43:39 +01:00
Auto merge of #21694 - chansuke:format_script_plugins, r=jdm
Format components/script_plugins <!-- Please describe your changes on the following line: --> --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [ ] These changes fix part of #21373. - [x] These changes do not require tests because they formatting code only. <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/21694) <!-- Reviewable:end -->
This commit is contained in:
commit
eb77036ab5
3 changed files with 80 additions and 49 deletions
|
@ -13,8 +13,6 @@
|
||||||
//! - `#[dom_struct]` : Implies #[derive(JSTraceable, DenyPublicFields)]`, and `#[must_root]`.
|
//! - `#[dom_struct]` : Implies #[derive(JSTraceable, DenyPublicFields)]`, and `#[must_root]`.
|
||||||
//! Use this for structs that correspond to a DOM type
|
//! Use this for structs that correspond to a DOM type
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#![deny(unsafe_code)]
|
#![deny(unsafe_code)]
|
||||||
#![feature(macro_at_most_once_rep)]
|
#![feature(macro_at_most_once_rep)]
|
||||||
#![feature(plugin)]
|
#![feature(plugin)]
|
||||||
|
|
|
@ -9,8 +9,11 @@ use rustc::ty;
|
||||||
use syntax::{ast, source_map, symbol::Ident};
|
use syntax::{ast, source_map, symbol::Ident};
|
||||||
use utils::{match_def_path, in_derive_expn};
|
use utils::{match_def_path, in_derive_expn};
|
||||||
|
|
||||||
declare_lint!(UNROOTED_MUST_ROOT, Deny,
|
declare_lint!(
|
||||||
"Warn and report usage of unrooted jsmanaged objects");
|
UNROOTED_MUST_ROOT,
|
||||||
|
Deny,
|
||||||
|
"Warn and report usage of unrooted jsmanaged objects"
|
||||||
|
);
|
||||||
|
|
||||||
/// Lint for ensuring safe usage of unrooted pointers
|
/// Lint for ensuring safe usage of unrooted pointers
|
||||||
///
|
///
|
||||||
|
@ -48,15 +51,24 @@ fn is_unrooted_ty(cx: &LateContext, ty: &ty::TyS, in_new_function: bool) -> bool
|
||||||
false
|
false
|
||||||
} else if cx.tcx.has_attr(did.did, "allow_unrooted_interior") {
|
} else if cx.tcx.has_attr(did.did, "allow_unrooted_interior") {
|
||||||
false
|
false
|
||||||
} else if match_def_path(cx, did.did, &["core", "cell", "Ref"])
|
} 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", "cell", "RefMut"]) ||
|
||||||
|| match_def_path(cx, did.did, &["core", "slice", "Iter"])
|
match_def_path(cx, did.did, &["core", "slice", "Iter"]) ||
|
||||||
|| match_def_path(cx, did.did, &["core", "slice", "IterMut"])
|
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", "Entry"]) ||
|
||||||
|| match_def_path(cx, did.did, &["std", "collections", "hash", "map", "OccupiedEntry"])
|
match_def_path(
|
||||||
|| match_def_path(cx, did.did, &["std", "collections", "hash", "map", "VacantEntry"])
|
cx,
|
||||||
|| match_def_path(cx, did.did, &["std", "collections", "hash", "map", "Iter"])
|
did.did,
|
||||||
|| match_def_path(cx, did.did, &["std", "collections", "hash", "set", "Iter"]) {
|
&["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.
|
// Structures which are semantically similar to an &ptr.
|
||||||
false
|
false
|
||||||
} else if did.is_box() && in_new_function {
|
} else if did.is_box() && in_new_function {
|
||||||
|
@ -66,10 +78,10 @@ fn is_unrooted_ty(cx: &LateContext, ty: &ty::TyS, in_new_function: bool) -> bool
|
||||||
true
|
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::RawPtr(..) => false, // don't recurse down *ptrs
|
||||||
ty::FnDef(..) | ty::FnPtr(_) => false,
|
ty::FnDef(..) | ty::FnPtr(_) => false,
|
||||||
_ => true
|
_ => true,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ret
|
ret
|
||||||
|
@ -83,12 +95,14 @@ impl LintPass for UnrootedPass {
|
||||||
|
|
||||||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnrootedPass {
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnrootedPass {
|
||||||
/// All structs containing #[must_root] types must be #[must_root] themselves
|
/// All structs containing #[must_root] types must be #[must_root] themselves
|
||||||
fn check_struct_def(&mut self,
|
fn check_struct_def(
|
||||||
cx: &LateContext,
|
&mut self,
|
||||||
def: &hir::VariantData,
|
cx: &LateContext,
|
||||||
_n: ast::Name,
|
def: &hir::VariantData,
|
||||||
_gen: &hir::Generics,
|
_n: ast::Name,
|
||||||
id: ast::NodeId) {
|
_gen: &hir::Generics,
|
||||||
|
id: ast::NodeId,
|
||||||
|
) {
|
||||||
let item = match cx.tcx.hir.get(id) {
|
let item = match cx.tcx.hir.get(id) {
|
||||||
hir::Node::Item(item) => item,
|
hir::Node::Item(item) => item,
|
||||||
_ => cx.tcx.hir.expect_item(cx.tcx.hir.get_parent(id)),
|
_ => cx.tcx.hir.expect_item(cx.tcx.hir.get_parent(id)),
|
||||||
|
@ -107,35 +121,45 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnrootedPass {
|
||||||
/// All enums containing #[must_root] types must be #[must_root] themselves
|
/// All enums containing #[must_root] types must be #[must_root] themselves
|
||||||
fn check_variant(&mut self, cx: &LateContext, var: &hir::Variant, _gen: &hir::Generics) {
|
fn check_variant(&mut self, cx: &LateContext, var: &hir::Variant, _gen: &hir::Generics) {
|
||||||
let ref map = cx.tcx.hir;
|
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 {
|
match var.node.data {
|
||||||
hir::VariantData::Tuple(ref fields, _) => {
|
hir::VariantData::Tuple(ref fields, _) => {
|
||||||
for ref field in fields {
|
for ref field in fields {
|
||||||
let def_id = cx.tcx.hir.local_def_id(field.id);
|
let def_id = cx.tcx.hir.local_def_id(field.id);
|
||||||
if is_unrooted_ty(cx, cx.tcx.type_of(def_id), false) {
|
if is_unrooted_ty(cx, cx.tcx.type_of(def_id), false) {
|
||||||
cx.span_lint(UNROOTED_MUST_ROOT, field.ty.span,
|
cx.span_lint(
|
||||||
"Type must be rooted, use #[must_root] on \
|
UNROOTED_MUST_ROOT,
|
||||||
the enum definition to propagate")
|
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
|
/// Function arguments that are #[must_root] types are not allowed
|
||||||
fn check_fn(&mut self,
|
fn check_fn(
|
||||||
cx: &LateContext<'a, 'tcx>,
|
&mut self,
|
||||||
kind: visit::FnKind,
|
cx: &LateContext<'a, 'tcx>,
|
||||||
decl: &'tcx hir::FnDecl,
|
kind: visit::FnKind,
|
||||||
body: &'tcx hir::Body,
|
decl: &'tcx hir::FnDecl,
|
||||||
span: source_map::Span,
|
body: &'tcx hir::Body,
|
||||||
id: ast::NodeId) {
|
span: source_map::Span,
|
||||||
|
id: ast::NodeId,
|
||||||
|
) {
|
||||||
let in_new_function = match kind {
|
let in_new_function = match kind {
|
||||||
visit::FnKind::ItemFn(n, _, _, _, _) |
|
visit::FnKind::ItemFn(n, _, _, _, _) |
|
||||||
visit::FnKind::Method(Ident { name: n, .. }, _, _, _) => {
|
visit::FnKind::Method(Ident { name: n, .. }, _, _, _) => {
|
||||||
&*n.as_str() == "new" || n.as_str().starts_with("new_")
|
&*n.as_str() == "new" || n.as_str().starts_with("new_")
|
||||||
}
|
},
|
||||||
visit::FnKind::Closure(_) => return,
|
visit::FnKind::Closure(_) => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -151,7 +175,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnrootedPass {
|
||||||
|
|
||||||
if !in_new_function {
|
if !in_new_function {
|
||||||
if is_unrooted_ty(cx, sig.output().skip_binder(), false) {
|
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",
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
fn require_rooted(cx: &LateContext, in_new_function: bool, subexpr: &hir::Expr) {
|
||||||
let ty = cx.tables.expr_ty(&subexpr);
|
let ty = cx.tables.expr_ty(&subexpr);
|
||||||
if is_unrooted_ty(cx, ty, in_new_function) {
|
if is_unrooted_ty(cx, ty, in_new_function) {
|
||||||
cx.span_lint(UNROOTED_MUST_ROOT,
|
cx.span_lint(
|
||||||
subexpr.span,
|
UNROOTED_MUST_ROOT,
|
||||||
&format!("Expression of type {:?} must be rooted", ty))
|
subexpr.span,
|
||||||
|
&format!("Expression of type {:?} must be rooted", ty),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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.
|
// TODO(pcwalton): Check generics with a whitelist of allowed generics.
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
visit::walk_expr(self, expr);
|
visit::walk_expr(self, expr);
|
||||||
|
@ -216,12 +246,14 @@ impl<'a, 'b, 'tcx> visit::Visitor<'tcx> for FnDefVisitor<'a, 'b, 'tcx> {
|
||||||
hir::PatKind::Binding(hir::BindingAnnotation::Mutable, _, _, _) => {
|
hir::PatKind::Binding(hir::BindingAnnotation::Mutable, _, _, _) => {
|
||||||
let ty = cx.tables.pat_ty(pat);
|
let ty = cx.tables.pat_ty(pat);
|
||||||
if is_unrooted_ty(cx, ty, self.in_new_function) {
|
if is_unrooted_ty(cx, ty, self.in_new_function) {
|
||||||
cx.span_lint(UNROOTED_MUST_ROOT,
|
cx.span_lint(
|
||||||
pat.span,
|
UNROOTED_MUST_ROOT,
|
||||||
&format!("Expression of type {:?} must be rooted", ty))
|
pat.span,
|
||||||
|
&format!("Expression of type {:?} must be rooted", ty),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
_ => {}
|
_ => {},
|
||||||
}
|
}
|
||||||
|
|
||||||
visit::walk_pat(self, pat);
|
visit::walk_pat(self, pat);
|
||||||
|
|
|
@ -22,10 +22,11 @@ pub fn match_def_path(cx: &LateContext, def_id: DefId, path: &[&str]) -> bool {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
other.into_iter()
|
other
|
||||||
.map(|e| e.data)
|
.into_iter()
|
||||||
.zip(path)
|
.map(|e| e.data)
|
||||||
.all(|(nm, p)| &*nm.as_interned_str().as_str() == *p)
|
.zip(path)
|
||||||
|
.all(|(nm, p)| &*nm.as_interned_str().as_str() == *p)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn in_derive_expn(span: Span) -> bool {
|
pub fn in_derive_expn(span: Span) -> bool {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue