Skip to content

Commit

Permalink
Merge DefPathData::VariantCtor and DefPathData::StructCtor
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Mar 24, 2019
1 parent 5bcf9f4 commit 2cbc25e
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 38 deletions.
10 changes: 2 additions & 8 deletions src/librustc/hir/map/def_collector.rs
Expand Up @@ -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);
}
}
_ => {}
Expand Down Expand Up @@ -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)
});
Expand Down
12 changes: 4 additions & 8 deletions src/librustc/hir/map/definitions.rs
Expand Up @@ -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
Expand Down Expand Up @@ -654,8 +652,7 @@ impl DefPathData {
CrateRoot |
Misc |
ClosureExpr |
StructCtor |
VariantCtor |
Ctor |
AnonConst |
ImplTrait => None
}
Expand Down Expand Up @@ -686,8 +683,7 @@ impl DefPathData {
Impl => "{{impl}}",
Misc => "{{misc}}",
ClosureExpr => "{{closure}}",
StructCtor => "{{struct constructor}}",
VariantCtor => "{{variant constructor}}",
Ctor => "{{constructor}}",
AnonConst => "{{constant}}",
ImplTrait => "{{opaque}}",
};
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/ty/instance.rs
Expand Up @@ -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
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/mod.rs
Expand Up @@ -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()
Expand Down
11 changes: 5 additions & 6 deletions src/librustc/ty/print/pretty.rs
Expand Up @@ -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);
Expand Down Expand Up @@ -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,

Expand Down Expand Up @@ -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),
_ => {}
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/util.rs
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_utils/symbol_names.rs
Expand Up @@ -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),
_ => {}
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_metadata/decoder.rs
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/borrow_check/mod.rs
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/borrow_check/nll/type_check/mod.rs
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/mod.rs
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/test/mir-opt/unusual-item-types.rs
Expand Up @@ -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;
//
Expand All @@ -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

0 comments on commit 2cbc25e

Please sign in to comment.