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

View File

@ -160,10 +160,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
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 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
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)
});

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 @@ impl DefPathData {
CrateRoot |
Misc |
ClosureExpr |
StructCtor |
VariantCtor |
Ctor |
AnonConst |
ImplTrait => None
}
@ -686,8 +683,7 @@ impl DefPathData {
Impl => "{{impl}}",
Misc => "{{misc}}",
ClosureExpr => "{{closure}}",
StructCtor => "{{struct constructor}}",
VariantCtor => "{{variant constructor}}",
Ctor => "{{constructor}}",
AnonConst => "{{constant}}",
ImplTrait => "{{opaque}}",
};

View File

@ -150,8 +150,7 @@ impl<'tcx> InstanceDef<'tcx> {
_ => return true
};
match tcx.def_key(def_id).disambiguated_data.data {
DefPathData::StructCtor | DefPathData::VariantCtor |
DefPathData::ClosureExpr => true,
DefPathData::Ctor | DefPathData::ClosureExpr => true,
_ => false
}
}

View File

@ -2960,8 +2960,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
} 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()

View File

@ -285,13 +285,13 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
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 @@ impl TyCtxt<'_, '_, '_> {
DefPathData::AnonConst |
DefPathData::ConstParam(..) |
DefPathData::ClosureExpr |
DefPathData::VariantCtor |
DefPathData::StructCtor => Namespace::ValueNS,
DefPathData::Ctor => Namespace::ValueNS,
DefPathData::MacroDef(..) => Namespace::MacroNS,
@ -1029,7 +1028,7 @@ impl<F: fmt::Write> Printer<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F> {
// Skip `::{{constructor}}` on tuple/unit structs.
match disambiguated_data.data {
DefPathData::StructCtor | DefPathData::VariantCtor => return Ok(self),
DefPathData::Ctor => return Ok(self),
_ => {}
}

View File

@ -549,8 +549,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
/// 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

View File

@ -522,7 +522,7 @@ impl Printer<'tcx, 'tcx> for SymbolPrinter<'_, 'tcx> {
// Skip `::{{constructor}}` on tuple/unit structs.
match disambiguated_data.data {
DefPathData::StructCtor => return Ok(self),
DefPathData::Ctor => return Ok(self),
_ => {}
}

View File

@ -947,11 +947,11 @@ impl<'a, 'tcx> CrateMetadata {
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

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

View File

@ -2685,8 +2685,8 @@ impl MirPass for TypeckMir {
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;

View File

@ -5334,7 +5334,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
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 {

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