]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #47892 - Badel2:const_type_id_of, r=oli-obk
authorkennytm <kennytm@gmail.com>
Sun, 4 Feb 2018 17:27:36 +0000 (01:27 +0800)
committerkennytm <kennytm@gmail.com>
Sun, 4 Feb 2018 17:27:36 +0000 (01:27 +0800)
Turn `type_id` into a constant intrinsic

https://github.com/rust-lang/rust/issues/27745

The method `get_type_id` in `Any` is intended to support reflection. It's currently unstable in favor of using an associated constant instead. This PR makes the `type_id` intrinsic a constant intrinsic, the same as `size_of` and `align_of`, allowing `TypeId::of` to be a `const fn`, which will allow using an associated constant in `Any`.

1  2 
src/libcore/lib.rs
src/librustc_const_eval/eval.rs

diff --combined src/libcore/lib.rs
index 2e1f925c49a36ba1fa1ca82f1b2e5be911235cfe,7f094e580ab5c16a4eddb0ae47f789e585f85e7c..59a296c2a762c5e5a25ac3d3e1bcb55e11722502
@@@ -91,6 -91,7 +91,7 @@@
  #![feature(untagged_unions)]
  #![feature(unwind_attributes)]
  #![feature(doc_spotlight)]
+ #![feature(rustc_const_unstable)]
  
  #[prelude_import]
  #[allow(unused)]
@@@ -168,7 -169,6 +169,7 @@@ pub mod slice
  pub mod str;
  pub mod hash;
  pub mod fmt;
 +pub mod time;
  
  // note: does not need to be public
  mod char_private;
index 1d2813e4f6704fac35e5f489e844c6fd32221f72,17a9454e9d45662c3cfee4ea850321cd81021e53..8e4ec93c14baebae44f7ca13e58ff099ca474883
@@@ -17,6 -17,7 +17,6 @@@ use rustc::hir::map::blocks::FnLikeNode
  use rustc::hir::def::{Def, CtorKind};
  use rustc::hir::def_id::DefId;
  use rustc::ty::{self, Ty, TyCtxt};
 -use rustc::ty::layout::LayoutOf;
  use rustc::ty::util::IntTypeExt;
  use rustc::ty::subst::{Substs, Subst};
  use rustc::util::common::ErrorReported;
@@@ -312,7 -313,7 +312,7 @@@ fn eval_const_expr_partial<'a, 'tcx>(cx
            if tcx.fn_sig(def_id).abi() == Abi::RustIntrinsic {
              let layout_of = |ty: Ty<'tcx>| {
                  let ty = tcx.erase_regions(&ty);
 -                (tcx.at(e.span), cx.param_env).layout_of(ty).map_err(|err| {
 +                tcx.at(e.span).layout_of(cx.param_env.and(ty)).map_err(|err| {
                      ConstEvalErr { span: e.span, kind: LayoutError(err) }
                  })
              };
                      return Ok(mk_const(Integral(Usize(ConstUsize::new(align,
                          tcx.sess.target.usize_ty).unwrap()))));
                  }
+                 "type_id" => {
+                     let type_id = tcx.type_id_hash(substs.type_at(0));
+                     return Ok(mk_const(Integral(U64(type_id))));
+                 }
                  _ => signal!(e, TypeckError)
              }
            }