Skip to content

Commit e51a166

Browse files
committed
Sync from rust a5c825cd824ee0ef9463021078a2f464b4cc1a0d
2 parents 8635445 + d79e13f commit e51a166

4 files changed

Lines changed: 89 additions & 16 deletions

File tree

example/mini_core.rs

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,38 @@
77
decl_macro,
88
rustc_attrs,
99
transparent_unions,
10+
pattern_types,
1011
auto_traits,
1112
freeze_impls
1213
)]
1314
#![cfg_attr(not(all(windows, target_env = "gnu")), feature(thread_local))]
1415
#![no_core]
1516
#![allow(dead_code, internal_features, ambiguous_wide_pointer_comparisons)]
1617

18+
#[lang = "pointee_trait"]
19+
pub trait Pointee: PointeeSized {
20+
#[lang = "metadata_type"]
21+
// needed so that layout_of will return `TooGeneric` instead of `Unknown`
22+
// when asked for the layout of `*const T`. Which is important for making
23+
// transmutes between raw pointers (and especially pattern types of raw pointers)
24+
// work.
25+
type Metadata: Copy + Sync + Unpin + Freeze;
26+
}
27+
28+
#[lang = "dyn_metadata"]
29+
pub struct DynMetadata<Dyn: PointeeSized> {
30+
_vtable_ptr: NonNull<VTable>,
31+
_phantom: PhantomData<Dyn>,
32+
}
33+
34+
unsafe extern "C" {
35+
/// Opaque type for accessing vtables.
36+
///
37+
/// Private implementation detail of `DynMetadata::size_of` etc.
38+
/// There is conceptually not actually any Abstract Machine memory behind this pointer.
39+
type VTable;
40+
}
41+
1742
#[lang = "pointee_sized"]
1843
pub trait PointeeSized {}
1944

@@ -104,7 +129,7 @@ unsafe impl<'a, T: PointeeSized> Sync for &'a T {}
104129
unsafe impl<T: Sync, const N: usize> Sync for [T; N] {}
105130

106131
#[lang = "freeze"]
107-
unsafe auto trait Freeze {}
132+
pub unsafe auto trait Freeze {}
108133

109134
unsafe impl<T: PointeeSized> Freeze for PhantomData<T> {}
110135
unsafe impl<T: PointeeSized> Freeze for *const T {}
@@ -569,10 +594,24 @@ pub trait Deref {
569594
fn deref(&self) -> &Self::Target;
570595
}
571596

597+
#[rustc_builtin_macro(pattern_type)]
598+
#[macro_export]
599+
macro_rules! pattern_type {
600+
($($arg:tt)*) => {
601+
/* compiler built-in */
602+
};
603+
}
604+
605+
impl<T: PointeeSized, U: PointeeSized> CoerceUnsized<pattern_type!(*const U is !null)> for pattern_type!(*const T is !null) where
606+
T: Unsize<U>
607+
{
608+
}
609+
610+
impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<pattern_type!(U is !null)> for pattern_type!(T is !null) {}
611+
572612
#[repr(transparent)]
573-
#[rustc_layout_scalar_valid_range_start(1)]
574613
#[rustc_nonnull_optimization_guaranteed]
575-
pub struct NonNull<T: PointeeSized>(pub *const T);
614+
pub struct NonNull<T: PointeeSized>(pub pattern_type!(*const T is !null));
576615

577616
impl<T: PointeeSized, U: PointeeSized> CoerceUnsized<NonNull<U>> for NonNull<T> where T: Unsize<U> {}
578617
impl<T: PointeeSized, U: PointeeSized> DispatchFromDyn<NonNull<U>> for NonNull<T> where T: Unsize<U> {}
@@ -599,7 +638,16 @@ impl<T> Box<T> {
599638
let size = size_of::<T>();
600639
let ptr = libc::malloc(size);
601640
intrinsics::copy(&val as *const T as *const u8, ptr, size);
602-
Box(Unique { pointer: NonNull(ptr as *const T), _marker: PhantomData }, Global)
641+
Box(
642+
Unique {
643+
pointer: NonNull(intrinsics::transmute::<
644+
*mut u8,
645+
pattern_type!(*const T is !null),
646+
>(ptr)),
647+
_marker: PhantomData,
648+
},
649+
Global,
650+
)
603651
}
604652
}
605653
}
@@ -608,7 +656,9 @@ impl<T: ?Sized, A> Drop for Box<T, A> {
608656
fn drop(&mut self) {
609657
// inner value is dropped by compiler
610658
unsafe {
611-
libc::free(self.0.pointer.0 as *mut u8);
659+
libc::free(intrinsics::transmute::<pattern_type!(*const T is !null), *const T>(
660+
self.0.pointer.0,
661+
) as *mut u8);
612662
}
613663
}
614664
}

example/mini_core_hello_world.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
#![feature(no_core, lang_items, never_type, extern_types, thread_local, repr_simd, rustc_private)]
1+
#![feature(
2+
no_core,
3+
lang_items,
4+
never_type,
5+
extern_types,
6+
thread_local,
7+
repr_simd,
8+
pattern_types,
9+
rustc_private
10+
)]
211
#![cfg_attr(not(any(jit, target_vendor = "apple", windows)), feature(linkage))]
312
#![no_core]
413
#![allow(dead_code, non_camel_case_types, internal_features)]
@@ -153,7 +162,10 @@ extern "C" fn bool_struct_in_11(_arg0: bool_11) {}
153162

154163
#[allow(unreachable_code)] // FIXME false positive
155164
fn main() {
156-
take_unique(Unique { pointer: unsafe { NonNull(1 as *mut ()) }, _marker: PhantomData });
165+
take_unique(Unique {
166+
pointer: unsafe { NonNull(intrinsics::transmute(1 as *mut ())) },
167+
_marker: PhantomData,
168+
});
157169
take_f32(0.1);
158170

159171
call_return_u128_pair();
@@ -219,7 +231,12 @@ fn main() {
219231
let noisy_unsized_drop = const { intrinsics::needs_drop::<NoisyDropUnsized>() };
220232
assert!(noisy_unsized_drop);
221233

222-
Unique { pointer: NonNull(1 as *mut &str), _marker: PhantomData } as Unique<dyn SomeTrait>;
234+
Unique {
235+
pointer: NonNull(intrinsics::transmute::<_, pattern_type!(*const &str is !null)>(
236+
1 as *mut &str,
237+
)),
238+
_marker: PhantomData,
239+
} as Unique<dyn SomeTrait>;
223240

224241
struct MyDst<T: ?Sized>(T);
225242

src/abi/pass_mode.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ fn reg_to_abi_param(reg: Reg) -> AbiParam {
2626
(RegKind::Float, 4) => types::F32,
2727
(RegKind::Float, 8) => types::F64,
2828
(RegKind::Float, 16) => types::F128,
29-
(RegKind::Vector, size) => types::I8.by(u32::try_from(size).unwrap()).unwrap(),
29+
(RegKind::Vector { hint_vector_elem: _ }, size) => {
30+
types::I8.by(u32::try_from(size).unwrap()).unwrap()
31+
}
3032
_ => unreachable!("{:?}", reg),
3133
};
3234
AbiParam::new(clif_ty)

src/driver/aot.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,9 @@ fn codegen_cgu_content(
380380

381381
fn module_codegen(
382382
tcx: TyCtxt<'_>,
383-
(global_asm_config, cgu_name, token): (
384-
Arc<GlobalAsmConfig>,
385-
rustc_span::Symbol,
386-
ConcurrencyLimiterToken,
387-
),
383+
global_asm_config: Arc<GlobalAsmConfig>,
384+
cgu_name: rustc_span::Symbol,
385+
token: ConcurrencyLimiterToken,
388386
) -> OngoingModuleCodegen {
389387
let mut module = make_module(tcx.sess, cgu_name.as_str().to_string());
390388

@@ -513,8 +511,14 @@ pub(crate) fn run_aot(tcx: TyCtxt<'_>) -> Box<OngoingCodegen> {
513511
let (module, _) = tcx.dep_graph.with_task(
514512
dep_node,
515513
tcx,
516-
(global_asm_config.clone(), cgu.name(), concurrency_limiter.acquire(tcx.dcx())),
517-
module_codegen,
514+
|| {
515+
module_codegen(
516+
tcx,
517+
global_asm_config.clone(),
518+
cgu.name(),
519+
concurrency_limiter.acquire(tcx.dcx()),
520+
)
521+
},
518522
Some(rustc_middle::dep_graph::hash_result),
519523
);
520524
IntoDynSyncSend(module)

0 commit comments

Comments
 (0)