Skip to content

Commit edb23be

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

File tree

102 files changed

+1252
-1460
lines changed

Some content is hidden

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

102 files changed

+1252
-1460
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,36 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
15351535
.unwrap();
15361536
}
15371537
}
1538+
CastKind::ConstToMut => {
1539+
let ty::RawPtr(ty_from, hir::Mutability::Not) =
1540+
op.ty(self.body, tcx).kind()
1541+
else {
1542+
span_mirbug!(self, rvalue, "unexpected base type for cast {:?}", ty,);
1543+
return;
1544+
};
1545+
let ty::RawPtr(ty_to, hir::Mutability::Mut) = ty.kind() else {
1546+
span_mirbug!(self, rvalue, "unexpected target type for cast {:?}", ty,);
1547+
return;
1548+
};
1549+
if let Err(terr) = self.sub_types(
1550+
*ty_from,
1551+
*ty_to,
1552+
location.to_locations(),
1553+
ConstraintCategory::Cast {
1554+
is_implicit_coercion: false,
1555+
unsize_to: None,
1556+
},
1557+
) {
1558+
span_mirbug!(
1559+
self,
1560+
rvalue,
1561+
"relating {:?} with {:?} yields {:?}",
1562+
ty_from,
1563+
ty_to,
1564+
terr
1565+
);
1566+
}
1567+
}
15381568
CastKind::Transmute => {
15391569
let ty_from = op.ty(self.body, tcx);
15401570
match ty_from.kind() {

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_codegen_ssa/src/mir/rvalue.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
444444
) => {
445445
bug!("{kind:?} is for borrowck, and should never appear in codegen");
446446
}
447-
mir::CastKind::PtrToPtr
447+
mir::CastKind::PtrToPtr | mir::CastKind::ConstToMut
448448
if bx.cx().is_backend_scalar_pair(operand.layout) =>
449449
{
450450
if let OperandValue::Pair(data_ptr, meta) = operand.val {
@@ -463,6 +463,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
463463
| mir::CastKind::FloatToFloat
464464
| mir::CastKind::IntToFloat
465465
| mir::CastKind::PtrToPtr
466+
| mir::CastKind::ConstToMut
466467
| mir::CastKind::FnPtrToPtr
467468
// Since int2ptr can have arbitrary integer types as input (so we have to do
468469
// sign extension and all that), it is currently best handled in the same code

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 {

compiler/rustc_const_eval/src/interpret/cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
6161
self.write_immediate(*res, dest)?;
6262
}
6363

64-
CastKind::FnPtrToPtr | CastKind::PtrToPtr => {
64+
CastKind::FnPtrToPtr | CastKind::PtrToPtr | CastKind::ConstToMut => {
6565
let src = self.read_immediate(src)?;
6666
let res = self.ptr_to_ptr(&src, cast_layout)?;
6767
self.write_immediate(*res, dest)?;
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
```

0 commit comments

Comments
 (0)