]> git.lizzy.rs Git - rust.git/commitdiff
evaluate the array length of fixed size array types in rustdoc
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Wed, 13 Jul 2016 08:35:58 +0000 (10:35 +0200)
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Wed, 13 Jul 2016 08:35:58 +0000 (10:35 +0200)
src/librustdoc/Cargo.toml
src/librustdoc/clean/mod.rs
src/librustdoc/lib.rs
src/rustc/Cargo.lock
src/test/rustdoc/issue-33302.rs

index cf87aabdfdb5e6c572a637569d6ed8176da35cc6..a41d3b0253a3668c794bb72e74fb02120de9c5d0 100644 (file)
@@ -14,6 +14,7 @@ arena = { path = "../libarena" }
 rustc = { path = "../librustc" }
 rustc_back = { path = "../librustc_back" }
 rustc_const_eval = { path = "../librustc_const_eval" }
+rustc_const_math = { path = "../librustc_const_math" }
 rustc_driver = { path = "../librustc_driver" }
 rustc_errors = { path = "../librustc_errors" }
 rustc_lint = { path = "../librustc_lint" }
index 7827459baa87f310818813f69320e9982cbd0859..0e9edb4dda8cee44195ae40f6d048b4e8e41a739 100644 (file)
@@ -1624,8 +1624,25 @@ fn clean(&self, cx: &DocContext) -> Type {
                 BorrowedRef {lifetime: l.clean(cx), mutability: m.mutbl.clean(cx),
                              type_: box m.ty.clean(cx)},
             TyVec(ref ty) => Vector(box ty.clean(cx)),
-            TyFixedLengthVec(ref ty, ref e) =>
-                FixedVector(box ty.clean(cx), pprust::expr_to_string(e)),
+            TyFixedLengthVec(ref ty, ref e) => {
+                let n = if let Some(tcx) = cx.tcx_opt() {
+                    use rustc_const_math::{ConstInt, ConstUsize};
+                    use rustc_const_eval::eval_const_expr;
+                    use rustc::middle::const_val::ConstVal;
+                    match eval_const_expr(tcx, e) {
+                        ConstVal::Integral(ConstInt::Usize(u)) => match u {
+                            ConstUsize::Us16(u) => u.to_string(),
+                            ConstUsize::Us32(u) => u.to_string(),
+                            ConstUsize::Us64(u) => u.to_string(),
+                        },
+                        // after type checking this can't fail
+                        _ => unreachable!(),
+                    }
+                } else {
+                    pprust::expr_to_string(e)
+                };
+                FixedVector(box ty.clean(cx), n)
+            },
             TyTup(ref tys) => Tuple(tys.clean(cx)),
             TyPath(None, ref p) => {
                 resolve_type(cx, p.clean(cx), self.id)
index 2015bb295eabd7be1f04f7b14bcd4008a61988f4..d0c4f126550ca177356b4032497309372d3cb436 100644 (file)
@@ -34,6 +34,7 @@
 extern crate libc;
 extern crate rustc;
 extern crate rustc_const_eval;
+extern crate rustc_const_math;
 extern crate rustc_trans;
 extern crate rustc_driver;
 extern crate rustc_resolve;
index c9d1eb39f0a3de464f6028d192e6ad4737ceb1e0..4def60e485f7e0a05635c3648f5edf0669ea535a 100644 (file)
@@ -350,6 +350,7 @@ dependencies = [
  "rustc 0.0.0",
  "rustc_back 0.0.0",
  "rustc_const_eval 0.0.0",
+ "rustc_const_math 0.0.0",
  "rustc_driver 0.0.0",
  "rustc_errors 0.0.0",
  "rustc_lint 0.0.0",
index b9188e8a4e9bacbb82a7507f72ee12a03a3f9cbf..c6da6b0575b87c3bef842f051525a3ae3df97920 100644 (file)
@@ -34,8 +34,8 @@ fn ignore(_: &X) {}
         }
 
         // @has issue_33302/struct.S.html \
-        //        '//h3[@class="impl"]' 'impl T<[i32; 4 * 4]> for S'
-        // @has - '//*[@id="associatedconstant.C"]' 'const C: [i32; 4 * 4] = [0; 4 * 4]'
+        //        '//h3[@class="impl"]' 'impl T<[i32; 16]> for S'
+        // @has - '//*[@id="associatedconstant.C"]' 'const C: [i32; 16] = [0; 4 * 4]'
         // @has - '//*[@id="associatedconstant.D"]' 'const D: i32 = 4 * 4'
         impl T<[i32; ($n * $n)]> for S {
             const C: [i32; ($n * $n)] = [0; ($n * $n)];