about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJed Davis <jld@panix.com>2014-03-04 20:41:21 -0800
committerJed Davis <jld@panix.com>2014-03-05 21:23:33 -0800
commitd90830221e76ca41ac336a32f308f58e45ca4ae8 (patch)
treef0f73df2afb666306e3c90913db65fc2071334f0
parent67c5d793add67878e6e472258748ac13e0f90366 (diff)
downloadrust-d90830221e76ca41ac336a32f308f58e45ca4ae8.tar.gz
rust-d90830221e76ca41ac336a32f308f58e45ca4ae8.zip
Fix ICE on statics with fancy nullable enums.
Closes #8506.

The `trans::adt` code for statics uses fields with `C_undef` values to
insert alignment padding (because LLVM's own alignment padding isn't
always sufficient for aggregate constants), and assumes that all fields
in the actual Rust value are represented by non-undef LLVM values, to
distinguish them from that padding.

But for nullable pointer enums, if non-null variant has fields other
than the pointer used as the discriminant, they would be set to undef in
the null case, to reflect that they're never accessed.

To avoid the obvious conflict between these two items, the latter undefs
were wrapped in unary LLVM structs to distinguish them from the former
undefs.  Except this doesn't actually work -- LLVM, not unreasonably,
treats the "wrapped undef" as a regular undef.

So this commit just sets all fields to null in the null pointer case of
a nullable pointer enum static, because the other fields don't really
need to be undef in the first place.
-rw-r--r--src/librustc/middle/trans/adt.rs21
-rw-r--r--src/test/run-pass/issue-8506.rs20
2 files changed, 28 insertions, 13 deletions
diff --git a/src/librustc/middle/trans/adt.rs b/src/librustc/middle/trans/adt.rs
index 2c68914d21f..ae0025ffa30 100644
--- a/src/librustc/middle/trans/adt.rs
+++ b/src/librustc/middle/trans/adt.rs
@@ -749,14 +749,15 @@ pub fn trans_const(ccx: &CrateContext, r: &Repr, discr: Disr,
             let contents = build_const_struct(ccx, st, vals);
             C_struct(contents, st.packed)
         }
-        NullablePointer{ nonnull: ref nonnull, nndiscr, ptrfield, .. } => {
+        NullablePointer{ nonnull: ref nonnull, nndiscr, .. } => {
             if discr == nndiscr {
                 C_struct(build_const_struct(ccx, nonnull, vals), false)
             } else {
-                let vals = nonnull.fields.iter().enumerate().map(|(i, &ty)| {
-                    let llty = type_of::sizing_type_of(ccx, ty);
-                    if i == ptrfield { C_null(llty) } else { C_undef(llty) }
-                }).collect::<~[ValueRef]>();
+                let vals = nonnull.fields.map(|&ty| {
+                    // Always use null even if it's not the `ptrfield`th
+                    // field; see #8506.
+                    C_null(type_of::sizing_type_of(ccx, ty))
+                });
                 C_struct(build_const_struct(ccx, nonnull, vals), false)
             }
         }
@@ -791,14 +792,8 @@ fn build_const_struct(ccx: &CrateContext, st: &Struct, vals: &[ValueRef])
             cfields.push(padding(target_offset - offset));
             offset = target_offset;
         }
-        let val = if is_undef(vals[i]) {
-            let wrapped = C_struct([vals[i]], false);
-            assert!(!is_undef(wrapped));
-            wrapped
-        } else {
-            vals[i]
-        };
-        cfields.push(val);
+        assert!(!is_undef(vals[i]));
+        cfields.push(vals[i]);
         offset += machine::llsize_of_alloc(ccx, llty) as u64
     }
 
diff --git a/src/test/run-pass/issue-8506.rs b/src/test/run-pass/issue-8506.rs
new file mode 100644
index 00000000000..b5e9141deef
--- /dev/null
+++ b/src/test/run-pass/issue-8506.rs
@@ -0,0 +1,20 @@
+// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#[allow(dead_code)];
+
+enum Either {
+    One,
+    Other(~str,~str)
+}
+
+static one : Either = One;
+
+pub fn main () { }