1
Fork 0

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, _) => { ItemKind::Struct(ref struct_def, _) | ItemKind::Union(ref struct_def, _) => {
// If this is a unit or tuple-like struct, register the constructor. // If this is a unit or tuple-like struct, register the constructor.
if let Some(ctor_hir_id) = struct_def.ctor_id() { if let Some(ctor_hir_id) = struct_def.ctor_id() {
this.create_def(ctor_hir_id, this.create_def(ctor_hir_id, DefPathData::Ctor, REGULAR_SPACE, i.span);
DefPathData::StructCtor,
REGULAR_SPACE,
i.span);
} }
} }
_ => {} _ => {}
@ -199,10 +196,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
v.span); v.span);
self.with_parent(def, |this| { self.with_parent(def, |this| {
if let Some(ctor_hir_id) = v.node.data.ctor_id() { if let Some(ctor_hir_id) = v.node.data.ctor_id() {
this.create_def(ctor_hir_id, this.create_def(ctor_hir_id, DefPathData::Ctor, REGULAR_SPACE, v.span);
DefPathData::VariantCtor,
REGULAR_SPACE,
v.span);
} }
visit::walk_variant(this, v, g, item_id) visit::walk_variant(this, v, g, item_id)
}); });

View file

@ -366,10 +366,8 @@ pub enum DefPathData {
EnumVariant(InternedString), EnumVariant(InternedString),
/// A struct field /// A struct field
Field(InternedString), Field(InternedString),
/// Implicit ctor for a unit or tuple-like struct /// Implicit ctor for a unit or tuple-like struct or enum variant.
StructCtor, Ctor,
/// Implicit ctor for a unit or tuple-like enum variant
VariantCtor,
/// A constant expression (see {ast,hir}::AnonConst). /// A constant expression (see {ast,hir}::AnonConst).
AnonConst, AnonConst,
/// An `impl Trait` type node /// An `impl Trait` type node
@ -654,8 +652,7 @@ impl DefPathData {
CrateRoot | CrateRoot |
Misc | Misc |
ClosureExpr | ClosureExpr |
StructCtor | Ctor |
VariantCtor |
AnonConst | AnonConst |
ImplTrait => None ImplTrait => None
} }
@ -686,8 +683,7 @@ impl DefPathData {
Impl => "{{impl}}", Impl => "{{impl}}",
Misc => "{{misc}}", Misc => "{{misc}}",
ClosureExpr => "{{closure}}", ClosureExpr => "{{closure}}",
StructCtor => "{{struct constructor}}", Ctor => "{{constructor}}",
VariantCtor => "{{variant constructor}}",
AnonConst => "{{constant}}", AnonConst => "{{constant}}",
ImplTrait => "{{opaque}}", ImplTrait => "{{opaque}}",
}; };

View file

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

View file

@ -2960,8 +2960,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
} else { } else {
let def_key = self.def_key(id); let def_key = self.def_key(id);
match def_key.disambiguated_data.data { match def_key.disambiguated_data.data {
// The name of a `StructCtor` or `VariantCtor` is that of its parent. // The name of a constructor is that of its parent.
hir_map::DefPathData::StructCtor | hir_map::DefPathData::VariantCtor => hir_map::DefPathData::Ctor =>
self.item_name(DefId { self.item_name(DefId {
krate: id.krate, krate: id.krate,
index: def_key.parent.unwrap() 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); let mut cur_def_key = self.tcx().def_key(def_id);
debug!("try_print_visible_def_path: cur_def_key={:?}", cur_def_key); 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 { match cur_def_key.disambiguated_data.data {
DefPathData::StructCtor | DefPathData::VariantCtor => { DefPathData::Ctor => {
let parent = DefId { let parent = DefId {
krate: def_id.krate, krate: def_id.krate,
index: cur_def_key.parent 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); cur_def_key = self.tcx().def_key(parent);
@ -864,8 +864,7 @@ impl TyCtxt<'_, '_, '_> {
DefPathData::AnonConst | DefPathData::AnonConst |
DefPathData::ConstParam(..) | DefPathData::ConstParam(..) |
DefPathData::ClosureExpr | DefPathData::ClosureExpr |
DefPathData::VariantCtor | DefPathData::Ctor => Namespace::ValueNS,
DefPathData::StructCtor => Namespace::ValueNS,
DefPathData::MacroDef(..) => Namespace::MacroNS, 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. // Skip `::{{constructor}}` on tuple/unit structs.
match disambiguated_data.data { 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 /// Returns `true` if this `DefId` refers to the implicit constructor for
/// a tuple struct like `struct Foo(u32)`, and `false` otherwise. /// a tuple struct like `struct Foo(u32)`, and `false` otherwise.
pub fn is_struct_constructor(self, def_id: DefId) -> bool { pub fn is_constructor(self, def_id: DefId) -> bool {
self.def_key(def_id).disambiguated_data.data == DefPathData::StructCtor self.def_key(def_id).disambiguated_data.data == DefPathData::Ctor
} }
/// Given the `DefId` of a fn or closure, returns the `DefId` of /// 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. // Skip `::{{constructor}}` on tuple/unit structs.
match disambiguated_data.data { 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([]); 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 // we assume that someone passing in a tuple struct ctor is actually wanting to
// look at the definition // look at the definition
let def_key = self.def_key(node_id); 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() def_key.parent.unwrap()
} else { } else {
node_id 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 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(); return_early = !tcx.has_attr(def_id, "rustc_mir") && !tcx.use_mir_borrowck();
if tcx.is_struct_constructor(def_id) { if tcx.is_constructor(def_id) {
// We are not borrow checking the automatically generated struct constructors // 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` // because we want to accept structs such as this (taken from the `linked-hash-map`
// crate): // crate):
// ```rust // ```rust

View file

@ -2685,8 +2685,8 @@ impl MirPass for TypeckMir {
return; return;
} }
if tcx.is_struct_constructor(def_id) { if tcx.is_constructor(def_id) {
// We just assume that the automatically generated struct constructors are // We just assume that the automatically generated struct/variant constructors are
// correct. See the comment in the `mir_borrowck` implementation for an // correct. See the comment in the `mir_borrowck` implementation for an
// explanation why we need this. // explanation why we need this.
return; 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))) 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>) { pub fn rewrite_self_ctor(&self, def: Def, span: Span) -> (Def, DefId, Ty<'tcx>) {
let tcx = self.tcx; let tcx = self.tcx;
if let Def::SelfCtor(impl_def_id) = def { 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 // 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 { // fn Test::X(_1: usize) -> Test {
// let mut _0: Test; // let mut _0: Test;
// //
@ -81,4 +81,4 @@ fn main() {
// return; // return;
// } // }
// } // }
// END rustc.Test-X-{{variant constructor}}.mir_map.0.mir // END rustc.Test-X-{{constructor}}.mir_map.0.mir