Skip to content

Commit 4285e58

Browse files
Auto merge of #148190 - RalfJung:box_new, r=<try>
replace box_new with lower-level intrinsics
2 parents e22dab3 + 5ca0e9c commit 4285e58

File tree

94 files changed

+1155
-1439
lines changed

Some content is hidden

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

94 files changed

+1155
-1439
lines changed

compiler/rustc_codegen_cranelift/example/mini_core.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -622,11 +622,6 @@ impl<T: ?Sized> Deref for Box<T> {
622622
}
623623
}
624624

625-
#[lang = "exchange_malloc"]
626-
unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
627-
unsafe { libc::malloc(size) }
628-
}
629-
630625
#[lang = "drop"]
631626
pub trait Drop {
632627
fn drop(&mut self);

compiler/rustc_codegen_gcc/example/mini_core.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -628,11 +628,6 @@ impl<T: ?Sized, A: Allocator> Deref for Box<T, A> {
628628
}
629629
}
630630

631-
#[lang = "exchange_malloc"]
632-
unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
633-
libc::malloc(size)
634-
}
635-
636631
#[lang = "drop"]
637632
pub trait Drop {
638633
fn drop(&mut self);

compiler/rustc_const_eval/messages.ftl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,6 @@ const_eval_too_many_caller_args =
384384
385385
const_eval_unallowed_fn_pointer_call = function pointer calls are not allowed in {const_eval_const_context}s
386386
387-
const_eval_unallowed_heap_allocations =
388-
allocations are not allowed in {const_eval_const_context}s
389-
.label = allocation not allowed in {const_eval_const_context}s
390-
.teach_note =
391-
The runtime heap is not yet available at compile-time, so no runtime heap allocations can be created.
392-
393387
const_eval_unallowed_inline_asm =
394388
inline assembly is not allowed in {const_eval_const_context}s
395389

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -845,13 +845,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
845845
return;
846846
}
847847

848-
// This can be called on stable via the `vec!` macro.
849-
if tcx.is_lang_item(callee, LangItem::ExchangeMalloc) {
850-
self.check_op(ops::HeapAllocation);
851-
// Allow this call, skip all the checks below.
852-
return;
853-
}
854-
855848
// Intrinsics are language primitives, not regular calls, so treat them separately.
856849
if let Some(intrinsic) = tcx.intrinsic(callee) {
857850
if !tcx.is_const_fn(callee) {

compiler/rustc_const_eval/src/check_consts/ops.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -541,18 +541,6 @@ impl<'tcx> NonConstOp<'tcx> for Coroutine {
541541
}
542542
}
543543

544-
#[derive(Debug)]
545-
pub(crate) struct HeapAllocation;
546-
impl<'tcx> NonConstOp<'tcx> for HeapAllocation {
547-
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
548-
ccx.dcx().create_err(errors::UnallowedHeapAllocations {
549-
span,
550-
kind: ccx.const_kind(),
551-
teach: ccx.tcx.sess.teach(E0010),
552-
})
553-
}
554-
}
555-
556544
#[derive(Debug)]
557545
pub(crate) struct InlineAsm;
558546
impl<'tcx> NonConstOp<'tcx> for InlineAsm {

compiler/rustc_const_eval/src/errors.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,6 @@ pub(crate) struct UnallowedOpInConstContext {
215215
pub msg: String,
216216
}
217217

218-
#[derive(Diagnostic)]
219-
#[diag(const_eval_unallowed_heap_allocations, code = E0010)]
220-
pub(crate) struct UnallowedHeapAllocations {
221-
#[primary_span]
222-
#[label]
223-
pub span: Span,
224-
pub kind: ConstContext,
225-
#[note(const_eval_teach_note)]
226-
pub teach: bool,
227-
}
228-
229218
#[derive(Diagnostic)]
230219
#[diag(const_eval_unallowed_inline_asm, code = E0015)]
231220
pub(crate) struct UnallowedInlineAsm {
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
#### Note: this error code is no longer emitted by the compiler.
2+
13
The value of statics and constants must be known at compile time, and they live
24
for the entire lifetime of a program. Creating a boxed value allocates memory on
35
the heap at runtime, and therefore cannot be done at compile time.
46

57
Erroneous code example:
68

7-
```compile_fail,E0010
9+
```ignore (no longer emitted)
810
const CON : Vec<i32> = vec![1, 2, 3];
911
```

compiler/rustc_error_codes/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
//
2424
// Do *not* remove entries from this list. Instead, just add a note th the corresponding markdown
2525
// file saying that this error is not emitted by the compiler any more (see E0001.md for an
26-
// example), and remove all code examples that do not build any more.
26+
// example), and remove all code examples that do not build any more by marking them
27+
// with `ignore (no longer emitted)`.
2728
#[macro_export]
2829
macro_rules! error_codes {
2930
($macro:path) => (

compiler/rustc_hir/src/lang_items.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ language_item_table! {
331331
FormatArgument, sym::format_argument, format_argument, Target::Struct, GenericRequirement::None;
332332
FormatArguments, sym::format_arguments, format_arguments, Target::Struct, GenericRequirement::None;
333333

334-
ExchangeMalloc, sym::exchange_malloc, exchange_malloc_fn, Target::Fn, GenericRequirement::None;
335334
DropInPlace, sym::drop_in_place, drop_in_place_fn, Target::Fn, GenericRequirement::Minimum(1);
336335
AllocLayout, sym::alloc_layout, alloc_layout, Target::Struct, GenericRequirement::None;
337336

compiler/rustc_hir_analysis/src/check/intrinsic.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hi
7676
| sym::autodiff
7777
| sym::bitreverse
7878
| sym::black_box
79-
| sym::box_new
8079
| sym::breakpoint
8180
| sym::bswap
8281
| sym::caller_location
@@ -217,6 +216,7 @@ fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hi
217216
| sym::wrapping_add
218217
| sym::wrapping_mul
219218
| sym::wrapping_sub
219+
| sym::write_box_via_move
220220
// tidy-alphabetical-end
221221
=> hir::Safety::Safe,
222222
_ => hir::Safety::Unsafe,
@@ -556,6 +556,13 @@ pub(crate) fn check_intrinsic_type(
556556
sym::write_via_move => {
557557
(1, 0, vec![Ty::new_mut_ptr(tcx, param(0)), param(0)], tcx.types.unit)
558558
}
559+
sym::write_box_via_move => {
560+
let t = param(0);
561+
let maybe_uninit_t = Ty::new_maybe_uninit(tcx, t);
562+
let box_mu_t = Ty::new_box(tcx, maybe_uninit_t);
563+
564+
(1, 0, vec![box_mu_t, param(0)], box_mu_t)
565+
}
559566

560567
sym::typed_swap_nonoverlapping => {
561568
(1, 0, vec![Ty::new_mut_ptr(tcx, param(0)); 2], tcx.types.unit)
@@ -648,8 +655,6 @@ pub(crate) fn check_intrinsic_type(
648655

649656
sym::ub_checks | sym::overflow_checks => (0, 0, Vec::new(), tcx.types.bool),
650657

651-
sym::box_new => (1, 0, vec![param(0)], Ty::new_box(tcx, param(0))),
652-
653658
// contract_check_requires::<C>(C) -> bool, where C: impl Fn() -> bool
654659
sym::contract_check_requires => (1, 0, vec![param(0)], tcx.types.unit),
655660
sym::contract_check_ensures => {

0 commit comments

Comments
 (0)