Skip to content

Commit dce604a

Browse files
committed
Auto merge of #44295 - plietar:extern-types, r=arielb1
Implement RFC 1861: Extern types A few notes : - Type parameters are not supported. This was an unresolved question from the RFC. It is not clear how useful this feature is, and how variance should be treated. This can be added in a future PR. - `size_of_val` / `align_of_val` can be called with extern types, and respectively return 0 and 1. This differs from the RFC, which specified that they should panic, but after discussion with @eddyb on IRC this seems like a better solution. If/when a `DynSized` trait is added, this will be disallowed statically. - Auto traits are not implemented by default, since the contents of extern types is unknown. This means extern types are `!Sync`, `!Send` and `!Freeze`. This seems like the correct behaviour to me. Manual `unsafe impl Sync for Foo` is still possible. - This PR allows extern type to be used as the tail of a struct, as described by the RFC : ```rust extern { type OpaqueTail; } #[repr(C)] struct FfiStruct { data: u8, more_data: u32, tail: OpaqueTail, } ``` However this is undesirable, as the alignment of `tail` is unknown (the current PR assumes an alignment of 1). Unfortunately we can't prevent it in the general case as the tail could be a type parameter : ```rust #[repr(C)] struct FfiStruct<T: ?Sized> { data: u8, more_data: u32, tail: T, } ``` Adding a `DynSized` trait would solve this as well, by requiring tail fields to be bound by it. - Despite being unsized, pointers to extern types are thin and can be casted from/to integers. However it is not possible to write a `null<T>() -> *const T` function which works with extern types, as I've explained here : #43467 (comment) - Trait objects cannot be built from extern types. I intend to support it eventually, although how this interacts with `DynSized`/`size_of_val` is still unclear. - The definition of `c_void` is unmodified
2 parents 7da9a5e + 1e9e319 commit dce604a

Some content is hidden

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

82 files changed

+732
-114
lines changed

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

src/librustc/traits/coherence.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,10 @@ fn ty_is_local_constructor(ty: Ty, infer_is_local: InferIsLocal)-> bool {
304304
def.did.is_local()
305305
}
306306

307+
ty::TyForeign(did) => {
308+
did.is_local()
309+
}
310+
307311
ty::TyDynamic(ref tt, ..) => {
308312
tt.principal().map_or(false, |p| p.def_id().is_local())
309313
}

0 commit comments

Comments
 (0)