]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_typeck/src/check/gather_locals.rs
Auto merge of #82127 - tgnottingham:tune-ahead-of-time-codegen, r=varkor
[rust.git] / compiler / rustc_typeck / src / check / gather_locals.rs
1 use crate::check::{FnCtxt, LocalTy, UserType};
2 use rustc_hir as hir;
3 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
4 use rustc_hir::PatKind;
5 use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
6 use rustc_middle::ty::Ty;
7 use rustc_span::Span;
8 use rustc_trait_selection::traits;
9 use std::mem;
10
11 pub(super) struct GatherLocalsVisitor<'a, 'tcx> {
12     fcx: &'a FnCtxt<'a, 'tcx>,
13     parent_id: hir::HirId,
14     // parameters are special cases of patterns, but we want to handle them as
15     // *distinct* cases. so track when we are hitting a pattern *within* an fn
16     // parameter.
17     outermost_fn_param_pat: bool,
18 }
19
20 impl<'a, 'tcx> GatherLocalsVisitor<'a, 'tcx> {
21     pub(super) fn new(fcx: &'a FnCtxt<'a, 'tcx>, parent_id: hir::HirId) -> Self {
22         Self { fcx, parent_id, outermost_fn_param_pat: false }
23     }
24
25     fn assign(&mut self, span: Span, nid: hir::HirId, ty_opt: Option<LocalTy<'tcx>>) -> Ty<'tcx> {
26         match ty_opt {
27             None => {
28                 // Infer the variable's type.
29                 let var_ty = self.fcx.next_ty_var(TypeVariableOrigin {
30                     kind: TypeVariableOriginKind::TypeInference,
31                     span,
32                 });
33                 self.fcx
34                     .locals
35                     .borrow_mut()
36                     .insert(nid, LocalTy { decl_ty: var_ty, revealed_ty: var_ty });
37                 var_ty
38             }
39             Some(typ) => {
40                 // Take type that the user specified.
41                 self.fcx.locals.borrow_mut().insert(nid, typ);
42                 typ.revealed_ty
43             }
44         }
45     }
46 }
47
48 impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
49     type Map = intravisit::ErasedMap<'tcx>;
50
51     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
52         NestedVisitorMap::None
53     }
54
55     // Add explicitly-declared locals.
56     fn visit_local(&mut self, local: &'tcx hir::Local<'tcx>) {
57         let local_ty = match local.ty {
58             Some(ref ty) => {
59                 let o_ty = self.fcx.to_ty(&ty);
60
61                 let revealed_ty = if self.fcx.tcx.features().impl_trait_in_bindings {
62                     self.fcx.instantiate_opaque_types_from_value(self.parent_id, o_ty, ty.span)
63                 } else {
64                     o_ty
65                 };
66
67                 let c_ty =
68                     self.fcx.inh.infcx.canonicalize_user_type_annotation(UserType::Ty(revealed_ty));
69                 debug!(
70                     "visit_local: ty.hir_id={:?} o_ty={:?} revealed_ty={:?} c_ty={:?}",
71                     ty.hir_id, o_ty, revealed_ty, c_ty
72                 );
73                 self.fcx
74                     .typeck_results
75                     .borrow_mut()
76                     .user_provided_types_mut()
77                     .insert(ty.hir_id, c_ty);
78
79                 Some(LocalTy { decl_ty: o_ty, revealed_ty })
80             }
81             None => None,
82         };
83         self.assign(local.span, local.hir_id, local_ty);
84
85         debug!(
86             "local variable {:?} is assigned type {}",
87             local.pat,
88             self.fcx.ty_to_string(&*self.fcx.locals.borrow().get(&local.hir_id).unwrap().decl_ty)
89         );
90         intravisit::walk_local(self, local);
91     }
92
93     fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
94         let old_outermost_fn_param_pat = mem::replace(&mut self.outermost_fn_param_pat, true);
95         intravisit::walk_param(self, param);
96         self.outermost_fn_param_pat = old_outermost_fn_param_pat;
97     }
98
99     // Add pattern bindings.
100     fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) {
101         if let PatKind::Binding(_, _, ident, _) = p.kind {
102             let var_ty = self.assign(p.span, p.hir_id, None);
103
104             if self.outermost_fn_param_pat {
105                 if !self.fcx.tcx.features().unsized_fn_params {
106                     self.fcx.require_type_is_sized(
107                         var_ty,
108                         p.span,
109                         traits::SizedArgumentType(Some(p.span)),
110                     );
111                 }
112             } else {
113                 if !self.fcx.tcx.features().unsized_locals {
114                     self.fcx.require_type_is_sized(var_ty, p.span, traits::VariableType(p.hir_id));
115                 }
116             }
117
118             debug!(
119                 "pattern binding {} is assigned to {} with type {:?}",
120                 ident,
121                 self.fcx.ty_to_string(&*self.fcx.locals.borrow().get(&p.hir_id).unwrap().decl_ty),
122                 var_ty
123             );
124         }
125         let old_outermost_fn_param_pat = mem::replace(&mut self.outermost_fn_param_pat, false);
126         intravisit::walk_pat(self, p);
127         self.outermost_fn_param_pat = old_outermost_fn_param_pat;
128     }
129
130     // Don't descend into the bodies of nested closures.
131     fn visit_fn(
132         &mut self,
133         _: intravisit::FnKind<'tcx>,
134         _: &'tcx hir::FnDecl<'tcx>,
135         _: hir::BodyId,
136         _: Span,
137         _: hir::HirId,
138     ) {
139     }
140 }