Add unrooted_must_root lint for usages of JS<T> in let/for bindings

This commit is contained in:
Manish Goregaokar 2014-09-16 22:35:41 +05:30
parent 12dc54d238
commit bded5c3703
11 changed files with 55 additions and 15 deletions

View file

@ -8,6 +8,7 @@
#![feature(globs, macro_rules, phase, thread_local, unsafe_destructor)] #![feature(globs, macro_rules, phase, thread_local, unsafe_destructor)]
#![deny(unused_imports, unused_variable)] #![deny(unused_imports, unused_variable)]
#![allow(unrooted_must_root)]
#[phase(plugin, link)] #[phase(plugin, link)]
extern crate log; extern crate log;

View file

@ -85,7 +85,8 @@ impl LintPass for UnrootedPass {
fn check_struct_def(&mut self, cx: &Context, def: &ast::StructDef, _i: ast::Ident, _gen: &ast::Generics, id: ast::NodeId) { fn check_struct_def(&mut self, cx: &Context, def: &ast::StructDef, _i: ast::Ident, _gen: &ast::Generics, id: ast::NodeId) {
if cx.tcx.map.expect_item(id).attrs.iter().all(|a| !a.check_name("must_root")) { if cx.tcx.map.expect_item(id).attrs.iter().all(|a| !a.check_name("must_root")) {
for ref field in def.fields.iter() { for ref field in def.fields.iter() {
lint_unrooted_ty(cx, &*field.node.ty, "Type must be rooted, use #[must_root] on the struct definition to propagate"); lint_unrooted_ty(cx, &*field.node.ty,
"Type must be rooted, use #[must_root] on the struct definition to propagate");
} }
} }
} }
@ -96,7 +97,8 @@ impl LintPass for UnrootedPass {
match var.node.kind { match var.node.kind {
ast::TupleVariantKind(ref vec) => { ast::TupleVariantKind(ref vec) => {
for ty in vec.iter() { for ty in vec.iter() {
lint_unrooted_ty(cx, &*ty.ty, "Type must be rooted, use #[must_root] on the enum definition to propagate") lint_unrooted_ty(cx, &*ty.ty,
"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
@ -104,23 +106,55 @@ impl LintPass for UnrootedPass {
} }
} }
fn check_fn(&mut self, cx: &Context, kind: &syntax::visit::FnKind, decl: &ast::FnDecl, block: &ast::Block, _span: syntax::codemap::Span, _id: ast::NodeId) { fn check_fn(&mut self, cx: &Context, kind: &syntax::visit::FnKind, decl: &ast::FnDecl,
block: &ast::Block, _span: syntax::codemap::Span, _id: ast::NodeId) {
match *kind { match *kind {
syntax::visit::FkItemFn(i, _, _, _) | syntax::visit::FkItemFn(i, _, _, _) |
syntax::visit::FkMethod(i, _, _) if i.as_str() == "new" || i.as_str() == "new_inherited" => { syntax::visit::FkMethod(i, _, _) if i.as_str() == "new" || i.as_str() == "new_inherited" => {
return;
} }
_ => () _ => ()
} }
match block.rules { match block.rules {
ast::DefaultBlock => { ast::DefaultBlock => {
for arg in decl.inputs.iter() { for arg in decl.inputs.iter() {
lint_unrooted_ty(cx, &*arg.ty, "Type must be rooted, use #[must_root] on the struct definition to propagate") lint_unrooted_ty(cx, &*arg.ty,
"Type must be rooted, use #[must_root] on the struct definition to propagate")
} }
} }
_ => () // fn is `unsafe` _ => () // fn is `unsafe`
} }
} }
// Partially copied from rustc::middle::lint::builtin
// Catches `let` statements which store a #[must_root] value
// Expressions which return out of blocks eventually end up in a `let`
// statement or a function return (which will be caught when it is used elsewhere)
fn check_stmt(&mut self, cx: &Context, s: &ast::Stmt) {
// Catch the let binding
let expr = match s.node {
ast::StmtDecl(ref decl, _) => match decl.node {
ast::DeclLocal(ref loc) => match loc.init {
Some(ref e) => &**e,
_ => return
},
_ => return
},
_ => return
};
let t = expr_ty(cx.tcx, &*expr);
match ty::get(t).sty {
ty::ty_struct(did, _) |
ty::ty_enum(did, _) => {
if ty::has_attr(cx.tcx, did, "must_root") {
cx.span_lint(UNROOTED_MUST_ROOT, expr.span,
format!("Expression of type {} must be rooted", t.repr(cx.tcx)).as_slice());
}
}
_ => {}
}
}
} }
#[plugin_registrar] #[plugin_registrar]

View file

@ -108,8 +108,8 @@ impl Attr {
pub fn new(window: &JSRef<Window>, local_name: Atom, value: AttrValue, pub fn new(window: &JSRef<Window>, local_name: Atom, value: AttrValue,
name: Atom, namespace: Namespace, name: Atom, namespace: Namespace,
prefix: Option<DOMString>, owner: &JSRef<Element>) -> Temporary<Attr> { prefix: Option<DOMString>, owner: &JSRef<Element>) -> Temporary<Attr> {
let attr = Attr::new_inherited(local_name, value, name, namespace, prefix, owner); reflect_dom_object(box Attr::new_inherited(local_name, value, name, namespace, prefix, owner),
reflect_dom_object(box attr, &Window(*window), AttrBinding::Wrap) &Window(*window), AttrBinding::Wrap)
} }
} }

View file

@ -139,6 +139,7 @@ pub struct CallSetup {
impl CallSetup { impl CallSetup {
/// Performs the setup needed to make a call. /// Performs the setup needed to make a call.
#[allow(unrooted_must_root)]
pub fn new<T: CallbackContainer>(callback: &T, handling: ExceptionHandling) -> CallSetup { pub fn new<T: CallbackContainer>(callback: &T, handling: ExceptionHandling) -> CallSetup {
let global = global_object_for_js_object(callback.callback()); let global = global_object_for_js_object(callback.callback());
let global = global.root(); let global = global.root();

View file

@ -668,6 +668,7 @@ pub extern fn outerize_global(_cx: *mut JSContext, obj: JSHandleObject) -> *mut
} }
/// Returns the global object of the realm that the given JS object was created in. /// Returns the global object of the realm that the given JS object was created in.
#[allow(unrooted_must_root)]
pub fn global_object_for_js_object(obj: *mut JSObject) -> GlobalField { pub fn global_object_for_js_object(obj: *mut JSObject) -> GlobalField {
unsafe { unsafe {
let global = GetGlobalForObjectCrossCompartment(obj); let global = GetGlobalForObjectCrossCompartment(obj);
@ -689,6 +690,7 @@ pub fn global_object_for_js_object(obj: *mut JSObject) -> GlobalField {
/// Get the `JSContext` for the `JSRuntime` associated with the thread /// Get the `JSContext` for the `JSRuntime` associated with the thread
/// this object is on. /// this object is on.
#[allow(unrooted_must_root)]
fn cx_for_dom_reflector(obj: *mut JSObject) -> *mut JSContext { fn cx_for_dom_reflector(obj: *mut JSObject) -> *mut JSContext {
let global = global_object_for_js_object(obj); let global = global_object_for_js_object(obj);
let global = global.root(); let global = global.root();

View file

@ -310,8 +310,8 @@ impl Document {
} }
pub fn new(window: &JSRef<Window>, url: Option<Url>, doctype: IsHTMLDocument, content_type: Option<DOMString>) -> Temporary<Document> { pub fn new(window: &JSRef<Window>, url: Option<Url>, doctype: IsHTMLDocument, content_type: Option<DOMString>) -> Temporary<Document> {
let document = Document::new_inherited(window, url, doctype, content_type); let document = reflect_dom_object(box Document::new_inherited(window, url, doctype, content_type),
let document = reflect_dom_object(box document, &Window(*window), &Window(*window),
DocumentBinding::Wrap).root(); DocumentBinding::Wrap).root();
let node: &JSRef<Node> = NodeCast::from_ref(&*document); let node: &JSRef<Node> = NodeCast::from_ref(&*document);

View file

@ -220,6 +220,7 @@ pub trait LayoutElementHelpers {
} }
impl LayoutElementHelpers for JS<Element> { impl LayoutElementHelpers for JS<Element> {
#[allow(unrooted_must_root)]
unsafe fn html_element_in_html_document_for_layout(&self) -> bool { unsafe fn html_element_in_html_document_for_layout(&self) -> bool {
if (*self.unsafe_get()).namespace != namespace::HTML { if (*self.unsafe_get()).namespace != namespace::HTML {
return false return false

View file

@ -55,6 +55,7 @@ impl FormData {
} }
impl<'a> FormDataMethods for JSRef<'a, FormData> { impl<'a> FormDataMethods for JSRef<'a, FormData> {
#[allow(unrooted_must_root)]
fn Append(&self, name: DOMString, value: &JSRef<Blob>, filename: Option<DOMString>) { fn Append(&self, name: DOMString, value: &JSRef<Blob>, filename: Option<DOMString>) {
let file = FileData(JS::from_rooted(&self.get_file_from_blob(value, filename))); let file = FileData(JS::from_rooted(&self.get_file_from_blob(value, filename)));
self.data.deref().borrow_mut().insert_or_update_with(name.clone(), vec!(file.clone()), self.data.deref().borrow_mut().insert_or_update_with(name.clone(), vec!(file.clone()),
@ -86,7 +87,7 @@ impl<'a> FormDataMethods for JSRef<'a, FormData> {
fn Has(&self, name: DOMString) -> bool { fn Has(&self, name: DOMString) -> bool {
self.data.deref().borrow().contains_key_equiv(&name) self.data.deref().borrow().contains_key_equiv(&name)
} }
#[allow(unrooted_must_root)]
fn Set(&self, name: DOMString, value: &JSRef<Blob>, filename: Option<DOMString>) { fn Set(&self, name: DOMString, value: &JSRef<Blob>, filename: Option<DOMString>) {
let file = FileData(JS::from_rooted(&self.get_file_from_blob(value, filename))); let file = FileData(JS::from_rooted(&self.get_file_from_blob(value, filename)));
self.data.deref().borrow_mut().insert(name, vec!(file)); self.data.deref().borrow_mut().insert(name, vec!(file));

View file

@ -29,8 +29,8 @@ impl Performance {
} }
pub fn new(window: &JSRef<Window>) -> Temporary<Performance> { pub fn new(window: &JSRef<Window>) -> Temporary<Performance> {
let performance = Performance::new_inherited(window); reflect_dom_object(box Performance::new_inherited(window),
reflect_dom_object(box performance, &Window(*window), &Window(*window),
PerformanceBinding::Wrap) PerformanceBinding::Wrap)
} }
} }

View file

@ -133,7 +133,7 @@ pub struct XMLHttpRequest {
impl XMLHttpRequest { impl XMLHttpRequest {
pub fn new_inherited(global: &GlobalRef) -> XMLHttpRequest { pub fn new_inherited(global: &GlobalRef) -> XMLHttpRequest {
let xhr = XMLHttpRequest { XMLHttpRequest {
eventtarget: XMLHttpRequestEventTarget::new_inherited(XMLHttpRequestTypeId), eventtarget: XMLHttpRequestEventTarget::new_inherited(XMLHttpRequestTypeId),
ready_state: Traceable::new(Cell::new(Unsent)), ready_state: Traceable::new(Cell::new(Unsent)),
timeout: Traceable::new(Cell::new(0u32)), timeout: Traceable::new(Cell::new(0u32)),
@ -163,8 +163,7 @@ impl XMLHttpRequest {
fetch_time: Traceable::new(Cell::new(0)), fetch_time: Traceable::new(Cell::new(0)),
timeout_pinned: Traceable::new(Cell::new(false)), timeout_pinned: Traceable::new(Cell::new(false)),
terminate_sender: Untraceable::new(RefCell::new(None)), terminate_sender: Untraceable::new(RefCell::new(None)),
}; }
xhr
} }
pub fn new(global: &GlobalRef) -> Temporary<XMLHttpRequest> { pub fn new(global: &GlobalRef) -> Temporary<XMLHttpRequest> {
reflect_dom_object(box XMLHttpRequest::new_inherited(global), reflect_dom_object(box XMLHttpRequest::new_inherited(global),

View file

@ -58,6 +58,7 @@ pub mod dom {
/// Generated JS-Rust bindings. /// Generated JS-Rust bindings.
pub mod codegen { pub mod codegen {
#[allow(unrooted_must_root)]
pub mod Bindings; pub mod Bindings;
pub mod InterfaceTypes; pub mod InterfaceTypes;
pub mod InheritTypes; pub mod InheritTypes;