Merge DefPathData::VariantCtor and DefPathData::StructCtor

This commit is contained in:
Vadim Petrochenkov
2019-03-24 17:49:58 +03:00
parent 5bcf9f4f11
commit 2cbc25e6fc
12 changed files with 26 additions and 38 deletions
+2 -8
View File
@@ -160,10 +160,7 @@ fn visit_item(&mut self, i: &'a Item) {
ItemKind::Struct(ref struct_def, _) | ItemKind::Union(ref struct_def, _) => {
// If this is a unit or tuple-like struct, register the constructor.
if let Some(ctor_hir_id) = struct_def.ctor_id() {
this.create_def(ctor_hir_id,
DefPathData::StructCtor,
REGULAR_SPACE,
i.span);
this.create_def(ctor_hir_id, DefPathData::Ctor, REGULAR_SPACE, i.span);
}
}
_ => {}
@@ -199,10 +196,7 @@ fn visit_variant(&mut self, v: &'a Variant, g: &'a Generics, item_id: NodeId) {
v.span);
self.with_parent(def, |this| {
if let Some(ctor_hir_id) = v.node.data.ctor_id() {
this.create_def(ctor_hir_id,
DefPathData::VariantCtor,
REGULAR_SPACE,
v.span);
this.create_def(ctor_hir_id, DefPathData::Ctor, REGULAR_SPACE, v.span);
}
visit::walk_variant(this, v, g, item_id)
});
+4 -8
View File
@@ -366,10 +366,8 @@ pub enum DefPathData {
EnumVariant(InternedString),
/// A struct field
Field(InternedString),
/// Implicit ctor for a unit or tuple-like struct
StructCtor,
/// Implicit ctor for a unit or tuple-like enum variant
VariantCtor,
/// Implicit ctor for a unit or tuple-like struct or enum variant.
Ctor,
/// A constant expression (see {ast,hir}::AnonConst).
AnonConst,
/// An `impl Trait` type node
@@ -654,8 +652,7 @@ pub fn get_opt_name(&self) -> Option<InternedString> {
CrateRoot |
Misc |
ClosureExpr |
StructCtor |
VariantCtor |
Ctor |
AnonConst |
ImplTrait => None
}
@@ -686,8 +683,7 @@ pub fn as_interned_str(&self) -> InternedString {
Impl => "{{impl}}",
Misc => "{{misc}}",
ClosureExpr => "{{closure}}",
StructCtor => "{{struct constructor}}",
VariantCtor => "{{variant constructor}}",
Ctor => "{{constructor}}",
AnonConst => "{{constant}}",
ImplTrait => "{{opaque}}",
};
+1 -2
View File
@@ -150,8 +150,7 @@ pub fn is_inline<'a>(
_ => return true
};
match tcx.def_key(def_id).disambiguated_data.data {
DefPathData::StructCtor | DefPathData::VariantCtor |
DefPathData::ClosureExpr => true,
DefPathData::Ctor | DefPathData::ClosureExpr => true,
_ => false
}
}
+2 -2
View File
@@ -2960,8 +2960,8 @@ pub fn item_name(self, id: DefId) -> InternedString {
} else {
let def_key = self.def_key(id);
match def_key.disambiguated_data.data {
// The name of a `StructCtor` or `VariantCtor` is that of its parent.
hir_map::DefPathData::StructCtor | hir_map::DefPathData::VariantCtor =>
// The name of a constructor is that of its parent.
hir_map::DefPathData::Ctor =>
self.item_name(DefId {
krate: id.krate,
index: def_key.parent.unwrap()
+5 -6
View File
@@ -285,13 +285,13 @@ fn try_print_visible_def_path(
let mut cur_def_key = self.tcx().def_key(def_id);
debug!("try_print_visible_def_path: cur_def_key={:?}", cur_def_key);
// For a UnitStruct or TupleStruct we want the name of its parent rather than <unnamed>.
// For a constructor we want the name of its parent rather than <unnamed>.
match cur_def_key.disambiguated_data.data {
DefPathData::StructCtor | DefPathData::VariantCtor => {
DefPathData::Ctor => {
let parent = DefId {
krate: def_id.krate,
index: cur_def_key.parent
.expect("DefPathData::StructCtor/VariantData missing a parent"),
.expect("DefPathData::Ctor/VariantData missing a parent"),
};
cur_def_key = self.tcx().def_key(parent);
@@ -864,8 +864,7 @@ fn guess_def_namespace(self, def_id: DefId) -> Namespace {
DefPathData::AnonConst |
DefPathData::ConstParam(..) |
DefPathData::ClosureExpr |
DefPathData::VariantCtor |
DefPathData::StructCtor => Namespace::ValueNS,
DefPathData::Ctor => Namespace::ValueNS,
DefPathData::MacroDef(..) => Namespace::MacroNS,
@@ -1029,7 +1028,7 @@ fn path_append(
// Skip `::{{constructor}}` on tuple/unit structs.
match disambiguated_data.data {
DefPathData::StructCtor | DefPathData::VariantCtor => return Ok(self),
DefPathData::Ctor => return Ok(self),
_ => {}
}
+2 -2
View File
@@ -549,8 +549,8 @@ pub fn is_trait_alias(self, def_id: DefId) -> bool {
/// Returns `true` if this `DefId` refers to the implicit constructor for
/// a tuple struct like `struct Foo(u32)`, and `false` otherwise.
pub fn is_struct_constructor(self, def_id: DefId) -> bool {
self.def_key(def_id).disambiguated_data.data == DefPathData::StructCtor
pub fn is_constructor(self, def_id: DefId) -> bool {
self.def_key(def_id).disambiguated_data.data == DefPathData::Ctor
}
/// Given the `DefId` of a fn or closure, returns the `DefId` of
+1 -1
View File
@@ -522,7 +522,7 @@ fn path_append(
// Skip `::{{constructor}}` on tuple/unit structs.
match disambiguated_data.data {
DefPathData::StructCtor => return Ok(self),
DefPathData::Ctor => return Ok(self),
_ => {}
}
+2 -2
View File
@@ -947,11 +947,11 @@ pub fn get_item_attrs(&self, node_id: DefIndex, sess: &Session) -> Lrc<[ast::Att
return Lrc::new([]);
}
// The attributes for a tuple struct are attached to the definition, not the ctor;
// The attributes for a tuple struct/variant are attached to the definition, not the ctor;
// we assume that someone passing in a tuple struct ctor is actually wanting to
// look at the definition
let def_key = self.def_key(node_id);
let item_id = if def_key.disambiguated_data.data == DefPathData::StructCtor {
let item_id = if def_key.disambiguated_data.data == DefPathData::Ctor {
def_key.parent.unwrap()
} else {
node_id
+2 -2
View File
@@ -75,8 +75,8 @@ fn mir_borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> BorrowC
// Return early if we are not supposed to use MIR borrow checker for this function.
return_early = !tcx.has_attr(def_id, "rustc_mir") && !tcx.use_mir_borrowck();
if tcx.is_struct_constructor(def_id) {
// We are not borrow checking the automatically generated struct constructors
if tcx.is_constructor(def_id) {
// We are not borrow checking the automatically generated struct/variant constructors
// because we want to accept structs such as this (taken from the `linked-hash-map`
// crate):
// ```rust
@@ -2685,8 +2685,8 @@ fn run_pass<'a, 'tcx>(
return;
}
if tcx.is_struct_constructor(def_id) {
// We just assume that the automatically generated struct constructors are
if tcx.is_constructor(def_id) {
// We just assume that the automatically generated struct/variant constructors are
// correct. See the comment in the `mir_borrowck` implementation for an
// explanation why we need this.
return;
+1 -1
View File
@@ -5334,7 +5334,7 @@ fn could_remove_semicolon(
Some(original_span.with_lo(original_span.hi() - BytePos(1)))
}
// Rewrite `SelfCtor` to `StructCtor`
// Rewrite `SelfCtor` to `Ctor`
pub fn rewrite_self_ctor(&self, def: Def, span: Span) -> (Def, DefId, Ty<'tcx>) {
let tcx = self.tcx;
if let Def::SelfCtor(impl_def_id) = def {
+2 -2
View File
@@ -72,7 +72,7 @@ fn main() {
// }
// END rustc.ptr-real_drop_in_place.std__vec__Vec_i32_.AddMovesForPackedDrops.before.mir
// START rustc.Test-X-{{variant constructor}}.mir_map.0.mir
// START rustc.Test-X-{{constructor}}.mir_map.0.mir
// fn Test::X(_1: usize) -> Test {
// let mut _0: Test;
//
@@ -81,4 +81,4 @@ fn main() {
// return;
// }
// }
// END rustc.Test-X-{{variant constructor}}.mir_map.0.mir
// END rustc.Test-X-{{constructor}}.mir_map.0.mir