]> git.lizzy.rs Git - rust.git/commitdiff
Fix an LLVM assertion when matching against static strings
authorJakub Wieczorek <jakub@jakub.cc>
Sun, 8 Jun 2014 17:37:45 +0000 (19:37 +0200)
committerJakub Wieczorek <jakub@jakub.cc>
Sun, 8 Jun 2014 17:43:38 +0000 (19:43 +0200)
Fixes #8315
Fixes #11940

src/librustc/middle/trans/_match.rs
src/librustc/middle/trans/common.rs
src/librustc/middle/trans/context.rs
src/librustc/middle/trans/type_.rs
src/test/run-pass/issue-11940.rs [new file with mode: 0644]

index 2a3ec63e995ef3b089211fe9313339ca81acbc07..f6fb6c8ba2013e6c4f0dacdab5c96d6c009ce500 100644 (file)
@@ -312,9 +312,12 @@ fn trans_opt<'a>(bcx: &'a Block<'a>, o: &Opt) -> opt_result<'a> {
             let datum = datum::rvalue_scratch_datum(bcx, struct_ty, "");
             return single_result(Result::new(bcx, datum.val));
         }
-        lit(ConstLit(lit_id)) => {
-            let (llval, _) = consts::get_const_val(bcx.ccx(), lit_id);
-            return single_result(Result::new(bcx, llval));
+        lit(l @ ConstLit(ref def_id)) => {
+            let lit_ty = ty::node_id_to_type(bcx.tcx(), lit_to_expr(bcx.tcx(), &l).id);
+            let (llval, _) = consts::get_const_val(bcx.ccx(), *def_id);
+            let lit_datum = immediate_rvalue(llval, lit_ty);
+            let lit_datum = unpack_datum!(bcx, lit_datum.to_appropriate_datum(bcx));
+            return single_result(Result::new(bcx, lit_datum.val));
         }
         var(disr_val, ref repr) => {
             return adt::trans_case(bcx, &**repr, disr_val);
index 1bcf47531dd561bb757da7e95b834f7e75e3ce24..9bd6b8ed3618c97b57397d274a6212b0af441960 100644 (file)
@@ -599,7 +599,7 @@ pub fn C_str_slice(cx: &CrateContext, s: InternedString) -> ValueRef {
         let len = s.get().len();
         let cs = llvm::LLVMConstPointerCast(C_cstr(cx, s, false),
                                             Type::i8p(cx).to_ref());
-        C_struct(cx, [cs, C_uint(cx, len)], false)
+        C_named_struct(cx.tn.find_type("str_slice").unwrap(), [cs, C_uint(cx, len)])
     }
 }
 
index 8607d52b6241fa8f9acbc0e05b915d148a1c3ccf..a80ae9e2596da0c16a2f0c2ad488f2a215dfb43e 100644 (file)
@@ -233,12 +233,12 @@ pub fn new(name: &str,
             ccx.int_type = Type::int(&ccx);
             ccx.opaque_vec_type = Type::opaque_vec(&ccx);
 
-            ccx.tn.associate_type("tydesc", &Type::tydesc(&ccx));
-
             let mut str_slice_ty = Type::named_struct(&ccx, "str_slice");
             str_slice_ty.set_struct_body([Type::i8p(&ccx), ccx.int_type], false);
             ccx.tn.associate_type("str_slice", &str_slice_ty);
 
+            ccx.tn.associate_type("tydesc", &Type::tydesc(&ccx, str_slice_ty));
+
             if ccx.sess().count_llvm_insns() {
                 base::init_insn_ctxt()
             }
index dfe06f9ca4c89a9b87bac7fde066c03b127e1827..5d58500f761a4581044d0af023814b4c5a4391ee 100644 (file)
@@ -186,7 +186,7 @@ pub fn glue_fn(ccx: &CrateContext, t: Type) -> Type {
         Type::func([t], &Type::void(ccx))
     }
 
-    pub fn tydesc(ccx: &CrateContext) -> Type {
+    pub fn tydesc(ccx: &CrateContext, str_slice_ty: Type) -> Type {
         let mut tydesc = Type::named_struct(ccx, "tydesc");
         let glue_fn_ty = Type::glue_fn(ccx, Type::i8p(ccx)).ptr_to();
 
@@ -200,7 +200,7 @@ pub fn tydesc(ccx: &CrateContext) -> Type {
                      int_ty,     // align
                      glue_fn_ty, // drop
                      glue_fn_ty, // visit
-                     Type::struct_(ccx, [Type::i8p(ccx), Type::int(ccx)], false)]; // name
+                     str_slice_ty]; // name
         tydesc.set_struct_body(elems, false);
 
         tydesc
diff --git a/src/test/run-pass/issue-11940.rs b/src/test/run-pass/issue-11940.rs
new file mode 100644 (file)
index 0000000..bbe7eff
--- /dev/null
@@ -0,0 +1,19 @@
+// Copyright 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.
+
+static TEST_STR: &'static str = "abcd";
+
+fn main() {
+    let s = "abcd";
+    match s {
+        TEST_STR => (),
+        _ => unreachable!()
+    }
+}