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.
6 use crate::hair::util::UserAnnotatedTyHelpers;
8 use rustc_data_structures::indexed_vec::Idx;
9 use rustc::hir::def_id::DefId;
11 use rustc::middle::region;
12 use rustc::infer::InferCtxt;
13 use rustc::ty::subst::Subst;
14 use rustc::ty::{self, Ty, TyCtxt};
15 use rustc::ty::subst::{Kind, InternalSubsts};
16 use rustc::ty::layout::VariantIdx;
19 use syntax::symbol::Symbol;
21 use crate::hair::constant::{lit_to_const, LitToConstError};
24 pub struct Cx<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
25 tcx: TyCtxt<'a, 'gcx, 'tcx>,
26 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
28 pub root_lint_level: hir::HirId,
29 pub param_env: ty::ParamEnv<'gcx>,
31 /// Identity `InternalSubsts` for use with const-evaluation.
32 pub identity_substs: &'gcx InternalSubsts<'gcx>,
34 pub region_scope_tree: &'gcx region::ScopeTree,
35 pub tables: &'a ty::TypeckTables<'gcx>,
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 /// What kind of body is being compiled.
42 pub body_owner_kind: hir::BodyOwnerKind,
44 /// Whether this constant/function needs overflow checks.
47 /// See field with the same name on `Mir`.
48 control_flow_destroyed: Vec<(Span, String)>,
51 impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
52 pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
53 src_id: hir::HirId) -> Cx<'a, 'gcx, 'tcx> {
55 let src_def_id = tcx.hir().local_def_id_from_hir_id(src_id);
56 let body_owner_kind = tcx.hir().body_owner_kind_by_hir_id(src_id);
58 let constness = match body_owner_kind {
59 hir::BodyOwnerKind::Const |
60 hir::BodyOwnerKind::Static(_) => hir::Constness::Const,
61 hir::BodyOwnerKind::Closure |
62 hir::BodyOwnerKind::Fn => hir::Constness::NotConst,
65 let attrs = tcx.hir().attrs_by_hir_id(src_id);
67 // Some functions always have overflow checks enabled,
68 // however, they may not get codegen'd, depending on
69 // the settings for the crate they are codegened in.
70 let mut check_overflow = attr::contains_name(attrs, "rustc_inherit_overflow_checks");
72 // Respect -C overflow-checks.
73 check_overflow |= tcx.sess.overflow_checks();
75 // Constants always need overflow checks.
76 check_overflow |= constness == hir::Constness::Const;
81 root_lint_level: src_id,
82 param_env: tcx.param_env(src_def_id),
83 identity_substs: InternalSubsts::identity_for_item(tcx.global_tcx(), src_def_id),
84 region_scope_tree: tcx.region_scope_tree(src_def_id),
85 tables: tcx.typeck_tables_of(src_def_id),
89 control_flow_destroyed: Vec::new(),
93 pub fn control_flow_destroyed(self) -> Vec<(Span, String)> {
94 self.control_flow_destroyed
98 impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
99 /// Normalizes `ast` into the appropriate "mirror" type.
100 pub fn mirror<M: Mirror<'tcx>>(&mut self, ast: M) -> M::Output {
101 ast.make_mirror(self)
104 pub fn usize_ty(&mut self) -> Ty<'tcx> {
108 pub fn usize_literal(&mut self, value: u64) -> &'tcx ty::Const<'tcx> {
109 self.tcx.mk_const(ty::Const::from_usize(self.tcx, value))
112 pub fn bool_ty(&mut self) -> Ty<'tcx> {
116 pub fn unit_ty(&mut self) -> Ty<'tcx> {
120 pub fn true_literal(&mut self) -> &'tcx ty::Const<'tcx> {
121 self.tcx.mk_const(ty::Const::from_bool(self.tcx, true))
124 pub fn false_literal(&mut self) -> &'tcx ty::Const<'tcx> {
125 self.tcx.mk_const(ty::Const::from_bool(self.tcx, false))
128 pub fn const_eval_literal(
130 lit: &'tcx ast::LitKind,
134 ) -> ty::Const<'tcx> {
135 trace!("const_eval_literal: {:#?}, {:?}, {:?}, {:?}", lit, ty, sp, neg);
137 match lit_to_const(lit, self.tcx, ty, neg) {
139 Err(LitToConstError::UnparseableFloat) => {
140 // FIXME(#31407) this is only necessary because float parsing is buggy
141 self.tcx.sess.span_err(sp, "could not evaluate float literal (see issue #31407)");
142 // create a dummy value and continue compiling
143 Const::from_bits(self.tcx, 0, self.param_env.and(ty))
145 Err(LitToConstError::Reported) => {
146 // create a dummy value and continue compiling
147 Const::from_bits(self.tcx, 0, self.param_env.and(ty))
152 pub fn pattern_from_hir(&mut self, p: &hir::Pat) -> Pattern<'tcx> {
153 let tcx = self.tcx.global_tcx();
154 let p = match tcx.hir().get_by_hir_id(p.hir_id) {
155 Node::Pat(p) | Node::Binding(p) => p,
156 node => bug!("pattern became {:?}", node)
158 Pattern::from_hir(tcx,
159 self.param_env.and(self.identity_substs),
164 pub fn trait_method(&mut self,
168 params: &[Kind<'tcx>])
169 -> (Ty<'tcx>, ty::Const<'tcx>) {
170 let method_name = Symbol::intern(method_name);
171 let substs = self.tcx.mk_substs_trait(self_ty, params);
172 for item in self.tcx.associated_items(trait_def_id) {
173 if item.kind == ty::AssociatedKind::Method && item.ident.name == method_name {
174 let method_ty = self.tcx.type_of(item.def_id);
175 let method_ty = method_ty.subst(self.tcx, substs);
176 return (method_ty, ty::Const::zero_sized(method_ty));
180 bug!("found no method `{}` in `{:?}`", method_name, trait_def_id);
183 pub fn all_fields(&mut self, adt_def: &ty::AdtDef, variant_index: VariantIdx) -> Vec<Field> {
184 (0..adt_def.variants[variant_index].fields.len())
189 pub fn needs_drop(&mut self, ty: Ty<'tcx>) -> bool {
190 let (ty, param_env) = self.tcx.lift_to_global(&(ty, self.param_env)).unwrap_or_else(|| {
191 bug!("MIR: Cx::needs_drop({:?}, {:?}) got \
192 type with inference types/regions",
195 ty.needs_drop(self.tcx.global_tcx(), param_env)
198 pub fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx> {
202 pub fn tables(&self) -> &'a ty::TypeckTables<'gcx> {
206 pub fn check_overflow(&self) -> bool {
210 pub fn type_is_copy_modulo_regions(&self, ty: Ty<'tcx>, span: Span) -> bool {
211 self.infcx.type_is_copy_modulo_regions(self.param_env, ty, span)
215 impl UserAnnotatedTyHelpers<'gcx, 'tcx> for Cx<'_, 'gcx, 'tcx> {
216 fn tcx(&self) -> TyCtxt<'_, 'gcx, 'tcx> {
220 fn tables(&self) -> &ty::TypeckTables<'tcx> {