Skip to content

evaluate the array length of fixed size array types in rustdoc #34684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/librustdoc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
21 changes: 19 additions & 2 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1624,8 +1624,25 @@ impl Clean<Type> 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)
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/rustc/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/test/rustdoc/issue-33302.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
Expand Down