Lifted generics into TraitItem and ImplItem from MethodSig -- HIR now matches AST

This commit is contained in:
Sunjay Varma 2017-09-25 17:24:20 -04:00
parent 892e4689ef
commit bb30144605
9 changed files with 42 additions and 71 deletions

View file

@ -780,9 +780,7 @@ pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'
FnKind::ItemFn(_, generics, ..) => {
visitor.visit_generics(generics);
}
FnKind::Method(_, sig, ..) => {
visitor.visit_generics(&sig.generics);
}
FnKind::Method(..) |
FnKind::Closure(_) => {}
}
}
@ -802,6 +800,7 @@ pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
visitor.visit_name(trait_item.span, trait_item.name);
walk_list!(visitor, visit_attribute, &trait_item.attrs);
visitor.visit_generics(&trait_item.generics);
match trait_item.node {
TraitItemKind::Const(ref ty, default) => {
visitor.visit_id(trait_item.id);
@ -810,7 +809,6 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai
}
TraitItemKind::Method(ref sig, TraitMethod::Required(ref names)) => {
visitor.visit_id(trait_item.id);
visitor.visit_generics(&sig.generics);
visitor.visit_fn_decl(&sig.decl);
for name in names {
visitor.visit_name(name.span, name.node);
@ -852,6 +850,7 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
ref vis,
ref defaultness,
ref attrs,
ref generics,
ref node,
span
} = *impl_item;
@ -860,6 +859,7 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
visitor.visit_vis(vis);
visitor.visit_defaultness(defaultness);
walk_list!(visitor, visit_attribute, attrs);
visitor.visit_generics(generics);
match *node {
ImplItemKind::Const(ref ty, body) => {
visitor.visit_id(impl_item.id);

View file

@ -1539,6 +1539,7 @@ impl<'a> LoweringContext<'a> {
hir_id,
name: this.lower_ident(i.ident),
attrs: this.lower_attrs(&i.attrs),
generics: this.lower_generics(&i.generics),
node: match i.node {
TraitItemKind::Const(ref ty, ref default) => {
hir::TraitItemKind::Const(this.lower_ty(ty),
@ -1548,7 +1549,7 @@ impl<'a> LoweringContext<'a> {
}
TraitItemKind::Method(ref sig, None) => {
let names = this.lower_fn_args_to_names(&sig.decl);
hir::TraitItemKind::Method(this.lower_method_sig(&i.generics, sig),
hir::TraitItemKind::Method(this.lower_method_sig(sig),
hir::TraitMethod::Required(names))
}
TraitItemKind::Method(ref sig, Some(ref body)) => {
@ -1556,7 +1557,7 @@ impl<'a> LoweringContext<'a> {
let body = this.lower_block(body, false);
this.expr_block(body, ThinVec::new())
});
hir::TraitItemKind::Method(this.lower_method_sig(&i.generics, sig),
hir::TraitItemKind::Method(this.lower_method_sig(sig),
hir::TraitMethod::Provided(body_id))
}
TraitItemKind::Type(ref bounds, ref default) => {
@ -1603,6 +1604,7 @@ impl<'a> LoweringContext<'a> {
hir_id,
name: this.lower_ident(i.ident),
attrs: this.lower_attrs(&i.attrs),
generics: this.lower_generics(&i.generics),
vis: this.lower_visibility(&i.vis, None),
defaultness: this.lower_defaultness(i.defaultness, true /* [1] */),
node: match i.node {
@ -1615,7 +1617,7 @@ impl<'a> LoweringContext<'a> {
let body = this.lower_block(body, false);
this.expr_block(body, ThinVec::new())
});
hir::ImplItemKind::Method(this.lower_method_sig(&i.generics, sig), body_id)
hir::ImplItemKind::Method(this.lower_method_sig(sig), body_id)
}
ImplItemKind::Type(ref ty) => hir::ImplItemKind::Type(this.lower_ty(ty)),
ImplItemKind::Macro(..) => panic!("Shouldn't exist any more"),
@ -1727,9 +1729,8 @@ impl<'a> LoweringContext<'a> {
})
}
fn lower_method_sig(&mut self, generics: &Generics, sig: &MethodSig) -> hir::MethodSig {
fn lower_method_sig(&mut self, sig: &MethodSig) -> hir::MethodSig {
hir::MethodSig {
generics: self.lower_generics(generics),
abi: sig.abi,
unsafety: self.lower_unsafety(sig.unsafety),
constness: self.lower_constness(sig.constness),

View file

@ -1295,7 +1295,6 @@ pub struct MethodSig {
pub constness: Constness,
pub abi: Abi,
pub decl: P<FnDecl>,
pub generics: Generics,
}
// The bodies for items are stored "out of line", in a separate
@ -1316,6 +1315,7 @@ pub struct TraitItem {
pub name: Name,
pub hir_id: HirId,
pub attrs: HirVec<Attribute>,
pub generics: Generics,
pub node: TraitItemKind,
pub span: Span,
}
@ -1360,6 +1360,7 @@ pub struct ImplItem {
pub vis: Visibility,
pub defaultness: Defaultness,
pub attrs: HirVec<Attribute>,
pub generics: Generics,
pub node: ImplItemKind,
pub span: Span,
}

View file

@ -880,6 +880,7 @@ impl<'a> State<'a> {
pub fn print_method_sig(&mut self,
name: ast::Name,
m: &hir::MethodSig,
generics: &hir::Generics,
vis: &hir::Visibility,
arg_names: &[Spanned<ast::Name>],
body_id: Option<hir::BodyId>)
@ -889,7 +890,7 @@ impl<'a> State<'a> {
m.constness,
m.abi,
Some(name),
&m.generics,
generics,
vis,
arg_names,
body_id)
@ -905,12 +906,12 @@ impl<'a> State<'a> {
self.print_associated_const(ti.name, &ty, default, &hir::Inherited)?;
}
hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Required(ref arg_names)) => {
self.print_method_sig(ti.name, sig, &hir::Inherited, arg_names, None)?;
self.print_method_sig(ti.name, sig, &ti.generics, &hir::Inherited, arg_names, None)?;
self.s.word(";")?;
}
hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Provided(body)) => {
self.head("")?;
self.print_method_sig(ti.name, sig, &hir::Inherited, &[], Some(body))?;
self.print_method_sig(ti.name, sig, &ti.generics, &hir::Inherited, &[], Some(body))?;
self.nbsp()?;
self.end()?; // need to close a box
self.end()?; // need to close a box
@ -938,7 +939,7 @@ impl<'a> State<'a> {
}
hir::ImplItemKind::Method(ref sig, body) => {
self.head("")?;
self.print_method_sig(ii.name, sig, &ii.vis, &[], Some(body))?;
self.print_method_sig(ii.name, sig, &ii.generics, &ii.vis, &[], Some(body))?;
self.nbsp()?;
self.end()?; // need to close a box
self.end()?; // need to close a box

View file

@ -232,8 +232,7 @@ impl_stable_hash_for!(struct hir::MethodSig {
unsafety,
constness,
abi,
decl,
generics
decl
});
impl_stable_hash_for!(struct hir::TypeBinding {
@ -709,6 +708,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for hir::TraitItem {
hir_id: _,
name,
ref attrs,
ref generics,
ref node,
span
} = *self;
@ -716,6 +716,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for hir::TraitItem {
hcx.hash_hir_item_like(attrs, |hcx| {
name.hash_stable(hcx, hasher);
attrs.hash_stable(hcx, hasher);
generics.hash_stable(hcx, hasher);
node.hash_stable(hcx, hasher);
span.hash_stable(hcx, hasher);
});
@ -744,6 +745,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for hir::ImplItem {
ref vis,
defaultness,
ref attrs,
ref generics,
ref node,
span
} = *self;
@ -753,6 +755,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for hir::ImplItem {
vis.hash_stable(hcx, hasher);
defaultness.hash_stable(hcx, hasher);
attrs.hash_stable(hcx, hasher);
generics.hash_stable(hcx, hasher);
node.hash_stable(hcx, hasher);
span.hash_stable(hcx, hasher);
});

View file

@ -58,11 +58,10 @@ fn item_might_be_inlined(item: &hir::Item) -> bool {
}
fn method_might_be_inlined<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
sig: &hir::MethodSig,
impl_item: &hir::ImplItem,
impl_src: DefId) -> bool {
if attr::requests_inline(&impl_item.attrs) ||
generics_require_inlining(&sig.generics) {
generics_require_inlining(&impl_item.generics) {
return true
}
if let Some(impl_node_id) = tcx.hir.as_local_node_id(impl_src) {
@ -176,8 +175,8 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
Some(hir_map::NodeImplItem(impl_item)) => {
match impl_item.node {
hir::ImplItemKind::Const(..) => true,
hir::ImplItemKind::Method(ref sig, _) => {
if generics_require_inlining(&sig.generics) ||
hir::ImplItemKind::Method(..) => {
if generics_require_inlining(&impl_item.generics) ||
attr::requests_inline(&impl_item.attrs) {
true
} else {
@ -293,9 +292,9 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
hir::ImplItemKind::Const(_, body) => {
self.visit_nested_body(body);
}
hir::ImplItemKind::Method(ref sig, body) => {
hir::ImplItemKind::Method(_, body) => {
let did = self.tcx.hir.get_parent_did(search_item);
if method_might_be_inlined(self.tcx, sig, impl_item, did) {
if method_might_be_inlined(self.tcx, impl_item, did) {
self.visit_nested_body(body)
}
}

View file

@ -412,7 +412,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
if let hir::TraitItemKind::Method(ref sig, _) = trait_item.node {
self.visit_early_late(
Some(self.hir_map.get_parent(trait_item.id)),
&sig.decl, &sig.generics,
&sig.decl, &trait_item.generics,
|this| intravisit::walk_trait_item(this, trait_item))
} else {
intravisit::walk_trait_item(self, trait_item);
@ -423,7 +423,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
if let hir::ImplItemKind::Method(ref sig, _) = impl_item.node {
self.visit_early_late(
Some(self.hir_map.get_parent(impl_item.id)),
&sig.decl, &sig.generics,
&sig.decl, &impl_item.generics,
|this| intravisit::walk_impl_item(this, impl_item))
} else {
intravisit::walk_impl_item(self, impl_item);

View file

@ -568,15 +568,11 @@ fn compare_number_of_generics<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let num_trait_m_type_params = trait_m_generics.types.len();
if num_impl_m_type_params != num_trait_m_type_params {
let impl_m_node_id = tcx.hir.as_local_node_id(impl_m.def_id).unwrap();
let span = match tcx.hir.expect_impl_item(impl_m_node_id).node {
ImplItemKind::Method(ref impl_m_sig, _) => {
if impl_m_sig.generics.is_parameterized() {
impl_m_sig.generics.span
} else {
impl_m_span
}
}
_ => bug!("{:?} is not a method", impl_m),
let impl_m_item = tcx.hir.expect_impl_item(impl_m_node_id);
let span = if impl_m_item.generics.is_parameterized() {
impl_m_item.generics.span
} else {
impl_m_span
};
let mut err = struct_span_err!(tcx.sess,

View file

@ -261,19 +261,9 @@ fn type_param_predicates<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let item_node_id = tcx.hir.as_local_node_id(item_def_id).unwrap();
let ast_generics = match tcx.hir.get(item_node_id) {
NodeTraitItem(item) => {
match item.node {
TraitItemKind::Method(ref sig, _) => &sig.generics,
_ => return result
}
}
NodeTraitItem(item) => &item.generics,
NodeImplItem(item) => {
match item.node {
ImplItemKind::Method(ref sig, _) => &sig.generics,
_ => return result
}
}
NodeImplItem(item) => &item.generics,
NodeItem(item) => {
match item.node {
@ -818,12 +808,12 @@ fn has_late_bound_regions<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
match node {
hir_map::NodeTraitItem(item) => match item.node {
hir::TraitItemKind::Method(ref sig, _) =>
has_late_bound_regions(tcx, &sig.generics, &sig.decl),
has_late_bound_regions(tcx, &item.generics, &sig.decl),
_ => None,
},
hir_map::NodeImplItem(item) => match item.node {
hir::ImplItemKind::Method(ref sig, _) =>
has_late_bound_regions(tcx, &sig.generics, &sig.decl),
has_late_bound_regions(tcx, &item.generics, &sig.decl),
_ => None,
},
hir_map::NodeForeignItem(item) => match item.node {
@ -881,19 +871,9 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let no_generics = hir::Generics::empty();
let ast_generics = match node {
NodeTraitItem(item) => {
match item.node {
TraitItemKind::Method(ref sig, _) => &sig.generics,
_ => &no_generics
}
}
NodeTraitItem(item) => &item.generics,
NodeImplItem(item) => {
match item.node {
ImplItemKind::Method(ref sig, _) => &sig.generics,
_ => &no_generics
}
}
NodeImplItem(item) => &item.generics,
NodeItem(item) => {
match item.node {
@ -1353,19 +1333,9 @@ fn explicit_predicates_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let icx = ItemCtxt::new(tcx, def_id);
let no_generics = hir::Generics::empty();
let ast_generics = match node {
NodeTraitItem(item) => {
match item.node {
TraitItemKind::Method(ref sig, _) => &sig.generics,
_ => &no_generics
}
}
NodeTraitItem(item) => &item.generics,
NodeImplItem(item) => {
match item.node {
ImplItemKind::Method(ref sig, _) => &sig.generics,
_ => &no_generics
}
}
NodeImplItem(item) => &item.generics,
NodeItem(item) => {
match item.node {