1 //! This module contains the code to convert from the wacky tcx data
2 //! structures into the hair. The `builder` is generally ignorant of
3 //! the tcx etc, and instead goes through the `Cx` for most of its
8 use hair::util::UserAnnotatedTyHelpers;
10 use rustc_data_structures::indexed_vec::Idx;
11 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
13 use rustc::middle::region;
14 use rustc::infer::InferCtxt;
15 use rustc::ty::subst::Subst;
16 use rustc::ty::{self, Ty, TyCtxt};
17 use rustc::ty::subst::{Kind, Substs};
18 use rustc::ty::layout::VariantIdx;
21 use syntax::symbol::Symbol;
23 use rustc_data_structures::sync::Lrc;
24 use hair::constant::{lit_to_const, LitToConstError};
27 pub struct Cx<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
28 tcx: TyCtxt<'a, 'gcx, 'tcx>,
29 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
31 pub root_lint_level: ast::NodeId,
32 pub param_env: ty::ParamEnv<'gcx>,
34 /// Identity `Substs` for use with const-evaluation.
35 pub identity_substs: &'gcx Substs<'gcx>,
37 pub region_scope_tree: Lrc<region::ScopeTree>,
38 pub tables: &'a ty::TypeckTables<'gcx>,
40 /// This is `Constness::Const` if we are compiling a `static`,
41 /// `const`, or the body of a `const fn`.
42 constness: hir::Constness,
44 /// What kind of body is being compiled.
45 pub body_owner_kind: hir::BodyOwnerKind,
47 /// True if this constant/function needs overflow checks.
50 /// See field with the same name on `Mir`
51 control_flow_destroyed: Vec<(Span, String)>,
54 impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
55 pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
56 src_id: ast::NodeId) -> Cx<'a, 'gcx, 'tcx> {
58 let src_def_id = tcx.hir().local_def_id(src_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::Fn => hir::Constness::NotConst,
67 let attrs = tcx.hir().attrs(src_id);
69 // Some functions always have overflow checks enabled,
70 // however, they may not get codegen'd, depending on
71 // the settings for the crate they are codegened in.
72 let mut check_overflow = attr::contains_name(attrs, "rustc_inherit_overflow_checks");
74 // Respect -C overflow-checks.
75 check_overflow |= tcx.sess.overflow_checks();
77 // Constants always need overflow checks.
78 check_overflow |= constness == hir::Constness::Const;
80 let lint_level = lint_level_for_hir_id(tcx, src_id);
84 root_lint_level: lint_level,
85 param_env: tcx.param_env(src_def_id),
86 identity_substs: Substs::identity_for_item(tcx.global_tcx(), src_def_id),
87 region_scope_tree: tcx.region_scope_tree(src_def_id),
88 tables: tcx.typeck_tables_of(src_def_id),
92 control_flow_destroyed: Vec::new(),
96 pub fn control_flow_destroyed(self) -> Vec<(Span, String)> {
97 self.control_flow_destroyed
101 impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
102 /// Normalizes `ast` into the appropriate `mirror` type.
103 pub fn mirror<M: Mirror<'tcx>>(&mut self, ast: M) -> M::Output {
104 ast.make_mirror(self)
107 pub fn usize_ty(&mut self) -> Ty<'tcx> {
111 pub fn usize_literal(&mut self, value: u64) -> &'tcx ty::LazyConst<'tcx> {
112 self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_usize(self.tcx, value)))
115 pub fn bool_ty(&mut self) -> Ty<'tcx> {
119 pub fn unit_ty(&mut self) -> Ty<'tcx> {
123 pub fn true_literal(&mut self) -> &'tcx ty::LazyConst<'tcx> {
124 self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bool(self.tcx, true)))
127 pub fn false_literal(&mut self) -> &'tcx ty::LazyConst<'tcx> {
128 self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bool(self.tcx, false)))
131 pub fn const_eval_literal(
133 lit: &'tcx ast::LitKind,
137 ) -> &'tcx ty::Const<'tcx> {
138 trace!("const_eval_literal: {:#?}, {:?}, {:?}, {:?}", lit, ty, sp, neg);
140 match lit_to_const(lit, self.tcx, ty, neg) {
142 Err(LitToConstError::UnparseableFloat) => {
143 // FIXME(#31407) this is only necessary because float parsing is buggy
144 self.tcx.sess.span_err(sp, "could not evaluate float literal (see issue #31407)");
145 // create a dummy value and continue compiling
146 Const::from_bits(self.tcx, 0, self.param_env.and(ty))
148 Err(LitToConstError::Reported) => {
149 // create a dummy value and continue compiling
150 Const::from_bits(self.tcx, 0, self.param_env.and(ty))
155 pub fn pattern_from_hir(&mut self, p: &hir::Pat) -> Pattern<'tcx> {
156 let tcx = self.tcx.global_tcx();
157 let p = match tcx.hir().get(p.id) {
158 Node::Pat(p) | Node::Binding(p) => p,
159 node => bug!("pattern became {:?}", node)
161 Pattern::from_hir(tcx,
162 self.param_env.and(self.identity_substs),
167 pub fn trait_method(&mut self,
171 params: &[Kind<'tcx>])
172 -> (Ty<'tcx>, &'tcx ty::Const<'tcx>) {
173 let method_name = Symbol::intern(method_name);
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::AssociatedKind::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 let (ty, param_env) = self.tcx.lift_to_global(&(ty, self.param_env)).unwrap_or_else(|| {
194 bug!("MIR: Cx::needs_drop({:?}, {:?}) got \
195 type with inference types/regions",
198 ty.needs_drop(self.tcx.global_tcx(), param_env)
201 fn lint_level_of(&self, node_id: ast::NodeId) -> LintLevel {
202 let hir_id = self.tcx.hir().definitions().node_to_hir_id(node_id);
203 let has_lint_level = self.tcx.dep_graph.with_ignore(|| {
204 self.tcx.lint_levels(LOCAL_CRATE).lint_level_set(hir_id).is_some()
208 LintLevel::Explicit(node_id)
214 pub fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx> {
218 pub fn tables(&self) -> &'a ty::TypeckTables<'gcx> {
222 pub fn check_overflow(&self) -> bool {
226 pub fn type_moves_by_default(&self, ty: Ty<'tcx>, span: Span) -> bool {
227 self.infcx.type_moves_by_default(self.param_env, ty, span)
231 impl UserAnnotatedTyHelpers<'gcx, 'tcx> for Cx<'_, 'gcx, 'tcx> {
232 fn tcx(&self) -> TyCtxt<'_, 'gcx, 'tcx> {
236 fn tables(&self) -> &ty::TypeckTables<'tcx> {
241 fn lint_level_for_hir_id(tcx: TyCtxt, mut id: ast::NodeId) -> ast::NodeId {
242 // Right now we insert a `with_ignore` node in the dep graph here to
243 // ignore the fact that `lint_levels` below depends on the entire crate.
244 // For now this'll prevent false positives of recompiling too much when
247 // Once red/green incremental compilation lands we should be able to
248 // remove this because while the crate changes often the lint level map
249 // will change rarely.
250 tcx.dep_graph.with_ignore(|| {
251 let sets = tcx.lint_levels(LOCAL_CRATE);
253 let hir_id = tcx.hir().definitions().node_to_hir_id(id);
254 if sets.lint_level_set(hir_id).is_some() {
257 let next = tcx.hir().get_parent_node(id);
259 bug!("lint traversal reached the root of the crate");