Skip to content

Commit 495566e

Browse files
committed
Decouple structure kinds from NodeIds
1 parent 2314ab2 commit 495566e

File tree

29 files changed

+113
-131
lines changed

29 files changed

+113
-131
lines changed

src/librustc/front/map/collector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
150150
}
151151
ItemStruct(ref struct_def, _) => {
152152
// If this is a tuple-like struct, register the constructor.
153-
if let Some(ctor_id) = struct_def.ctor_id {
154-
self.insert_def(ctor_id,
153+
if struct_def.kind != VariantKind::Dict {
154+
self.insert_def(struct_def.id,
155155
NodeStructCtor(&**struct_def),
156156
DefPathData::StructCtor);
157157
}

src/librustc/front/map/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,8 @@ impl<'ast> Map<'ast> {
480480
}
481481
}
482482
Some(NodeVariant(variant)) => {
483-
match variant.node.def.ctor_id {
484-
None => &variant.node.def,
483+
match variant.node.def.kind {
484+
VariantKind::Dict => &variant.node.def,
485485
_ => panic!("struct ID bound to enum variant that isn't struct-like"),
486486
}
487487
}

src/librustc/metadata/encoder.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -381,12 +381,8 @@ fn each_auxiliary_node_id<F>(item: &hir::Item, callback: F) -> bool where
381381
match item.node {
382382
hir::ItemStruct(ref struct_def, _) => {
383383
// If this is a newtype struct, return the constructor.
384-
match struct_def.ctor_id {
385-
Some(ctor_id) if !struct_def.fields.is_empty() &&
386-
struct_def.fields[0].node.kind.is_unnamed() => {
387-
continue_ = callback(ctor_id);
388-
}
389-
_ => {}
384+
if struct_def.kind == hir::VariantKind::Tuple {
385+
continue_ = callback(struct_def.id);
390386
}
391387
}
392388
_ => {}
@@ -1085,9 +1081,8 @@ fn encode_info_for_item<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
10851081
}
10861082

10871083
// If this is a tuple-like struct, encode the type of the constructor.
1088-
if let Some(ctor_id) = struct_def.ctor_id {
1089-
encode_info_for_struct_ctor(ecx, rbml_w, item.name,
1090-
ctor_id, index, item.id);
1084+
if struct_def.kind != hir::VariantKind::Dict {
1085+
encode_info_for_struct_ctor(ecx, rbml_w, item.name, struct_def.id, index, item.id);
10911086
}
10921087
}
10931088
hir::ItemDefaultImpl(unsafety, _) => {

src/librustc/middle/dead.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,9 @@ fn find_live(tcx: &ty::ctxt,
426426

427427
fn get_struct_ctor_id(item: &hir::Item) -> Option<ast::NodeId> {
428428
match item.node {
429-
hir::ItemStruct(ref struct_def, _) => struct_def.ctor_id,
429+
hir::ItemStruct(ref struct_def, _) if struct_def.kind != hir::VariantKind::Dict => {
430+
Some(struct_def.id)
431+
}
430432
_ => None
431433
}
432434
}

src/librustc/middle/stability.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Annotator<'a, 'tcx> {
185185
|v| visit::walk_item(v, i), required);
186186

187187
if let hir::ItemStruct(ref sd, _) = i.node {
188-
sd.ctor_id.map(|id| {
189-
self.annotate(id, true, &i.attrs, i.span, |_| {}, true)
190-
});
188+
if sd.kind != hir::VariantKind::Dict {
189+
self.annotate(sd.id, true, &i.attrs, i.span, |_| {}, true)
190+
}
191191
}
192192
}
193193

src/librustc_front/fold.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,10 +699,11 @@ pub fn noop_fold_where_predicate<T: Folder>(pred: WherePredicate, fld: &mut T) -
699699
}
700700

701701
pub fn noop_fold_struct_def<T: Folder>(struct_def: P<StructDef>, fld: &mut T) -> P<StructDef> {
702-
struct_def.map(|StructDef { fields, ctor_id }| {
702+
struct_def.map(|StructDef { fields, id, kind }| {
703703
StructDef {
704704
fields: fields.move_map(|f| fld.fold_struct_field(f)),
705-
ctor_id: ctor_id.map(|cid| fld.new_id(cid)),
705+
id: fld.new_id(id),
706+
kind: kind,
706707
}
707708
})
708709
}

src/librustc_front/hir.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ pub use self::Ty_::*;
3333
pub use self::TyParamBound::*;
3434
pub use self::UnOp::*;
3535
pub use self::UnsafeSource::*;
36-
pub use self::VariantKind::*;
3736
pub use self::ViewPath_::*;
3837
pub use self::Visibility::*;
3938
pub use self::PathParameters::*;
@@ -1021,14 +1020,6 @@ pub struct VariantArg {
10211020
pub id: NodeId,
10221021
}
10231022

1024-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1025-
pub enum VariantKind {
1026-
/// Tuple variant, e.g. `Foo(A, B)`
1027-
TupleVariantKind(Vec<VariantArg>),
1028-
/// Struct variant, e.g. `Foo {x: A, y: B}`
1029-
StructVariantKind(P<StructDef>),
1030-
}
1031-
10321023
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
10331024
pub struct EnumDef {
10341025
pub variants: Vec<P<Variant>>,
@@ -1176,13 +1167,21 @@ impl StructFieldKind {
11761167
}
11771168
}
11781169

1170+
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1171+
pub enum VariantKind {
1172+
Dict,
1173+
Tuple,
1174+
Unit,
1175+
}
1176+
11791177
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
11801178
pub struct StructDef {
11811179
/// Fields, not including ctor
11821180
pub fields: Vec<StructField>,
11831181
/// ID of the constructor. This is only used for tuple- or enum-like
11841182
/// structs.
1185-
pub ctor_id: Option<NodeId>,
1183+
pub id: NodeId,
1184+
pub kind: VariantKind,
11861185
}
11871186

11881187
/*

src/librustc_front/lowering.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,12 @@ pub fn lower_where_predicate(_lctx: &LoweringContext,
502502
pub fn lower_struct_def(_lctx: &LoweringContext, sd: &StructDef) -> P<hir::StructDef> {
503503
P(hir::StructDef {
504504
fields: sd.fields.iter().map(|f| lower_struct_field(_lctx, f)).collect(),
505-
ctor_id: sd.ctor_id,
505+
id: sd.id,
506+
kind: match sd.kind {
507+
VariantKind::Dict => hir::VariantKind::Dict,
508+
VariantKind::Tuple => hir::VariantKind::Tuple,
509+
VariantKind::Unit => hir::VariantKind::Unit,
510+
}
506511
})
507512
}
508513

src/librustc_front/print/pprust.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,8 +896,8 @@ impl<'a> State<'a> {
896896
-> io::Result<()> {
897897
try!(self.print_name(name));
898898
try!(self.print_generics(generics));
899-
if ::util::struct_def_is_tuple_like(struct_def) {
900-
if !struct_def.fields.is_empty() {
899+
if struct_def.kind != hir::VariantKind::Dict {
900+
if struct_def.kind == hir::VariantKind::Tuple {
901901
try!(self.popen());
902902
try!(self.commasep(Inconsistent,
903903
&struct_def.fields,

src/librustc_front/util.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ pub fn binop_to_string(op: BinOp_) -> &'static str {
8181
}
8282
}
8383

84-
/// Returns true if the given struct def is tuple-like; i.e. that its fields
85-
/// are unnamed.
86-
pub fn struct_def_is_tuple_like(struct_def: &hir::StructDef) -> bool {
87-
struct_def.ctor_id.is_some()
88-
}
89-
9084
pub fn stmt_id(s: &Stmt) -> NodeId {
9185
match s.node {
9286
StmtDecl(_, id) => id,
@@ -298,7 +292,7 @@ impl<'a, 'v, O: ast_util::IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O>
298292
_: &hir::Generics,
299293
id: NodeId) {
300294
self.operation.visit_id(id);
301-
struct_def.ctor_id.map(|ctor_id| self.operation.visit_id(ctor_id));
295+
self.operation.visit_id(struct_def.id);
302296
visit::walk_struct_def(self, struct_def);
303297
}
304298

0 commit comments

Comments
 (0)