Skip to content

Commit 77f7e85

Browse files
plietarPaul Liétar
authored andcommitted
Implement RFC 1861: Extern types
1 parent bed9a85 commit 77f7e85

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+737
-120
lines changed

src/libcore/ptr.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,9 @@ impl<T: ?Sized> *const T {
485485
/// ```
486486
#[stable(feature = "rust1", since = "1.0.0")]
487487
#[inline]
488-
pub fn is_null(self) -> bool where T: Sized {
489-
self == null()
488+
pub fn is_null(self) -> bool {
489+
// cast to () pointer, as T may not be sized
490+
self as *const () == null()
490491
}
491492

492493
/// Returns `None` if the pointer is null, or else returns a reference to
@@ -517,7 +518,7 @@ impl<T: ?Sized> *const T {
517518
/// ```
518519
#[stable(feature = "ptr_as_ref", since = "1.9.0")]
519520
#[inline]
520-
pub unsafe fn as_ref<'a>(self) -> Option<&'a T> where T: Sized {
521+
pub unsafe fn as_ref<'a>(self) -> Option<&'a T> {
521522
if self.is_null() {
522523
None
523524
} else {
@@ -1116,8 +1117,9 @@ impl<T: ?Sized> *mut T {
11161117
/// ```
11171118
#[stable(feature = "rust1", since = "1.0.0")]
11181119
#[inline]
1119-
pub fn is_null(self) -> bool where T: Sized {
1120-
self == null_mut()
1120+
pub fn is_null(self) -> bool {
1121+
// cast to () pointer, as T may not be sized
1122+
self as *mut () == null_mut()
11211123
}
11221124

11231125
/// Returns `None` if the pointer is null, or else returns a reference to
@@ -1148,7 +1150,7 @@ impl<T: ?Sized> *mut T {
11481150
/// ```
11491151
#[stable(feature = "ptr_as_ref", since = "1.9.0")]
11501152
#[inline]
1151-
pub unsafe fn as_ref<'a>(self) -> Option<&'a T> where T: Sized {
1153+
pub unsafe fn as_ref<'a>(self) -> Option<&'a T> {
11521154
if self.is_null() {
11531155
None
11541156
} else {
@@ -1272,7 +1274,7 @@ impl<T: ?Sized> *mut T {
12721274
/// ```
12731275
#[stable(feature = "ptr_as_ref", since = "1.9.0")]
12741276
#[inline]
1275-
pub unsafe fn as_mut<'a>(self) -> Option<&'a mut T> where T: Sized {
1277+
pub unsafe fn as_mut<'a>(self) -> Option<&'a mut T> {
12761278
if self.is_null() {
12771279
None
12781280
} else {

src/librustc/hir/def.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub enum Def {
3535
Variant(DefId),
3636
Trait(DefId),
3737
TyAlias(DefId),
38+
TyForeign(DefId),
3839
AssociatedTy(DefId),
3940
PrimTy(hir::PrimTy),
4041
TyParam(DefId),
@@ -152,7 +153,7 @@ impl Def {
152153
Def::AssociatedTy(id) | Def::TyParam(id) | Def::Struct(id) | Def::StructCtor(id, ..) |
153154
Def::Union(id) | Def::Trait(id) | Def::Method(id) | Def::Const(id) |
154155
Def::AssociatedConst(id) | Def::Macro(id, ..) |
155-
Def::GlobalAsm(id) => {
156+
Def::GlobalAsm(id) | Def::TyForeign(id) => {
156157
id
157158
}
158159

@@ -186,6 +187,7 @@ impl Def {
186187
Def::StructCtor(.., CtorKind::Fictive) => bug!("impossible struct constructor"),
187188
Def::Union(..) => "union",
188189
Def::Trait(..) => "trait",
190+
Def::TyForeign(..) => "foreign type",
189191
Def::Method(..) => "method",
190192
Def::Const(..) => "constant",
191193
Def::AssociatedConst(..) => "associated constant",

src/librustc/hir/intravisit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v
704704
}
705705
}
706706
ForeignItemStatic(ref typ, _) => visitor.visit_ty(typ),
707+
ForeignItemType => (),
707708
}
708709

709710
walk_list!(visitor, visit_attribute, &foreign_item.attrs);

src/librustc/hir/lowering.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,9 @@ impl<'a> LoweringContext<'a> {
17221722
ForeignItemKind::Static(ref t, m) => {
17231723
hir::ForeignItemStatic(this.lower_ty(t), m)
17241724
}
1725+
ForeignItemKind::Ty => {
1726+
hir::ForeignItemType
1727+
}
17251728
},
17261729
vis: this.lower_visibility(&i.vis, None),
17271730
span: i.span,

src/librustc/hir/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,13 +1912,16 @@ pub enum ForeignItem_ {
19121912
/// A foreign static item (`static ext: u8`), with optional mutability
19131913
/// (the boolean is true when mutable)
19141914
ForeignItemStatic(P<Ty>, bool),
1915+
/// A foreign type
1916+
ForeignItemType,
19151917
}
19161918

19171919
impl ForeignItem_ {
19181920
pub fn descriptive_variant(&self) -> &str {
19191921
match *self {
19201922
ForeignItemFn(..) => "foreign function",
19211923
ForeignItemStatic(..) => "foreign static item",
1924+
ForeignItemType => "foreign type",
19221925
}
19231926
}
19241927
}

src/librustc/hir/print.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,13 @@ impl<'a> State<'a> {
478478
self.end()?; // end the head-ibox
479479
self.end() // end the outer cbox
480480
}
481+
hir::ForeignItemType => {
482+
self.head(&visibility_qualified(&item.vis, "type"))?;
483+
self.print_name(item.name)?;
484+
self.s.word(";")?;
485+
self.end()?; // end the head-ibox
486+
self.end() // end the outer cbox
487+
}
481488
}
482489
}
483490

src/librustc/ich/impls_hir.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,8 @@ impl_stable_hash_for!(struct hir::ForeignItem {
977977

978978
impl_stable_hash_for!(enum hir::ForeignItem_ {
979979
ForeignItemFn(fn_decl, arg_names, generics),
980-
ForeignItemStatic(ty, is_mutbl)
980+
ForeignItemStatic(ty, is_mutbl),
981+
ForeignItemType
981982
});
982983

983984
impl_stable_hash_for!(enum hir::Stmt_ {
@@ -1086,6 +1087,7 @@ impl_stable_hash_for!(enum hir::def::Def {
10861087
PrimTy(prim_ty),
10871088
TyParam(def_id),
10881089
SelfTy(trait_def_id, impl_def_id),
1090+
TyForeign(def_id),
10891091
Fn(def_id),
10901092
Const(def_id),
10911093
Static(def_id, is_mutbl),

src/librustc/ich/impls_ty.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,8 +610,7 @@ for ty::TypeVariants<'gcx>
610610
def_id.hash_stable(hcx, hasher);
611611
closure_substs.hash_stable(hcx, hasher);
612612
}
613-
TyGenerator(def_id, closure_substs, interior)
614-
=> {
613+
TyGenerator(def_id, closure_substs, interior) => {
615614
def_id.hash_stable(hcx, hasher);
616615
closure_substs.hash_stable(hcx, hasher);
617616
interior.hash_stable(hcx, hasher);
@@ -630,6 +629,9 @@ for ty::TypeVariants<'gcx>
630629
TyParam(param_ty) => {
631630
param_ty.hash_stable(hcx, hasher);
632631
}
632+
TyForeign(def_id) => {
633+
def_id.hash_stable(hcx, hasher);
634+
}
633635
TyInfer(..) => {
634636
bug!("ty::TypeVariants::hash_stable() - Unexpected variant {:?}.", *self)
635637
}

src/librustc/infer/freshen.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
312312
ty::TyNever |
313313
ty::TyTuple(..) |
314314
ty::TyProjection(..) |
315+
ty::TyForeign(..) |
315316
ty::TyParam(..) |
316317
ty::TyAnon(..) => {
317318
t.super_fold_with(self)

src/librustc/middle/resolve_lifetime.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
365365
hir::ForeignItemStatic(..) => {
366366
intravisit::walk_foreign_item(self, item);
367367
}
368+
hir::ForeignItemType => {
369+
intravisit::walk_foreign_item(self, item);
370+
}
368371
}
369372
}
370373

0 commit comments

Comments
 (0)