]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs
Auto merge of #104696 - matthiaskrgr:rollup-gi1pdb0, r=matthiaskrgr
[rust.git] / compiler / rustc_hir_typeck / src / fn_ctxt / mod.rs
1 mod _impl;
2 mod arg_matrix;
3 mod checks;
4 mod suggestions;
5
6 pub use _impl::*;
7 use rustc_errors::ErrorGuaranteed;
8 pub use suggestions::*;
9
10 use crate::coercion::DynamicCoerceMany;
11 use crate::{Diverges, EnclosingBreakables, Inherited, UnsafetyState};
12 use rustc_hir as hir;
13 use rustc_hir::def_id::DefId;
14 use rustc_hir_analysis::astconv::AstConv;
15 use rustc_infer::infer;
16 use rustc_infer::infer::error_reporting::TypeErrCtxt;
17 use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
18 use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
19 use rustc_middle::ty::subst::GenericArgKind;
20 use rustc_middle::ty::visit::TypeVisitable;
21 use rustc_middle::ty::{self, Const, Ty, TyCtxt};
22 use rustc_session::Session;
23 use rustc_span::symbol::Ident;
24 use rustc_span::{self, Span};
25 use rustc_trait_selection::traits::{ObligationCause, ObligationCauseCode};
26
27 use std::cell::{Cell, RefCell};
28 use std::ops::Deref;
29
30 /// The `FnCtxt` stores type-checking context needed to type-check bodies of
31 /// functions, closures, and `const`s, including performing type inference
32 /// with [`InferCtxt`].
33 ///
34 /// This is in contrast to [`ItemCtxt`], which is used to type-check item *signatures*
35 /// and thus does not perform type inference.
36 ///
37 /// See [`ItemCtxt`]'s docs for more.
38 ///
39 /// [`ItemCtxt`]: rustc_hir_analysis::collect::ItemCtxt
40 /// [`InferCtxt`]: infer::InferCtxt
41 pub struct FnCtxt<'a, 'tcx> {
42     pub(super) body_id: hir::HirId,
43
44     /// The parameter environment used for proving trait obligations
45     /// in this function. This can change when we descend into
46     /// closures (as they bring new things into scope), hence it is
47     /// not part of `Inherited` (as of the time of this writing,
48     /// closures do not yet change the environment, but they will
49     /// eventually).
50     pub(super) param_env: ty::ParamEnv<'tcx>,
51
52     /// Number of errors that had been reported when we started
53     /// checking this function. On exit, if we find that *more* errors
54     /// have been reported, we will skip regionck and other work that
55     /// expects the types within the function to be consistent.
56     // FIXME(matthewjasper) This should not exist, and it's not correct
57     // if type checking is run in parallel.
58     err_count_on_creation: usize,
59
60     /// If `Some`, this stores coercion information for returned
61     /// expressions. If `None`, this is in a context where return is
62     /// inappropriate, such as a const expression.
63     ///
64     /// This is a `RefCell<DynamicCoerceMany>`, which means that we
65     /// can track all the return expressions and then use them to
66     /// compute a useful coercion from the set, similar to a match
67     /// expression or other branching context. You can use methods
68     /// like `expected_ty` to access the declared return type (if
69     /// any).
70     pub(super) ret_coercion: Option<RefCell<DynamicCoerceMany<'tcx>>>,
71
72     /// First span of a return site that we find. Used in error messages.
73     pub(super) ret_coercion_span: Cell<Option<Span>>,
74
75     pub(super) resume_yield_tys: Option<(Ty<'tcx>, Ty<'tcx>)>,
76
77     pub(super) ps: Cell<UnsafetyState>,
78
79     /// Whether the last checked node generates a divergence (e.g.,
80     /// `return` will set this to `Always`). In general, when entering
81     /// an expression or other node in the tree, the initial value
82     /// indicates whether prior parts of the containing expression may
83     /// have diverged. It is then typically set to `Maybe` (and the
84     /// old value remembered) for processing the subparts of the
85     /// current expression. As each subpart is processed, they may set
86     /// the flag to `Always`, etc. Finally, at the end, we take the
87     /// result and "union" it with the original value, so that when we
88     /// return the flag indicates if any subpart of the parent
89     /// expression (up to and including this part) has diverged. So,
90     /// if you read it after evaluating a subexpression `X`, the value
91     /// you get indicates whether any subexpression that was
92     /// evaluating up to and including `X` diverged.
93     ///
94     /// We currently use this flag only for diagnostic purposes:
95     ///
96     /// - To warn about unreachable code: if, after processing a
97     ///   sub-expression but before we have applied the effects of the
98     ///   current node, we see that the flag is set to `Always`, we
99     ///   can issue a warning. This corresponds to something like
100     ///   `foo(return)`; we warn on the `foo()` expression. (We then
101     ///   update the flag to `WarnedAlways` to suppress duplicate
102     ///   reports.) Similarly, if we traverse to a fresh statement (or
103     ///   tail expression) from an `Always` setting, we will issue a
104     ///   warning. This corresponds to something like `{return;
105     ///   foo();}` or `{return; 22}`, where we would warn on the
106     ///   `foo()` or `22`.
107     ///
108     /// An expression represents dead code if, after checking it,
109     /// the diverges flag is set to something other than `Maybe`.
110     pub(super) diverges: Cell<Diverges>,
111
112     pub(super) enclosing_breakables: RefCell<EnclosingBreakables<'tcx>>,
113
114     pub(super) inh: &'a Inherited<'tcx>,
115
116     pub(super) fallback_has_occurred: Cell<bool>,
117 }
118
119 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
120     pub fn new(
121         inh: &'a Inherited<'tcx>,
122         param_env: ty::ParamEnv<'tcx>,
123         body_id: hir::HirId,
124     ) -> FnCtxt<'a, 'tcx> {
125         FnCtxt {
126             body_id,
127             param_env,
128             err_count_on_creation: inh.tcx.sess.err_count(),
129             ret_coercion: None,
130             ret_coercion_span: Cell::new(None),
131             resume_yield_tys: None,
132             ps: Cell::new(UnsafetyState::function(hir::Unsafety::Normal, hir::CRATE_HIR_ID)),
133             diverges: Cell::new(Diverges::Maybe),
134             enclosing_breakables: RefCell::new(EnclosingBreakables {
135                 stack: Vec::new(),
136                 by_id: Default::default(),
137             }),
138             inh,
139             fallback_has_occurred: Cell::new(false),
140         }
141     }
142
143     pub fn cause(&self, span: Span, code: ObligationCauseCode<'tcx>) -> ObligationCause<'tcx> {
144         ObligationCause::new(span, self.body_id, code)
145     }
146
147     pub fn misc(&self, span: Span) -> ObligationCause<'tcx> {
148         self.cause(span, ObligationCauseCode::MiscObligation)
149     }
150
151     pub fn sess(&self) -> &Session {
152         &self.tcx.sess
153     }
154
155     /// Creates an `TypeErrCtxt` with a reference to the in-progress
156     /// `TypeckResults` which is used for diagnostics.
157     /// Use [`InferCtxt::err_ctxt`] to start one without a `TypeckResults`.
158     ///
159     /// [`InferCtxt::err_ctxt`]: infer::InferCtxt::err_ctxt
160     pub fn err_ctxt(&'a self) -> TypeErrCtxt<'a, 'tcx> {
161         TypeErrCtxt {
162             infcx: &self.infcx,
163             typeck_results: Some(self.typeck_results.borrow()),
164             fallback_has_occurred: self.fallback_has_occurred.get(),
165         }
166     }
167
168     pub fn errors_reported_since_creation(&self) -> bool {
169         self.tcx.sess.err_count() > self.err_count_on_creation
170     }
171 }
172
173 impl<'a, 'tcx> Deref for FnCtxt<'a, 'tcx> {
174     type Target = Inherited<'tcx>;
175     fn deref(&self) -> &Self::Target {
176         &self.inh
177     }
178 }
179
180 impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
181     fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
182         self.tcx
183     }
184
185     fn item_def_id(&self) -> DefId {
186         self.body_id.owner.to_def_id()
187     }
188
189     fn get_type_parameter_bounds(
190         &self,
191         _: Span,
192         def_id: DefId,
193         _: Ident,
194     ) -> ty::GenericPredicates<'tcx> {
195         let tcx = self.tcx;
196         let item_def_id = tcx.hir().ty_param_owner(def_id.expect_local());
197         let generics = tcx.generics_of(item_def_id);
198         let index = generics.param_def_id_to_index[&def_id];
199         ty::GenericPredicates {
200             parent: None,
201             predicates: tcx.arena.alloc_from_iter(
202                 self.param_env.caller_bounds().iter().filter_map(|predicate| {
203                     match predicate.kind().skip_binder() {
204                         ty::PredicateKind::Trait(data) if data.self_ty().is_param(index) => {
205                             // HACK(eddyb) should get the original `Span`.
206                             let span = tcx.def_span(def_id);
207                             Some((predicate, span))
208                         }
209                         _ => None,
210                     }
211                 }),
212             ),
213         }
214     }
215
216     fn re_infer(&self, def: Option<&ty::GenericParamDef>, span: Span) -> Option<ty::Region<'tcx>> {
217         let v = match def {
218             Some(def) => infer::EarlyBoundRegion(span, def.name),
219             None => infer::MiscVariable(span),
220         };
221         Some(self.next_region_var(v))
222     }
223
224     fn allow_ty_infer(&self) -> bool {
225         true
226     }
227
228     fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
229         if let Some(param) = param {
230             if let GenericArgKind::Type(ty) = self.var_for_def(span, param).unpack() {
231                 return ty;
232             }
233             unreachable!()
234         } else {
235             self.next_ty_var(TypeVariableOrigin {
236                 kind: TypeVariableOriginKind::TypeInference,
237                 span,
238             })
239         }
240     }
241
242     fn ct_infer(
243         &self,
244         ty: Ty<'tcx>,
245         param: Option<&ty::GenericParamDef>,
246         span: Span,
247     ) -> Const<'tcx> {
248         if let Some(param) = param {
249             if let GenericArgKind::Const(ct) = self.var_for_def(span, param).unpack() {
250                 return ct;
251             }
252             unreachable!()
253         } else {
254             self.next_const_var(
255                 ty,
256                 ConstVariableOrigin { kind: ConstVariableOriginKind::ConstInference, span },
257             )
258         }
259     }
260
261     fn projected_ty_from_poly_trait_ref(
262         &self,
263         span: Span,
264         item_def_id: DefId,
265         item_segment: &hir::PathSegment<'_>,
266         poly_trait_ref: ty::PolyTraitRef<'tcx>,
267     ) -> Ty<'tcx> {
268         let trait_ref = self.replace_bound_vars_with_fresh_vars(
269             span,
270             infer::LateBoundRegionConversionTime::AssocTypeProjection(item_def_id),
271             poly_trait_ref,
272         );
273
274         let item_substs = <dyn AstConv<'tcx>>::create_substs_for_associated_item(
275             self,
276             span,
277             item_def_id,
278             item_segment,
279             trait_ref.substs,
280         );
281
282         self.tcx().mk_projection(item_def_id, item_substs)
283     }
284
285     fn normalize_ty(&self, span: Span, ty: Ty<'tcx>) -> Ty<'tcx> {
286         if ty.has_escaping_bound_vars() {
287             ty // FIXME: normalization and escaping regions
288         } else {
289             self.normalize_associated_types_in(span, ty)
290         }
291     }
292
293     fn set_tainted_by_errors(&self, e: ErrorGuaranteed) {
294         self.infcx.set_tainted_by_errors(e)
295     }
296
297     fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, _span: Span) {
298         self.write_ty(hir_id, ty)
299     }
300 }