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, sym};
21 use crate::hair::constant::{lit_to_const, LitToConstError};
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_from_hir_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 |
63 hir::BodyOwnerKind::Static(_) => hir::Constness::Const,
64 hir::BodyOwnerKind::Closure |
65 hir::BodyOwnerKind::Fn => hir::Constness::NotConst,
68 let attrs = tcx.hir().attrs(src_id);
70 // Some functions always have overflow checks enabled,
71 // however, they may not get codegen'd, depending on
72 // the settings for the crate they are codegened in.
73 let mut check_overflow = attr::contains_name(attrs, sym::rustc_inherit_overflow_checks);
75 // Respect -C overflow-checks.
76 check_overflow |= tcx.sess.overflow_checks();
78 // Constants always need overflow checks.
79 check_overflow |= constness == hir::Constness::Const;
84 root_lint_level: src_id,
85 param_env: tcx.param_env(src_def_id),
86 identity_substs: InternalSubsts::identity_for_item(tcx.global_tcx(), src_def_id),
87 region_scope_tree: tcx.region_scope_tree(src_def_id),
90 body_owner: src_def_id,
93 control_flow_destroyed: Vec::new(),
97 pub fn control_flow_destroyed(self) -> Vec<(Span, String)> {
98 self.control_flow_destroyed
102 impl<'a, 'tcx> Cx<'a, 'tcx> {
103 /// Normalizes `ast` into the appropriate "mirror" type.
104 pub fn mirror<M: Mirror<'tcx>>(&mut self, ast: M) -> M::Output {
105 ast.make_mirror(self)
108 pub fn usize_ty(&mut self) -> Ty<'tcx> {
112 pub fn usize_literal(&mut self, value: u64) -> &'tcx ty::Const<'tcx> {
113 ty::Const::from_usize(self.tcx, value)
116 pub fn bool_ty(&mut self) -> Ty<'tcx> {
120 pub fn unit_ty(&mut self) -> Ty<'tcx> {
124 pub fn true_literal(&mut self) -> &'tcx ty::Const<'tcx> {
125 ty::Const::from_bool(self.tcx, true)
128 pub fn false_literal(&mut self) -> &'tcx ty::Const<'tcx> {
129 ty::Const::from_bool(self.tcx, false)
132 pub fn const_eval_literal(
134 lit: &'tcx ast::LitKind,
138 ) -> &'tcx ty::Const<'tcx> {
139 trace!("const_eval_literal: {:#?}, {:?}, {:?}, {:?}", lit, ty, sp, neg);
141 match lit_to_const(lit, self.tcx, ty, neg) {
143 Err(LitToConstError::UnparseableFloat) => {
144 // FIXME(#31407) this is only necessary because float parsing is buggy
145 self.tcx.sess.span_err(sp, "could not evaluate float literal (see issue #31407)");
146 // create a dummy value and continue compiling
147 Const::from_bits(self.tcx, 0, self.param_env.and(ty))
149 Err(LitToConstError::Reported) => {
150 // create a dummy value and continue compiling
151 Const::from_bits(self.tcx, 0, self.param_env.and(ty))
156 pub fn pattern_from_hir(&mut self, p: &hir::Pat) -> Pattern<'tcx> {
157 let tcx = self.tcx.global_tcx();
158 let p = match tcx.hir().get(p.hir_id) {
159 Node::Pat(p) | Node::Binding(p) => p,
160 node => bug!("pattern became {:?}", node)
162 Pattern::from_hir(tcx,
163 self.param_env.and(self.identity_substs),
168 pub fn trait_method(&mut self,
172 params: &[Kind<'tcx>])
173 -> (Ty<'tcx>, &'tcx ty::Const<'tcx>) {
174 let substs = self.tcx.mk_substs_trait(self_ty, params);
175 for item in self.tcx.associated_items(trait_def_id) {
176 if item.kind == ty::AssocKind::Method && item.ident.name == method_name {
177 let method_ty = self.tcx.type_of(item.def_id);
178 let method_ty = method_ty.subst(self.tcx, substs);
179 return (method_ty, ty::Const::zero_sized(self.tcx, method_ty));
183 bug!("found no method `{}` in `{:?}`", method_name, trait_def_id);
186 pub fn all_fields(&mut self, adt_def: &ty::AdtDef, variant_index: VariantIdx) -> Vec<Field> {
187 (0..adt_def.variants[variant_index].fields.len())
192 pub fn needs_drop(&mut self, ty: Ty<'tcx>) -> bool {
193 ty.needs_drop(self.tcx.global_tcx(), self.param_env)
196 pub fn tcx(&self) -> TyCtxt<'tcx> {
200 pub fn tables(&self) -> &'a ty::TypeckTables<'tcx> {
204 pub fn check_overflow(&self) -> bool {
208 pub fn type_is_copy_modulo_regions(&self, ty: Ty<'tcx>, span: Span) -> bool {
209 self.infcx.type_is_copy_modulo_regions(self.param_env, ty, span)
213 impl UserAnnotatedTyHelpers<'tcx> for Cx<'_, 'tcx> {
214 fn tcx(&self) -> TyCtxt<'tcx> {
218 fn tables(&self) -> &ty::TypeckTables<'tcx> {