Skip to content

Commit 304717b

Browse files
committed
Auto merge of #46894 - detrumi:fix-const-eval-trait, r=eddyb
Const-eval array lengths in rustdoc. Fixes #46727 r? @eddyb Big thanks to @eddyb for helping me figure this out.
2 parents 4ce6b9a + d10d389 commit 304717b

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/librustdoc/clean/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ use rustc::middle::resolve_lifetime as rl;
3333
use rustc::middle::lang_items;
3434
use rustc::hir::def::{Def, CtorKind};
3535
use rustc::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
36-
use rustc::traits::Reveal;
3736
use rustc::ty::subst::Substs;
3837
use rustc::ty::{self, Ty, AdtKind};
3938
use rustc::middle::stability;
@@ -2062,7 +2061,7 @@ impl Clean<Type> for hir::Ty {
20622061
TySlice(ref ty) => Slice(box ty.clean(cx)),
20632062
TyArray(ref ty, n) => {
20642063
let def_id = cx.tcx.hir.body_owner_def_id(n);
2065-
let param_env = ty::ParamEnv::empty(Reveal::UserFacing);
2064+
let param_env = cx.tcx.param_env(def_id);
20662065
let substs = Substs::identity_for_item(cx.tcx, def_id);
20672066
let n = cx.tcx.const_eval(param_env.and((def_id, substs))).unwrap();
20682067
let n = if let ConstVal::Integral(ConstInt::Usize(n)) = n.val {
@@ -2191,6 +2190,11 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
21912190
ty::TyStr => Primitive(PrimitiveType::Str),
21922191
ty::TySlice(ty) => Slice(box ty.clean(cx)),
21932192
ty::TyArray(ty, n) => {
2193+
let mut n = cx.tcx.lift(&n).unwrap();
2194+
if let ConstVal::Unevaluated(def_id, substs) = n.val {
2195+
let param_env = cx.tcx.param_env(def_id);
2196+
n = cx.tcx.const_eval(param_env.and((def_id, substs))).unwrap()
2197+
};
21942198
let n = if let ConstVal::Integral(ConstInt::Usize(n)) = n.val {
21952199
n.to_string()
21962200
} else if let ConstVal::Unevaluated(def_id, _) = n.val {
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Cmetadata=aux
12+
13+
pub trait Foo {}
14+
15+
pub struct Bar<T> { x: T }
16+
17+
impl<T> Foo for Bar<[T; 1 + 1 + 1]> {}

src/test/rustdoc/issue-46727.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:issue-46727.rs
12+
13+
extern crate issue_46727;
14+
15+
// @has issue_46727/trait.Foo.html
16+
// @has - '//code' 'impl<T> Foo for Bar<[T; 3]>'
17+
pub use issue_46727::{Foo, Bar};

0 commit comments

Comments
 (0)