Skip to content

Commit fae65f8

Browse files
committed
add write_box_via_move intrinsic and use it for vec!
This allows us to get rid of box_new entirely
1 parent 9044961 commit fae65f8

File tree

69 files changed

+1115
-1313
lines changed

Some content is hidden

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

69 files changed

+1115
-1313
lines changed

compiler/rustc_codegen_cranelift/example/mini_core.rs

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

623-
#[lang = "exchange_malloc"]
624-
unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
625-
libc::malloc(size)
626-
}
627-
628623
#[lang = "drop"]
629624
pub trait Drop {
630625
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
@@ -848,13 +848,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
848848
return;
849849
}
850850

851-
// This can be called on stable via the `vec!` macro.
852-
if tcx.is_lang_item(callee, LangItem::ExchangeMalloc) {
853-
self.check_op(ops::HeapAllocation);
854-
// Allow this call, skip all the checks below.
855-
return;
856-
}
857-
858851
// Intrinsics are language primitives, not regular calls, so treat them separately.
859852
if let Some(intrinsic) = tcx.intrinsic(callee) {
860853
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
@@ -490,18 +490,6 @@ impl<'tcx> NonConstOp<'tcx> for Coroutine {
490490
}
491491
}
492492

493-
#[derive(Debug)]
494-
pub(crate) struct HeapAllocation;
495-
impl<'tcx> NonConstOp<'tcx> for HeapAllocation {
496-
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
497-
ccx.dcx().create_err(errors::UnallowedHeapAllocations {
498-
span,
499-
kind: ccx.const_kind(),
500-
teach: ccx.tcx.sess.teach(E0010),
501-
})
502-
}
503-
}
504-
505493
#[derive(Debug)]
506494
pub(crate) struct InlineAsm;
507495
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+
```
810
const CON : Vec<i32> = vec![1, 2, 3];
911
```

compiler/rustc_hir/src/lang_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ language_item_table! {
332332
FormatPlaceholder, sym::format_placeholder, format_placeholder, Target::Struct, GenericRequirement::None;
333333
FormatUnsafeArg, sym::format_unsafe_arg, format_unsafe_arg, Target::Struct, GenericRequirement::None;
334334

335-
ExchangeMalloc, sym::exchange_malloc, exchange_malloc_fn, Target::Fn, GenericRequirement::None;
335+
BoxUninitAsMutPtr, sym::box_uninit_as_mut_ptr, box_uninit_as_mut_ptr_fn, Target::Fn, GenericRequirement::Exact(1);
336336
DropInPlace, sym::drop_in_place, drop_in_place_fn, Target::Fn, GenericRequirement::Minimum(1);
337337
AllocLayout, sym::alloc_layout, alloc_layout, Target::Struct, GenericRequirement::None;
338338

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
@@ -215,6 +214,7 @@ fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hi
215214
| sym::wrapping_add
216215
| sym::wrapping_mul
217216
| sym::wrapping_sub
217+
| sym::write_box_via_move
218218
// tidy-alphabetical-end
219219
=> hir::Safety::Safe,
220220
_ => hir::Safety::Unsafe,
@@ -553,6 +553,13 @@ pub(crate) fn check_intrinsic_type(
553553
sym::write_via_move => {
554554
(1, 0, vec![Ty::new_mut_ptr(tcx, param(0)), param(0)], tcx.types.unit)
555555
}
556+
sym::write_box_via_move => {
557+
let t = param(0);
558+
let maybe_uninit_t = Ty::new_maybe_uninit(tcx, t);
559+
let box_mu_t = Ty::new_box(tcx, maybe_uninit_t);
560+
561+
(1, 0, vec![box_mu_t, param(0)], box_mu_t)
562+
}
556563

557564
sym::typed_swap_nonoverlapping => {
558565
(1, 0, vec![Ty::new_mut_ptr(tcx, param(0)); 2], tcx.types.unit)
@@ -645,8 +652,6 @@ pub(crate) fn check_intrinsic_type(
645652

646653
sym::ub_checks => (0, 0, Vec::new(), tcx.types.bool),
647654

648-
sym::box_new => (1, 0, vec![param(0)], Ty::new_box(tcx, param(0))),
649-
650655
// contract_check_requires::<C>(C) -> bool, where C: impl Fn() -> bool
651656
sym::contract_check_requires => (1, 0, vec![param(0)], tcx.types.unit),
652657
sym::contract_check_ensures => {

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3022,6 +3022,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30223022
{
30233023
let deref_kind = if checked_ty.is_box() {
30243024
// detect Box::new(..)
3025+
// FIXME: use `box_new` diagnostic item instead?
30253026
if let ExprKind::Call(box_new, [_]) = expr.kind
30263027
&& let ExprKind::Path(qpath) = &box_new.kind
30273028
&& let Res::Def(DefKind::AssocFn, fn_id) =

0 commit comments

Comments
 (0)