Skip to content

Commit b65783f

Browse files
Ariel Ben-Yehudaarielb1
authored andcommitted
---
yaml --- r: 223228 b: refs/heads/auto c: 213b6d7 h: refs/heads/master v: v3
1 parent e8950ee commit b65783f

File tree

5 files changed

+113
-16
lines changed

5 files changed

+113
-16
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
88
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
99
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1010
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
11-
refs/heads/auto: 764310e7bb90ec14a6c6a399e703f6b455ba52d3
11+
refs/heads/auto: 213b6d71f502a8568208cf549050a71cc24b1338
1212
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1313
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336
1414
refs/tags/0.2: 1754d02027f2924bed83b0160ee340c7f41d5ea1

branches/auto/src/librustc/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#![feature(clone_from_slice)]
3333
#![feature(collections)]
3434
#![feature(const_fn)]
35+
#![feature(core)]
3536
#![feature(duration)]
3637
#![feature(duration_span)]
3738
#![feature(dynamic_lib)]
@@ -42,6 +43,7 @@
4243
#![feature(iter_cmp)]
4344
#![feature(iter_arith)]
4445
#![feature(libc)]
46+
#![feature(nonzero)]
4547
#![feature(num_bits_bytes)]
4648
#![feature(path_ext)]
4749
#![feature(quote)]
@@ -65,6 +67,7 @@
6567
#![allow(trivial_casts)]
6668

6769
extern crate arena;
70+
extern crate core;
6871
extern crate flate;
6972
extern crate fmt_macros;
7073
extern crate getopts;

branches/auto/src/librustc/metadata/decoder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,8 @@ pub fn get_adt_def<'tcx>(cdata: Cmd,
394394
item_id: ast::NodeId,
395395
tcx: &ty::ctxt<'tcx>) -> &'tcx ty::ADTDef<'tcx>
396396
{
397-
tcx.intern_adt_def(ast::DefId { krate: cdata.cnum, node: item_id })
397+
tcx.intern_adt_def(ast::DefId { krate: cdata.cnum, node: item_id },
398+
ty::ADTKind::Enum)
398399
}
399400

400401
pub fn get_predicates<'tcx>(cdata: Cmd,

branches/auto/src/librustc/middle/ty.rs

Lines changed: 105 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,15 @@ use std::cell::{Cell, RefCell, Ref};
7474
use std::cmp;
7575
use std::fmt;
7676
use std::hash::{Hash, SipHasher, Hasher};
77+
use std::marker::PhantomData;
7778
use std::mem;
7879
use std::ops;
7980
use std::rc::Rc;
8081
use std::vec::IntoIter;
8182
use collections::enum_set::{self, EnumSet, CLike};
83+
use core::nonzero::NonZero;
8284
use std::collections::{HashMap, HashSet};
85+
use rustc_data_structures::ivar;
8386
use syntax::abi;
8487
use syntax::ast::{CrateNum, DefId, ItemImpl, ItemTrait, LOCAL_CRATE};
8588
use syntax::ast::{MutImmutable, MutMutable, Name, NamedField, NodeId};
@@ -721,7 +724,7 @@ pub struct CtxtArenas<'tcx> {
721724

722725
// references
723726
trait_defs: TypedArena<TraitDef<'tcx>>,
724-
adt_defs: TypedArena<ADTDef<'tcx>>,
727+
adt_defs: TypedArena<ADTDef_<'tcx, 'tcx>>,
725728
}
726729

727730
impl<'tcx> CtxtArenas<'tcx> {
@@ -1020,9 +1023,10 @@ impl<'tcx> ctxt<'tcx> {
10201023
interned
10211024
}
10221025

1023-
pub fn intern_adt_def(&self, did: DefId) -> &'tcx ADTDef<'tcx> {
1024-
let def = ADTDef::new(self, did);
1026+
pub fn intern_adt_def(&self, did: DefId, kind: ADTKind) -> &'tcx ADTDef_<'tcx, 'tcx> {
1027+
let def = ADTDef_::new(self, did, kind);
10251028
let interned = self.arenas.adt_defs.alloc(def);
1029+
// this will need a transmute when reverse-variance is removed
10261030
self.adt_defs.borrow_mut().insert(did, interned);
10271031
interned
10281032
}
@@ -1395,6 +1399,61 @@ impl<'tcx> Hash for TyS<'tcx> {
13951399

13961400
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
13971401

1402+
/// An IVar that contains a Ty. 'lt is a (reverse-variant) upper bound
1403+
/// on the lifetime of the IVar. This is required because of variance
1404+
/// problems: the IVar needs to be variant with respect to 'tcx (so
1405+
/// it can be referred to from Ty) but can only be modified if its
1406+
/// lifetime is exactly 'tcx.
1407+
///
1408+
/// Safety invariants:
1409+
/// (A) self.0, if fulfilled, is a valid Ty<'tcx>
1410+
/// (B) no aliases to this value with a 'tcx longer than this
1411+
/// value's 'lt exist
1412+
///
1413+
/// NonZero is used rather than Unique because Unique isn't Copy.
1414+
pub struct TyIVar<'tcx, 'lt: 'tcx>(ivar::Ivar<NonZero<*const TyS<'static>>>,
1415+
PhantomData<fn(TyS<'lt>)->TyS<'tcx>>);
1416+
1417+
impl<'tcx, 'lt> TyIVar<'tcx, 'lt> {
1418+
#[inline]
1419+
pub fn new() -> Self {
1420+
// Invariant (A) satisfied because the IVar is unfulfilled
1421+
// Invariant (B) because 'lt : 'tcx
1422+
TyIVar(ivar::Ivar::new(), PhantomData)
1423+
}
1424+
1425+
#[inline]
1426+
pub fn get(&self) -> Option<Ty<'tcx>> {
1427+
match self.0.get() {
1428+
None => None,
1429+
// valid because of invariant (A)
1430+
Some(v) => Some(unsafe { &*(*v as *const TyS<'tcx>) })
1431+
}
1432+
}
1433+
#[inline]
1434+
pub fn unwrap(&self) -> Ty<'tcx> {
1435+
self.get().unwrap()
1436+
}
1437+
1438+
pub fn fulfill(&self, value: Ty<'lt>) {
1439+
// Invariant (A) is fulfilled, because by (B), every alias
1440+
// of this has a 'tcx longer than 'lt.
1441+
let value: *const TyS<'lt> = value;
1442+
// FIXME(27214): unneeded [as *const ()]
1443+
let value = value as *const () as *const TyS<'static>;
1444+
self.0.fulfill(unsafe { NonZero::new(value) })
1445+
}
1446+
}
1447+
1448+
impl<'tcx, 'lt> fmt::Debug for TyIVar<'tcx, 'lt> {
1449+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1450+
match self.get() {
1451+
Some(val) => write!(f, "TyIVar({:?})", val),
1452+
None => f.write_str("TyIVar(<unfulfilled>)")
1453+
}
1454+
}
1455+
}
1456+
13981457
/// An entry in the type interner.
13991458
pub struct InternedTy<'tcx> {
14001459
ty: Ty<'tcx>
@@ -3210,44 +3269,69 @@ bitflags! {
32103269
const IS_PHANTOM_DATA = 1 << 1,
32113270
const IS_DTORCK = 1 << 2, // is this a dtorck type?
32123271
const IS_DTORCK_VALID = 1 << 3,
3272+
const IS_ENUM = 1 << 4
32133273
}
32143274
}
32153275

3216-
/// The definition of an abstract data type - a struct or enum.
3217-
pub struct ADTDef<'tcx> {
3276+
pub type ADTDef<'tcx> = ADTDef_<'tcx, 'static>;
3277+
3278+
pub struct VariantDef<'tcx, 'lt: 'tcx> {
3279+
pub did: DefId,
3280+
pub name: Name, // struct's name if this is a struct
3281+
pub disr_val: Disr,
3282+
pub fields: Vec<FieldDef<'tcx, 'lt>>
3283+
}
3284+
3285+
pub struct FieldDef<'tcx, 'lt: 'tcx> {
3286+
pub did: DefId,
3287+
pub name: Name, // XXX if tuple-like
3288+
pub vis: ast::Visibility,
3289+
// TyIVar is used here to allow for
3290+
ty: TyIVar<'tcx, 'lt>
3291+
}
3292+
3293+
/// The definition of an abstract data type - a struct or enum. 'lt
3294+
/// is here so 'tcx can be variant.
3295+
pub struct ADTDef_<'tcx, 'lt: 'tcx> {
32183296
pub did: DefId,
3297+
pub variants: Vec<VariantDef<'tcx, 'lt>>,
32193298
flags: Cell<ADTFlags>,
3220-
marker: ::std::marker::PhantomData<&'tcx ()>,
32213299
}
32223300

3223-
impl<'tcx> PartialEq for ADTDef<'tcx> {
3301+
impl<'tcx, 'lt> PartialEq for ADTDef_<'tcx, 'lt> {
32243302
// ADTDef are always interned and this is part of TyS equality
32253303
#[inline]
32263304
fn eq(&self, other: &Self) -> bool { self as *const _ == other as *const _ }
32273305
}
32283306

3229-
impl<'tcx> Eq for ADTDef<'tcx> {}
3307+
impl<'tcx, 'lt> Eq for ADTDef_<'tcx, 'lt> {}
32303308

3231-
impl<'tcx> Hash for ADTDef<'tcx> {
3309+
impl<'tcx, 'lt> Hash for ADTDef_<'tcx, 'lt> {
32323310
#[inline]
32333311
fn hash<H: Hasher>(&self, s: &mut H) {
32343312
(self as *const ADTDef).hash(s)
32353313
}
32363314
}
32373315

3238-
impl<'tcx> ADTDef<'tcx> {
3239-
fn new(tcx: &ctxt<'tcx>, did: DefId) -> Self {
3316+
#[derive(Copy, Clone, Debug)]
3317+
pub enum ADTKind { Struct, Enum }
3318+
3319+
impl<'tcx, 'lt> ADTDef_<'tcx, 'lt> {
3320+
fn new(tcx: &ctxt<'tcx>, did: DefId, kind: ADTKind) -> Self {
32403321
let mut flags = ADTFlags::NO_ADT_FLAGS;
32413322
if tcx.has_attr(did, "fundamental") {
32423323
flags = flags | ADTFlags::IS_FUNDAMENTAL;
32433324
}
32443325
if Some(did) == tcx.lang_items.phantom_data() {
32453326
flags = flags | ADTFlags::IS_PHANTOM_DATA;
32463327
}
3328+
if let ADTKind::Enum = kind {
3329+
flags = flags | ADTFlags::IS_ENUM;
3330+
}
32473331
ADTDef {
32483332
did: did,
3333+
variants: vec![],
32493334
flags: Cell::new(flags),
3250-
marker: ::std::marker::PhantomData
32513335
}
32523336
}
32533337

@@ -3258,6 +3342,15 @@ impl<'tcx> ADTDef<'tcx> {
32583342
self.flags.set(self.flags.get() | ADTFlags::IS_DTORCK_VALID)
32593343
}
32603344

3345+
#[inline]
3346+
pub fn adt_kind(&self) -> ADTKind {
3347+
if self.flags.get().intersects(ADTFlags::IS_ENUM) {
3348+
ADTKind::Enum
3349+
} else {
3350+
ADTKind::Struct
3351+
}
3352+
}
3353+
32613354
#[inline]
32623355
pub fn is_dtorck(&'tcx self, tcx: &ctxt<'tcx>) -> bool {
32633356
if !self.flags.get().intersects(ADTFlags::IS_DTORCK_VALID) {

branches/auto/src/librustc_typeck/collect.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,14 +1473,14 @@ fn compute_type_scheme_of_item<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
14731473
// Create a new generic polytype.
14741474
let ty_generics = ty_generics_for_type_or_impl(ccx, generics);
14751475
let substs = mk_item_substs(ccx, &ty_generics);
1476-
let def = tcx.intern_adt_def(local_def(it.id));
1476+
let def = tcx.intern_adt_def(local_def(it.id), ty::ADTKind::Enum);
14771477
let t = tcx.mk_enum(def, tcx.mk_substs(substs));
14781478
ty::TypeScheme { ty: t, generics: ty_generics }
14791479
}
14801480
ast::ItemStruct(_, ref generics) => {
14811481
let ty_generics = ty_generics_for_type_or_impl(ccx, generics);
14821482
let substs = mk_item_substs(ccx, &ty_generics);
1483-
let def = tcx.intern_adt_def(local_def(it.id));
1483+
let def = tcx.intern_adt_def(local_def(it.id), ty::ADTKind::Struct);
14841484
let t = tcx.mk_struct(def, tcx.mk_substs(substs));
14851485
ty::TypeScheme { ty: t, generics: ty_generics }
14861486
}

0 commit comments

Comments
 (0)