Skip to content

Commit 5e65109

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 93d4548 commit 5e65109

81 files changed

Lines changed: 1073 additions & 1376 deletions

File tree

Some content is hidden

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

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/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
@@ -540,18 +540,6 @@ impl<'tcx> NonConstOp<'tcx> for Coroutine {
540540
}
541541
}
542542

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

compiler/rustc_const_eval/src/errors.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -289,31 +289,6 @@ pub(crate) struct UnallowedOpInConstContext {
289289
pub msg: String,
290290
}
291291

292-
#[derive(Diagnostic)]
293-
#[diag(r#"allocations are not allowed in {$kind ->
294-
[const] constant
295-
[static] static
296-
[const_fn] constant function
297-
*[other] {""}
298-
}s"#, code = E0010)]
299-
pub(crate) struct UnallowedHeapAllocations {
300-
#[primary_span]
301-
#[label(
302-
r#"allocation not allowed in {$kind ->
303-
[const] constant
304-
[static] static
305-
[const_fn] constant function
306-
*[other] {""}
307-
}s"#
308-
)]
309-
pub span: Span,
310-
pub kind: ConstContext,
311-
#[note(
312-
"the runtime heap is not yet available at compile-time, so no runtime heap allocations can be created"
313-
)]
314-
pub teach: bool,
315-
}
316-
317292
#[derive(Diagnostic)]
318293
#[diag(r#"inline assembly is not allowed in {$kind ->
319294
[const] constant
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
@@ -20,7 +20,8 @@
2020
//
2121
// Do *not* remove entries from this list. Instead, just add a note to the corresponding markdown
2222
// file saying that this error is not emitted by the compiler any more (see E0001.md for an
23-
// example), and remove all code examples that do not build any more.
23+
// example), and remove all code examples that do not build any more by marking them
24+
// with `ignore (no longer emitted)`.
2425
#[macro_export]
2526
#[rustfmt::skip]
2627
macro_rules! error_codes {

compiler/rustc_hir/src/lang_items.rs

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

323-
ExchangeMalloc, sym::exchange_malloc, exchange_malloc_fn, Target::Fn, GenericRequirement::None;
324323
DropInPlace, sym::drop_in_place, drop_in_place_fn, Target::Fn, GenericRequirement::Minimum(1);
325324
AllocLayout, sym::alloc_layout, alloc_layout, Target::Struct, GenericRequirement::None;
326325

compiler/rustc_hir_analysis/src/check/intrinsic.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hi
7777
| sym::autodiff
7878
| sym::bitreverse
7979
| sym::black_box
80-
| sym::box_new
8180
| sym::breakpoint
8281
| sym::bswap
8382
| sym::caller_location
@@ -223,6 +222,7 @@ fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hi
223222
| sym::wrapping_add
224223
| sym::wrapping_mul
225224
| sym::wrapping_sub
225+
| sym::write_box_via_move
226226
// tidy-alphabetical-end
227227
=> hir::Safety::Safe,
228228
_ => hir::Safety::Unsafe,
@@ -584,6 +584,13 @@ pub(crate) fn check_intrinsic_type(
584584
sym::write_via_move => {
585585
(1, 0, vec![Ty::new_mut_ptr(tcx, param(0)), param(0)], tcx.types.unit)
586586
}
587+
sym::write_box_via_move => {
588+
let t = param(0);
589+
let maybe_uninit_t = Ty::new_maybe_uninit(tcx, t);
590+
let box_mu_t = Ty::new_box(tcx, maybe_uninit_t);
591+
592+
(1, 0, vec![box_mu_t, param(0)], box_mu_t)
593+
}
587594

588595
sym::typed_swap_nonoverlapping => {
589596
(1, 0, vec![Ty::new_mut_ptr(tcx, param(0)); 2], tcx.types.unit)
@@ -689,8 +696,6 @@ pub(crate) fn check_intrinsic_type(
689696

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

692-
sym::box_new => (1, 0, vec![param(0)], Ty::new_box(tcx, param(0))),
693-
694699
// contract_check_requires::<C>(C) -> bool, where C: impl Fn() -> bool
695700
sym::contract_check_requires => (1, 0, vec![param(0)], tcx.types.unit),
696701
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
@@ -3110,6 +3110,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
31103110
{
31113111
let deref_kind = if checked_ty.is_box() {
31123112
// detect Box::new(..)
3113+
// FIXME: use `box_new` diagnostic item instead?
31133114
if let ExprKind::Call(box_new, [_]) = expr.kind
31143115
&& let ExprKind::Path(qpath) = &box_new.kind
31153116
&& let Res::Def(DefKind::AssocFn, fn_id) =

0 commit comments

Comments
 (0)