Skip to content

Commit

Permalink
Rollup merge of rust-lang#46894 - detrumi:fix-const-eval-trait, r=eddyb
Browse files Browse the repository at this point in the history
Const-eval array lengths in rustdoc.

Fixes rust-lang#46727
r? @eddyb

Big thanks to @eddyb for helping me figure this out.
  • Loading branch information
kennytm authored Dec 23, 2017
2 parents 330c261 + d10d389 commit d2d3b82
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use rustc::middle::resolve_lifetime as rl;
use rustc::middle::lang_items;
use rustc::hir::def::{Def, CtorKind};
use rustc::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc::traits::Reveal;
use rustc::ty::subst::Substs;
use rustc::ty::{self, Ty, AdtKind};
use rustc::middle::stability;
Expand Down Expand Up @@ -2062,7 +2061,7 @@ impl Clean<Type> for hir::Ty {
TySlice(ref ty) => Slice(box ty.clean(cx)),
TyArray(ref ty, n) => {
let def_id = cx.tcx.hir.body_owner_def_id(n);
let param_env = ty::ParamEnv::empty(Reveal::UserFacing);
let param_env = cx.tcx.param_env(def_id);
let substs = Substs::identity_for_item(cx.tcx, def_id);
let n = cx.tcx.const_eval(param_env.and((def_id, substs))).unwrap();
let n = if let ConstVal::Integral(ConstInt::Usize(n)) = n.val {
Expand Down Expand Up @@ -2191,6 +2190,11 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
ty::TyStr => Primitive(PrimitiveType::Str),
ty::TySlice(ty) => Slice(box ty.clean(cx)),
ty::TyArray(ty, n) => {
let mut n = cx.tcx.lift(&n).unwrap();
if let ConstVal::Unevaluated(def_id, substs) = n.val {
let param_env = cx.tcx.param_env(def_id);
n = cx.tcx.const_eval(param_env.and((def_id, substs))).unwrap()
};
let n = if let ConstVal::Integral(ConstInt::Usize(n)) = n.val {
n.to_string()
} else if let ConstVal::Unevaluated(def_id, _) = n.val {
Expand Down
17 changes: 17 additions & 0 deletions src/test/rustdoc/auxiliary/issue-46727.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-flags: -Cmetadata=aux

pub trait Foo {}

pub struct Bar<T> { x: T }

impl<T> Foo for Bar<[T; 1 + 1 + 1]> {}
17 changes: 17 additions & 0 deletions src/test/rustdoc/issue-46727.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:issue-46727.rs

extern crate issue_46727;

// @has issue_46727/trait.Foo.html
// @has - '//code' 'impl<T> Foo for Bar<[T; 3]>'
pub use issue_46727::{Foo, Bar};

0 comments on commit d2d3b82

Please sign in to comment.