Auto merge of #8030 - Manishearth:fix-lint-fn, r=frewsxcv

Fix unrooted_must_root lint to handle arguments/return types properly

r? @jdm

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/8030)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2015-10-15 15:00:00 -06:00
commit 9d5f09e09c
8 changed files with 44 additions and 11 deletions

View file

@ -4,12 +4,11 @@
use rustc::front::map as ast_map; use rustc::front::map as ast_map;
use rustc::lint::{LateContext, LintPass, LintArray, LateLintPass, LintContext}; use rustc::lint::{LateContext, LintPass, LintArray, LateLintPass, LintContext};
use rustc::middle::astconv_util::ast_ty_to_prim_ty;
use rustc::middle::ty; use rustc::middle::ty;
use rustc_front::{hir, visit}; use rustc_front::{hir, visit};
use syntax::attr::AttrMetaMethods; use syntax::attr::AttrMetaMethods;
use syntax::{ast, codemap}; use syntax::{ast, codemap};
use utils::{match_def_path, unsafe_context}; use utils::{match_def_path, unsafe_context, in_derive_expn};
declare_lint!(UNROOTED_MUST_ROOT, Deny, declare_lint!(UNROOTED_MUST_ROOT, Deny,
"Warn and report usage of unrooted jsmanaged objects"); "Warn and report usage of unrooted jsmanaged objects");
@ -107,7 +106,7 @@ impl LateLintPass for UnrootedPass {
match var.node.kind { match var.node.kind {
hir::TupleVariantKind(ref vec) => { hir::TupleVariantKind(ref vec) => {
for ty in vec { for ty in vec {
ast_ty_to_prim_ty(cx.tcx, &*ty.ty).map(|t| { cx.tcx.ast_ty_to_ty_cache.borrow().get(&ty.id).map(|t| {
if is_unrooted_ty(cx, t, false) { if is_unrooted_ty(cx, t, false) {
cx.span_lint(UNROOTED_MUST_ROOT, ty.ty.span, cx.span_lint(UNROOTED_MUST_ROOT, ty.ty.span,
"Type must be rooted, use #[must_root] on \ "Type must be rooted, use #[must_root] on \
@ -122,7 +121,7 @@ impl LateLintPass for UnrootedPass {
} }
/// 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, cx: &LateContext, kind: visit::FnKind, decl: &hir::FnDecl, fn check_fn(&mut self, cx: &LateContext, kind: visit::FnKind, decl: &hir::FnDecl,
block: &hir::Block, _span: codemap::Span, id: ast::NodeId) { block: &hir::Block, span: codemap::Span, id: ast::NodeId) {
match kind { match kind {
visit::FnKind::ItemFn(n, _, _, _, _, _) | visit::FnKind::ItemFn(n, _, _, _, _, _) |
visit::FnKind::Method(n, _, _) if n.as_str() == "new" visit::FnKind::Method(n, _, _) if n.as_str() == "new"
@ -145,12 +144,25 @@ impl LateLintPass for UnrootedPass {
match block.rules { match block.rules {
hir::DefaultBlock => { hir::DefaultBlock => {
for arg in &decl.inputs { for arg in &decl.inputs {
ast_ty_to_prim_ty(cx.tcx, &*arg.ty).map(|t| { cx.tcx.ast_ty_to_ty_cache.borrow().get(&arg.ty.id).map(|t| {
if is_unrooted_ty(cx, t, false) { if is_unrooted_ty(cx, t, false) {
if in_derive_expn(cx, span) {
return;
}
cx.span_lint(UNROOTED_MUST_ROOT, arg.ty.span, "Type must be rooted") cx.span_lint(UNROOTED_MUST_ROOT, arg.ty.span, "Type must be rooted")
} }
}); });
} }
if let hir::Return(ref ty) = decl.output {
cx.tcx.ast_ty_to_ty_cache.borrow().get(&ty.id).map(|t| {
if is_unrooted_ty(cx, t, false) {
if in_derive_expn(cx, span) {
return;
}
cx.span_lint(UNROOTED_MUST_ROOT, ty.span, "Type must be rooted")
}
});
}
} }
_ => () // fn is `unsafe` _ => () // fn is `unsafe`
} }

View file

@ -3,12 +3,13 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use rustc::front::map as ast_map; use rustc::front::map as ast_map;
use rustc::lint::LateContext; use rustc::lint::{LateContext, LintContext};
use rustc::middle::def; use rustc::middle::def;
use rustc::middle::def_id::DefId; use rustc::middle::def_id::DefId;
use rustc_front::hir; use rustc_front::hir;
use syntax::ast; use syntax::ast;
use syntax::attr::mark_used; use syntax::attr::mark_used;
use syntax::codemap::{ExpnFormat, Span};
use syntax::ptr::P; use syntax::ptr::P;
@ -100,3 +101,16 @@ pub fn match_def_path(cx: &LateContext, def_id: DefId, path: &[&str]) -> bool {
cx.tcx.with_path(def_id, |iter| iter.map(|elem| elem.name()) cx.tcx.with_path(def_id, |iter| iter.map(|elem| elem.name())
.zip(path.iter()).all(|(nm, p)| &nm.as_str() == p)) .zip(path.iter()).all(|(nm, p)| &nm.as_str() == p))
} }
pub fn in_derive_expn(cx: &LateContext, span: Span) -> bool {
cx.sess().codemap().with_expn_info(span.expn_id,
|info| {
if let Some(i) = info {
if let ExpnFormat::MacroAttribute(n) = i.callee.format {
if n.as_str().contains("derive") {
true
} else { false }
} else { false }
} else { false }
})
}

View file

@ -210,6 +210,7 @@ impl GlobalRoot {
impl GlobalField { impl GlobalField {
/// Create a new `GlobalField` from a rooted reference. /// Create a new `GlobalField` from a rooted reference.
#[allow(unrooted_must_root)]
pub fn from_rooted(global: &GlobalRef) -> GlobalField { pub fn from_rooted(global: &GlobalRef) -> GlobalField {
match *global { match *global {
GlobalRef::Window(window) => GlobalField::Window(JS::from_ref(window)), GlobalRef::Window(window) => GlobalField::Window(JS::from_ref(window)),

View file

@ -67,12 +67,14 @@ impl<T: Reflectable> JS<T> {
} }
/// Create a JS<T> from a Root<T> /// Create a JS<T> from a Root<T>
/// XXX Not a great API. Should be a call on Root<T> instead /// XXX Not a great API. Should be a call on Root<T> instead
#[allow(unrooted_must_root)]
pub fn from_rooted(root: &Root<T>) -> JS<T> { pub fn from_rooted(root: &Root<T>) -> JS<T> {
JS { JS {
ptr: unsafe { NonZero::new(&**root) } ptr: unsafe { NonZero::new(&**root) }
} }
} }
/// Create a JS<T> from a &T /// Create a JS<T> from a &T
#[allow(unrooted_must_root)]
pub fn from_ref(obj: &T) -> JS<T> { pub fn from_ref(obj: &T) -> JS<T> {
JS { JS {
ptr: unsafe { NonZero::new(&*obj) } ptr: unsafe { NonZero::new(&*obj) }
@ -125,6 +127,7 @@ impl<T> PartialEq for LayoutJS<T> {
impl <T> Clone for JS<T> { impl <T> Clone for JS<T> {
#[inline] #[inline]
#[allow(unrooted_must_root)]
fn clone(&self) -> JS<T> { fn clone(&self) -> JS<T> {
JS { JS {
ptr: self.ptr.clone() ptr: self.ptr.clone()
@ -288,6 +291,7 @@ impl<T: Reflectable> MutNullableHeap<JS<T>> {
} }
impl<T: HeapGCValue + Copy> Default for MutNullableHeap<T> { impl<T: HeapGCValue + Copy> Default for MutNullableHeap<T> {
#[allow(unrooted_must_root)]
fn default() -> MutNullableHeap<T> { fn default() -> MutNullableHeap<T> {
MutNullableHeap { MutNullableHeap {
ptr: Cell::new(None) ptr: Cell::new(None)

View file

@ -1352,10 +1352,12 @@ impl Node {
Node::new_(flags, Some(doc)) Node::new_(flags, Some(doc))
} }
#[allow(unrooted_must_root)]
pub fn new_document_node() -> Node { pub fn new_document_node() -> Node {
Node::new_(NodeFlags::new() | IS_IN_DOC, None) Node::new_(NodeFlags::new() | IS_IN_DOC, None)
} }
#[allow(unrooted_must_root)]
fn new_(flags: NodeFlags, doc: Option<&Document>) -> Node { fn new_(flags: NodeFlags, doc: Option<&Document>) -> Node {
Node { Node {
eventtarget: EventTarget::new_inherited(), eventtarget: EventTarget::new_inherited(),

View file

@ -43,6 +43,7 @@ pub struct Sink {
} }
impl Sink { impl Sink {
#[allow(unrooted_must_root)] // method is only run at parse time
pub fn get_or_create(&self, child: NodeOrText<JS<Node>>) -> Root<Node> { pub fn get_or_create(&self, child: NodeOrText<JS<Node>>) -> Root<Node> {
match child { match child {
NodeOrText::AppendNode(n) => n.root(), NodeOrText::AppendNode(n) => n.root(),

View file

@ -147,10 +147,10 @@ impl WebGLRenderingContext {
} }
} }
pub fn bound_texture_for(&self, target: u32) -> Option<JS<WebGLTexture>> { pub fn bound_texture_for(&self, target: u32) -> Option<Root<WebGLTexture>> {
match target { match target {
constants::TEXTURE_2D => self.bound_texture_2d.get(), constants::TEXTURE_2D => self.bound_texture_2d.get().map(|t| t.root()),
constants::TEXTURE_CUBE_MAP => self.bound_texture_cube_map.get(), constants::TEXTURE_CUBE_MAP => self.bound_texture_cube_map.get().map(|t| t.root()),
_ => unreachable!(), _ => unreachable!(),
} }
@ -906,7 +906,6 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
constants::TEXTURE_2D | constants::TEXTURE_2D |
constants::TEXTURE_CUBE_MAP => { constants::TEXTURE_CUBE_MAP => {
if let Some(texture) = self.bound_texture_for(target) { if let Some(texture) = self.bound_texture_for(target) {
let texture = texture.root();
let result = texture.r().tex_parameter(target, name, TexParameterValue::Float(value)); let result = texture.r().tex_parameter(target, name, TexParameterValue::Float(value));
handle_potential_webgl_error!(self, result); handle_potential_webgl_error!(self, result);
} else { } else {
@ -924,7 +923,6 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
constants::TEXTURE_2D | constants::TEXTURE_2D |
constants::TEXTURE_CUBE_MAP => { constants::TEXTURE_CUBE_MAP => {
if let Some(texture) = self.bound_texture_for(target) { if let Some(texture) = self.bound_texture_for(target) {
let texture = texture.root();
let result = texture.r().tex_parameter(target, name, TexParameterValue::Int(value)); let result = texture.r().tex_parameter(target, name, TexParameterValue::Int(value));
handle_potential_webgl_error!(self, result); handle_potential_webgl_error!(self, result);
} else { } else {

View file

@ -119,6 +119,7 @@ impl Page {
old old
} }
#[allow(unrooted_must_root)]
pub fn set_frame(&self, frame: Option<Frame>) { pub fn set_frame(&self, frame: Option<Frame>) {
*self.frame.borrow_mut() = frame; *self.frame.borrow_mut() = frame;
} }