From 9d33ce58b104f1f0e29a69b30c6b4353f1e28c52 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Wed, 13 Jul 2016 10:35:58 +0200 Subject: [PATCH] evaluate the array length of fixed size array types in rustdoc --- src/librustdoc/Cargo.toml | 1 + src/librustdoc/clean/mod.rs | 21 +++++++++++++++++++-- src/librustdoc/lib.rs | 1 + src/rustc/Cargo.lock | 1 + src/test/rustdoc/issue-33302.rs | 4 ++-- 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/librustdoc/Cargo.toml b/src/librustdoc/Cargo.toml index cf87aabdfdb..a41d3b0253a 100644 --- a/src/librustdoc/Cargo.toml +++ b/src/librustdoc/Cargo.toml @@ -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" } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 7827459baa8..0e9edb4dda8 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1624,8 +1624,25 @@ impl Clean for hir::Ty { 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) diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 2015bb295ea..d0c4f126550 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -34,6 +34,7 @@ extern crate getopts; 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; diff --git a/src/rustc/Cargo.lock b/src/rustc/Cargo.lock index c9d1eb39f0a..4def60e485f 100644 --- a/src/rustc/Cargo.lock +++ b/src/rustc/Cargo.lock @@ -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", diff --git a/src/test/rustdoc/issue-33302.rs b/src/test/rustdoc/issue-33302.rs index b9188e8a4e9..c6da6b0575b 100644 --- a/src/test/rustdoc/issue-33302.rs +++ b/src/test/rustdoc/issue-33302.rs @@ -34,8 +34,8 @@ macro_rules! make { } // @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)];