Skip to content

Commit db4770f

Browse files
committed
Remove CtorOf from Node::Ctor.
This commit removes `CtorOf` from `Node::Ctor` as the parent of the constructor can be determined by looking at the node's parent in the few places where knowing this is necessary.
1 parent 782a6de commit db4770f

File tree

9 files changed

+61
-48
lines changed

9 files changed

+61
-48
lines changed

src/librustc/hir/map/collector.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
362362
if let ItemKind::Struct(ref struct_def, _) = i.node {
363363
// If this is a tuple or unit-like struct, register the constructor.
364364
if let Some(ctor_hir_id) = struct_def.ctor_hir_id() {
365-
this.insert(i.span,
366-
ctor_hir_id,
367-
Node::Ctor(hir::CtorOf::Struct, struct_def));
365+
this.insert(i.span, ctor_hir_id, Node::Ctor(struct_def));
368366
}
369367
}
370368
intravisit::walk_item(this, i);
@@ -521,7 +519,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
521519
self.with_parent(v.node.id, |this| {
522520
// Register the constructor of this variant.
523521
if let Some(ctor_hir_id) = v.node.data.ctor_hir_id() {
524-
this.insert(v.span, ctor_hir_id, Node::Ctor(hir::CtorOf::Variant, &v.node.data));
522+
this.insert(v.span, ctor_hir_id, Node::Ctor(&v.node.data));
525523
}
526524
intravisit::walk_variant(this, v, g, item_id);
527525
});

src/librustc/hir/map/mod.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,15 @@ impl<'hir> Map<'hir> {
369369
let def_id = self.local_def_id_from_hir_id(variant.node.id);
370370
Some(Def::Variant(def_id))
371371
}
372-
Node::Ctor(ctor_of, variant_data) => {
372+
Node::Ctor(variant_data) => {
373+
let ctor_of = match self.find(self.get_parent_node(node_id)) {
374+
Some(Node::Item(..)) => CtorOf::Struct,
375+
Some(Node::Variant(..)) => CtorOf::Variant,
376+
_ => unreachable!(),
377+
};
373378
variant_data.ctor_hir_id()
374379
.map(|hir_id| self.local_def_id_from_hir_id(hir_id))
375-
.map(|def_id| Def::Ctor(
376-
ctor_of, def_id, def::CtorKind::from_hir(variant_data),
377-
))
380+
.map(|def_id| Def::Ctor(ctor_of, def_id, def::CtorKind::from_hir(variant_data)))
378381
}
379382
Node::AnonConst(_) |
380383
Node::Field(_) |
@@ -951,7 +954,7 @@ impl<'hir> Map<'hir> {
951954
}
952955
}
953956
Some(Node::Variant(variant)) => &variant.node.data,
954-
Some(Node::Ctor(_, data)) => data,
957+
Some(Node::Ctor(data)) => data,
955958
_ => bug!("expected struct or variant, found {}", self.hir_to_string(id))
956959
}
957960
}
@@ -1070,10 +1073,11 @@ impl<'hir> Map<'hir> {
10701073
Some(Node::Binding(pat)) => pat.span,
10711074
Some(Node::Pat(pat)) => pat.span,
10721075
Some(Node::Block(block)) => block.span,
1073-
Some(Node::Ctor(CtorOf::Struct, _)) =>
1074-
self.expect_item(self.get_parent(id)).span,
1075-
Some(Node::Ctor(CtorOf::Variant, _)) =>
1076-
self.expect_variant(self.node_to_hir_id(self.get_parent_node(id))).span,
1076+
Some(Node::Ctor(..)) => match self.find(self.get_parent_node(id)) {
1077+
Some(Node::Item(item)) => item.span,
1078+
Some(Node::Variant(variant)) => variant.span,
1079+
_ => unreachable!(),
1080+
}
10771081
Some(Node::Lifetime(lifetime)) => lifetime.span,
10781082
Some(Node::GenericParam(param)) => param.span,
10791083
Some(Node::Visibility(&Spanned {

src/librustc/hir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2590,7 +2590,7 @@ pub enum Node<'hir> {
25902590

25912591
/// `Ctor` refers to the constructor of an enum variant or struct. Only tuple or unit variants
25922592
/// with synthesized constructors.
2593-
Ctor(CtorOf, &'hir VariantData),
2593+
Ctor(&'hir VariantData),
25942594

25952595
Lifetime(&'hir Lifetime),
25962596
GenericParam(&'hir GenericParam),

src/librustc/traits/error_reporting.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
10771077
_ => ArgKind::empty()
10781078
}).collect::<Vec<ArgKind>>())
10791079
}
1080-
Node::Ctor(_, ref variant_data) => {
1080+
Node::Ctor(ref variant_data) => {
10811081
let span = variant_data.ctor_hir_id()
10821082
.map(|hir_id| self.tcx.hir().span_by_hir_id(hir_id))
10831083
.unwrap_or(DUMMY_SP);

src/librustc_borrowck/borrowck/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, owner_def_id: DefId)
8585
let owner_id = tcx.hir().as_local_hir_id(owner_def_id).unwrap();
8686

8787
match tcx.hir().get_by_hir_id(owner_id) {
88-
Node::Ctor(_, _) => {
88+
Node::Ctor(..) => {
8989
// We get invoked with anything that has MIR, but some of
9090
// those things (notably the synthesized constructors from
9191
// tuple structs/variants) do not have an associated body

src/librustc_mir/build/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Mir<'t
3131

3232
// Figure out what primary body this item has.
3333
let (body_id, return_ty_span) = match tcx.hir().get_by_hir_id(id) {
34-
Node::Ctor(_, ctor) => return create_constructor_shim(tcx, id, ctor),
34+
Node::Ctor(ctor) => return create_constructor_shim(tcx, id, ctor),
3535

3636
Node::Expr(hir::Expr { node: hir::ExprKind::Closure(_, decl, body_id, _, _), .. })
3737
| Node::Item(hir::Item { node: hir::ItemKind::Fn(decl, _, _, body_id), .. })

src/librustc_privacy/lib.rs

+39-27
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ fn def_id_visibility<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
225225
let vis = match tcx.hir().get_by_hir_id(hir_id) {
226226
Node::Item(item) => &item.vis,
227227
Node::ForeignItem(foreign_item) => &foreign_item.vis,
228-
Node::TraitItem(..) | Node::Variant(..) | Node::Ctor(hir::CtorOf::Variant, ..) => {
228+
Node::TraitItem(..) | Node::Variant(..) => {
229229
return def_id_visibility(tcx, tcx.hir().get_parent_did_by_hir_id(hir_id));
230230
}
231231
Node::ImplItem(impl_item) => {
@@ -239,36 +239,48 @@ fn def_id_visibility<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
239239
node => bug!("unexpected node kind: {:?}", node),
240240
}
241241
}
242-
Node::Ctor(hir::CtorOf::Struct, vdata) => {
243-
let struct_hir_id = tcx.hir().get_parent_item(hir_id);
244-
let item = match tcx.hir().get_by_hir_id(struct_hir_id) {
245-
Node::Item(item) => item,
246-
node => bug!("unexpected node kind: {:?}", node),
247-
};
248-
let (mut ctor_vis, mut span, mut descr) =
249-
(ty::Visibility::from_hir(&item.vis, struct_hir_id, tcx),
250-
item.vis.span, item.vis.node.descr());
251-
for field in vdata.fields() {
252-
let field_vis = ty::Visibility::from_hir(&field.vis, hir_id, tcx);
253-
if ctor_vis.is_at_least(field_vis, tcx) {
254-
ctor_vis = field_vis;
255-
span = field.vis.span;
256-
descr = field.vis.node.descr();
242+
Node::Ctor(vdata) => {
243+
let parent_hir_id = tcx.hir().get_parent_node_by_hir_id(hir_id);
244+
match tcx.hir().get_by_hir_id(parent_hir_id) {
245+
Node::Variant(..) => {
246+
let parent_did = tcx.hir().local_def_id_from_hir_id(parent_hir_id);
247+
return def_id_visibility(tcx, parent_did);
257248
}
258-
}
249+
Node::Item(..) => {
250+
let item = match tcx.hir().get_by_hir_id(parent_hir_id) {
251+
Node::Item(item) => item,
252+
node => bug!("unexpected node kind: {:?}", node),
253+
};
254+
let (mut ctor_vis, mut span, mut descr) =
255+
(ty::Visibility::from_hir(&item.vis, parent_hir_id, tcx),
256+
item.vis.span, item.vis.node.descr());
257+
for field in vdata.fields() {
258+
let field_vis = ty::Visibility::from_hir(&field.vis, hir_id, tcx);
259+
if ctor_vis.is_at_least(field_vis, tcx) {
260+
ctor_vis = field_vis;
261+
span = field.vis.span;
262+
descr = field.vis.node.descr();
263+
}
264+
}
259265

260-
// If the structure is marked as non_exhaustive then lower the
261-
// visibility to within the crate.
262-
if ctor_vis == ty::Visibility::Public {
263-
let adt_def = tcx.adt_def(tcx.hir().get_parent_did_by_hir_id(hir_id));
264-
if adt_def.non_enum_variant().is_field_list_non_exhaustive() {
265-
ctor_vis = ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX));
266-
span = attr::find_by_name(&item.attrs, "non_exhaustive").unwrap().span;
267-
descr = "crate-visible";
266+
// If the structure is marked as non_exhaustive then lower the
267+
// visibility to within the crate.
268+
if ctor_vis == ty::Visibility::Public {
269+
let adt_def =
270+
tcx.adt_def(tcx.hir().get_parent_did_by_hir_id(hir_id));
271+
if adt_def.non_enum_variant().is_field_list_non_exhaustive() {
272+
ctor_vis =
273+
ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX));
274+
span = attr::find_by_name(&item.attrs, "non_exhaustive")
275+
.unwrap().span;
276+
descr = "crate-visible";
277+
}
278+
}
279+
280+
return (ctor_vis, span, descr);
268281
}
282+
node => bug!("unexpected node kind: {:?}", node),
269283
}
270-
271-
return (ctor_vis, span, descr);
272284
}
273285
Node::Expr(expr) => {
274286
return (ty::Visibility::Restricted(

src/librustc_resolve/error_reporting.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use std::cmp::Reverse;
22

33
use errors::{Applicability, DiagnosticBuilder, DiagnosticId};
44
use log::debug;
5-
use rustc::hir::def::{Def, CtorKind};
6-
use rustc::hir::def::Namespace::*;
5+
use rustc::hir::def::{Def, CtorKind, Namespace::*};
76
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
87
use rustc::session::config::nightly_options;
98
use syntax::ast::{ExprKind};

src/librustc_typeck/collect.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,7 @@ fn type_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Ty<'tcx> {
12471247
ForeignItemKind::Type => tcx.mk_foreign(def_id),
12481248
},
12491249

1250-
Node::Ctor(_, &ref def) | Node::Variant(&Spanned {
1250+
Node::Ctor(&ref def) | Node::Variant(&Spanned {
12511251
node: hir::VariantKind { data: ref def, .. },
12521252
..
12531253
}) => match *def {
@@ -1625,7 +1625,7 @@ fn fn_sig<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> ty::PolyFnSig
16251625
compute_sig_of_foreign_fn_decl(tcx, def_id, fn_decl, abi)
16261626
}
16271627

1628-
Ctor(_, data) | Variant(Spanned {
1628+
Ctor(data) | Variant(Spanned {
16291629
node: hir::VariantKind { data, .. },
16301630
..
16311631
}) if data.ctor_hir_id().is_some() => {

0 commit comments

Comments
 (0)