Skip to content

Commit 91abfd4

Browse files
committed
auto merge of #9832 : luqmana/rust/sps, r=alexcrichton
Fixes #9830.
2 parents 0bad7e1 + e88064d commit 91abfd4

File tree

6 files changed

+26
-28
lines changed

6 files changed

+26
-28
lines changed

src/librustc/middle/trans/adt.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -505,26 +505,27 @@ pub fn trans_const(ccx: &mut CrateContext, r: &Repr, discr: Disr,
505505
}
506506
Univariant(ref st, _dro) => {
507507
assert_eq!(discr, 0);
508-
C_struct(build_const_struct(ccx, st, vals))
508+
let contents = build_const_struct(ccx, st, vals);
509+
C_struct(contents, st.packed)
509510
}
510511
General(ref cases) => {
511512
let case = &cases[discr];
512513
let max_sz = cases.iter().map(|x| x.size).max().unwrap();
513514
let discr_ty = C_disr(ccx, discr);
514515
let contents = build_const_struct(ccx, case,
515516
~[discr_ty] + vals);
516-
C_struct(contents + &[padding(max_sz - case.size)])
517+
C_struct(contents + &[padding(max_sz - case.size)], false)
517518
}
518519
NullablePointer{ nonnull: ref nonnull, nndiscr, ptrfield, _ } => {
519520
if discr == nndiscr {
520-
C_struct(build_const_struct(ccx, nonnull, vals))
521+
C_struct(build_const_struct(ccx, nonnull, vals), false)
521522
} else {
522523
assert_eq!(vals.len(), 0);
523524
let vals = do nonnull.fields.iter().enumerate().map |(i, &ty)| {
524525
let llty = type_of::sizing_type_of(ccx, ty);
525526
if i == ptrfield { C_null(llty) } else { C_undef(llty) }
526527
}.collect::<~[ValueRef]>();
527-
C_struct(build_const_struct(ccx, nonnull, vals))
528+
C_struct(build_const_struct(ccx, nonnull, vals), false)
528529
}
529530
}
530531
}
@@ -559,7 +560,7 @@ fn build_const_struct(ccx: &mut CrateContext, st: &Struct, vals: &[ValueRef])
559560
offset = target_offset;
560561
}
561562
let val = if is_undef(vals[i]) {
562-
let wrapped = C_struct([vals[i]]);
563+
let wrapped = C_struct([vals[i]], false);
563564
assert!(!is_undef(wrapped));
564565
wrapped
565566
} else {

src/librustc/middle/trans/base.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2936,7 +2936,7 @@ pub fn create_module_map(ccx: &mut CrateContext) -> (ValueRef, uint, uint) {
29362936
let elt = C_struct([
29372937
C_estr_slice(ccx, *key),
29382938
v_ptr
2939-
]);
2939+
], false);
29402940
elts.push(elt);
29412941
}
29422942
unsafe {
@@ -3012,13 +3012,13 @@ pub fn fill_crate_map(ccx: &mut CrateContext, map: ValueRef) {
30123012
p2i(ccx, mod_map),
30133013
// byte size of the module map array, an entry consists of two integers
30143014
C_int(ccx, ((mod_count * mod_struct_size) as int))
3015-
]),
3015+
], false),
30163016
C_struct([
30173017
p2i(ccx, vec_elements),
30183018
// byte size of the subcrates array, an entry consists of an integer
30193019
C_int(ccx, (subcrates.len() * llsize_of_alloc(ccx, ccx.int_type)) as int)
3020-
])
3021-
]));
3020+
], false)
3021+
], false));
30223022
}
30233023
}
30243024

@@ -3052,7 +3052,7 @@ pub fn write_metadata(cx: &CrateContext, crate: &ast::Crate) {
30523052

30533053
let encode_parms = crate_ctxt_to_encode_parms(cx, encode_inlined_item);
30543054
let llmeta = C_bytes(encoder::encode_metadata(encode_parms, crate));
3055-
let llconst = C_struct([llmeta]);
3055+
let llconst = C_struct([llmeta], false);
30563056
let mut llglobal = do "rust_metadata".with_c_str |buf| {
30573057
unsafe {
30583058
llvm::LLVMAddGlobal(cx.llmod, val_ty(llconst).to_ref(), buf)

src/librustc/middle/trans/common.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ pub fn C_floating(s: &str, t: Type) -> ValueRef {
848848
}
849849

850850
pub fn C_nil() -> ValueRef {
851-
return C_struct([]);
851+
C_struct([], false)
852852
}
853853

854854
pub fn C_bool(val: bool) -> ValueRef {
@@ -913,7 +913,7 @@ pub fn C_estr_slice(cx: &mut CrateContext, s: @str) -> ValueRef {
913913
unsafe {
914914
let len = s.len();
915915
let cs = llvm::LLVMConstPointerCast(C_cstr(cx, s), Type::i8p().to_ref());
916-
C_struct([cs, C_uint(cx, len)])
916+
C_struct([cs, C_uint(cx, len)], false)
917917
}
918918
}
919919

@@ -927,18 +927,10 @@ pub fn C_zero_byte_arr(size: uint) -> ValueRef {
927927
}
928928
}
929929

930-
pub fn C_struct(elts: &[ValueRef]) -> ValueRef {
930+
pub fn C_struct(elts: &[ValueRef], packed: bool) -> ValueRef {
931931
unsafe {
932932
do elts.as_imm_buf |ptr, len| {
933-
llvm::LLVMConstStructInContext(base::task_llcx(), ptr, len as c_uint, False)
934-
}
935-
}
936-
}
937-
938-
pub fn C_packed_struct(elts: &[ValueRef]) -> ValueRef {
939-
unsafe {
940-
do elts.as_imm_buf |ptr, len| {
941-
llvm::LLVMConstStructInContext(base::task_llcx(), ptr, len as c_uint, True)
933+
llvm::LLVMConstStructInContext(base::task_llcx(), ptr, len as c_uint, packed as Bool)
942934
}
943935
}
944936
}

src/librustc/middle/trans/consts.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub fn const_vec(cx: @mut CrateContext, e: &ast::Expr, es: &[@ast::Expr])
9494
let (vs, inlineable) = vec::unzip(es.iter().map(|e| const_expr(cx, *e)));
9595
// If the vector contains enums, an LLVM array won't work.
9696
let v = if vs.iter().any(|vi| val_ty(*vi) != llunitty) {
97-
C_struct(vs)
97+
C_struct(vs, false)
9898
} else {
9999
C_array(llunitty, vs)
100100
};
@@ -186,7 +186,7 @@ pub fn const_expr(cx: @mut CrateContext, e: &ast::Expr) -> (ValueRef, bool) {
186186
match adjustment {
187187
None => { }
188188
Some(@ty::AutoAddEnv(ty::re_static, ast::BorrowedSigil)) => {
189-
llconst = C_struct([llconst, C_null(Type::opaque_box(cx).ptr_to())])
189+
llconst = C_struct([llconst, C_null(Type::opaque_box(cx).ptr_to())], false)
190190
}
191191
Some(@ty::AutoAddEnv(ref r, ref s)) => {
192192
cx.sess.span_bug(e.span, format!("unexpected static function: \
@@ -227,7 +227,7 @@ pub fn const_expr(cx: @mut CrateContext, e: &ast::Expr) -> (ValueRef, bool) {
227227
match ty::get(ty).sty {
228228
ty::ty_evec(_, ty::vstore_fixed(*)) => {
229229
let size = machine::llsize_of(cx, val_ty(llconst));
230-
llconst = C_struct([llptr, size]);
230+
llconst = C_struct([llptr, size], false);
231231
}
232232
_ => {}
233233
}
@@ -559,7 +559,7 @@ fn const_expr_unadjusted(cx: @mut CrateContext,
559559
llvm::LLVMSetGlobalConstant(gv, True);
560560
SetLinkage(gv, PrivateLinkage);
561561
let p = const_ptrcast(cx, gv, llunitty);
562-
(C_struct([p, sz]), false)
562+
(C_struct([p, sz], false), false)
563563
}
564564
_ => cx.sess.span_bug(e.span, "bad const-slice expr")
565565
}
@@ -575,7 +575,7 @@ fn const_expr_unadjusted(cx: @mut CrateContext,
575575
};
576576
let vs = vec::from_elem(n, const_expr(cx, elem).first());
577577
let v = if vs.iter().any(|vi| val_ty(*vi) != llunitty) {
578-
C_struct(vs)
578+
C_struct(vs, false)
579579
} else {
580580
C_array(llunitty, vs)
581581
};

src/librustc/middle/trans/meth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ pub fn make_vtable(ccx: &mut CrateContext,
576576
components.push(ptr)
577577
}
578578

579-
let tbl = C_struct(components);
579+
let tbl = C_struct(components, false);
580580
let sym = token::gensym("vtable");
581581
let vt_gvar = do format!("vtable{}", sym).with_c_str |buf| {
582582
llvm::LLVMAddGlobal(ccx.llmod, val_ty(tbl).to_ref(), buf)

src/test/run-pass/packed-struct-size.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ struct S7_Option {
5050
d: Option<@mut f64>
5151
}
5252

53+
// Placing packed structs in statics should work
54+
static TEST_S4: S4 = S4 { a: 1, b: [2, 3, 4] };
55+
static TEST_S5: S5 = S5 { a: 3, b: 67 };
56+
static TEST_S3_Foo: S3_Foo = S3_Foo { a: 1, b: 2, c: Baz };
57+
5358

5459
pub fn main() {
5560
assert_eq!(sys::size_of::<S4>(), 4);

0 commit comments

Comments
 (0)