Skip to content

Commit 65f672c

Browse files
author
bors-servo
authored
Auto merge of rust-lang#640 - fitzgen:stylo-stuff, r=emilio
More fixes for Stylo See each commit message for details. rust-lang@bce1330 is the big correctness fix for a bug we were hitting in stylo. r? @emilio
2 parents c6a1e00 + 9689db6 commit 65f672c

11 files changed

+406
-267
lines changed

src/codegen/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use ir::item_kind::ItemKind;
2121
use ir::layout::Layout;
2222
use ir::module::Module;
2323
use ir::objc::{ObjCInterface, ObjCMethod};
24-
use ir::template::{AsNamed, TemplateInstantiation};
25-
use ir::ty::{TemplateDeclaration, Type, TypeKind};
24+
use ir::template::{AsNamed, TemplateInstantiation, TemplateParameters};
25+
use ir::ty::{Type, TypeKind};
2626
use ir::var::Var;
2727

2828
use std::borrow::Cow;
@@ -2647,8 +2647,10 @@ impl TryToRustTy for TemplateInstantiation {
26472647
// This can happen if we generated an opaque type for a partial
26482648
// template specialization, and we've hit an instantiation of
26492649
// that partial specialization.
2650-
extra_assert!(ctx.resolve_type_through_type_refs(decl)
2651-
.is_opaque());
2650+
extra_assert!(decl.into_resolver()
2651+
.through_type_refs()
2652+
.resolve(ctx)
2653+
.is_opaque(ctx));
26522654
return Err(error::Error::InstantiationOfOpaqueType);
26532655
}
26542656
};

src/ir/comp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::derive::{CanDeriveCopy, CanDeriveDebug, CanDeriveDefault};
66
use super::item::Item;
77
use super::layout::Layout;
88
use super::traversal::{EdgeKind, Trace, Tracer};
9-
use super::ty::TemplateDeclaration;
9+
use super::template::TemplateParameters;
1010
use clang;
1111
use parse::{ClangItemParser, ParseError};
1212
use std::cell::Cell;
@@ -812,7 +812,7 @@ impl CompInfo {
812812
}
813813
}
814814

815-
impl TemplateDeclaration for CompInfo {
815+
impl TemplateParameters for CompInfo {
816816
fn self_template_params(&self,
817817
_ctx: &BindgenContext)
818818
-> Option<Vec<ItemId>> {

src/ir/context.rs

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use super::item::{Item, ItemCanonicalPath, ItemSet};
66
use super::item_kind::ItemKind;
77
use super::module::{Module, ModuleKind};
88
use super::named::{UsedTemplateParameters, analyze};
9-
use super::template::TemplateInstantiation;
9+
use super::template::{TemplateInstantiation, TemplateParameters};
1010
use super::traversal::{self, Edge, ItemTraversal};
11-
use super::ty::{FloatKind, TemplateDeclaration, Type, TypeKind};
11+
use super::ty::{FloatKind, Type, TypeKind};
1212
use BindgenOptions;
1313
use cexpr;
1414
use callbacks::ParseCallbacks;
@@ -712,21 +712,6 @@ impl<'ctx> BindgenContext<'ctx> {
712712
}
713713
}
714714

715-
/// Resolve the given `ItemId` into a `Type`, and keep doing so while we see
716-
/// `ResolvedTypeRef`s to other items until we get to the final `Type`.
717-
pub fn resolve_type_through_type_refs(&self, item_id: ItemId) -> &Type {
718-
assert!(self.collected_typerefs());
719-
720-
let mut id = item_id;
721-
loop {
722-
let ty = self.resolve_type(id);
723-
match *ty.kind() {
724-
TypeKind::ResolvedTypeRef(next_id) => id = next_id,
725-
_ => return ty,
726-
}
727-
}
728-
}
729-
730715
/// Get the current module.
731716
pub fn current_module(&self) -> ItemId {
732717
self.current_module
@@ -1420,6 +1405,73 @@ impl<'ctx> BindgenContext<'ctx> {
14201405
}
14211406
}
14221407

1408+
/// A builder struct for configuring item resolution options.
1409+
#[derive(Debug, Copy, Clone)]
1410+
pub struct ItemResolver {
1411+
id: ItemId,
1412+
through_type_refs: bool,
1413+
through_type_aliases: bool,
1414+
}
1415+
1416+
impl ItemId {
1417+
/// Create an `ItemResolver` from this item id.
1418+
pub fn into_resolver(self) -> ItemResolver {
1419+
self.into()
1420+
}
1421+
}
1422+
1423+
impl From<ItemId> for ItemResolver {
1424+
fn from(id: ItemId) -> ItemResolver {
1425+
ItemResolver::new(id)
1426+
}
1427+
}
1428+
1429+
impl ItemResolver {
1430+
/// Construct a new `ItemResolver` from the given id.
1431+
pub fn new(id: ItemId) -> ItemResolver {
1432+
ItemResolver {
1433+
id: id,
1434+
through_type_refs: false,
1435+
through_type_aliases: false,
1436+
}
1437+
}
1438+
1439+
/// Keep resolving through `Type::TypeRef` items.
1440+
pub fn through_type_refs(mut self) -> ItemResolver {
1441+
self.through_type_refs = true;
1442+
self
1443+
}
1444+
1445+
/// Keep resolving through `Type::Alias` items.
1446+
pub fn through_type_aliases(mut self) -> ItemResolver {
1447+
self.through_type_aliases = true;
1448+
self
1449+
}
1450+
1451+
/// Finish configuring and perform the actual item resolution.
1452+
pub fn resolve<'a, 'b>(self, ctx: &'a BindgenContext<'b>) -> &'a Item {
1453+
assert!(ctx.collected_typerefs());
1454+
1455+
let mut id = self.id;
1456+
loop {
1457+
let item = ctx.resolve_item(id);
1458+
let ty_kind = item.as_type().map(|t| t.kind());
1459+
match ty_kind {
1460+
Some(&TypeKind::ResolvedTypeRef(next_id)) if self.through_type_refs => {
1461+
id = next_id;
1462+
}
1463+
// We intentionally ignore template aliases here, as they are
1464+
// more complicated, and don't represent a simple renaming of
1465+
// some type.
1466+
Some(&TypeKind::Alias(next_id)) if self.through_type_aliases => {
1467+
id = next_id;
1468+
}
1469+
_ => return item,
1470+
}
1471+
}
1472+
}
1473+
}
1474+
14231475
/// A type that we are in the middle of parsing.
14241476
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14251477
pub struct PartialType {
@@ -1449,7 +1501,7 @@ impl PartialType {
14491501
}
14501502
}
14511503

1452-
impl TemplateDeclaration for PartialType {
1504+
impl TemplateParameters for PartialType {
14531505
fn self_template_params(&self,
14541506
_ctx: &BindgenContext)
14551507
-> Option<Vec<ItemId>> {

src/ir/item.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use super::function::Function;
88
use super::item_kind::ItemKind;
99
use super::layout::Opaque;
1010
use super::module::Module;
11-
use super::template::AsNamed;
11+
use super::template::{AsNamed, TemplateParameters};
1212
use super::traversal::{EdgeKind, Trace, Tracer};
13-
use super::ty::{TemplateDeclaration, Type, TypeKind};
13+
use super::ty::{Type, TypeKind};
1414
use clang;
1515
use clang_sys;
1616
use parse::{ClangItemParser, ClangSubItemParser, ParseError, ParseResult};
@@ -830,7 +830,7 @@ impl DotAttributes for Item {
830830
}
831831
}
832832

833-
impl TemplateDeclaration for ItemId {
833+
impl TemplateParameters for ItemId {
834834
fn self_template_params(&self,
835835
ctx: &BindgenContext)
836836
-> Option<Vec<ItemId>> {
@@ -839,15 +839,15 @@ impl TemplateDeclaration for ItemId {
839839
}
840840
}
841841

842-
impl TemplateDeclaration for Item {
842+
impl TemplateParameters for Item {
843843
fn self_template_params(&self,
844844
ctx: &BindgenContext)
845845
-> Option<Vec<ItemId>> {
846846
self.kind.self_template_params(ctx)
847847
}
848848
}
849849

850-
impl TemplateDeclaration for ItemKind {
850+
impl TemplateParameters for ItemKind {
851851
fn self_template_params(&self,
852852
ctx: &BindgenContext)
853853
-> Option<Vec<ItemId>> {

0 commit comments

Comments
 (0)