@@ -67,7 +67,7 @@ use middle::typeck::MethodCall;
67
67
use util:: common:: indenter;
68
68
use util:: ppaux:: Repr ;
69
69
use util:: nodemap:: NodeMap ;
70
- use middle:: trans:: machine:: llsize_of;
70
+ use middle:: trans:: machine:: { llsize_of, llsize_of_alloc } ;
71
71
use middle:: trans:: type_:: Type ;
72
72
73
73
use std:: slice;
@@ -1200,12 +1200,19 @@ fn trans_boxed_expr<'a>(bcx: &'a Block<'a>,
1200
1200
let size = llsize_of ( bcx. ccx ( ) , llty) ;
1201
1201
let Result { bcx : bcx, val : val } = malloc_raw_dyn ( bcx, contents_ty,
1202
1202
heap_exchange, size) ;
1203
- let custom_cleanup_scope = fcx. push_custom_cleanup_scope ( ) ;
1204
- fcx. schedule_free_value ( cleanup:: CustomScope ( custom_cleanup_scope) ,
1205
- val, heap_exchange) ;
1206
- let bcx = trans_into ( bcx, contents, SaveIn ( val) ) ;
1207
- fcx. pop_custom_cleanup_scope ( custom_cleanup_scope) ;
1208
- immediate_rvalue_bcx ( bcx, val, box_ty) . to_expr_datumblock ( )
1203
+ // Unique boxes do not allocate for zero-size types. The standard library may assume
1204
+ // that `free` is never called on the pointer returned for `~ZeroSizeType`.
1205
+ if llsize_of_alloc ( bcx. ccx ( ) , llty) == 0 {
1206
+ let bcx = trans_into ( bcx, contents, SaveIn ( val) ) ;
1207
+ immediate_rvalue_bcx ( bcx, val, box_ty) . to_expr_datumblock ( )
1208
+ } else {
1209
+ let custom_cleanup_scope = fcx. push_custom_cleanup_scope ( ) ;
1210
+ fcx. schedule_free_value ( cleanup:: CustomScope ( custom_cleanup_scope) ,
1211
+ val, heap_exchange) ;
1212
+ let bcx = trans_into ( bcx, contents, SaveIn ( val) ) ;
1213
+ fcx. pop_custom_cleanup_scope ( custom_cleanup_scope) ;
1214
+ immediate_rvalue_bcx ( bcx, val, box_ty) . to_expr_datumblock ( )
1215
+ }
1209
1216
} else {
1210
1217
let base:: MallocResult { bcx, smart_ptr : bx, body } =
1211
1218
base:: malloc_general ( bcx, contents_ty, heap) ;
0 commit comments