1
Fork 0

rustc: Harmonize DefKind and DefPathData

`DefPathData::(ClosureExpr,ImplTrait)` are renamed to match `DefKind::(Closure,OpaqueTy)`.

`DefPathData::ImplTraitAssocTy` is replaced with `DefPathData::TypeNS(kw::Empty)` because both correspond to `DefKind::AssocTy`.
It's possible that introducing `(DefKind,DefPathData)::AssocOpaqueTy` could be a better solution, but that would be a much more invasive change.

Const generic parameters introduced for effects are moved from `DefPathData::TypeNS` to `DefPathData::ValueNS`, because constants are values.

`DefPathData` is no longer passed to `create_def` functions to avoid redundancy.
This commit is contained in:
Vadim Petrochenkov 2023-12-03 12:29:59 +03:00
parent 7ceaf19868
commit 17e799c270
17 changed files with 133 additions and 132 deletions

View file

@ -4,9 +4,8 @@ use rustc_ast::*;
use rustc_expand::expand::AstFragment;
use rustc_hir::def::{CtorKind, CtorOf, DefKind};
use rustc_hir::def_id::LocalDefId;
use rustc_hir::definitions::*;
use rustc_span::hygiene::LocalExpnId;
use rustc_span::symbol::sym;
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::Span;
pub(crate) fn collect_definitions(
@ -30,16 +29,19 @@ impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> {
fn create_def(
&mut self,
node_id: NodeId,
data: DefPathData,
name: Symbol,
def_kind: DefKind,
span: Span,
) -> LocalDefId {
let parent_def = self.parent_def;
debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
debug!(
"create_def(node_id={:?}, def_kind={:?}, parent_def={:?})",
node_id, def_kind, parent_def
);
self.resolver.create_def(
parent_def,
node_id,
data,
name,
def_kind,
self.expansion.to_expn_id(),
span.with_parent(None),
@ -76,8 +78,7 @@ impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> {
self.visit_macro_invoc(field.id);
} else {
let name = field.ident.map_or_else(|| sym::integer(index(self)), |ident| ident.name);
let def =
self.create_def(field.id, DefPathData::ValueNs(name), DefKind::Field, field.span);
let def = self.create_def(field.id, name, DefKind::Field, field.span);
self.with_parent(def, |this| visit::walk_field_def(this, field));
}
}
@ -97,40 +98,36 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
// Pick the def data. This need not be unique, but the more
// information we encapsulate into, the better
let mut opt_macro_data = None;
let ty_data = DefPathData::TypeNs(i.ident.name);
let value_data = DefPathData::ValueNs(i.ident.name);
let (def_data, def_kind) = match &i.kind {
ItemKind::Impl(i) => {
(DefPathData::Impl, DefKind::Impl { of_trait: i.of_trait.is_some() })
}
ItemKind::ForeignMod(..) => (DefPathData::ForeignMod, DefKind::ForeignMod),
ItemKind::Mod(..) => (ty_data, DefKind::Mod),
ItemKind::Trait(..) => (ty_data, DefKind::Trait),
ItemKind::TraitAlias(..) => (ty_data, DefKind::TraitAlias),
ItemKind::Enum(..) => (ty_data, DefKind::Enum),
ItemKind::Struct(..) => (ty_data, DefKind::Struct),
ItemKind::Union(..) => (ty_data, DefKind::Union),
ItemKind::ExternCrate(..) => (ty_data, DefKind::ExternCrate),
ItemKind::TyAlias(..) => (ty_data, DefKind::TyAlias),
ItemKind::Static(s) => (value_data, DefKind::Static(s.mutability)),
ItemKind::Const(..) => (value_data, DefKind::Const),
ItemKind::Fn(..) => (value_data, DefKind::Fn),
let def_kind = match &i.kind {
ItemKind::Impl(i) => DefKind::Impl { of_trait: i.of_trait.is_some() },
ItemKind::ForeignMod(..) => DefKind::ForeignMod,
ItemKind::Mod(..) => DefKind::Mod,
ItemKind::Trait(..) => DefKind::Trait,
ItemKind::TraitAlias(..) => DefKind::TraitAlias,
ItemKind::Enum(..) => DefKind::Enum,
ItemKind::Struct(..) => DefKind::Struct,
ItemKind::Union(..) => DefKind::Union,
ItemKind::ExternCrate(..) => DefKind::ExternCrate,
ItemKind::TyAlias(..) => DefKind::TyAlias,
ItemKind::Static(s) => DefKind::Static(s.mutability),
ItemKind::Const(..) => DefKind::Const,
ItemKind::Fn(..) => DefKind::Fn,
ItemKind::MacroDef(..) => {
let macro_data = self.resolver.compile_macro(i, self.resolver.tcx.sess.edition());
let macro_kind = macro_data.ext.macro_kind();
opt_macro_data = Some(macro_data);
(DefPathData::MacroNs(i.ident.name), DefKind::Macro(macro_kind))
DefKind::Macro(macro_kind)
}
ItemKind::MacCall(..) => {
visit::walk_item(self, i);
return self.visit_macro_invoc(i.id);
}
ItemKind::GlobalAsm(..) => (DefPathData::GlobalAsm, DefKind::GlobalAsm),
ItemKind::GlobalAsm(..) => DefKind::GlobalAsm,
ItemKind::Use(..) => {
return visit::walk_item(self, i);
}
};
let def_id = self.create_def(i.id, def_data, def_kind, i.span);
let def_id = self.create_def(i.id, i.ident.name, def_kind, i.span);
if let Some(macro_data) = opt_macro_data {
self.resolver.macro_map.insert(def_id.to_def_id(), macro_data);
@ -144,7 +141,7 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(struct_def) {
this.create_def(
ctor_node_id,
DefPathData::Ctor,
kw::Empty,
DefKind::Ctor(CtorOf::Struct, ctor_kind),
i.span,
);
@ -174,12 +171,8 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
// then the closure_def will never be used, and we should avoid generating a
// def-id for it.
if let Some(body) = body {
let closure_def = self.create_def(
closure_id,
DefPathData::ClosureExpr,
DefKind::Closure,
span,
);
let closure_def =
self.create_def(closure_id, kw::Empty, DefKind::Closure, span);
self.with_parent(closure_def, |this| this.visit_block(body));
}
return;
@ -190,21 +183,19 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
}
fn visit_use_tree(&mut self, use_tree: &'a UseTree, id: NodeId, _nested: bool) {
self.create_def(id, DefPathData::Use, DefKind::Use, use_tree.span);
self.create_def(id, kw::Empty, DefKind::Use, use_tree.span);
visit::walk_use_tree(self, use_tree, id);
}
fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
let (def_data, def_kind) = match fi.kind {
ForeignItemKind::Static(_, mt, _) => {
(DefPathData::ValueNs(fi.ident.name), DefKind::Static(mt))
}
ForeignItemKind::Fn(_) => (DefPathData::ValueNs(fi.ident.name), DefKind::Fn),
ForeignItemKind::TyAlias(_) => (DefPathData::TypeNs(fi.ident.name), DefKind::ForeignTy),
let def_kind = match fi.kind {
ForeignItemKind::Static(_, mt, _) => DefKind::Static(mt),
ForeignItemKind::Fn(_) => DefKind::Fn,
ForeignItemKind::TyAlias(_) => DefKind::ForeignTy,
ForeignItemKind::MacCall(_) => return self.visit_macro_invoc(fi.id),
};
let def = self.create_def(fi.id, def_data, def_kind, fi.span);
let def = self.create_def(fi.id, fi.ident.name, def_kind, fi.span);
self.with_parent(def, |this| visit::walk_foreign_item(this, fi));
}
@ -213,13 +204,12 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
if v.is_placeholder {
return self.visit_macro_invoc(v.id);
}
let def =
self.create_def(v.id, DefPathData::TypeNs(v.ident.name), DefKind::Variant, v.span);
let def = self.create_def(v.id, v.ident.name, DefKind::Variant, v.span);
self.with_parent(def, |this| {
if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(&v.data) {
this.create_def(
ctor_node_id,
DefPathData::Ctor,
kw::Empty,
DefKind::Ctor(CtorOf::Variant, ctor_kind),
v.span,
);
@ -242,15 +232,12 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
self.visit_macro_invoc(param.id);
return;
}
let name = param.ident.name;
let (def_path_data, def_kind) = match param.kind {
GenericParamKind::Lifetime { .. } => {
(DefPathData::LifetimeNs(name), DefKind::LifetimeParam)
}
GenericParamKind::Type { .. } => (DefPathData::TypeNs(name), DefKind::TyParam),
GenericParamKind::Const { .. } => (DefPathData::ValueNs(name), DefKind::ConstParam),
let def_kind = match param.kind {
GenericParamKind::Lifetime { .. } => DefKind::LifetimeParam,
GenericParamKind::Type { .. } => DefKind::TyParam,
GenericParamKind::Const { .. } => DefKind::ConstParam,
};
self.create_def(param.id, def_path_data, def_kind, param.ident.span);
self.create_def(param.id, param.ident.name, def_kind, param.ident.span);
// impl-Trait can happen inside generic parameters, like
// ```
@ -264,14 +251,14 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
}
fn visit_assoc_item(&mut self, i: &'a AssocItem, ctxt: visit::AssocCtxt) {
let (def_data, def_kind) = match &i.kind {
AssocItemKind::Fn(..) => (DefPathData::ValueNs(i.ident.name), DefKind::AssocFn),
AssocItemKind::Const(..) => (DefPathData::ValueNs(i.ident.name), DefKind::AssocConst),
AssocItemKind::Type(..) => (DefPathData::TypeNs(i.ident.name), DefKind::AssocTy),
let def_kind = match &i.kind {
AssocItemKind::Fn(..) => DefKind::AssocFn,
AssocItemKind::Const(..) => DefKind::AssocConst,
AssocItemKind::Type(..) => DefKind::AssocTy,
AssocItemKind::MacCall(..) => return self.visit_macro_invoc(i.id),
};
let def = self.create_def(i.id, def_data, def_kind, i.span);
let def = self.create_def(i.id, i.ident.name, def_kind, i.span);
self.with_parent(def, |this| visit::walk_assoc_item(this, i, ctxt));
}
@ -283,12 +270,7 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
}
fn visit_anon_const(&mut self, constant: &'a AnonConst) {
let def = self.create_def(
constant.id,
DefPathData::AnonConst,
DefKind::AnonConst,
constant.value.span,
);
let def = self.create_def(constant.id, kw::Empty, DefKind::AnonConst, constant.value.span);
self.with_parent(def, |this| visit::walk_anon_const(this, constant));
}
@ -298,25 +280,21 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
ExprKind::Closure(ref closure) => {
// Async closures desugar to closures inside of closures, so
// we must create two defs.
let closure_def =
self.create_def(expr.id, DefPathData::ClosureExpr, DefKind::Closure, expr.span);
let closure_def = self.create_def(expr.id, kw::Empty, DefKind::Closure, expr.span);
match closure.asyncness {
Async::Yes { closure_id, .. } => self.create_def(
closure_id,
DefPathData::ClosureExpr,
DefKind::Closure,
expr.span,
),
Async::Yes { closure_id, .. } => {
self.create_def(closure_id, kw::Empty, DefKind::Closure, expr.span)
}
Async::No => closure_def,
}
}
ExprKind::Gen(_, _, _) => {
self.create_def(expr.id, DefPathData::ClosureExpr, DefKind::Closure, expr.span)
self.create_def(expr.id, kw::Empty, DefKind::Closure, expr.span)
}
ExprKind::ConstBlock(ref constant) => {
let def = self.create_def(
constant.id,
DefPathData::AnonConst,
kw::Empty,
DefKind::InlineConst,
constant.value.span,
);

View file

@ -45,7 +45,6 @@ use rustc_hir::def::NonMacroAttrKind;
use rustc_hir::def::{self, CtorOf, DefKind, DocLinkResMap, LifetimeRes, PartialRes, PerNS};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalDefIdMap, LocalDefIdSet};
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::definitions::DefPathData;
use rustc_hir::{PrimTy, TraitCandidate};
use rustc_index::IndexVec;
use rustc_metadata::creader::{CStore, CrateLoader};
@ -1212,11 +1211,12 @@ impl<'tcx> Resolver<'_, 'tcx> {
&mut self,
parent: LocalDefId,
node_id: ast::NodeId,
data: DefPathData,
name: Symbol,
def_kind: DefKind,
expn_id: ExpnId,
span: Span,
) -> LocalDefId {
let data = def_kind.def_path_data(name);
assert!(
!self.node_id_to_def_id.contains_key(&node_id),
"adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}",