Make hir().get_generics and generics_of consistent.
This commit is contained in:
parent
7e9b92cb43
commit
623ea5f5d2
3 changed files with 23 additions and 71 deletions
|
@ -3004,13 +3004,12 @@ impl ItemKind<'_> {
|
|||
Some(match *self {
|
||||
ItemKind::Fn(_, ref generics, _)
|
||||
| ItemKind::TyAlias(_, ref generics)
|
||||
| ItemKind::OpaqueTy(OpaqueTy {
|
||||
ref generics, origin: OpaqueTyOrigin::TyAlias, ..
|
||||
})
|
||||
| ItemKind::OpaqueTy(OpaqueTy { ref generics, .. })
|
||||
| ItemKind::Enum(_, ref generics)
|
||||
| ItemKind::Struct(_, ref generics)
|
||||
| ItemKind::Union(_, ref generics)
|
||||
| ItemKind::Trait(_, _, ref generics, _, _)
|
||||
| ItemKind::TraitAlias(ref generics, _)
|
||||
| ItemKind::Impl(Impl { ref generics, .. }) => generics,
|
||||
_ => return None,
|
||||
})
|
||||
|
@ -3210,13 +3209,8 @@ impl<'hir> OwnerNode<'hir> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn generics(&self) -> Option<&'hir Generics<'hir>> {
|
||||
match self {
|
||||
OwnerNode::TraitItem(TraitItem { generics, .. })
|
||||
| OwnerNode::ImplItem(ImplItem { generics, .. }) => Some(generics),
|
||||
OwnerNode::Item(item) => item.kind.generics(),
|
||||
_ => None,
|
||||
}
|
||||
pub fn generics(self) -> Option<&'hir Generics<'hir>> {
|
||||
Node::generics(self.into())
|
||||
}
|
||||
|
||||
pub fn def_id(self) -> LocalDefId {
|
||||
|
@ -3403,9 +3397,12 @@ impl<'hir> Node<'hir> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn generics(&self) -> Option<&'hir Generics<'hir>> {
|
||||
pub fn generics(self) -> Option<&'hir Generics<'hir>> {
|
||||
match self {
|
||||
Node::TraitItem(TraitItem { generics, .. })
|
||||
Node::ForeignItem(ForeignItem {
|
||||
kind: ForeignItemKind::Fn(_, _, generics), ..
|
||||
})
|
||||
| Node::TraitItem(TraitItem { generics, .. })
|
||||
| Node::ImplItem(ImplItem { generics, .. }) => Some(generics),
|
||||
Node::Item(item) => item.kind.generics(),
|
||||
_ => None,
|
||||
|
|
|
@ -361,27 +361,7 @@ impl<'hir> Map<'hir> {
|
|||
|
||||
pub fn get_generics(self, id: LocalDefId) -> Option<&'hir Generics<'hir>> {
|
||||
let node = self.tcx.hir_owner(id)?;
|
||||
match node.node {
|
||||
OwnerNode::ImplItem(impl_item) => Some(&impl_item.generics),
|
||||
OwnerNode::TraitItem(trait_item) => Some(&trait_item.generics),
|
||||
OwnerNode::ForeignItem(ForeignItem {
|
||||
kind: ForeignItemKind::Fn(_, _, generics),
|
||||
..
|
||||
})
|
||||
| OwnerNode::Item(Item {
|
||||
kind:
|
||||
ItemKind::Fn(_, generics, _)
|
||||
| ItemKind::TyAlias(_, generics)
|
||||
| ItemKind::Enum(_, generics)
|
||||
| ItemKind::Struct(_, generics)
|
||||
| ItemKind::Union(_, generics)
|
||||
| ItemKind::Trait(_, _, generics, ..)
|
||||
| ItemKind::TraitAlias(generics, _)
|
||||
| ItemKind::Impl(Impl { generics, .. }),
|
||||
..
|
||||
}) => Some(generics),
|
||||
_ => None,
|
||||
}
|
||||
node.node.generics()
|
||||
}
|
||||
|
||||
pub fn item(self, id: ItemId) -> &'hir Item<'hir> {
|
||||
|
|
|
@ -1588,41 +1588,20 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
|
|||
_ => None,
|
||||
};
|
||||
|
||||
let mut opt_self = None;
|
||||
let mut allow_defaults = false;
|
||||
|
||||
let no_generics = hir::Generics::empty();
|
||||
let ast_generics = match node {
|
||||
Node::TraitItem(item) => &item.generics,
|
||||
|
||||
Node::ImplItem(item) => &item.generics,
|
||||
|
||||
let ast_generics = node.generics().unwrap_or(&no_generics);
|
||||
let (opt_self, allow_defaults) = match node {
|
||||
Node::Item(item) => {
|
||||
match item.kind {
|
||||
ItemKind::Fn(.., ref generics, _)
|
||||
| ItemKind::Impl(hir::Impl { ref generics, .. }) => generics,
|
||||
|
||||
ItemKind::TyAlias(_, ref generics)
|
||||
| ItemKind::Enum(_, ref generics)
|
||||
| ItemKind::Struct(_, ref generics)
|
||||
| ItemKind::OpaqueTy(hir::OpaqueTy { ref generics, .. })
|
||||
| ItemKind::Union(_, ref generics) => {
|
||||
allow_defaults = true;
|
||||
generics
|
||||
}
|
||||
|
||||
ItemKind::Trait(_, _, ref generics, ..)
|
||||
| ItemKind::TraitAlias(ref generics, ..) => {
|
||||
ItemKind::Trait(..) | ItemKind::TraitAlias(..) => {
|
||||
// Add in the self type parameter.
|
||||
//
|
||||
// Something of a hack: use the node id for the trait, also as
|
||||
// the node id for the Self type parameter.
|
||||
let param_id = item.def_id;
|
||||
|
||||
opt_self = Some(ty::GenericParamDef {
|
||||
let opt_self = Some(ty::GenericParamDef {
|
||||
index: 0,
|
||||
name: kw::SelfUpper,
|
||||
def_id: param_id.to_def_id(),
|
||||
def_id,
|
||||
pure_wrt_drop: false,
|
||||
kind: ty::GenericParamDefKind::Type {
|
||||
has_default: false,
|
||||
|
@ -1631,21 +1610,17 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
|
|||
},
|
||||
});
|
||||
|
||||
allow_defaults = true;
|
||||
generics
|
||||
(opt_self, true)
|
||||
}
|
||||
|
||||
_ => &no_generics,
|
||||
ItemKind::TyAlias(..)
|
||||
| ItemKind::Enum(..)
|
||||
| ItemKind::Struct(..)
|
||||
| ItemKind::OpaqueTy(..)
|
||||
| ItemKind::Union(..) => (None, true),
|
||||
_ => (None, false),
|
||||
}
|
||||
}
|
||||
|
||||
Node::ForeignItem(item) => match item.kind {
|
||||
ForeignItemKind::Static(..) => &no_generics,
|
||||
ForeignItemKind::Fn(_, _, ref generics) => generics,
|
||||
ForeignItemKind::Type => &no_generics,
|
||||
},
|
||||
|
||||
_ => &no_generics,
|
||||
_ => (None, false),
|
||||
};
|
||||
|
||||
let has_self = opt_self.is_some();
|
||||
|
|
Loading…
Add table
Reference in a new issue