33mod init_mask;
44mod provenance_map;
55
6+ use std:: alloc:: { self , Layout } ;
67use std:: borrow:: Cow ;
78use std:: hash:: Hash ;
89use std:: ops:: { Deref , DerefMut , Range } ;
@@ -434,7 +435,7 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
434435 // available to the compiler can change between runs. Normally queries are always
435436 // deterministic. However, we can be non-deterministic here because all uses of const
436437 // evaluation (including ConstProp!) will make compilation fail (via hard error
437- // or ICE ) upon encountering a `MemoryExhausted` error.
438+ // or OOM ) upon encountering a `MemoryExhausted` error.
438439 let bytes = Bytes :: zeroed ( size, align, params) . ok_or_else ( fail) ?;
439440
440441 Ok ( Allocation {
@@ -468,7 +469,7 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
468469 . into ( )
469470 }
470471
471- /// Try to create an Allocation of `size` bytes, panics if there is not enough memory
472+ /// Try to create an Allocation of `size` bytes. Aborts if there is not enough memory
472473 /// available to the compiler to do so.
473474 ///
474475 /// Example use case: To obtain an Allocation filled with specific data,
@@ -480,10 +481,15 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> {
480481 params : <Bytes as AllocBytes >:: AllocParams ,
481482 ) -> Self {
482483 match Self :: new_inner ( size, align, init, params, || {
483- panic ! (
484- "interpreter ran out of memory: cannot create allocation of {} bytes" ,
485- size. bytes( )
486- ) ;
484+ // `size` may actually be bigger than isize::MAX since it is a *target* size.
485+ // Clamp it to isize::MAX to still give a somewhat reasonable error message.
486+ alloc:: handle_alloc_error (
487+ Layout :: from_size_align (
488+ size. bytes ( ) . min ( isize:: MAX as u64 ) as usize ,
489+ align. bytes_usize ( ) ,
490+ )
491+ . unwrap ( ) ,
492+ )
487493 } ) {
488494 Ok ( x) => x,
489495 Err ( x) => x,
0 commit comments