Skip to content

Commit bf8648d

Browse files
committed
auto merge of #14121 : luqmana/rust/option-ffi, r=alexcrichton
This slightly adjusts the NullablePointer representation for some enums in the case where the non-nullable variant has a single field (the ptr field) to be just that, the pointer. This is in contrast to the current behaviour where we'd wrap that single pointer in a LLVM struct. Fixes #11040 & #11303.
2 parents a62395f + be79edb commit bf8648d

File tree

5 files changed

+158
-59
lines changed

5 files changed

+158
-59
lines changed

src/librustc/middle/trans/adt.rs

+103-44
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ pub enum Repr {
8585
* all start with a field for the discriminant.
8686
*/
8787
General(IntType, Vec<Struct>),
88+
/**
89+
* Two cases distinguished by a nullable pointer: the case with discriminant
90+
* `nndiscr` must have single field which is known to be nonnull due to its type.
91+
* The other case is known to be zero sized. Hence we represent the enum
92+
* as simply a nullable pointer: if not null it indicates the `nndiscr` variant,
93+
* otherwise it indicates the other case.
94+
*/
95+
RawNullablePointer {
96+
pub nndiscr: Disr,
97+
pub nnty: ty::t,
98+
pub nullfields: Vec<ty::t>
99+
},
88100
/**
89101
* Two cases distinguished by a nullable pointer: the case with discriminant
90102
* `nndiscr` is represented by the struct `nonnull`, where the `ptrfield`th
@@ -96,7 +108,7 @@ pub enum Repr {
96108
* is represented such that `None` is a null pointer and `Some` is the
97109
* identity function.
98110
*/
99-
NullablePointer {
111+
StructWrappedNullablePointer {
100112
pub nonnull: Struct,
101113
pub nndiscr: Disr,
102114
pub ptrfield: uint,
@@ -200,17 +212,23 @@ fn represent_type_uncached(cx: &CrateContext, t: ty::t) -> Repr {
200212
if cases.get(1 - discr).is_zerolen(cx) {
201213
match cases.get(discr).find_ptr() {
202214
Some(ptrfield) => {
203-
return NullablePointer {
204-
nndiscr: discr as u64,
205-
nonnull: mk_struct(cx,
206-
cases.get(discr)
207-
.tys
208-
.as_slice(),
209-
false),
210-
ptrfield: ptrfield,
211-
nullfields: cases.get(1 - discr).tys
212-
.clone()
213-
}
215+
let st = mk_struct(cx, cases.get(discr).tys.as_slice(),
216+
false);
217+
218+
return if st.fields.len() == 1 {
219+
RawNullablePointer {
220+
nndiscr: discr as Disr,
221+
nnty: *st.fields.get(0),
222+
nullfields: cases.get(1 - discr).tys.clone()
223+
}
224+
} else {
225+
StructWrappedNullablePointer {
226+
nndiscr: discr as Disr,
227+
nonnull: st,
228+
ptrfield: ptrfield,
229+
nullfields: cases.get(1 - discr).tys.clone()
230+
}
231+
};
214232
}
215233
None => { }
216234
}
@@ -413,8 +431,8 @@ pub fn incomplete_type_of(cx: &CrateContext, r: &Repr, name: &str) -> Type {
413431
}
414432
pub fn finish_type_of(cx: &CrateContext, r: &Repr, llty: &mut Type) {
415433
match *r {
416-
CEnum(..) | General(..) => { }
417-
Univariant(ref st, _) | NullablePointer{ nonnull: ref st, .. } =>
434+
CEnum(..) | General(..) | RawNullablePointer { .. } => { }
435+
Univariant(ref st, _) | StructWrappedNullablePointer { nonnull: ref st, .. } =>
418436
llty.set_struct_body(struct_llfields(cx, st, false).as_slice(),
419437
st.packed)
420438
}
@@ -423,7 +441,8 @@ pub fn finish_type_of(cx: &CrateContext, r: &Repr, llty: &mut Type) {
423441
fn generic_type_of(cx: &CrateContext, r: &Repr, name: Option<&str>, sizing: bool) -> Type {
424442
match *r {
425443
CEnum(ity, _, _) => ll_inttype(cx, ity),
426-
Univariant(ref st, _) | NullablePointer{ nonnull: ref st, .. } => {
444+
RawNullablePointer { nnty, .. } => type_of::sizing_type_of(cx, nnty),
445+
Univariant(ref st, _) | StructWrappedNullablePointer { nonnull: ref st, .. } => {
427446
match name {
428447
None => {
429448
Type::struct_(cx, struct_llfields(cx, st, sizing).as_slice(),
@@ -495,12 +514,10 @@ fn struct_llfields(cx: &CrateContext, st: &Struct, sizing: bool) -> Vec<Type> {
495514
pub fn trans_switch(bcx: &Block, r: &Repr, scrutinee: ValueRef)
496515
-> (_match::branch_kind, Option<ValueRef>) {
497516
match *r {
498-
CEnum(..) | General(..) => {
517+
CEnum(..) | General(..) |
518+
RawNullablePointer { .. } | StructWrappedNullablePointer { .. } => {
499519
(_match::switch, Some(trans_get_discr(bcx, r, scrutinee, None)))
500520
}
501-
NullablePointer{ nonnull: ref nonnull, nndiscr, ptrfield, .. } => {
502-
(_match::switch, Some(nullable_bitdiscr(bcx, nonnull, nndiscr, ptrfield, scrutinee)))
503-
}
504521
Univariant(..) => {
505522
(_match::single, None)
506523
}
@@ -528,8 +545,14 @@ pub fn trans_get_discr(bcx: &Block, r: &Repr, scrutinee: ValueRef, cast_to: Opti
528545
val = C_u8(bcx.ccx(), 0);
529546
signed = false;
530547
}
531-
NullablePointer{ nonnull: ref nonnull, nndiscr, ptrfield, .. } => {
532-
val = nullable_bitdiscr(bcx, nonnull, nndiscr, ptrfield, scrutinee);
548+
RawNullablePointer { nndiscr, nnty, .. } => {
549+
let cmp = if nndiscr == 0 { IntEQ } else { IntNE };
550+
let llptrty = type_of::sizing_type_of(bcx.ccx(), nnty);
551+
val = ICmp(bcx, cmp, Load(bcx, scrutinee), C_null(llptrty));
552+
signed = false;
553+
}
554+
StructWrappedNullablePointer { nonnull: ref nonnull, nndiscr, ptrfield, .. } => {
555+
val = struct_wrapped_nullable_bitdiscr(bcx, nonnull, nndiscr, ptrfield, scrutinee);
533556
signed = false;
534557
}
535558
}
@@ -539,10 +562,10 @@ pub fn trans_get_discr(bcx: &Block, r: &Repr, scrutinee: ValueRef, cast_to: Opti
539562
}
540563
}
541564

542-
fn nullable_bitdiscr(bcx: &Block, nonnull: &Struct, nndiscr: Disr, ptrfield: uint,
543-
scrutinee: ValueRef) -> ValueRef {
544-
let cmp = if nndiscr == 0 { IntEQ } else { IntNE };
565+
fn struct_wrapped_nullable_bitdiscr(bcx: &Block, nonnull: &Struct, nndiscr: Disr, ptrfield: uint,
566+
scrutinee: ValueRef) -> ValueRef {
545567
let llptr = Load(bcx, GEPi(bcx, scrutinee, [0, ptrfield]));
568+
let cmp = if nndiscr == 0 { IntEQ } else { IntNE };
546569
let llptrty = type_of::type_of(bcx.ccx(), *nonnull.fields.get(ptrfield));
547570
ICmp(bcx, cmp, llptr, C_null(llptrty))
548571
}
@@ -590,7 +613,8 @@ pub fn trans_case<'a>(bcx: &'a Block<'a>, r: &Repr, discr: Disr)
590613
Univariant(..) => {
591614
bcx.ccx().sess().bug("no cases for univariants or structs")
592615
}
593-
NullablePointer{ .. } => {
616+
RawNullablePointer { .. } |
617+
StructWrappedNullablePointer { .. } => {
594618
assert!(discr == 0 || discr == 1);
595619
_match::single_result(Result::new(bcx, C_i1(bcx.ccx(), discr != 0)))
596620
}
@@ -621,7 +645,13 @@ pub fn trans_start_init(bcx: &Block, r: &Repr, val: ValueRef, discr: Disr) {
621645
Univariant(..) => {
622646
assert_eq!(discr, 0);
623647
}
624-
NullablePointer{ nonnull: ref nonnull, nndiscr, ptrfield, .. } => {
648+
RawNullablePointer { nndiscr, nnty, ..} => {
649+
if discr != nndiscr {
650+
let llptrty = type_of::sizing_type_of(bcx.ccx(), nnty);
651+
Store(bcx, C_null(llptrty), val)
652+
}
653+
}
654+
StructWrappedNullablePointer { nonnull: ref nonnull, nndiscr, ptrfield, .. } => {
625655
if discr != nndiscr {
626656
let llptrptr = GEPi(bcx, val, [0, ptrfield]);
627657
let llptrty = type_of::type_of(bcx.ccx(),
@@ -651,8 +681,11 @@ pub fn num_args(r: &Repr, discr: Disr) -> uint {
651681
st.fields.len() - (if dtor { 1 } else { 0 })
652682
}
653683
General(_, ref cases) => cases.get(discr as uint).fields.len() - 1,
654-
NullablePointer{ nonnull: ref nonnull, nndiscr,
655-
nullfields: ref nullfields, .. } => {
684+
RawNullablePointer { nndiscr, ref nullfields, .. } => {
685+
if discr == nndiscr { 1 } else { nullfields.len() }
686+
}
687+
StructWrappedNullablePointer { nonnull: ref nonnull, nndiscr,
688+
nullfields: ref nullfields, .. } => {
656689
if discr == nndiscr { nonnull.fields.len() } else { nullfields.len() }
657690
}
658691
}
@@ -675,19 +708,25 @@ pub fn trans_field_ptr(bcx: &Block, r: &Repr, val: ValueRef, discr: Disr,
675708
General(_, ref cases) => {
676709
struct_field_ptr(bcx, cases.get(discr as uint), val, ix + 1, true)
677710
}
678-
NullablePointer{ nonnull: ref nonnull, nullfields: ref nullfields,
679-
nndiscr, .. } => {
680-
if discr == nndiscr {
681-
struct_field_ptr(bcx, nonnull, val, ix, false)
682-
} else {
683-
// The unit-like case might have a nonzero number of unit-like fields.
684-
// (e.g., Result or Either with () as one side.)
685-
let ty = type_of::type_of(bcx.ccx(), *nullfields.get(ix));
686-
assert_eq!(machine::llsize_of_alloc(bcx.ccx(), ty), 0);
687-
// The contents of memory at this pointer can't matter, but use
688-
// the value that's "reasonable" in case of pointer comparison.
689-
PointerCast(bcx, val, ty.ptr_to())
690-
}
711+
RawNullablePointer { nndiscr, ref nullfields, .. } |
712+
StructWrappedNullablePointer { nndiscr, ref nullfields, .. } if discr != nndiscr => {
713+
// The unit-like case might have a nonzero number of unit-like fields.
714+
// (e.d., Result of Either with (), as one side.)
715+
let ty = type_of::type_of(bcx.ccx(), *nullfields.get(ix));
716+
assert_eq!(machine::llsize_of_alloc(bcx.ccx(), ty), 0);
717+
// The contents of memory at this pointer can't matter, but use
718+
// the value that's "reasonable" in case of pointer comparision.
719+
PointerCast(bcx, val, ty.ptr_to())
720+
}
721+
RawNullablePointer { nndiscr, nnty, .. } => {
722+
assert_eq!(ix, 0);
723+
assert_eq!(discr, nndiscr);
724+
let ty = type_of::type_of(bcx.ccx(), nnty);
725+
PointerCast(bcx, val, ty.ptr_to())
726+
}
727+
StructWrappedNullablePointer { ref nonnull, nndiscr, .. } => {
728+
assert_eq!(discr, nndiscr);
729+
struct_field_ptr(bcx, nonnull, val, ix, false)
691730
}
692731
}
693732
}
@@ -759,7 +798,15 @@ pub fn trans_const(ccx: &CrateContext, r: &Repr, discr: Disr,
759798
let contents = build_const_struct(ccx, st, vals);
760799
C_struct(ccx, contents.as_slice(), st.packed)
761800
}
762-
NullablePointer{ nonnull: ref nonnull, nndiscr, .. } => {
801+
RawNullablePointer { nndiscr, nnty, .. } => {
802+
if discr == nndiscr {
803+
assert_eq!(vals.len(), 1);
804+
vals[0]
805+
} else {
806+
C_null(type_of::sizing_type_of(ccx, nnty))
807+
}
808+
}
809+
StructWrappedNullablePointer { nonnull: ref nonnull, nndiscr, .. } => {
763810
if discr == nndiscr {
764811
C_struct(ccx, build_const_struct(ccx,
765812
nonnull,
@@ -867,7 +914,15 @@ pub fn const_get_discrim(ccx: &CrateContext, r: &Repr, val: ValueRef)
867914
}
868915
}
869916
Univariant(..) => 0,
870-
NullablePointer{ nndiscr, ptrfield, .. } => {
917+
RawNullablePointer { nndiscr, .. } => {
918+
if is_null(val) {
919+
/* subtraction as uint is ok because nndiscr is either 0 or 1 */
920+
(1 - nndiscr) as Disr
921+
} else {
922+
nndiscr
923+
}
924+
}
925+
StructWrappedNullablePointer { nndiscr, ptrfield, .. } => {
871926
if is_null(const_struct_field(ccx, val, ptrfield)) {
872927
/* subtraction as uint is ok because nndiscr is either 0 or 1 */
873928
(1 - nndiscr) as Disr
@@ -891,7 +946,11 @@ pub fn const_get_field(ccx: &CrateContext, r: &Repr, val: ValueRef,
891946
CEnum(..) => ccx.sess().bug("element access in C-like enum const"),
892947
Univariant(..) => const_struct_field(ccx, val, ix),
893948
General(..) => const_struct_field(ccx, val, ix + 1),
894-
NullablePointer{ .. } => const_struct_field(ccx, val, ix)
949+
RawNullablePointer { .. } => {
950+
assert_eq!(ix, 0);
951+
val
952+
}
953+
StructWrappedNullablePointer{ .. } => const_struct_field(ccx, val, ix)
895954
}
896955
}
897956

src/librustc/middle/trans/debuginfo.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1675,7 +1675,10 @@ fn prepare_enum_metadata(cx: &CrateContext,
16751675
}),
16761676
}
16771677
}
1678-
adt::NullablePointer { nonnull: ref struct_def, nndiscr, .. } => {
1678+
adt::RawNullablePointer { nnty, .. } => {
1679+
FinalMetadata(type_metadata(cx, nnty, span))
1680+
}
1681+
adt::StructWrappedNullablePointer { nonnull: ref struct_def, nndiscr, .. } => {
16791682
let (metadata_stub,
16801683
variant_llvm_type,
16811684
member_description_factory) =

src/test/debuginfo/option-like-enum.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
// gdb-command:finish
1717

1818
// gdb-command:print some
19-
// gdb-check:$1 = {0x12345678}
19+
// gdb-check:$1 = (u32 *) 0x12345678
2020

2121
// gdb-command:print none
22-
// gdb-check:$2 = {0x0}
22+
// gdb-check:$2 = (u32 *) 0x0
2323

2424
// gdb-command:print full
2525
// gdb-check:$3 = {454545, 0x87654321, 9988}
@@ -41,6 +41,8 @@
4141
// contains a non-nullable pointer, then this value is used as the discriminator.
4242
// The test cases in this file make sure that something readable is generated for
4343
// this kind of types.
44+
// If the non-empty variant contains a single non-nullable pointer than the whole
45+
// item is represented as just a pointer and not wrapped in a struct.
4446
// Unfortunately (for these test cases) the content of the non-discriminant fields
4547
// in the null-case is not defined. So we just read the discriminator field in
4648
// this case (by casting the value to a memory-equivalent struct).

src/test/debuginfo/recursive-struct.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -20,53 +20,53 @@
2020

2121
// gdb-command:print stack_unique.value
2222
// gdb-check:$1 = 0
23-
// gdb-command:print stack_unique.next.val->value
23+
// gdb-command:print stack_unique.next->value
2424
// gdb-check:$2 = 1
2525

2626
// gdb-command:print unique_unique->value
2727
// gdb-check:$3 = 2
28-
// gdb-command:print unique_unique->next.val->value
28+
// gdb-command:print unique_unique->next->value
2929
// gdb-check:$4 = 3
3030

3131
// gdb-command:print box_unique->val.value
3232
// gdb-check:$5 = 4
33-
// gdb-command:print box_unique->val.next.val->value
33+
// gdb-command:print box_unique->val.next->value
3434
// gdb-check:$6 = 5
3535

3636
// gdb-command:print vec_unique[0].value
3737
// gdb-check:$7 = 6.5
38-
// gdb-command:print vec_unique[0].next.val->value
38+
// gdb-command:print vec_unique[0].next->value
3939
// gdb-check:$8 = 7.5
4040

4141
// gdb-command:print borrowed_unique->value
4242
// gdb-check:$9 = 8.5
43-
// gdb-command:print borrowed_unique->next.val->value
43+
// gdb-command:print borrowed_unique->next->value
4444
// gdb-check:$10 = 9.5
4545

4646
// MANAGED
4747
// gdb-command:print stack_managed.value
4848
// gdb-check:$11 = 10
49-
// gdb-command:print stack_managed.next.val->val.value
49+
// gdb-command:print stack_managed.next.val->value
5050
// gdb-check:$12 = 11
5151

5252
// gdb-command:print unique_managed->value
5353
// gdb-check:$13 = 12
54-
// gdb-command:print unique_managed->next.val->val.value
54+
// gdb-command:print unique_managed->next.val->value
5555
// gdb-check:$14 = 13
5656

57-
// gdb-command:print box_managed->val.value
57+
// gdb-command:print box_managed.val->value
5858
// gdb-check:$15 = 14
59-
// gdb-command:print box_managed->val.next.val->val.value
59+
// gdb-command:print box_managed->val->next.val->value
6060
// gdb-check:$16 = 15
6161

6262
// gdb-command:print vec_managed[0].value
6363
// gdb-check:$17 = 16.5
64-
// gdb-command:print vec_managed[0].next.val->val.value
64+
// gdb-command:print vec_managed[0].next.val->value
6565
// gdb-check:$18 = 17.5
6666

6767
// gdb-command:print borrowed_managed->value
6868
// gdb-check:$19 = 18.5
69-
// gdb-command:print borrowed_managed->next.val->val.value
69+
// gdb-command:print borrowed_managed->next.val->value
7070
// gdb-check:$20 = 19.5
7171

7272
// LONG CYCLE
@@ -97,7 +97,7 @@
9797
// gdb-command:print (*****long_cycle_w_anonymous_types).value
9898
// gdb-check:$31 = 30
9999

100-
// gdb-command:print (*****((*****long_cycle_w_anonymous_types).next.val)).value
100+
// gdb-command:print (*****((*****long_cycle_w_anonymous_types).next)).value
101101
// gdb-check:$32 = 31
102102

103103
// gdb-command:continue
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// #11303, #11040:
12+
// This would previously crash on i686 linux due to abi differences
13+
// between returning an Option<T> and T, where T is a non nullable
14+
// pointer.
15+
// If we have an enum with two variants such that one is zero sized
16+
// and the other contains a nonnullable pointer, we don't use a
17+
// separate discriminant. Instead we use that pointer field to differentiate
18+
// between the 2 cases.
19+
// Also, if the variant with the nonnullable pointer has no other fields
20+
// then we simply express the enum as just a pointer and not wrap it
21+
// in a struct.
22+
23+
use std::mem;
24+
25+
#[inline(never)]
26+
extern "C" fn foo<'a>(x: &'a int) -> Option<&'a int> { Some(x) }
27+
28+
static FOO: int = 0xDEADBEE;
29+
30+
pub fn main() {
31+
unsafe {
32+
let f: extern "C" fn<'a>(&'a int) -> &'a int = mem::transmute(foo);
33+
assert_eq!(*f(&FOO), FOO);
34+
}
35+
}

0 commit comments

Comments
 (0)