Skip to content

Commit

Permalink
Preserve struct/variant kinds in metadata
Browse files Browse the repository at this point in the history
Add tests for use of empty structs in cross-crate scenarios
  • Loading branch information
petrochenkov committed Jan 15, 2016
1 parent 1f4e317 commit ccb4b35
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 50 deletions.
11 changes: 3 additions & 8 deletions src/librustc/middle/ty/mod.rs
Expand Up @@ -49,7 +49,7 @@ use std::collections::{HashMap, HashSet};
use syntax::ast::{self, CrateNum, Name, NodeId};
use syntax::attr::{self, AttrMetaMethods};
use syntax::codemap::{DUMMY_SP, Span};
use syntax::parse::token::{InternedString, special_idents};
use syntax::parse::token::InternedString;

use rustc_front::hir;
use rustc_front::hir::{ItemImpl, ItemTrait};
Expand Down Expand Up @@ -1353,6 +1353,7 @@ pub struct VariantDefData<'tcx, 'container: 'tcx> {
pub name: Name, // struct's name if this is a struct
pub disr_val: Disr,
pub fields: Vec<FieldDefData<'tcx, 'container>>,
pub kind: VariantKind,
}

pub struct FieldDefData<'tcx, 'container: 'tcx> {
Expand Down Expand Up @@ -1607,13 +1608,7 @@ impl<'tcx, 'container> VariantDefData<'tcx, 'container> {
}

pub fn kind(&self) -> VariantKind {
match self.fields.get(0) {
None => VariantKind::Unit,
Some(&FieldDefData { name, .. }) if name == special_idents::unnamed_field.name => {
VariantKind::Tuple
}
Some(_) => VariantKind::Struct
}
self.kind
}

pub fn is_tuple_struct(&self) -> bool {
Expand Down
32 changes: 24 additions & 8 deletions src/librustc_metadata/decoder.rs
Expand Up @@ -101,12 +101,15 @@ enum Family {
Mod, // m
ForeignMod, // n
Enum, // t
TupleVariant, // v
StructVariant, // V
TupleVariant, // v
UnitVariant, // w
Impl, // i
DefaultImpl, // d
DefaultImpl, // d
Trait, // I
Struct, // S
TupleStruct, // s
UnitStruct, // u
PublicField, // g
InheritedField, // N
Constant, // C
Expand All @@ -126,12 +129,15 @@ fn item_family(item: rbml::Doc) -> Family {
'm' => Mod,
'n' => ForeignMod,
't' => Enum,
'v' => TupleVariant,
'V' => StructVariant,
'v' => TupleVariant,
'w' => UnitVariant,
'i' => Impl,
'd' => DefaultImpl,
'I' => Trait,
'S' => Struct,
's' => TupleStruct,
'u' => UnitStruct,
'g' => PublicField,
'N' => InheritedField,
c => panic!("unexpected family char: {}", c)
Expand Down Expand Up @@ -282,7 +288,7 @@ fn item_to_def_like(cdata: Cmd, item: rbml::Doc, did: DefId) -> DefLike {
}
ImmStatic => DlDef(def::DefStatic(did, false)),
MutStatic => DlDef(def::DefStatic(did, true)),
Struct => DlDef(def::DefStruct(did)),
Struct | TupleStruct | UnitStruct => DlDef(def::DefStruct(did)),
Fn => DlDef(def::DefFn(did, false)),
CtorFn => DlDef(def::DefFn(did, true)),
Method | StaticMethod => {
Expand All @@ -302,7 +308,7 @@ fn item_to_def_like(cdata: Cmd, item: rbml::Doc, did: DefId) -> DefLike {
let enum_did = item_require_parent_item(cdata, item);
DlDef(def::DefVariant(enum_did, did, true))
}
TupleVariant => {
TupleVariant | UnitVariant => {
let enum_did = item_require_parent_item(cdata, item);
DlDef(def::DefVariant(enum_did, did, false))
}
Expand Down Expand Up @@ -365,6 +371,14 @@ pub fn get_adt_def<'tcx>(intr: &IdentInterner,
item_id: DefIndex,
tcx: &ty::ctxt<'tcx>) -> ty::AdtDefMaster<'tcx>
{
fn family_to_variant_kind<'tcx>(family: Family, tcx: &ty::ctxt<'tcx>) -> ty::VariantKind {
match family {
Struct | StructVariant => ty::VariantKind::Struct,
TupleStruct | TupleVariant => ty::VariantKind::Tuple,
UnitStruct | UnitVariant => ty::VariantKind::Unit,
_ => tcx.sess.bug(&format!("unexpected family: {:?}", family)),
}
}
fn get_enum_variants<'tcx>(intr: &IdentInterner,
cdata: Cmd,
doc: rbml::Doc,
Expand All @@ -384,7 +398,8 @@ pub fn get_adt_def<'tcx>(intr: &IdentInterner,
did: did,
name: item_name(intr, item),
fields: get_variant_fields(intr, cdata, item, tcx),
disr_val: disr
disr_val: disr,
kind: family_to_variant_kind(item_family(item), tcx),
}
}).collect()
}
Expand Down Expand Up @@ -417,7 +432,8 @@ pub fn get_adt_def<'tcx>(intr: &IdentInterner,
did: did,
name: item_name(intr, doc),
fields: get_variant_fields(intr, cdata, doc, tcx),
disr_val: 0
disr_val: 0,
kind: family_to_variant_kind(item_family(doc), tcx),
}
}

Expand All @@ -428,7 +444,7 @@ pub fn get_adt_def<'tcx>(intr: &IdentInterner,
(ty::AdtKind::Enum,
get_enum_variants(intr, cdata, doc, tcx))
}
Struct => {
Struct | TupleStruct | UnitStruct => {
let ctor_did =
reader::maybe_get_doc(doc, tag_items_data_item_struct_ctor).
map_or(did, |ctor_doc| translated_def_id(cdata, ctor_doc));
Expand Down
11 changes: 8 additions & 3 deletions src/librustc_metadata/encoder.rs
Expand Up @@ -285,8 +285,9 @@ fn encode_enum_variant_info<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
rbml_w.start_tag(tag_items_data_item);
encode_def_id_and_key(ecx, rbml_w, vid);
encode_family(rbml_w, match variant.kind() {
ty::VariantKind::Unit | ty::VariantKind::Tuple => 'v',
ty::VariantKind::Struct => 'V'
ty::VariantKind::Struct => 'V',
ty::VariantKind::Tuple => 'v',
ty::VariantKind::Unit => 'w',
});
encode_name(rbml_w, variant.name);
encode_parent_item(rbml_w, ecx.tcx.map.local_def_id(id));
Expand Down Expand Up @@ -1043,7 +1044,11 @@ fn encode_info_for_item<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
/* Now, make an item for the class itself */
rbml_w.start_tag(tag_items_data_item);
encode_def_id_and_key(ecx, rbml_w, def_id);
encode_family(rbml_w, 'S');
encode_family(rbml_w, match *struct_def {
hir::VariantData::Struct(..) => 'S',
hir::VariantData::Tuple(..) => 's',
hir::VariantData::Unit(..) => 'u',
});
encode_bounds_and_type_for_item(rbml_w, ecx, index, item.id);

encode_item_variances(rbml_w, ecx, item.id);
Expand Down
7 changes: 6 additions & 1 deletion src/librustc_typeck/collect.rs
Expand Up @@ -1006,7 +1006,12 @@ fn convert_struct_variant<'tcx>(tcx: &ty::ctxt<'tcx>,
did: did,
name: name,
disr_val: disr_val,
fields: fields
fields: fields,
kind: match *def {
hir::VariantData::Struct(..) => ty::VariantKind::Struct,
hir::VariantData::Tuple(..) => ty::VariantKind::Tuple,
hir::VariantData::Unit(..) => ty::VariantKind::Unit,
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/test/auxiliary/empty-struct.rs
@@ -0,0 +1,19 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(braced_empty_structs)]

pub struct XEmpty1 {}
pub struct XEmpty2;

pub enum XE {
XEmpty3 {},
XEmpty4,
}
17 changes: 14 additions & 3 deletions src/test/compile-fail/empty-struct-braces-expr.rs
Expand Up @@ -10,17 +10,28 @@

// Can't use empty braced struct as constant or constructor function

// aux-build:empty-struct.rs

#![feature(braced_empty_structs)]

extern crate empty_struct;
use empty_struct::*;

struct Empty1 {}

enum E {
Empty2 {}
Empty3 {}
}

fn main() {
let e1 = Empty1; //~ ERROR `Empty1` is the name of a struct or struct variant
let e1 = Empty1(); //~ ERROR `Empty1` is the name of a struct or struct variant
let e2 = E::Empty2; //~ ERROR `E::Empty2` is the name of a struct or struct variant
let e2 = E::Empty2(); //~ ERROR `E::Empty2` is the name of a struct or struct variant
let e3 = E::Empty3; //~ ERROR `E::Empty3` is the name of a struct or struct variant
let e3 = E::Empty3(); //~ ERROR `E::Empty3` is the name of a struct or struct variant

// FIXME: non-local struct kind should be known early (e.g. kept in `DefStruct`)
// let xe1 = XEmpty1; // ERROR `XEmpty1` is the name of a struct or struct variant
let xe1 = XEmpty1(); //~ ERROR expected function, found `empty_struct::XEmpty1`
let xe3 = XE::Empty3; //~ ERROR no associated item named `Empty3` found for type
let xe3 = XE::Empty3(); //~ ERROR no associated item named `Empty3` found for type
}
21 changes: 17 additions & 4 deletions src/test/compile-fail/empty-struct-braces-pat-1.rs
Expand Up @@ -10,22 +10,35 @@

// Can't use empty braced struct as constant pattern

// aux-build:empty-struct.rs

#![feature(braced_empty_structs)]

extern crate empty_struct;
use empty_struct::*;

struct Empty1 {}

enum E {
Empty2 {}
Empty3 {}
}

fn main() {
let e1 = Empty1 {};
let e2 = E::Empty2 {};
let e3 = E::Empty3 {};
let xe1 = XEmpty1 {};
let xe3 = XE::XEmpty3 {};

match e1 {
Empty1 => () // Not an error, `Empty1` is interpreted as a new binding
}
match e2 {
E::Empty2 => () //~ ERROR `E::Empty2` does not name a tuple variant or a tuple struct
match e3 {
E::Empty3 => () //~ ERROR `E::Empty3` does not name a tuple variant or a tuple struct
}
match xe1 {
XEmpty1 => () // Not an error, `XEmpty1` is interpreted as a new binding
}
match xe3 {
XE::XEmpty3 => () //~ ERROR no associated item named `XEmpty3` found for type
}
}
12 changes: 12 additions & 0 deletions src/test/compile-fail/empty-struct-braces-pat-2.rs
Expand Up @@ -10,18 +10,30 @@

// Can't use empty braced struct as enum pattern

// aux-build:empty-struct.rs

#![feature(braced_empty_structs)]

extern crate empty_struct;
use empty_struct::*;

struct Empty1 {}

fn main() {
let e1 = Empty1 {};
let xe1 = XEmpty1 {};

// Rejected by parser as yet
// match e1 {
// Empty1() => () // ERROR unresolved enum variant, struct or const `Empty1`
// }
// match xe1 {
// XEmpty1() => () // ERROR unresolved enum variant, struct or const `XEmpty1`
// }
match e1 {
Empty1(..) => () //~ ERROR unresolved enum variant, struct or const `Empty1`
}
match xe1 {
XEmpty1(..) => () //~ ERROR `XEmpty1` does not name a tuple variant or a tuple struct
}
}
24 changes: 18 additions & 6 deletions src/test/compile-fail/empty-struct-braces-pat-3.rs
Expand Up @@ -10,20 +10,32 @@

// Can't use empty braced struct as enum pattern

// aux-build:empty-struct.rs

#![feature(braced_empty_structs)]

extern crate empty_struct;
use empty_struct::*;

enum E {
Empty2 {}
Empty3 {}
}

fn main() {
let e2 = E::Empty2 {};
let e3 = E::Empty3 {};
let xe3 = XE::XEmpty3 {};

// Rejected by parser as yet
// match e2 {
// E::Empty2() => () // ERROR `E::Empty2` does not name a tuple variant or a tuple struct
// match e3 {
// E::Empty3() => () // ERROR `E::Empty3` does not name a tuple variant or a tuple struct
// }
match e2 {
E::Empty2(..) => () //~ ERROR `E::Empty2` does not name a tuple variant or a tuple struct
// match xe3 {
// E::Empty3() => () // ERROR `XE::XEmpty3` does not name a tuple variant or a tuple struct
// }
match e3 {
E::Empty3(..) => () //~ ERROR `E::Empty3` does not name a tuple variant or a tuple struct
}
match xe3 {
XE::XEmpty3(..) => () //~ ERROR no associated item named `XEmpty3` found for type
}
}
15 changes: 11 additions & 4 deletions src/test/compile-fail/empty-struct-unit-expr.rs
Expand Up @@ -10,15 +10,22 @@

// Can't use unit struct as constructor function

// aux-build:empty-struct.rs

#![feature(braced_empty_structs)]

struct Empty1;
extern crate empty_struct;
use empty_struct::*;

struct Empty2;

enum E {
Empty2
Empty4
}

fn main() {
let e1 = Empty1(); //~ ERROR expected function, found `Empty1`
let e2 = E::Empty2(); //~ ERROR expected function, found `E`
let e2 = Empty2(); //~ ERROR expected function, found `Empty2`
let e4 = E::Empty4(); //~ ERROR expected function, found `E`
let xe2 = XEmpty2(); //~ ERROR expected function, found `empty_struct::XEmpty2`
let xe4 = XE::XEmpty4(); //~ ERROR expected function, found `empty_struct::XE`
}

0 comments on commit ccb4b35

Please sign in to comment.