1 //! This module contains the fcuntaiontliy to convert from the wacky tcx data
2 //! structures into the HAIR. The `builder` is generally ignorant of the tcx,
3 //! etc., and instead goes through the `Cx` for most of its work.
5 use crate::hair::util::UserAnnotatedTyHelpers;
8 use crate::hair::constant::{lit_to_const, LitToConstError};
10 use rustc::hir::def_id::DefId;
12 use rustc::infer::InferCtxt;
13 use rustc::middle::region;
14 use rustc::ty::layout::VariantIdx;
15 use rustc::ty::subst::Subst;
16 use rustc::ty::subst::{GenericArg, InternalSubsts};
17 use rustc::ty::{self, Ty, TyCtxt};
18 use rustc_index::vec::Idx;
21 use syntax::symbol::{sym, Symbol};
24 pub struct Cx<'a, 'tcx> {
26 infcx: &'a InferCtxt<'a, 'tcx>,
28 pub root_lint_level: hir::HirId,
29 pub param_env: ty::ParamEnv<'tcx>,
31 /// Identity `InternalSubsts` for use with const-evaluation.
32 pub identity_substs: &'tcx InternalSubsts<'tcx>,
34 pub region_scope_tree: &'tcx region::ScopeTree,
35 pub tables: &'a ty::TypeckTables<'tcx>,
37 /// This is `Constness::Const` if we are compiling a `static`,
38 /// `const`, or the body of a `const fn`.
39 constness: hir::Constness,
41 /// The `DefId` of the owner of this body.
44 /// What kind of body is being compiled.
45 pub body_owner_kind: hir::BodyOwnerKind,
47 /// Whether this constant/function needs overflow checks.
50 /// See field with the same name on `mir::Body`.
51 control_flow_destroyed: Vec<(Span, String)>,
54 impl<'a, 'tcx> Cx<'a, 'tcx> {
55 pub fn new(infcx: &'a InferCtxt<'a, 'tcx>, src_id: hir::HirId) -> Cx<'a, 'tcx> {
57 let src_def_id = tcx.hir().local_def_id(src_id);
58 let tables = tcx.typeck_tables_of(src_def_id);
59 let body_owner_kind = tcx.hir().body_owner_kind(src_id);
61 let constness = match body_owner_kind {
62 hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) => hir::Constness::Const,
63 hir::BodyOwnerKind::Closure | hir::BodyOwnerKind::Fn => hir::Constness::NotConst,
66 let attrs = tcx.hir().attrs(src_id);
68 // Some functions always have overflow checks enabled,
69 // however, they may not get codegen'd, depending on
70 // the settings for the crate they are codegened in.
71 let mut check_overflow = attr::contains_name(attrs, sym::rustc_inherit_overflow_checks);
73 // Respect -C overflow-checks.
74 check_overflow |= tcx.sess.overflow_checks();
76 // Constants always need overflow checks.
77 check_overflow |= constness == hir::Constness::Const;
82 root_lint_level: src_id,
83 param_env: tcx.param_env(src_def_id),
84 identity_substs: InternalSubsts::identity_for_item(tcx, src_def_id),
85 region_scope_tree: tcx.region_scope_tree(src_def_id),
88 body_owner: src_def_id,
91 control_flow_destroyed: Vec::new(),
95 pub fn control_flow_destroyed(self) -> Vec<(Span, String)> {
96 self.control_flow_destroyed
100 impl<'a, 'tcx> Cx<'a, 'tcx> {
101 /// Normalizes `ast` into the appropriate "mirror" type.
102 pub fn mirror<M: Mirror<'tcx>>(&mut self, ast: M) -> M::Output {
103 ast.make_mirror(self)
106 pub fn usize_ty(&mut self) -> Ty<'tcx> {
110 pub fn usize_literal(&mut self, value: u64) -> &'tcx ty::Const<'tcx> {
111 ty::Const::from_usize(self.tcx, value)
114 pub fn bool_ty(&mut self) -> Ty<'tcx> {
118 pub fn unit_ty(&mut self) -> Ty<'tcx> {
122 pub fn true_literal(&mut self) -> &'tcx ty::Const<'tcx> {
123 ty::Const::from_bool(self.tcx, true)
126 pub fn false_literal(&mut self) -> &'tcx ty::Const<'tcx> {
127 ty::Const::from_bool(self.tcx, false)
130 pub fn const_eval_literal(
132 lit: &'tcx ast::LitKind,
136 ) -> &'tcx ty::Const<'tcx> {
137 trace!("const_eval_literal: {:#?}, {:?}, {:?}, {:?}", lit, ty, sp, neg);
139 match lit_to_const(lit, self.tcx, ty, neg) {
141 Err(LitToConstError::UnparseableFloat) => {
142 // FIXME(#31407) this is only necessary because float parsing is buggy
143 self.tcx.sess.span_err(sp, "could not evaluate float literal (see issue #31407)");
144 // create a dummy value and continue compiling
145 Const::from_bits(self.tcx, 0, self.param_env.and(ty))
147 Err(LitToConstError::Reported) => {
148 // create a dummy value and continue compiling
149 Const::from_bits(self.tcx, 0, self.param_env.and(ty))
154 pub fn pattern_from_hir(&mut self, p: &hir::Pat) -> Pat<'tcx> {
155 let p = match self.tcx.hir().get(p.hir_id) {
156 Node::Pat(p) | Node::Binding(p) => p,
157 node => bug!("pattern became {:?}", node),
159 Pat::from_hir(self.tcx, self.param_env.and(self.identity_substs), self.tables(), p)
167 params: &[GenericArg<'tcx>],
168 ) -> &'tcx ty::Const<'tcx> {
169 let substs = self.tcx.mk_substs_trait(self_ty, params);
170 for item in self.tcx.associated_items(trait_def_id) {
171 if item.kind == ty::AssocKind::Method && item.ident.name == method_name {
172 let method_ty = self.tcx.type_of(item.def_id);
173 let method_ty = method_ty.subst(self.tcx, substs);
174 return ty::Const::zero_sized(self.tcx, method_ty);
178 bug!("found no method `{}` in `{:?}`", method_name, trait_def_id);
181 pub fn all_fields(&mut self, adt_def: &ty::AdtDef, variant_index: VariantIdx) -> Vec<Field> {
182 (0..adt_def.variants[variant_index].fields.len()).map(Field::new).collect()
185 pub fn needs_drop(&mut self, ty: Ty<'tcx>) -> bool {
186 ty.needs_drop(self.tcx, self.param_env)
189 pub fn tcx(&self) -> TyCtxt<'tcx> {
193 pub fn tables(&self) -> &'a ty::TypeckTables<'tcx> {
197 pub fn check_overflow(&self) -> bool {
201 pub fn type_is_copy_modulo_regions(&self, ty: Ty<'tcx>, span: Span) -> bool {
202 self.infcx.type_is_copy_modulo_regions(self.param_env, ty, span)
206 impl UserAnnotatedTyHelpers<'tcx> for Cx<'_, 'tcx> {
207 fn tcx(&self) -> TyCtxt<'tcx> {
211 fn tables(&self) -> &ty::TypeckTables<'tcx> {