Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upDon't generate useless InheritTypes interfaces #7735
Conversation
|
I fixed the setting of hasProxyDescendant. This flag shouldn't be set on the proxy itself. This led to CSSStyleDeclaration's upcasts to be generated even though they are useless. |
|
Reviewed 1 of 1 files at r1. components/script/dom/bindings/codegen/CodegenRust.py, line 5812 [r2] (raw file): descriptors = config.getDescriptors(register=True, isCallback=False)
allprotos = [CGGeneric("use dom::types::*;\n"),
CGGeneric("use dom::bindings::js::{JS, LayoutJS, Root};\n"),
CGGeneric("use dom::bindings::trace::JSTraceable;\n"),
CGGeneric("use dom::bindings::utils::Reflectable;\n"),
CGGeneric("use js::jsapi::JSTracer;\n\n"),
CGGeneric("use std::mem;\n\n")]
allprotos.extend(CGGeneric(conversion)
for conversion in conversions(descriptor)
for descriptor in descriptors)
curr = CGList(allprotos)
curr = CGWrapper(curr, pre=AUTOGENERATED_WARNING_COMMENT)
return curr
def conversions(descriptor):
name = descriptor.name
upcast = (descriptor.interface.getUserData("hasConcreteDescendant", False) or
descriptor.interface.getUserData("hasProxyDescendant", False))
downcast = len(descriptor.prototypeChain) > 1
if upcast or downcast:
yield "pub struct %sCast;\n" % name
chain = descriptor.prototypeChain
# Define a `FooBase` trait for subclasses to implement, as well as the
# `FooCast::from_*` methods that use it.
if upcast:
yield string.Template("""\
/// Types which are derived from `${name}` and can be freely converted
/// to `${name}`
pub trait ${fromBound} : Sized {}
impl ${name}Cast {
#[inline]
/// Upcast an instance of a derived class of `${name}` to `${name}`
pub fn from_ref<'a, T: ${fromBound}+Reflectable>(derived: &'a T) -> &'a ${name} {
unsafe { mem::transmute(derived) }
}
#[inline]
#[allow(unrooted_must_root)]
pub fn from_layout_js<T: ${fromBound}+Reflectable>(derived: &LayoutJS<T>) -> LayoutJS<${name}> {
unsafe { mem::transmute_copy(derived) }
}
#[inline]
pub fn from_root<T: ${fromBound}+Reflectable>(derived: Root<T>) -> Root<${name}> {
unsafe { mem::transmute(derived) }
}
}
""").substitute({'name': name, 'fromBound': name + 'Base'})
else:
# The `FooBase` trait is not defined, so avoid implementing it by
# removing `Foo` itself from the chain.
chain = chain[:-1]
# Implement `BarBase` for `Foo`, for all `Bar` that `Foo` inherits from.
for proto in chain:
yield 'impl %s for %s {}\n' % (proto + 'Base', descriptor.concreteType)
# Define a `FooDerived` trait for superclasses to implement, as well as the
# `FooCast::to_*` methods that use it.
if downcast:
yield string.Template("""\
/// Types which `${name}` derives from
pub trait ${toBound} : Sized { fn ${checkFn}(&self) -> bool; }
impl %{name}Cast {
#[inline]
/// Downcast an instance of a base class of `${name}` to an instance of
/// `${name}`, if it internally is an instance of `${name}`
pub fn to_ref<'a, T: ${toBound}+Reflectable>(base: &'a T) -> Option<&'a ${name}> {
match base.${checkFn}() {
true => Some(unsafe { mem::transmute(base) }),
false => None
}
}
#[inline]
#[allow(unrooted_must_root)]
pub fn to_layout_js<T: ${toBound}+Reflectable>(base: &LayoutJS<T>) -> Option<LayoutJS<${name}>> {
unsafe {
match (*base.unsafe_get()).${checkFn}() {
true => Some(mem::transmute_copy(base)),
false => None
}
}
}
#[inline]
pub fn to_root<T: ${toBound}+Reflectable>(base: Root<T>) -> Option<Root<${name}>> {
match base.r().${checkFn}() {
true => Some(unsafe { mem::transmute(base) }),
false => None
}
}
}
""").substitute({'checkFn': 'is_' + name.lower(), 'name': name, 'toBound': name + 'Derived'})
# Implement the `FooDerived` trait for non-root superclasses by deferring to
# the direct superclass. This leaves the implementation of the `FooDerived`
# trait for the root superclass to manual code. `FooDerived` is not
# implemented for `Foo` itself.
for protoName in descriptor.prototypeChain[1:-1]:
protoDescriptor = config.getDescriptor(protoName)
yield string.Template("""\
impl ${name}Derived for ${baseName} {
#[inline]
fn ${fname}(&self) -> bool {
let base: &${parentName} = ${parentName}Cast::from_ref(self);
base.${fname}()
}
}
""").substitute({'fname': 'is_' + name.lower(),
'name': name,
'baseName': protoDescriptor.concreteType,
'parentName': protoDescriptor.prototypeChain[-2]})How would you feel about me rewriting the code in this fashion, and letting you rebase on top of it? components/script/dom/bindings/codegen/CodegenRust.py, line 5904 [r2] (raw file): Comments from the review on Reviewable.io |
|
Review status: 1 of 2 files reviewed at latest revision, 2 unresolved discussions, all commit checks successful. components/script/dom/bindings/codegen/CodegenRust.py, line 5812 [r2] (raw file): Comments from the review on Reviewable.io |
|
Review status: 1 of 2 files reviewed at latest revision, 2 unresolved discussions, all commit checks successful. components/script/dom/bindings/codegen/CodegenRust.py, line 5812 [r2] (raw file):
It forbids me to build this dictionary while generating the plain old conversions functions, I mean. Comments from the review on Reviewable.io |
|
Review status: 1 of 2 files reviewed at latest revision, 2 unresolved discussions, all commit checks successful. components/script/dom/bindings/codegen/CodegenRust.py, line 5812 [r2] (raw file): Comments from the review on Reviewable.io |
Interfaces with no descendant need neither a Base trait nor upcast functions, and interfaces with no ancestors neither a Derived trait nor downcast functions.
|
@bors-servo r+ |
|
|
Don't generate useless InheritTypes interfaces Interfaces with no descendant need neither a Base trait nor upcast functions, and interfaces with no ancestors neither a Derived trait nor downcast functions. <!-- Reviewable:start --> [<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7735) <!-- Reviewable:end -->
|
|
|
@bors-servo retry p=1 |
Don't generate useless InheritTypes interfaces Interfaces with no descendant need neither a Base trait nor upcast functions, and interfaces with no ancestors neither a Derived trait nor downcast functions. <!-- Reviewable:start --> [<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7735) <!-- Reviewable:end -->
|
|
|
@bors-servo retry |
|
|
|
|
nox commentedSep 25, 2015
Interfaces with no descendant need neither a Base trait nor upcast functions, and interfaces with no ancestors neither a Derived trait nor downcast functions.