mirror of
https://github.com/servo/servo.git
synced 2025-06-08 08:33:26 +00:00
Change #[privatize] into #[derive(DenyPublicFields)]
This commit is contained in:
parent
19c645ff68
commit
8bcf36b9a5
16 changed files with 69 additions and 65 deletions
10
Cargo.lock
generated
10
Cargo.lock
generated
|
@ -597,6 +597,14 @@ dependencies = [
|
||||||
"byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "deny_public_fields"
|
||||||
|
version = "0.0.1"
|
||||||
|
dependencies = [
|
||||||
|
"syn 0.10.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"synstructure 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "deque"
|
name = "deque"
|
||||||
version = "0.3.1"
|
version = "0.3.1"
|
||||||
|
@ -2030,6 +2038,7 @@ name = "plugin_compiletest"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"compiletest_helper 0.0.1",
|
"compiletest_helper 0.0.1",
|
||||||
|
"deny_public_fields 0.0.1",
|
||||||
"plugins 0.0.1",
|
"plugins 0.0.1",
|
||||||
"script 0.0.1",
|
"script 0.0.1",
|
||||||
]
|
]
|
||||||
|
@ -2256,6 +2265,7 @@ dependencies = [
|
||||||
"cmake 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)",
|
"cmake 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"cookie 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
"cookie 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"cssparser 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"cssparser 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"deny_public_fields 0.0.1",
|
||||||
"devtools_traits 0.0.1",
|
"devtools_traits 0.0.1",
|
||||||
"domobject_derive 0.0.1",
|
"domobject_derive 0.0.1",
|
||||||
"encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
|
"encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
|
14
components/deny_public_fields/Cargo.toml
Normal file
14
components/deny_public_fields/Cargo.toml
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
[package]
|
||||||
|
name = "deny_public_fields"
|
||||||
|
version = "0.0.1"
|
||||||
|
authors = ["The Servo Project Developers"]
|
||||||
|
license = "MPL-2.0"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
path = "lib.rs"
|
||||||
|
proc-macro = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
syn = "0.10"
|
||||||
|
synstructure = "0.4"
|
26
components/deny_public_fields/lib.rs
Normal file
26
components/deny_public_fields/lib.rs
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
extern crate proc_macro;
|
||||||
|
extern crate syn;
|
||||||
|
extern crate synstructure;
|
||||||
|
|
||||||
|
#[proc_macro_derive(DenyPublicFields)]
|
||||||
|
pub fn expand_token_stream(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||||
|
expand_string(&input.to_string()).parse().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expand_string(input: &str) -> String {
|
||||||
|
let type_ = syn::parse_macro_input(input).unwrap();
|
||||||
|
|
||||||
|
let style = synstructure::BindStyle::Ref.into();
|
||||||
|
synstructure::each_field(&type_, &style, |binding| {
|
||||||
|
if binding.field.vis != syn::Visibility::Inherited {
|
||||||
|
panic!("Field {} should not be public", binding.ident);
|
||||||
|
}
|
||||||
|
"".to_owned()
|
||||||
|
});
|
||||||
|
|
||||||
|
"".to_owned()
|
||||||
|
}
|
|
@ -11,7 +11,6 @@ pub fn expand_dom_struct(cx: &mut ExtCtxt, sp: Span, _: &MetaItem, anno: Annotat
|
||||||
if let Annotatable::Item(item) = anno {
|
if let Annotatable::Item(item) = anno {
|
||||||
let mut item2 = (*item).clone();
|
let mut item2 = (*item).clone();
|
||||||
item2.attrs.push(quote_attr!(cx, #[must_root]));
|
item2.attrs.push(quote_attr!(cx, #[must_root]));
|
||||||
item2.attrs.push(quote_attr!(cx, #[privatize]));
|
|
||||||
item2.attrs.push(quote_attr!(cx, #[repr(C)]));
|
item2.attrs.push(quote_attr!(cx, #[repr(C)]));
|
||||||
item2.attrs.push(quote_attr!(cx, #[derive(JSTraceable)]));
|
item2.attrs.push(quote_attr!(cx, #[derive(JSTraceable)]));
|
||||||
item2.attrs.push(quote_attr!(cx, #[derive(HeapSizeOf)]));
|
item2.attrs.push(quote_attr!(cx, #[derive(HeapSizeOf)]));
|
||||||
|
|
|
@ -6,11 +6,11 @@
|
||||||
//!
|
//!
|
||||||
//! Attributes this crate provides:
|
//! Attributes this crate provides:
|
||||||
//!
|
//!
|
||||||
//! - `#[privatize]` : Forces all fields in a struct/enum to be private
|
//! - `#[derive(DenyPublicFields)]` : Forces all fields in a struct/enum to be private
|
||||||
//! - `#[derive(JSTraceable)]` : Auto-derives an implementation of `JSTraceable` for a struct in the script crate
|
//! - `#[derive(JSTraceable)]` : Auto-derives an implementation of `JSTraceable` for a struct in the script crate
|
||||||
//! - `#[must_root]` : Prevents data of the marked type from being used on the stack.
|
//! - `#[must_root]` : Prevents data of the marked type from being used on the stack.
|
||||||
//! See the lints module for more details
|
//! See the lints module for more details
|
||||||
//! - `#[dom_struct]` : Implies `#[privatize]`,`#[derive(JSTraceable)]`, 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
|
||||||
|
|
||||||
|
|
||||||
|
@ -44,13 +44,11 @@ pub fn plugin_registrar(reg: &mut Registry) {
|
||||||
MultiModifier(box jstraceable::expand_dom_struct));
|
MultiModifier(box jstraceable::expand_dom_struct));
|
||||||
|
|
||||||
reg.register_late_lint_pass(box lints::unrooted_must_root::UnrootedPass::new());
|
reg.register_late_lint_pass(box lints::unrooted_must_root::UnrootedPass::new());
|
||||||
reg.register_late_lint_pass(box lints::privatize::PrivatizePass);
|
|
||||||
reg.register_late_lint_pass(box lints::inheritance_integrity::InheritancePass);
|
reg.register_late_lint_pass(box lints::inheritance_integrity::InheritancePass);
|
||||||
reg.register_early_lint_pass(box lints::ban::BanPass);
|
reg.register_early_lint_pass(box lints::ban::BanPass);
|
||||||
reg.register_attribute("_dom_struct_marker".to_string(), Whitelisted);
|
reg.register_attribute("_dom_struct_marker".to_string(), Whitelisted);
|
||||||
reg.register_attribute("allow_unrooted_interior".to_string(), Whitelisted);
|
reg.register_attribute("allow_unrooted_interior".to_string(), Whitelisted);
|
||||||
reg.register_attribute("must_root".to_string(), Whitelisted);
|
reg.register_attribute("must_root".to_string(), Whitelisted);
|
||||||
reg.register_attribute("privatize".to_string(), Whitelisted);
|
|
||||||
reg.register_attribute("servo_lang".to_string(), Whitelisted);
|
reg.register_attribute("servo_lang".to_string(), Whitelisted);
|
||||||
register_clippy(reg);
|
register_clippy(reg);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,5 +4,4 @@
|
||||||
|
|
||||||
pub mod ban;
|
pub mod ban;
|
||||||
pub mod inheritance_integrity;
|
pub mod inheritance_integrity;
|
||||||
pub mod privatize;
|
|
||||||
pub mod unrooted_must_root;
|
pub mod unrooted_must_root;
|
||||||
|
|
|
@ -1,41 +0,0 @@
|
||||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
||||||
|
|
||||||
use rustc::hir;
|
|
||||||
use rustc::lint::{LateContext, LintPass, LintArray, LateLintPass, LintContext};
|
|
||||||
use syntax::ast;
|
|
||||||
|
|
||||||
declare_lint!(PRIVATIZE, Deny,
|
|
||||||
"Allows to enforce private fields for struct definitions");
|
|
||||||
|
|
||||||
/// Lint for keeping DOM fields private
|
|
||||||
///
|
|
||||||
/// This lint (disable with `-A privatize`/`#[allow(privatize)]`) ensures all types marked with `#[privatize]`
|
|
||||||
/// have no public fields
|
|
||||||
pub struct PrivatizePass;
|
|
||||||
|
|
||||||
impl LintPass for PrivatizePass {
|
|
||||||
fn get_lints(&self) -> LintArray {
|
|
||||||
lint_array!(PRIVATIZE)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PrivatizePass {
|
|
||||||
fn check_struct_def(&mut self,
|
|
||||||
cx: &LateContext,
|
|
||||||
def: &hir::VariantData,
|
|
||||||
_n: ast::Name,
|
|
||||||
_gen: &hir::Generics,
|
|
||||||
id: ast::NodeId) {
|
|
||||||
if cx.tcx.has_attr(cx.tcx.hir.local_def_id(id), "privatize") {
|
|
||||||
for field in def.fields() {
|
|
||||||
if field.vis == hir::Public {
|
|
||||||
cx.span_lint(PRIVATIZE, field.span,
|
|
||||||
&format!("Field {} is public where only private fields are allowed",
|
|
||||||
field.name));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -35,6 +35,7 @@ canvas_traits = {path = "../canvas_traits"}
|
||||||
caseless = "0.1.0"
|
caseless = "0.1.0"
|
||||||
cookie = {version = "0.2.5", features = ["serialize-rustc"]}
|
cookie = {version = "0.2.5", features = ["serialize-rustc"]}
|
||||||
cssparser = {version = "0.8", features = ["heap_size", "serde-serialization"]}
|
cssparser = {version = "0.8", features = ["heap_size", "serde-serialization"]}
|
||||||
|
deny_public_fields = {path = "../deny_public_fields"}
|
||||||
devtools_traits = {path = "../devtools_traits"}
|
devtools_traits = {path = "../devtools_traits"}
|
||||||
domobject_derive = {path = "../domobject_derive"}
|
domobject_derive = {path = "../domobject_derive"}
|
||||||
encoding = "0.2"
|
encoding = "0.2"
|
||||||
|
|
|
@ -48,10 +48,8 @@ pub trait Iterable {
|
||||||
/// An iterator over the iterable entries of a given DOM interface.
|
/// An iterator over the iterable entries of a given DOM interface.
|
||||||
//FIXME: #12811 prevents dom_struct with type parameters
|
//FIXME: #12811 prevents dom_struct with type parameters
|
||||||
//#[dom_struct]
|
//#[dom_struct]
|
||||||
|
#[derive(DenyPublicFields, HeapSizeOf, JSTraceable)]
|
||||||
#[must_root]
|
#[must_root]
|
||||||
#[privatize]
|
|
||||||
#[derive(JSTraceable)]
|
|
||||||
#[derive(HeapSizeOf)]
|
|
||||||
pub struct IterableIterator<T: DomObject + JSTraceable + Iterable> {
|
pub struct IterableIterator<T: DomObject + JSTraceable + Iterable> {
|
||||||
reflector: Reflector,
|
reflector: Reflector,
|
||||||
iterable: JS<T>,
|
iterable: JS<T>,
|
||||||
|
|
|
@ -220,8 +220,7 @@ impl CompiledEventListener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(JSTraceable, Clone, PartialEq, HeapSizeOf)]
|
#[derive(Clone, DenyPublicFields, HeapSizeOf, JSTraceable, PartialEq)]
|
||||||
#[privatize]
|
|
||||||
/// A listener in a collection of event listeners.
|
/// A listener in a collection of event listeners.
|
||||||
struct EventListenerEntry {
|
struct EventListenerEntry {
|
||||||
phase: ListenerPhase,
|
phase: ListenerPhase,
|
||||||
|
|
|
@ -929,10 +929,8 @@ impl RangeMethods for Range {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(JSTraceable)]
|
#[derive(DenyPublicFields, HeapSizeOf, JSTraceable)]
|
||||||
#[must_root]
|
#[must_root]
|
||||||
#[privatize]
|
|
||||||
#[derive(HeapSizeOf)]
|
|
||||||
pub struct BoundaryPoint {
|
pub struct BoundaryPoint {
|
||||||
node: MutJS<Node>,
|
node: MutJS<Node>,
|
||||||
offset: Cell<u32>,
|
offset: Cell<u32>,
|
||||||
|
|
|
@ -38,6 +38,8 @@ extern crate cookie as cookie_rs;
|
||||||
extern crate core;
|
extern crate core;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate cssparser;
|
extern crate cssparser;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate deny_public_fields;
|
||||||
extern crate devtools_traits;
|
extern crate devtools_traits;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate domobject_derive;
|
extern crate domobject_derive;
|
||||||
|
|
|
@ -28,8 +28,7 @@ use std::rc::Rc;
|
||||||
#[derive(JSTraceable, PartialEq, Eq, Copy, Clone, HeapSizeOf, Hash, PartialOrd, Ord, Debug)]
|
#[derive(JSTraceable, PartialEq, Eq, Copy, Clone, HeapSizeOf, Hash, PartialOrd, Ord, Debug)]
|
||||||
pub struct OneshotTimerHandle(i32);
|
pub struct OneshotTimerHandle(i32);
|
||||||
|
|
||||||
#[derive(JSTraceable, HeapSizeOf)]
|
#[derive(DenyPublicFields, HeapSizeOf, JSTraceable)]
|
||||||
#[privatize]
|
|
||||||
pub struct OneshotTimers {
|
pub struct OneshotTimers {
|
||||||
js_timers: JsTimers,
|
js_timers: JsTimers,
|
||||||
#[ignore_heap_size_of = "Defined in std"]
|
#[ignore_heap_size_of = "Defined in std"]
|
||||||
|
@ -53,8 +52,7 @@ pub struct OneshotTimers {
|
||||||
expected_event_id: Cell<TimerEventId>,
|
expected_event_id: Cell<TimerEventId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(JSTraceable, HeapSizeOf)]
|
#[derive(DenyPublicFields, HeapSizeOf, JSTraceable)]
|
||||||
#[privatize]
|
|
||||||
struct OneshotTimer {
|
struct OneshotTimer {
|
||||||
handle: OneshotTimerHandle,
|
handle: OneshotTimerHandle,
|
||||||
source: TimerSource,
|
source: TimerSource,
|
||||||
|
@ -302,8 +300,7 @@ impl OneshotTimers {
|
||||||
#[derive(JSTraceable, PartialEq, Eq, Copy, Clone, HeapSizeOf, Hash, PartialOrd, Ord)]
|
#[derive(JSTraceable, PartialEq, Eq, Copy, Clone, HeapSizeOf, Hash, PartialOrd, Ord)]
|
||||||
pub struct JsTimerHandle(i32);
|
pub struct JsTimerHandle(i32);
|
||||||
|
|
||||||
#[derive(JSTraceable, HeapSizeOf)]
|
#[derive(DenyPublicFields, HeapSizeOf, JSTraceable)]
|
||||||
#[privatize]
|
|
||||||
pub struct JsTimers {
|
pub struct JsTimers {
|
||||||
next_timer_handle: Cell<JsTimerHandle>,
|
next_timer_handle: Cell<JsTimerHandle>,
|
||||||
active_timers: DOMRefCell<HashMap<JsTimerHandle, JsTimerEntry>>,
|
active_timers: DOMRefCell<HashMap<JsTimerHandle, JsTimerEntry>>,
|
||||||
|
|
|
@ -11,5 +11,6 @@ doctest = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
compiletest_helper = {path = "../helper"}
|
compiletest_helper = {path = "../helper"}
|
||||||
|
deny_public_fields = {path = "../../../components/deny_public_fields"}
|
||||||
plugins = {path = "../../../components/plugins"}
|
plugins = {path = "../../../components/plugins"}
|
||||||
script = {path = "../../../components/script"}
|
script = {path = "../../../components/script"}
|
||||||
|
|
|
@ -2,14 +2,15 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
#![feature(plugin, custom_attribute)]
|
|
||||||
#![plugin(plugins)]
|
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
#[privatize]
|
#[macro_use]
|
||||||
|
extern crate deny_public_fields;
|
||||||
|
|
||||||
|
#[derive(DenyPublicFields)]
|
||||||
|
//~^ ERROR custom derive attribute panicked
|
||||||
struct Foo {
|
struct Foo {
|
||||||
pub v1: i32,
|
pub v1: i32,
|
||||||
//~^ ERROR Field v1 is public where only private fields are allowed
|
|
||||||
v2: i32
|
v2: i32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
extern crate compiletest_helper;
|
extern crate compiletest_helper;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate deny_public_fields;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn compile_test() {
|
fn compile_test() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue