Skip to content

Commit 35d52a0

Browse files
committed
Add unions to definition map
1 parent 4001c03 commit 35d52a0

File tree

6 files changed

+28
-4
lines changed

6 files changed

+28
-4
lines changed

src/librustc/hir/def.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub enum Def {
4141
// If Def::Struct lives in value namespace (e.g. tuple struct, unit struct expressions)
4242
// it denotes a constructor and its DefId refers to NodeId of the struct's constructor.
4343
Struct(DefId),
44+
Union(DefId),
4445
Label(ast::NodeId),
4546
Method(DefId),
4647
Err,
@@ -109,7 +110,7 @@ impl Def {
109110

110111
Def::Fn(..) | Def::Mod(..) | Def::ForeignMod(..) | Def::Static(..) |
111112
Def::Variant(..) | Def::Enum(..) | Def::TyAlias(..) | Def::AssociatedTy(..) |
112-
Def::TyParam(..) | Def::Struct(..) | Def::Trait(..) |
113+
Def::TyParam(..) | Def::Struct(..) | Def::Union(..) | Def::Trait(..) |
113114
Def::Method(..) | Def::Const(..) | Def::AssociatedConst(..) |
114115
Def::PrimTy(..) | Def::Label(..) | Def::SelfTy(..) | Def::Err => {
115116
bug!("attempted .var_id() on invalid {:?}", self)
@@ -121,7 +122,7 @@ impl Def {
121122
match *self {
122123
Def::Fn(id) | Def::Mod(id) | Def::ForeignMod(id) | Def::Static(id, _) |
123124
Def::Variant(_, id) | Def::Enum(id) | Def::TyAlias(id) | Def::AssociatedTy(_, id) |
124-
Def::TyParam(id) | Def::Struct(id) | Def::Trait(id) |
125+
Def::TyParam(id) | Def::Struct(id) | Def::Union(id) | Def::Trait(id) |
125126
Def::Method(id) | Def::Const(id) | Def::AssociatedConst(id) |
126127
Def::Local(id, _) | Def::Upvar(id, _, _, _) => {
127128
id
@@ -147,6 +148,7 @@ impl Def {
147148
Def::TyAlias(..) => "type",
148149
Def::AssociatedTy(..) => "associated type",
149150
Def::Struct(..) => "struct",
151+
Def::Union(..) => "union",
150152
Def::Trait(..) => "trait",
151153
Def::Method(..) => "method",
152154
Def::Const(..) => "constant",

src/librustc/middle/mem_categorization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
572572
id, expr_ty, def);
573573

574574
match def {
575-
Def::Struct(..) | Def::Variant(..) | Def::Const(..) |
575+
Def::Struct(..) | Def::Union(..) | Def::Variant(..) | Def::Const(..) |
576576
Def::AssociatedConst(..) | Def::Fn(..) | Def::Method(..) => {
577577
Ok(self.cat_rvalue_node(id, span, expr_ty))
578578
}

src/librustc_metadata/astencode.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ impl tr for Def {
416416
Def::Upvar(did1, nid1, index, nid2)
417417
}
418418
Def::Struct(did) => Def::Struct(did.tr(dcx)),
419+
Def::Union(did) => Def::Union(did.tr(dcx)),
419420
Def::Label(nid) => Def::Label(dcx.tr_id(nid)),
420421
Def::Err => Def::Err,
421422
}

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,19 @@ impl<'b> Resolver<'b> {
278278
self.structs.insert(item_def_id, field_names);
279279
}
280280

281-
ItemKind::Union(..) => panic!("`union` is not yet implemented"),
281+
ItemKind::Union(ref vdata, _) => {
282+
let def = Def::Union(self.definitions.local_def_id(item.id));
283+
self.define(parent, name, TypeNS, (def, sp, vis));
284+
285+
// Record the def ID and fields of this union.
286+
let field_names = vdata.fields().iter().enumerate().map(|(index, field)| {
287+
self.resolve_visibility(&field.vis);
288+
field.ident.map(|ident| ident.name)
289+
.unwrap_or_else(|| token::intern(&index.to_string()))
290+
}).collect();
291+
let item_def_id = self.definitions.local_def_id(item.id);
292+
self.structs.insert(item_def_id, field_names);
293+
}
282294

283295
ItemKind::DefaultImpl(_, _) | ItemKind::Impl(..) => {}
284296

@@ -461,6 +473,13 @@ impl<'b> Resolver<'b> {
461473
let fields = self.session.cstore.struct_field_names(def_id);
462474
self.structs.insert(def_id, fields);
463475
}
476+
Def::Union(def_id) => {
477+
let _ = self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
478+
479+
// Record the def ID and fields of this union.
480+
let fields = self.session.cstore.struct_field_names(def_id);
481+
self.structs.insert(def_id, fields);
482+
}
464483
Def::Struct(..) => {}
465484
Def::Local(..) |
466485
Def::PrimTy(..) |

src/librustc_save_analysis/dump_visitor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
300300
}.lower(self.tcx));
301301
}
302302
Def::Struct(..) |
303+
Def::Union(..) |
303304
Def::Enum(..) |
304305
Def::TyAlias(..) |
305306
Def::AssociatedTy(..) |

src/librustc_typeck/check/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4118,6 +4118,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
41184118
match def {
41194119
// Case 1 and 1b. Reference to a *type* or *enum variant*.
41204120
Def::Struct(def_id) |
4121+
Def::Union(def_id) |
41214122
Def::Variant(_, def_id) |
41224123
Def::Enum(def_id) |
41234124
Def::TyAlias(def_id) |

0 commit comments

Comments
 (0)