Skip to content

Commit 8a04cb4

Browse files
committed
Stop backends from needing to support nullary intrinsics
1 parent 99b18d6 commit 8a04cb4

File tree

7 files changed

+33
-47
lines changed

7 files changed

+33
-47
lines changed

compiler/rustc_codegen_cranelift/example/mini_core_hello_world.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,14 @@ fn main() {
207207
assert_eq!(intrinsics::align_of::<u16>() as u8, 2);
208208
assert_eq!(intrinsics::align_of_val(&a) as u8, intrinsics::align_of::<&str>() as u8);
209209

210-
assert!(!intrinsics::needs_drop::<u8>());
211-
assert!(!intrinsics::needs_drop::<[u8]>());
212-
assert!(intrinsics::needs_drop::<NoisyDrop>());
213-
assert!(intrinsics::needs_drop::<NoisyDropUnsized>());
210+
let u8_needs_drop = const { intrinsics::needs_drop::<u8>() };
211+
assert!(!u8_needs_drop);
212+
let slice_needs_drop = const { intrinsics::needs_drop::<[u8]>() };
213+
assert!(!slice_needs_drop);
214+
let noisy_drop = const { intrinsics::needs_drop::<NoisyDrop>() };
215+
assert!(noisy_drop);
216+
let noisy_unsized_drop = const { intrinsics::needs_drop::<NoisyDropUnsized>() };
217+
assert!(noisy_unsized_drop);
214218

215219
Unique { pointer: NonNull(1 as *mut &str), _marker: PhantomData } as Unique<dyn SomeTrait>;
216220

compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -812,21 +812,6 @@ fn codegen_regular_intrinsic_call<'tcx>(
812812
dest.write_cvalue(fx, val);
813813
}
814814

815-
sym::needs_drop | sym::type_id | sym::type_name | sym::variant_count => {
816-
intrinsic_args!(fx, args => (); intrinsic);
817-
818-
let const_val = fx
819-
.tcx
820-
.const_eval_instance(
821-
ty::TypingEnv::fully_monomorphized(),
822-
instance,
823-
source_info.span,
824-
)
825-
.unwrap();
826-
let val = crate::constant::codegen_const_value(fx, const_val, ret.layout().ty);
827-
ret.write_cvalue(fx, val);
828-
}
829-
830815
sym::ptr_offset_from | sym::ptr_offset_from_unsigned => {
831816
intrinsic_args!(fx, args => (ptr, base); intrinsic);
832817
let ptr = ptr.load_scalar(fx);

compiler/rustc_codegen_gcc/example/mini_core_hello_world.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
)]
77
#![no_core]
88
#![allow(dead_code, internal_features, non_camel_case_types)]
9+
#![rustfmt::skip]
910

1011
extern crate mini_core;
1112

@@ -197,10 +198,10 @@ fn main() {
197198
assert_eq!(intrinsics::align_of::<u16>() as u8, 2);
198199
assert_eq!(intrinsics::align_of_val(&a) as u8, intrinsics::align_of::<&str>() as u8);
199200

200-
assert!(!intrinsics::needs_drop::<u8>());
201-
assert!(!intrinsics::needs_drop::<[u8]>());
202-
assert!(intrinsics::needs_drop::<NoisyDrop>());
203-
assert!(intrinsics::needs_drop::<NoisyDropUnsized>());
201+
assert!(!const { intrinsics::needs_drop::<u8>() });
202+
assert!(!const { intrinsics::needs_drop::<[u8]>() });
203+
assert!(const { intrinsics::needs_drop::<NoisyDrop>() });
204+
assert!(const { intrinsics::needs_drop::<NoisyDropUnsized>() });
204205

205206
Unique {
206207
pointer: 0 as *const &str,

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
150150
}
151151
value
152152
}
153-
sym::needs_drop | sym::type_id | sym::type_name | sym::variant_count => {
154-
let value = bx.tcx().const_eval_instance(bx.typing_env(), instance, span).unwrap();
155-
OperandRef::from_const(bx, value, result.layout.ty).immediate_or_packed_pair(bx)
156-
}
157153
sym::arith_offset => {
158154
let ty = fn_args.type_at(0);
159155
let layout = bx.layout_of(ty);

library/core/src/any.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ impl TypeId {
742742
#[stable(feature = "rust1", since = "1.0.0")]
743743
#[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
744744
pub const fn of<T: ?Sized + 'static>() -> TypeId {
745-
let t: u128 = intrinsics::type_id::<T>();
745+
let t: u128 = const { intrinsics::type_id::<T>() };
746746
let t1 = (t >> 64) as u64;
747747
let t2 = t as u64;
748748

@@ -824,7 +824,7 @@ impl fmt::Debug for TypeId {
824824
#[stable(feature = "type_name", since = "1.38.0")]
825825
#[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
826826
pub const fn type_name<T: ?Sized>() -> &'static str {
827-
intrinsics::type_name::<T>()
827+
const { intrinsics::type_name::<T>() }
828828
}
829829

830830
/// Returns the type name of the pointed-to value as a string slice.

library/core/src/intrinsics/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -839,10 +839,10 @@ pub const unsafe fn transmute_unchecked<Src, Dst>(src: Src) -> Dst;
839839
/// If the actual type neither requires drop glue nor implements
840840
/// `Copy`, then the return value of this function is unspecified.
841841
///
842-
/// Note that, unlike most intrinsics, this is safe to call;
843-
/// it does not require an `unsafe` block.
844-
/// Therefore, implementations must not require the user to uphold
845-
/// any safety invariants.
842+
/// Note that, unlike most intrinsics, this can only be called at compile-time
843+
/// as backends do not have an implementation for it. The only caller (its
844+
/// stable counterpart), wraps this intrinsic call in a `const` block so that
845+
/// backends only see an evaluated constant.
846846
///
847847
/// The stabilized version of this intrinsic is [`mem::needs_drop`](crate::mem::needs_drop).
848848
#[rustc_intrinsic_const_stable_indirect]
@@ -2655,10 +2655,10 @@ pub const fn align_of<T>() -> usize;
26552655
/// Returns the number of variants of the type `T` cast to a `usize`;
26562656
/// if `T` has no variants, returns `0`. Uninhabited variants will be counted.
26572657
///
2658-
/// Note that, unlike most intrinsics, this is safe to call;
2659-
/// it does not require an `unsafe` block.
2660-
/// Therefore, implementations must not require the user to uphold
2661-
/// any safety invariants.
2658+
/// Note that, unlike most intrinsics, this can only be called at compile-time
2659+
/// as backends do not have an implementation for it. The only caller (its
2660+
/// stable counterpart), wraps this intrinsic call in a `const` block so that
2661+
/// backends only see an evaluated constant.
26622662
///
26632663
/// The to-be-stabilized version of this intrinsic is [`crate::mem::variant_count`].
26642664
#[rustc_nounwind]
@@ -2694,10 +2694,10 @@ pub const unsafe fn align_of_val<T: ?Sized>(ptr: *const T) -> usize;
26942694

26952695
/// Gets a static string slice containing the name of a type.
26962696
///
2697-
/// Note that, unlike most intrinsics, this is safe to call;
2698-
/// it does not require an `unsafe` block.
2699-
/// Therefore, implementations must not require the user to uphold
2700-
/// any safety invariants.
2697+
/// Note that, unlike most intrinsics, this can only be called at compile-time
2698+
/// as backends do not have an implementation for it. The only caller (its
2699+
/// stable counterpart), wraps this intrinsic call in a `const` block so that
2700+
/// backends only see an evaluated constant.
27012701
///
27022702
/// The stabilized version of this intrinsic is [`core::any::type_name`].
27032703
#[rustc_nounwind]
@@ -2709,10 +2709,10 @@ pub const fn type_name<T: ?Sized>() -> &'static str;
27092709
/// function will return the same value for a type regardless of whichever
27102710
/// crate it is invoked in.
27112711
///
2712-
/// Note that, unlike most intrinsics, this is safe to call;
2713-
/// it does not require an `unsafe` block.
2714-
/// Therefore, implementations must not require the user to uphold
2715-
/// any safety invariants.
2712+
/// Note that, unlike most intrinsics, this can only be called at compile-time
2713+
/// as backends do not have an implementation for it. The only caller (its
2714+
/// stable counterpart), wraps this intrinsic call in a `const` block so that
2715+
/// backends only see an evaluated constant.
27162716
///
27172717
/// The stabilized version of this intrinsic is [`core::any::TypeId::of`].
27182718
#[rustc_nounwind]

library/core/src/mem/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ pub const unsafe fn align_of_val_raw<T: ?Sized>(val: *const T) -> usize {
616616
#[rustc_const_stable(feature = "const_mem_needs_drop", since = "1.36.0")]
617617
#[rustc_diagnostic_item = "needs_drop"]
618618
pub const fn needs_drop<T: ?Sized>() -> bool {
619-
intrinsics::needs_drop::<T>()
619+
const { intrinsics::needs_drop::<T>() }
620620
}
621621

622622
/// Returns the value of type `T` represented by the all-zero byte-pattern.
@@ -1215,7 +1215,7 @@ pub const fn discriminant<T>(v: &T) -> Discriminant<T> {
12151215
#[rustc_const_unstable(feature = "variant_count", issue = "73662")]
12161216
#[rustc_diagnostic_item = "mem_variant_count"]
12171217
pub const fn variant_count<T>() -> usize {
1218-
intrinsics::variant_count::<T>()
1218+
const { intrinsics::variant_count::<T>() }
12191219
}
12201220

12211221
/// Provides associated constants for various useful properties of types,

0 commit comments

Comments
 (0)