Skip to content

Commit c42ccb8

Browse files
committed
Auto merge of #30896 - petrochenkov:vkindmeta, r=alexcrichton
Also add tests for use of empty structs in cross-crate scenarios Some tests are commented out, they depend on fixes from #30882
2 parents e7e4ecc + ccb4b35 commit c42ccb8

12 files changed

+229
-50
lines changed

Diff for: src/librustc/middle/ty/mod.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use std::collections::{HashMap, HashSet};
4949
use syntax::ast::{self, CrateNum, Name, NodeId};
5050
use syntax::attr::{self, AttrMetaMethods};
5151
use syntax::codemap::{DUMMY_SP, Span};
52-
use syntax::parse::token::{InternedString, special_idents};
52+
use syntax::parse::token::InternedString;
5353

5454
use rustc_front::hir;
5555
use rustc_front::hir::{ItemImpl, ItemTrait};
@@ -1353,6 +1353,7 @@ pub struct VariantDefData<'tcx, 'container: 'tcx> {
13531353
pub name: Name, // struct's name if this is a struct
13541354
pub disr_val: Disr,
13551355
pub fields: Vec<FieldDefData<'tcx, 'container>>,
1356+
pub kind: VariantKind,
13561357
}
13571358

13581359
pub struct FieldDefData<'tcx, 'container: 'tcx> {
@@ -1607,13 +1608,7 @@ impl<'tcx, 'container> VariantDefData<'tcx, 'container> {
16071608
}
16081609

16091610
pub fn kind(&self) -> VariantKind {
1610-
match self.fields.get(0) {
1611-
None => VariantKind::Unit,
1612-
Some(&FieldDefData { name, .. }) if name == special_idents::unnamed_field.name => {
1613-
VariantKind::Tuple
1614-
}
1615-
Some(_) => VariantKind::Struct
1616-
}
1611+
self.kind
16171612
}
16181613

16191614
pub fn is_tuple_struct(&self) -> bool {

Diff for: src/librustc_metadata/decoder.rs

+24-8
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,15 @@ enum Family {
101101
Mod, // m
102102
ForeignMod, // n
103103
Enum, // t
104-
TupleVariant, // v
105104
StructVariant, // V
105+
TupleVariant, // v
106+
UnitVariant, // w
106107
Impl, // i
107-
DefaultImpl, // d
108+
DefaultImpl, // d
108109
Trait, // I
109110
Struct, // S
111+
TupleStruct, // s
112+
UnitStruct, // u
110113
PublicField, // g
111114
InheritedField, // N
112115
Constant, // C
@@ -126,12 +129,15 @@ fn item_family(item: rbml::Doc) -> Family {
126129
'm' => Mod,
127130
'n' => ForeignMod,
128131
't' => Enum,
129-
'v' => TupleVariant,
130132
'V' => StructVariant,
133+
'v' => TupleVariant,
134+
'w' => UnitVariant,
131135
'i' => Impl,
132136
'd' => DefaultImpl,
133137
'I' => Trait,
134138
'S' => Struct,
139+
's' => TupleStruct,
140+
'u' => UnitStruct,
135141
'g' => PublicField,
136142
'N' => InheritedField,
137143
c => panic!("unexpected family char: {}", c)
@@ -282,7 +288,7 @@ fn item_to_def_like(cdata: Cmd, item: rbml::Doc, did: DefId) -> DefLike {
282288
}
283289
ImmStatic => DlDef(def::DefStatic(did, false)),
284290
MutStatic => DlDef(def::DefStatic(did, true)),
285-
Struct => DlDef(def::DefStruct(did)),
291+
Struct | TupleStruct | UnitStruct => DlDef(def::DefStruct(did)),
286292
Fn => DlDef(def::DefFn(did, false)),
287293
CtorFn => DlDef(def::DefFn(did, true)),
288294
Method | StaticMethod => {
@@ -302,7 +308,7 @@ fn item_to_def_like(cdata: Cmd, item: rbml::Doc, did: DefId) -> DefLike {
302308
let enum_did = item_require_parent_item(cdata, item);
303309
DlDef(def::DefVariant(enum_did, did, true))
304310
}
305-
TupleVariant => {
311+
TupleVariant | UnitVariant => {
306312
let enum_did = item_require_parent_item(cdata, item);
307313
DlDef(def::DefVariant(enum_did, did, false))
308314
}
@@ -365,6 +371,14 @@ pub fn get_adt_def<'tcx>(intr: &IdentInterner,
365371
item_id: DefIndex,
366372
tcx: &ty::ctxt<'tcx>) -> ty::AdtDefMaster<'tcx>
367373
{
374+
fn family_to_variant_kind<'tcx>(family: Family, tcx: &ty::ctxt<'tcx>) -> ty::VariantKind {
375+
match family {
376+
Struct | StructVariant => ty::VariantKind::Struct,
377+
TupleStruct | TupleVariant => ty::VariantKind::Tuple,
378+
UnitStruct | UnitVariant => ty::VariantKind::Unit,
379+
_ => tcx.sess.bug(&format!("unexpected family: {:?}", family)),
380+
}
381+
}
368382
fn get_enum_variants<'tcx>(intr: &IdentInterner,
369383
cdata: Cmd,
370384
doc: rbml::Doc,
@@ -384,7 +398,8 @@ pub fn get_adt_def<'tcx>(intr: &IdentInterner,
384398
did: did,
385399
name: item_name(intr, item),
386400
fields: get_variant_fields(intr, cdata, item, tcx),
387-
disr_val: disr
401+
disr_val: disr,
402+
kind: family_to_variant_kind(item_family(item), tcx),
388403
}
389404
}).collect()
390405
}
@@ -417,7 +432,8 @@ pub fn get_adt_def<'tcx>(intr: &IdentInterner,
417432
did: did,
418433
name: item_name(intr, doc),
419434
fields: get_variant_fields(intr, cdata, doc, tcx),
420-
disr_val: 0
435+
disr_val: 0,
436+
kind: family_to_variant_kind(item_family(doc), tcx),
421437
}
422438
}
423439

@@ -428,7 +444,7 @@ pub fn get_adt_def<'tcx>(intr: &IdentInterner,
428444
(ty::AdtKind::Enum,
429445
get_enum_variants(intr, cdata, doc, tcx))
430446
}
431-
Struct => {
447+
Struct | TupleStruct | UnitStruct => {
432448
let ctor_did =
433449
reader::maybe_get_doc(doc, tag_items_data_item_struct_ctor).
434450
map_or(did, |ctor_doc| translated_def_id(cdata, ctor_doc));

Diff for: src/librustc_metadata/encoder.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,9 @@ fn encode_enum_variant_info<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
285285
rbml_w.start_tag(tag_items_data_item);
286286
encode_def_id_and_key(ecx, rbml_w, vid);
287287
encode_family(rbml_w, match variant.kind() {
288-
ty::VariantKind::Unit | ty::VariantKind::Tuple => 'v',
289-
ty::VariantKind::Struct => 'V'
288+
ty::VariantKind::Struct => 'V',
289+
ty::VariantKind::Tuple => 'v',
290+
ty::VariantKind::Unit => 'w',
290291
});
291292
encode_name(rbml_w, variant.name);
292293
encode_parent_item(rbml_w, ecx.tcx.map.local_def_id(id));
@@ -1043,7 +1044,11 @@ fn encode_info_for_item<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
10431044
/* Now, make an item for the class itself */
10441045
rbml_w.start_tag(tag_items_data_item);
10451046
encode_def_id_and_key(ecx, rbml_w, def_id);
1046-
encode_family(rbml_w, 'S');
1047+
encode_family(rbml_w, match *struct_def {
1048+
hir::VariantData::Struct(..) => 'S',
1049+
hir::VariantData::Tuple(..) => 's',
1050+
hir::VariantData::Unit(..) => 'u',
1051+
});
10471052
encode_bounds_and_type_for_item(rbml_w, ecx, index, item.id);
10481053

10491054
encode_item_variances(rbml_w, ecx, item.id);

Diff for: src/librustc_typeck/collect.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,12 @@ fn convert_struct_variant<'tcx>(tcx: &ty::ctxt<'tcx>,
10061006
did: did,
10071007
name: name,
10081008
disr_val: disr_val,
1009-
fields: fields
1009+
fields: fields,
1010+
kind: match *def {
1011+
hir::VariantData::Struct(..) => ty::VariantKind::Struct,
1012+
hir::VariantData::Tuple(..) => ty::VariantKind::Tuple,
1013+
hir::VariantData::Unit(..) => ty::VariantKind::Unit,
1014+
}
10101015
}
10111016
}
10121017

Diff for: src/test/auxiliary/empty-struct.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(braced_empty_structs)]
12+
13+
pub struct XEmpty1 {}
14+
pub struct XEmpty2;
15+
16+
pub enum XE {
17+
XEmpty3 {},
18+
XEmpty4,
19+
}

Diff for: src/test/compile-fail/empty-struct-braces-expr.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,28 @@
1010

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

13+
// aux-build:empty-struct.rs
14+
1315
#![feature(braced_empty_structs)]
1416

17+
extern crate empty_struct;
18+
use empty_struct::*;
19+
1520
struct Empty1 {}
1621

1722
enum E {
18-
Empty2 {}
23+
Empty3 {}
1924
}
2025

2126
fn main() {
2227
let e1 = Empty1; //~ ERROR `Empty1` is the name of a struct or struct variant
2328
let e1 = Empty1(); //~ ERROR `Empty1` is the name of a struct or struct variant
24-
let e2 = E::Empty2; //~ ERROR `E::Empty2` is the name of a struct or struct variant
25-
let e2 = E::Empty2(); //~ ERROR `E::Empty2` is the name of a struct or struct variant
29+
let e3 = E::Empty3; //~ ERROR `E::Empty3` is the name of a struct or struct variant
30+
let e3 = E::Empty3(); //~ ERROR `E::Empty3` is the name of a struct or struct variant
31+
32+
// FIXME: non-local struct kind should be known early (e.g. kept in `DefStruct`)
33+
// let xe1 = XEmpty1; // ERROR `XEmpty1` is the name of a struct or struct variant
34+
let xe1 = XEmpty1(); //~ ERROR expected function, found `empty_struct::XEmpty1`
35+
let xe3 = XE::Empty3; //~ ERROR no associated item named `Empty3` found for type
36+
let xe3 = XE::Empty3(); //~ ERROR no associated item named `Empty3` found for type
2637
}

Diff for: src/test/compile-fail/empty-struct-braces-pat-1.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,35 @@
1010

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

13+
// aux-build:empty-struct.rs
14+
1315
#![feature(braced_empty_structs)]
1416

17+
extern crate empty_struct;
18+
use empty_struct::*;
19+
1520
struct Empty1 {}
1621

1722
enum E {
18-
Empty2 {}
23+
Empty3 {}
1924
}
2025

2126
fn main() {
2227
let e1 = Empty1 {};
23-
let e2 = E::Empty2 {};
28+
let e3 = E::Empty3 {};
29+
let xe1 = XEmpty1 {};
30+
let xe3 = XE::XEmpty3 {};
2431

2532
match e1 {
2633
Empty1 => () // Not an error, `Empty1` is interpreted as a new binding
2734
}
28-
match e2 {
29-
E::Empty2 => () //~ ERROR `E::Empty2` does not name a tuple variant or a tuple struct
35+
match e3 {
36+
E::Empty3 => () //~ ERROR `E::Empty3` does not name a tuple variant or a tuple struct
37+
}
38+
match xe1 {
39+
XEmpty1 => () // Not an error, `XEmpty1` is interpreted as a new binding
40+
}
41+
match xe3 {
42+
XE::XEmpty3 => () //~ ERROR no associated item named `XEmpty3` found for type
3043
}
3144
}

Diff for: src/test/compile-fail/empty-struct-braces-pat-2.rs

+12
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,30 @@
1010

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

13+
// aux-build:empty-struct.rs
14+
1315
#![feature(braced_empty_structs)]
1416

17+
extern crate empty_struct;
18+
use empty_struct::*;
19+
1520
struct Empty1 {}
1621

1722
fn main() {
1823
let e1 = Empty1 {};
24+
let xe1 = XEmpty1 {};
1925

2026
// Rejected by parser as yet
2127
// match e1 {
2228
// Empty1() => () // ERROR unresolved enum variant, struct or const `Empty1`
2329
// }
30+
// match xe1 {
31+
// XEmpty1() => () // ERROR unresolved enum variant, struct or const `XEmpty1`
32+
// }
2433
match e1 {
2534
Empty1(..) => () //~ ERROR unresolved enum variant, struct or const `Empty1`
2635
}
36+
match xe1 {
37+
XEmpty1(..) => () //~ ERROR `XEmpty1` does not name a tuple variant or a tuple struct
38+
}
2739
}

Diff for: src/test/compile-fail/empty-struct-braces-pat-3.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,32 @@
1010

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

13+
// aux-build:empty-struct.rs
14+
1315
#![feature(braced_empty_structs)]
1416

17+
extern crate empty_struct;
18+
use empty_struct::*;
19+
1520
enum E {
16-
Empty2 {}
21+
Empty3 {}
1722
}
1823

1924
fn main() {
20-
let e2 = E::Empty2 {};
25+
let e3 = E::Empty3 {};
26+
let xe3 = XE::XEmpty3 {};
2127

2228
// Rejected by parser as yet
23-
// match e2 {
24-
// E::Empty2() => () // ERROR `E::Empty2` does not name a tuple variant or a tuple struct
29+
// match e3 {
30+
// E::Empty3() => () // ERROR `E::Empty3` does not name a tuple variant or a tuple struct
2531
// }
26-
match e2 {
27-
E::Empty2(..) => () //~ ERROR `E::Empty2` does not name a tuple variant or a tuple struct
32+
// match xe3 {
33+
// E::Empty3() => () // ERROR `XE::XEmpty3` does not name a tuple variant or a tuple struct
34+
// }
35+
match e3 {
36+
E::Empty3(..) => () //~ ERROR `E::Empty3` does not name a tuple variant or a tuple struct
37+
}
38+
match xe3 {
39+
XE::XEmpty3(..) => () //~ ERROR no associated item named `XEmpty3` found for type
2840
}
2941
}

Diff for: src/test/compile-fail/empty-struct-unit-expr.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,22 @@
1010

1111
// Can't use unit struct as constructor function
1212

13+
// aux-build:empty-struct.rs
14+
1315
#![feature(braced_empty_structs)]
1416

15-
struct Empty1;
17+
extern crate empty_struct;
18+
use empty_struct::*;
19+
20+
struct Empty2;
1621

1722
enum E {
18-
Empty2
23+
Empty4
1924
}
2025

2126
fn main() {
22-
let e1 = Empty1(); //~ ERROR expected function, found `Empty1`
23-
let e2 = E::Empty2(); //~ ERROR expected function, found `E`
27+
let e2 = Empty2(); //~ ERROR expected function, found `Empty2`
28+
let e4 = E::Empty4(); //~ ERROR expected function, found `E`
29+
let xe2 = XEmpty2(); //~ ERROR expected function, found `empty_struct::XEmpty2`
30+
let xe4 = XE::XEmpty4(); //~ ERROR expected function, found `empty_struct::XE`
2431
}

0 commit comments

Comments
 (0)