Skip to content

Commit dce604a

Browse files
committedOct 28, 2017
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

+3-1
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

+1
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);

0 commit comments

Comments
 (0)