Skip to content

Commit 3dbbe2f

Browse files
authored
Auto merge of rust-lang#34684 - oli-obk:eval_rustdoc_array_len, r=alexcrichton
evaluate the array length of fixed size array types in rustdoc mitgates rust-lang#34579 to fix it we'd need an expression simplifier. r? @steveklabnik cc @Osspial
2 parents db71987 + 9d33ce5 commit 3dbbe2f

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

src/librustdoc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ arena = { path = "../libarena" }
1414
rustc = { path = "../librustc" }
1515
rustc_back = { path = "../librustc_back" }
1616
rustc_const_eval = { path = "../librustc_const_eval" }
17+
rustc_const_math = { path = "../librustc_const_math" }
1718
rustc_driver = { path = "../librustc_driver" }
1819
rustc_errors = { path = "../librustc_errors" }
1920
rustc_lint = { path = "../librustc_lint" }

src/librustdoc/clean/mod.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -1624,8 +1624,25 @@ impl Clean<Type> for hir::Ty {
16241624
BorrowedRef {lifetime: l.clean(cx), mutability: m.mutbl.clean(cx),
16251625
type_: box m.ty.clean(cx)},
16261626
TyVec(ref ty) => Vector(box ty.clean(cx)),
1627-
TyFixedLengthVec(ref ty, ref e) =>
1628-
FixedVector(box ty.clean(cx), pprust::expr_to_string(e)),
1627+
TyFixedLengthVec(ref ty, ref e) => {
1628+
let n = if let Some(tcx) = cx.tcx_opt() {
1629+
use rustc_const_math::{ConstInt, ConstUsize};
1630+
use rustc_const_eval::eval_const_expr;
1631+
use rustc::middle::const_val::ConstVal;
1632+
match eval_const_expr(tcx, e) {
1633+
ConstVal::Integral(ConstInt::Usize(u)) => match u {
1634+
ConstUsize::Us16(u) => u.to_string(),
1635+
ConstUsize::Us32(u) => u.to_string(),
1636+
ConstUsize::Us64(u) => u.to_string(),
1637+
},
1638+
// after type checking this can't fail
1639+
_ => unreachable!(),
1640+
}
1641+
} else {
1642+
pprust::expr_to_string(e)
1643+
};
1644+
FixedVector(box ty.clean(cx), n)
1645+
},
16291646
TyTup(ref tys) => Tuple(tys.clean(cx)),
16301647
TyPath(None, ref p) => {
16311648
resolve_type(cx, p.clean(cx), self.id)

src/librustdoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ extern crate getopts;
3434
extern crate libc;
3535
extern crate rustc;
3636
extern crate rustc_const_eval;
37+
extern crate rustc_const_math;
3738
extern crate rustc_trans;
3839
extern crate rustc_driver;
3940
extern crate rustc_resolve;

src/rustc/Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/test/rustdoc/issue-33302.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ macro_rules! make {
3434
}
3535

3636
// @has issue_33302/struct.S.html \
37-
// '//h3[@class="impl"]' 'impl T<[i32; 4 * 4]> for S'
38-
// @has - '//*[@id="associatedconstant.C"]' 'const C: [i32; 4 * 4] = [0; 4 * 4]'
37+
// '//h3[@class="impl"]' 'impl T<[i32; 16]> for S'
38+
// @has - '//*[@id="associatedconstant.C"]' 'const C: [i32; 16] = [0; 4 * 4]'
3939
// @has - '//*[@id="associatedconstant.D"]' 'const D: i32 = 4 * 4'
4040
impl T<[i32; ($n * $n)]> for S {
4141
const C: [i32; ($n * $n)] = [0; ($n * $n)];

0 commit comments

Comments
 (0)