]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Rollup merge of #104965 - zacklukem:p-option-as_ref-docs, r=scottmcm
[rust.git] / compiler / rustc_hir_typeck / src / fn_ctxt / _impl.rs
1 use crate::callee::{self, DeferredCallResolution};
2 use crate::method::{self, MethodCallee, SelfSource};
3 use crate::rvalue_scopes;
4 use crate::{BreakableCtxt, Diverges, Expectation, FnCtxt, LocalTy, RawTy};
5 use rustc_data_structures::captures::Captures;
6 use rustc_data_structures::fx::FxHashSet;
7 use rustc_errors::{Applicability, Diagnostic, ErrorGuaranteed, MultiSpan};
8 use rustc_hir as hir;
9 use rustc_hir::def::{CtorOf, DefKind, Res};
10 use rustc_hir::def_id::DefId;
11 use rustc_hir::lang_items::LangItem;
12 use rustc_hir::{ExprKind, GenericArg, Node, QPath};
13 use rustc_hir_analysis::astconv::generics::{
14     check_generic_arg_count_for_call, create_substs_for_generic_args,
15 };
16 use rustc_hir_analysis::astconv::{
17     AstConv, CreateSubstsForGenericArgsCtxt, ExplicitLateBound, GenericArgCountMismatch,
18     GenericArgCountResult, IsMethodCall, PathSeg,
19 };
20 use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryResponse};
21 use rustc_infer::infer::error_reporting::TypeAnnotationNeeded::E0282;
22 use rustc_infer::infer::InferResult;
23 use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
24 use rustc_middle::ty::error::TypeError;
25 use rustc_middle::ty::fold::TypeFoldable;
26 use rustc_middle::ty::visit::TypeVisitable;
27 use rustc_middle::ty::{
28     self, AdtKind, CanonicalUserType, DefIdTree, GenericParamDefKind, Ty, UserType,
29 };
30 use rustc_middle::ty::{GenericArgKind, SubstsRef, UserSelfTy, UserSubsts};
31 use rustc_session::lint;
32 use rustc_span::def_id::LocalDefId;
33 use rustc_span::hygiene::DesugaringKind;
34 use rustc_span::symbol::{kw, sym, Ident};
35 use rustc_span::{Span, DUMMY_SP};
36 use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
37 use rustc_trait_selection::traits::{self, NormalizeExt, ObligationCauseCode, ObligationCtxt};
38
39 use std::collections::hash_map::Entry;
40 use std::slice;
41
42 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
43     /// Produces warning on the given node, if the current point in the
44     /// function is unreachable, and there hasn't been another warning.
45     pub(in super::super) fn warn_if_unreachable(&self, id: hir::HirId, span: Span, kind: &str) {
46         // FIXME: Combine these two 'if' expressions into one once
47         // let chains are implemented
48         if let Diverges::Always { span: orig_span, custom_note } = self.diverges.get() {
49             // If span arose from a desugaring of `if` or `while`, then it is the condition itself,
50             // which diverges, that we are about to lint on. This gives suboptimal diagnostics.
51             // Instead, stop here so that the `if`- or `while`-expression's block is linted instead.
52             if !span.is_desugaring(DesugaringKind::CondTemporary)
53                 && !span.is_desugaring(DesugaringKind::Async)
54                 && !orig_span.is_desugaring(DesugaringKind::Await)
55             {
56                 self.diverges.set(Diverges::WarnedAlways);
57
58                 debug!("warn_if_unreachable: id={:?} span={:?} kind={}", id, span, kind);
59
60                 let msg = format!("unreachable {}", kind);
61                 self.tcx().struct_span_lint_hir(
62                     lint::builtin::UNREACHABLE_CODE,
63                     id,
64                     span,
65                     &msg,
66                     |lint| {
67                         lint.span_label(span, &msg).span_label(
68                             orig_span,
69                             custom_note
70                                 .unwrap_or("any code following this expression is unreachable"),
71                         )
72                     },
73                 )
74             }
75         }
76     }
77
78     /// Resolves type and const variables in `ty` if possible. Unlike the infcx
79     /// version (resolve_vars_if_possible), this version will
80     /// also select obligations if it seems useful, in an effort
81     /// to get more type information.
82     pub(in super::super) fn resolve_vars_with_obligations(&self, ty: Ty<'tcx>) -> Ty<'tcx> {
83         self.resolve_vars_with_obligations_and_mutate_fulfillment(ty, |_| {})
84     }
85
86     #[instrument(skip(self, mutate_fulfillment_errors), level = "debug", ret)]
87     pub(in super::super) fn resolve_vars_with_obligations_and_mutate_fulfillment(
88         &self,
89         mut ty: Ty<'tcx>,
90         mutate_fulfillment_errors: impl Fn(&mut Vec<traits::FulfillmentError<'tcx>>),
91     ) -> Ty<'tcx> {
92         // No Infer()? Nothing needs doing.
93         if !ty.has_non_region_infer() {
94             debug!("no inference var, nothing needs doing");
95             return ty;
96         }
97
98         // If `ty` is a type variable, see whether we already know what it is.
99         ty = self.resolve_vars_if_possible(ty);
100         if !ty.has_non_region_infer() {
101             debug!(?ty);
102             return ty;
103         }
104
105         // If not, try resolving pending obligations as much as
106         // possible. This can help substantially when there are
107         // indirect dependencies that don't seem worth tracking
108         // precisely.
109         self.select_obligations_where_possible(mutate_fulfillment_errors);
110         self.resolve_vars_if_possible(ty)
111     }
112
113     pub(in super::super) fn record_deferred_call_resolution(
114         &self,
115         closure_def_id: LocalDefId,
116         r: DeferredCallResolution<'tcx>,
117     ) {
118         let mut deferred_call_resolutions = self.deferred_call_resolutions.borrow_mut();
119         deferred_call_resolutions.entry(closure_def_id).or_default().push(r);
120     }
121
122     pub(in super::super) fn remove_deferred_call_resolutions(
123         &self,
124         closure_def_id: LocalDefId,
125     ) -> Vec<DeferredCallResolution<'tcx>> {
126         let mut deferred_call_resolutions = self.deferred_call_resolutions.borrow_mut();
127         deferred_call_resolutions.remove(&closure_def_id).unwrap_or_default()
128     }
129
130     pub fn tag(&self) -> String {
131         format!("{:p}", self)
132     }
133
134     pub fn local_ty(&self, span: Span, nid: hir::HirId) -> LocalTy<'tcx> {
135         self.locals.borrow().get(&nid).cloned().unwrap_or_else(|| {
136             span_bug!(span, "no type for local variable {}", self.tcx.hir().node_to_string(nid))
137         })
138     }
139
140     #[inline]
141     pub fn write_ty(&self, id: hir::HirId, ty: Ty<'tcx>) {
142         debug!("write_ty({:?}, {:?}) in fcx {}", id, self.resolve_vars_if_possible(ty), self.tag());
143         self.typeck_results.borrow_mut().node_types_mut().insert(id, ty);
144
145         if let Err(e) = ty.error_reported() {
146             self.set_tainted_by_errors(e);
147         }
148     }
149
150     pub fn write_field_index(&self, hir_id: hir::HirId, index: usize) {
151         self.typeck_results.borrow_mut().field_indices_mut().insert(hir_id, index);
152     }
153
154     #[instrument(level = "debug", skip(self))]
155     pub(in super::super) fn write_resolution(
156         &self,
157         hir_id: hir::HirId,
158         r: Result<(DefKind, DefId), ErrorGuaranteed>,
159     ) {
160         self.typeck_results.borrow_mut().type_dependent_defs_mut().insert(hir_id, r);
161     }
162
163     #[instrument(level = "debug", skip(self))]
164     pub fn write_method_call(&self, hir_id: hir::HirId, method: MethodCallee<'tcx>) {
165         self.write_resolution(hir_id, Ok((DefKind::AssocFn, method.def_id)));
166         self.write_substs(hir_id, method.substs);
167     }
168
169     pub fn write_substs(&self, node_id: hir::HirId, substs: SubstsRef<'tcx>) {
170         if !substs.is_empty() {
171             debug!("write_substs({:?}, {:?}) in fcx {}", node_id, substs, self.tag());
172
173             self.typeck_results.borrow_mut().node_substs_mut().insert(node_id, substs);
174         }
175     }
176
177     /// Given the substs that we just converted from the HIR, try to
178     /// canonicalize them and store them as user-given substitutions
179     /// (i.e., substitutions that must be respected by the NLL check).
180     ///
181     /// This should be invoked **before any unifications have
182     /// occurred**, so that annotations like `Vec<_>` are preserved
183     /// properly.
184     #[instrument(skip(self), level = "debug")]
185     pub fn write_user_type_annotation_from_substs(
186         &self,
187         hir_id: hir::HirId,
188         def_id: DefId,
189         substs: SubstsRef<'tcx>,
190         user_self_ty: Option<UserSelfTy<'tcx>>,
191     ) {
192         debug!("fcx {}", self.tag());
193
194         if Self::can_contain_user_lifetime_bounds((substs, user_self_ty)) {
195             let canonicalized = self.canonicalize_user_type_annotation(UserType::TypeOf(
196                 def_id,
197                 UserSubsts { substs, user_self_ty },
198             ));
199             debug!(?canonicalized);
200             self.write_user_type_annotation(hir_id, canonicalized);
201         }
202     }
203
204     #[instrument(skip(self), level = "debug")]
205     pub fn write_user_type_annotation(
206         &self,
207         hir_id: hir::HirId,
208         canonical_user_type_annotation: CanonicalUserType<'tcx>,
209     ) {
210         debug!("fcx {}", self.tag());
211
212         if !canonical_user_type_annotation.is_identity() {
213             self.typeck_results
214                 .borrow_mut()
215                 .user_provided_types_mut()
216                 .insert(hir_id, canonical_user_type_annotation);
217         } else {
218             debug!("skipping identity substs");
219         }
220     }
221
222     #[instrument(skip(self, expr), level = "debug")]
223     pub fn apply_adjustments(&self, expr: &hir::Expr<'_>, adj: Vec<Adjustment<'tcx>>) {
224         debug!("expr = {:#?}", expr);
225
226         if adj.is_empty() {
227             return;
228         }
229
230         for a in &adj {
231             if let Adjust::NeverToAny = a.kind {
232                 if a.target.is_ty_var() {
233                     self.diverging_type_vars.borrow_mut().insert(a.target);
234                     debug!("apply_adjustments: adding `{:?}` as diverging type var", a.target);
235                 }
236             }
237         }
238
239         let autoborrow_mut = adj.iter().any(|adj| {
240             matches!(
241                 adj,
242                 &Adjustment {
243                     kind: Adjust::Borrow(AutoBorrow::Ref(_, AutoBorrowMutability::Mut { .. })),
244                     ..
245                 }
246             )
247         });
248
249         match self.typeck_results.borrow_mut().adjustments_mut().entry(expr.hir_id) {
250             Entry::Vacant(entry) => {
251                 entry.insert(adj);
252             }
253             Entry::Occupied(mut entry) => {
254                 debug!(" - composing on top of {:?}", entry.get());
255                 match (&entry.get()[..], &adj[..]) {
256                     // Applying any adjustment on top of a NeverToAny
257                     // is a valid NeverToAny adjustment, because it can't
258                     // be reached.
259                     (&[Adjustment { kind: Adjust::NeverToAny, .. }], _) => return,
260                     (
261                         &[
262                             Adjustment { kind: Adjust::Deref(_), .. },
263                             Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(..)), .. },
264                         ],
265                         &[
266                             Adjustment { kind: Adjust::Deref(_), .. },
267                             .., // Any following adjustments are allowed.
268                         ],
269                     ) => {
270                         // A reborrow has no effect before a dereference.
271                     }
272                     // FIXME: currently we never try to compose autoderefs
273                     // and ReifyFnPointer/UnsafeFnPointer, but we could.
274                     _ => {
275                         self.tcx.sess.delay_span_bug(
276                             expr.span,
277                             &format!(
278                                 "while adjusting {:?}, can't compose {:?} and {:?}",
279                                 expr,
280                                 entry.get(),
281                                 adj
282                             ),
283                         );
284                     }
285                 }
286                 *entry.get_mut() = adj;
287             }
288         }
289
290         // If there is an mutable auto-borrow, it is equivalent to `&mut <expr>`.
291         // In this case implicit use of `Deref` and `Index` within `<expr>` should
292         // instead be `DerefMut` and `IndexMut`, so fix those up.
293         if autoborrow_mut {
294             self.convert_place_derefs_to_mutable(expr);
295         }
296     }
297
298     /// Instantiates and normalizes the bounds for a given item
299     pub(in super::super) fn instantiate_bounds(
300         &self,
301         span: Span,
302         def_id: DefId,
303         substs: SubstsRef<'tcx>,
304     ) -> (ty::InstantiatedPredicates<'tcx>, Vec<Span>) {
305         let bounds = self.tcx.predicates_of(def_id);
306         let spans: Vec<Span> = bounds.predicates.iter().map(|(_, span)| *span).collect();
307         let result = bounds.instantiate(self.tcx, substs);
308         let result = self.normalize(span, result);
309         debug!(
310             "instantiate_bounds(bounds={:?}, substs={:?}) = {:?}, {:?}",
311             bounds, substs, result, spans,
312         );
313         (result, spans)
314     }
315
316     pub(in super::super) fn normalize<T>(&self, span: Span, value: T) -> T
317     where
318         T: TypeFoldable<'tcx>,
319     {
320         self.register_infer_ok_obligations(
321             self.at(&self.misc(span), self.param_env).normalize(value),
322         )
323     }
324
325     pub fn require_type_meets(
326         &self,
327         ty: Ty<'tcx>,
328         span: Span,
329         code: traits::ObligationCauseCode<'tcx>,
330         def_id: DefId,
331     ) {
332         self.register_bound(ty, def_id, traits::ObligationCause::new(span, self.body_id, code));
333     }
334
335     pub fn require_type_is_sized(
336         &self,
337         ty: Ty<'tcx>,
338         span: Span,
339         code: traits::ObligationCauseCode<'tcx>,
340     ) {
341         if !ty.references_error() {
342             let lang_item = self.tcx.require_lang_item(LangItem::Sized, None);
343             self.require_type_meets(ty, span, code, lang_item);
344         }
345     }
346
347     pub fn require_type_is_sized_deferred(
348         &self,
349         ty: Ty<'tcx>,
350         span: Span,
351         code: traits::ObligationCauseCode<'tcx>,
352     ) {
353         if !ty.references_error() {
354             self.deferred_sized_obligations.borrow_mut().push((ty, span, code));
355         }
356     }
357
358     pub fn register_bound(
359         &self,
360         ty: Ty<'tcx>,
361         def_id: DefId,
362         cause: traits::ObligationCause<'tcx>,
363     ) {
364         if !ty.references_error() {
365             self.fulfillment_cx.borrow_mut().register_bound(
366                 self,
367                 self.param_env,
368                 ty,
369                 def_id,
370                 cause,
371             );
372         }
373     }
374
375     pub fn handle_raw_ty(&self, span: Span, ty: Ty<'tcx>) -> RawTy<'tcx> {
376         RawTy { raw: ty, normalized: self.normalize(span, ty) }
377     }
378
379     pub fn to_ty(&self, ast_t: &hir::Ty<'_>) -> RawTy<'tcx> {
380         let t = self.astconv().ast_ty_to_ty(ast_t);
381         self.register_wf_obligation(t.into(), ast_t.span, traits::WellFormed(None));
382         self.handle_raw_ty(ast_t.span, t)
383     }
384
385     pub fn to_ty_saving_user_provided_ty(&self, ast_ty: &hir::Ty<'_>) -> Ty<'tcx> {
386         let ty = self.to_ty(ast_ty);
387         debug!("to_ty_saving_user_provided_ty: ty={:?}", ty);
388
389         if Self::can_contain_user_lifetime_bounds(ty.raw) {
390             let c_ty = self.canonicalize_response(UserType::Ty(ty.raw));
391             debug!("to_ty_saving_user_provided_ty: c_ty={:?}", c_ty);
392             self.typeck_results.borrow_mut().user_provided_types_mut().insert(ast_ty.hir_id, c_ty);
393         }
394
395         ty.normalized
396     }
397
398     pub(super) fn user_substs_for_adt(ty: RawTy<'tcx>) -> UserSubsts<'tcx> {
399         match (ty.raw.kind(), ty.normalized.kind()) {
400             (ty::Adt(_, substs), _) => UserSubsts { substs, user_self_ty: None },
401             (_, ty::Adt(adt, substs)) => UserSubsts {
402                 substs,
403                 user_self_ty: Some(UserSelfTy { impl_def_id: adt.did(), self_ty: ty.raw }),
404             },
405             _ => bug!("non-adt type {:?}", ty),
406         }
407     }
408
409     pub fn array_length_to_const(&self, length: &hir::ArrayLen) -> ty::Const<'tcx> {
410         match length {
411             &hir::ArrayLen::Infer(_, span) => self.ct_infer(self.tcx.types.usize, None, span),
412             hir::ArrayLen::Body(anon_const) => {
413                 let span = self.tcx.def_span(anon_const.def_id);
414                 let c = ty::Const::from_anon_const(self.tcx, anon_const.def_id);
415                 self.register_wf_obligation(c.into(), span, ObligationCauseCode::WellFormed(None));
416                 self.normalize(span, c)
417             }
418         }
419     }
420
421     pub fn const_arg_to_const(
422         &self,
423         ast_c: &hir::AnonConst,
424         param_def_id: DefId,
425     ) -> ty::Const<'tcx> {
426         let const_def =
427             ty::WithOptConstParam { did: ast_c.def_id, const_param_did: Some(param_def_id) };
428         let c = ty::Const::from_opt_const_arg_anon_const(self.tcx, const_def);
429         self.register_wf_obligation(
430             c.into(),
431             self.tcx.hir().span(ast_c.hir_id),
432             ObligationCauseCode::WellFormed(None),
433         );
434         c
435     }
436
437     // If the type given by the user has free regions, save it for later, since
438     // NLL would like to enforce those. Also pass in types that involve
439     // projections, since those can resolve to `'static` bounds (modulo #54940,
440     // which hopefully will be fixed by the time you see this comment, dear
441     // reader, although I have my doubts). Also pass in types with inference
442     // types, because they may be repeated. Other sorts of things are already
443     // sufficiently enforced with erased regions. =)
444     fn can_contain_user_lifetime_bounds<T>(t: T) -> bool
445     where
446         T: TypeVisitable<'tcx>,
447     {
448         t.has_free_regions() || t.has_projections() || t.has_infer_types()
449     }
450
451     pub fn node_ty(&self, id: hir::HirId) -> Ty<'tcx> {
452         match self.typeck_results.borrow().node_types().get(id) {
453             Some(&t) => t,
454             None if let Some(e) = self.tainted_by_errors() => self.tcx.ty_error_with_guaranteed(e),
455             None => {
456                 bug!(
457                     "no type for node {}: {} in fcx {}",
458                     id,
459                     self.tcx.hir().node_to_string(id),
460                     self.tag()
461                 );
462             }
463         }
464     }
465
466     pub fn node_ty_opt(&self, id: hir::HirId) -> Option<Ty<'tcx>> {
467         match self.typeck_results.borrow().node_types().get(id) {
468             Some(&t) => Some(t),
469             None if let Some(e) = self.tainted_by_errors() => Some(self.tcx.ty_error_with_guaranteed(e)),
470             None => None,
471         }
472     }
473
474     /// Registers an obligation for checking later, during regionck, that `arg` is well-formed.
475     pub fn register_wf_obligation(
476         &self,
477         arg: ty::GenericArg<'tcx>,
478         span: Span,
479         code: traits::ObligationCauseCode<'tcx>,
480     ) {
481         // WF obligations never themselves fail, so no real need to give a detailed cause:
482         let cause = traits::ObligationCause::new(span, self.body_id, code);
483         self.register_predicate(traits::Obligation::new(
484             self.tcx,
485             cause,
486             self.param_env,
487             ty::Binder::dummy(ty::PredicateKind::WellFormed(arg)),
488         ));
489     }
490
491     /// Registers obligations that all `substs` are well-formed.
492     pub fn add_wf_bounds(&self, substs: SubstsRef<'tcx>, expr: &hir::Expr<'_>) {
493         for arg in substs.iter().filter(|arg| {
494             matches!(arg.unpack(), GenericArgKind::Type(..) | GenericArgKind::Const(..))
495         }) {
496             self.register_wf_obligation(arg, expr.span, traits::WellFormed(None));
497         }
498     }
499
500     // FIXME(arielb1): use this instead of field.ty everywhere
501     // Only for fields! Returns <none> for methods>
502     // Indifferent to privacy flags
503     pub fn field_ty(
504         &self,
505         span: Span,
506         field: &'tcx ty::FieldDef,
507         substs: SubstsRef<'tcx>,
508     ) -> Ty<'tcx> {
509         self.normalize(span, field.ty(self.tcx, substs))
510     }
511
512     pub(in super::super) fn resolve_rvalue_scopes(&self, def_id: DefId) {
513         let scope_tree = self.tcx.region_scope_tree(def_id);
514         let rvalue_scopes = { rvalue_scopes::resolve_rvalue_scopes(self, &scope_tree, def_id) };
515         let mut typeck_results = self.inh.typeck_results.borrow_mut();
516         typeck_results.rvalue_scopes = rvalue_scopes;
517     }
518
519     pub(in super::super) fn resolve_generator_interiors(&self, def_id: DefId) {
520         let mut generators = self.deferred_generator_interiors.borrow_mut();
521         for (body_id, interior, kind) in generators.drain(..) {
522             self.select_obligations_where_possible(|_| {});
523             crate::generator_interior::resolve_interior(self, def_id, body_id, interior, kind);
524         }
525     }
526
527     #[instrument(skip(self), level = "debug")]
528     pub(in super::super) fn select_all_obligations_or_error(&self) {
529         let mut errors = self.fulfillment_cx.borrow_mut().select_all_or_error(&self);
530
531         if !errors.is_empty() {
532             self.adjust_fulfillment_errors_for_expr_obligation(&mut errors);
533             self.err_ctxt().report_fulfillment_errors(&errors, self.inh.body_id);
534         }
535     }
536
537     /// Select as many obligations as we can at present.
538     pub(in super::super) fn select_obligations_where_possible(
539         &self,
540         mutate_fulfillment_errors: impl Fn(&mut Vec<traits::FulfillmentError<'tcx>>),
541     ) {
542         let mut result = self.fulfillment_cx.borrow_mut().select_where_possible(self);
543         if !result.is_empty() {
544             mutate_fulfillment_errors(&mut result);
545             self.adjust_fulfillment_errors_for_expr_obligation(&mut result);
546             self.err_ctxt().report_fulfillment_errors(&result, self.inh.body_id);
547         }
548     }
549
550     /// For the overloaded place expressions (`*x`, `x[3]`), the trait
551     /// returns a type of `&T`, but the actual type we assign to the
552     /// *expression* is `T`. So this function just peels off the return
553     /// type by one layer to yield `T`.
554     pub(in super::super) fn make_overloaded_place_return_type(
555         &self,
556         method: MethodCallee<'tcx>,
557     ) -> ty::TypeAndMut<'tcx> {
558         // extract method return type, which will be &T;
559         let ret_ty = method.sig.output();
560
561         // method returns &T, but the type as visible to user is T, so deref
562         ret_ty.builtin_deref(true).unwrap()
563     }
564
565     #[instrument(skip(self), level = "debug")]
566     fn self_type_matches_expected_vid(&self, self_ty: Ty<'tcx>, expected_vid: ty::TyVid) -> bool {
567         let self_ty = self.shallow_resolve(self_ty);
568         debug!(?self_ty);
569
570         match *self_ty.kind() {
571             ty::Infer(ty::TyVar(found_vid)) => {
572                 // FIXME: consider using `sub_root_var` here so we
573                 // can see through subtyping.
574                 let found_vid = self.root_var(found_vid);
575                 debug!("self_type_matches_expected_vid - found_vid={:?}", found_vid);
576                 expected_vid == found_vid
577             }
578             _ => false,
579         }
580     }
581
582     #[instrument(skip(self), level = "debug")]
583     pub(in super::super) fn obligations_for_self_ty<'b>(
584         &'b self,
585         self_ty: ty::TyVid,
586     ) -> impl DoubleEndedIterator<Item = traits::PredicateObligation<'tcx>> + Captures<'tcx> + 'b
587     {
588         // FIXME: consider using `sub_root_var` here so we
589         // can see through subtyping.
590         let ty_var_root = self.root_var(self_ty);
591         trace!("pending_obligations = {:#?}", self.fulfillment_cx.borrow().pending_obligations());
592
593         self.fulfillment_cx.borrow().pending_obligations().into_iter().filter_map(
594             move |obligation| match &obligation.predicate.kind().skip_binder() {
595                 ty::PredicateKind::Clause(ty::Clause::Projection(data))
596                     if self.self_type_matches_expected_vid(
597                         data.projection_ty.self_ty(),
598                         ty_var_root,
599                     ) =>
600                 {
601                     Some(obligation)
602                 }
603                 ty::PredicateKind::Clause(ty::Clause::Trait(data))
604                     if self.self_type_matches_expected_vid(data.self_ty(), ty_var_root) =>
605                 {
606                     Some(obligation)
607                 }
608
609                 ty::PredicateKind::Clause(ty::Clause::Trait(..))
610                 | ty::PredicateKind::Clause(ty::Clause::Projection(..))
611                 | ty::PredicateKind::Subtype(..)
612                 | ty::PredicateKind::Coerce(..)
613                 | ty::PredicateKind::Clause(ty::Clause::RegionOutlives(..))
614                 | ty::PredicateKind::Clause(ty::Clause::TypeOutlives(..))
615                 | ty::PredicateKind::WellFormed(..)
616                 | ty::PredicateKind::ObjectSafe(..)
617                 | ty::PredicateKind::ConstEvaluatable(..)
618                 | ty::PredicateKind::ConstEquate(..)
619                 // N.B., this predicate is created by breaking down a
620                 // `ClosureType: FnFoo()` predicate, where
621                 // `ClosureType` represents some `Closure`. It can't
622                 // possibly be referring to the current closure,
623                 // because we haven't produced the `Closure` for
624                 // this closure yet; this is exactly why the other
625                 // code is looking for a self type of an unresolved
626                 // inference variable.
627                 | ty::PredicateKind::ClosureKind(..)
628                 | ty::PredicateKind::Ambiguous
629                 | ty::PredicateKind::TypeWellFormedFromEnv(..) => None,
630             },
631         )
632     }
633
634     pub(in super::super) fn type_var_is_sized(&self, self_ty: ty::TyVid) -> bool {
635         let sized_did = self.tcx.lang_items().sized_trait();
636         self.obligations_for_self_ty(self_ty).any(|obligation| {
637             match obligation.predicate.kind().skip_binder() {
638                 ty::PredicateKind::Clause(ty::Clause::Trait(data)) => {
639                     Some(data.def_id()) == sized_did
640                 }
641                 _ => false,
642             }
643         })
644     }
645
646     pub(in super::super) fn err_args(&self, len: usize) -> Vec<Ty<'tcx>> {
647         vec![self.tcx.ty_error(); len]
648     }
649
650     /// Unifies the output type with the expected type early, for more coercions
651     /// and forward type information on the input expressions.
652     #[instrument(skip(self, call_span), level = "debug")]
653     pub(in super::super) fn expected_inputs_for_expected_output(
654         &self,
655         call_span: Span,
656         expected_ret: Expectation<'tcx>,
657         formal_ret: Ty<'tcx>,
658         formal_args: &[Ty<'tcx>],
659     ) -> Option<Vec<Ty<'tcx>>> {
660         let formal_ret = self.resolve_vars_with_obligations(formal_ret);
661         let ret_ty = expected_ret.only_has_type(self)?;
662
663         // HACK(oli-obk): This is a hack to keep RPIT and TAIT in sync wrt their behaviour.
664         // Without it, the inference
665         // variable will get instantiated with the opaque type. The inference variable often
666         // has various helpful obligations registered for it that help closures figure out their
667         // signature. If we infer the inference var to the opaque type, the closure won't be able
668         // to find those obligations anymore, and it can't necessarily find them from the opaque
669         // type itself. We could be more powerful with inference if we *combined* the obligations
670         // so that we got both the obligations from the opaque type and the ones from the inference
671         // variable. That will accept more code than we do right now, so we need to carefully consider
672         // the implications.
673         // Note: this check is pessimistic, as the inference type could be matched with something other
674         // than the opaque type, but then we need a new `TypeRelation` just for this specific case and
675         // can't re-use `sup` below.
676         // See tests/ui/impl-trait/hidden-type-is-opaque.rs and
677         // tests/ui/impl-trait/hidden-type-is-opaque-2.rs for examples that hit this path.
678         if formal_ret.has_infer_types() {
679             for ty in ret_ty.walk() {
680                 if let ty::subst::GenericArgKind::Type(ty) = ty.unpack()
681                     && let ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) = *ty.kind()
682                     && let Some(def_id) = def_id.as_local()
683                     && self.opaque_type_origin(def_id, DUMMY_SP).is_some() {
684                     return None;
685                 }
686             }
687         }
688
689         let expect_args = self
690             .fudge_inference_if_ok(|| {
691                 let ocx = ObligationCtxt::new_in_snapshot(self);
692
693                 // Attempt to apply a subtyping relationship between the formal
694                 // return type (likely containing type variables if the function
695                 // is polymorphic) and the expected return type.
696                 // No argument expectations are produced if unification fails.
697                 let origin = self.misc(call_span);
698                 ocx.sup(&origin, self.param_env, ret_ty, formal_ret)?;
699                 if !ocx.select_where_possible().is_empty() {
700                     return Err(TypeError::Mismatch);
701                 }
702
703                 // Record all the argument types, with the substitutions
704                 // produced from the above subtyping unification.
705                 Ok(Some(formal_args.iter().map(|&ty| self.resolve_vars_if_possible(ty)).collect()))
706             })
707             .unwrap_or_default();
708         debug!(?formal_args, ?formal_ret, ?expect_args, ?expected_ret);
709         expect_args
710     }
711
712     pub(in super::super) fn resolve_lang_item_path(
713         &self,
714         lang_item: hir::LangItem,
715         span: Span,
716         hir_id: hir::HirId,
717         expr_hir_id: Option<hir::HirId>,
718     ) -> (Res, Ty<'tcx>) {
719         let def_id = self.tcx.require_lang_item(lang_item, Some(span));
720         let def_kind = self.tcx.def_kind(def_id);
721
722         let item_ty = if let DefKind::Variant = def_kind {
723             self.tcx.bound_type_of(self.tcx.parent(def_id))
724         } else {
725             self.tcx.bound_type_of(def_id)
726         };
727         let substs = self.fresh_substs_for_item(span, def_id);
728         let ty = item_ty.subst(self.tcx, substs);
729
730         self.write_resolution(hir_id, Ok((def_kind, def_id)));
731
732         let code = match lang_item {
733             hir::LangItem::IntoFutureIntoFuture => {
734                 Some(ObligationCauseCode::AwaitableExpr(expr_hir_id))
735             }
736             hir::LangItem::IteratorNext | hir::LangItem::IntoIterIntoIter => {
737                 Some(ObligationCauseCode::ForLoopIterator)
738             }
739             hir::LangItem::TryTraitFromOutput
740             | hir::LangItem::TryTraitFromResidual
741             | hir::LangItem::TryTraitBranch => Some(ObligationCauseCode::QuestionMark),
742             _ => None,
743         };
744         if let Some(code) = code {
745             self.add_required_obligations_with_code(span, def_id, substs, move |_, _| code.clone());
746         } else {
747             self.add_required_obligations_for_hir(span, def_id, substs, hir_id);
748         }
749
750         (Res::Def(def_kind, def_id), ty)
751     }
752
753     /// Resolves an associated value path into a base type and associated constant, or method
754     /// resolution. The newly resolved definition is written into `type_dependent_defs`.
755     pub fn resolve_ty_and_res_fully_qualified_call(
756         &self,
757         qpath: &'tcx QPath<'tcx>,
758         hir_id: hir::HirId,
759         span: Span,
760     ) -> (Res, Option<RawTy<'tcx>>, &'tcx [hir::PathSegment<'tcx>]) {
761         debug!(
762             "resolve_ty_and_res_fully_qualified_call: qpath={:?} hir_id={:?} span={:?}",
763             qpath, hir_id, span
764         );
765         let (ty, qself, item_segment) = match *qpath {
766             QPath::Resolved(ref opt_qself, ref path) => {
767                 return (
768                     path.res,
769                     opt_qself.as_ref().map(|qself| self.to_ty(qself)),
770                     path.segments,
771                 );
772             }
773             QPath::TypeRelative(ref qself, ref segment) => {
774                 // Don't use `self.to_ty`, since this will register a WF obligation.
775                 // If we're trying to call a non-existent method on a trait
776                 // (e.g. `MyTrait::missing_method`), then resolution will
777                 // give us a `QPath::TypeRelative` with a trait object as
778                 // `qself`. In that case, we want to avoid registering a WF obligation
779                 // for `dyn MyTrait`, since we don't actually need the trait
780                 // to be object-safe.
781                 // We manually call `register_wf_obligation` in the success path
782                 // below.
783                 let ty = self.astconv().ast_ty_to_ty_in_path(qself);
784                 (self.handle_raw_ty(span, ty), qself, segment)
785             }
786             QPath::LangItem(..) => {
787                 bug!("`resolve_ty_and_res_fully_qualified_call` called on `LangItem`")
788             }
789         };
790         if let Some(&cached_result) = self.typeck_results.borrow().type_dependent_defs().get(hir_id)
791         {
792             self.register_wf_obligation(ty.raw.into(), qself.span, traits::WellFormed(None));
793             // Return directly on cache hit. This is useful to avoid doubly reporting
794             // errors with default match binding modes. See #44614.
795             let def = cached_result.map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id));
796             return (def, Some(ty), slice::from_ref(&**item_segment));
797         }
798         let item_name = item_segment.ident;
799         let result = self
800             .resolve_fully_qualified_call(span, item_name, ty.normalized, qself.span, hir_id)
801             .or_else(|error| {
802                 let result = match error {
803                     method::MethodError::PrivateMatch(kind, def_id, _) => Ok((kind, def_id)),
804                     _ => Err(ErrorGuaranteed::unchecked_claim_error_was_emitted()),
805                 };
806
807                 // If we have a path like `MyTrait::missing_method`, then don't register
808                 // a WF obligation for `dyn MyTrait` when method lookup fails. Otherwise,
809                 // register a WF obligation so that we can detect any additional
810                 // errors in the self type.
811                 if !(matches!(error, method::MethodError::NoMatch(_)) && ty.normalized.is_trait()) {
812                     self.register_wf_obligation(
813                         ty.raw.into(),
814                         qself.span,
815                         traits::WellFormed(None),
816                     );
817                 }
818                 if item_name.name != kw::Empty {
819                     if let Some(mut e) = self.report_method_error(
820                         span,
821                         ty.normalized,
822                         item_name,
823                         SelfSource::QPath(qself),
824                         error,
825                         None,
826                         Expectation::NoExpectation,
827                     ) {
828                         e.emit();
829                     }
830                 }
831                 result
832             });
833
834         if result.is_ok() {
835             self.register_wf_obligation(ty.raw.into(), qself.span, traits::WellFormed(None));
836         }
837
838         // Write back the new resolution.
839         self.write_resolution(hir_id, result);
840         (
841             result.map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id)),
842             Some(ty),
843             slice::from_ref(&**item_segment),
844         )
845     }
846
847     /// Given a function `Node`, return its `FnDecl` if it exists, or `None` otherwise.
848     pub(in super::super) fn get_node_fn_decl(
849         &self,
850         node: Node<'tcx>,
851     ) -> Option<(&'tcx hir::FnDecl<'tcx>, Ident, bool)> {
852         match node {
853             Node::Item(&hir::Item { ident, kind: hir::ItemKind::Fn(ref sig, ..), .. }) => {
854                 // This is less than ideal, it will not suggest a return type span on any
855                 // method called `main`, regardless of whether it is actually the entry point,
856                 // but it will still present it as the reason for the expected type.
857                 Some((&sig.decl, ident, ident.name != sym::main))
858             }
859             Node::TraitItem(&hir::TraitItem {
860                 ident,
861                 kind: hir::TraitItemKind::Fn(ref sig, ..),
862                 ..
863             }) => Some((&sig.decl, ident, true)),
864             Node::ImplItem(&hir::ImplItem {
865                 ident,
866                 kind: hir::ImplItemKind::Fn(ref sig, ..),
867                 ..
868             }) => Some((&sig.decl, ident, false)),
869             _ => None,
870         }
871     }
872
873     /// Given a `HirId`, return the `FnDecl` of the method it is enclosed by and whether a
874     /// suggestion can be made, `None` otherwise.
875     pub fn get_fn_decl(&self, blk_id: hir::HirId) -> Option<(&'tcx hir::FnDecl<'tcx>, bool)> {
876         // Get enclosing Fn, if it is a function or a trait method, unless there's a `loop` or
877         // `while` before reaching it, as block tail returns are not available in them.
878         self.tcx.hir().get_return_block(blk_id).and_then(|blk_id| {
879             let parent = self.tcx.hir().get(blk_id);
880             self.get_node_fn_decl(parent).map(|(fn_decl, _, is_main)| (fn_decl, is_main))
881         })
882     }
883
884     pub(in super::super) fn note_internal_mutation_in_method(
885         &self,
886         err: &mut Diagnostic,
887         expr: &hir::Expr<'_>,
888         expected: Ty<'tcx>,
889         found: Ty<'tcx>,
890     ) {
891         if found != self.tcx.types.unit {
892             return;
893         }
894         if let ExprKind::MethodCall(path_segment, rcvr, ..) = expr.kind {
895             if self
896                 .typeck_results
897                 .borrow()
898                 .expr_ty_adjusted_opt(rcvr)
899                 .map_or(true, |ty| expected.peel_refs() != ty.peel_refs())
900             {
901                 return;
902             }
903             let mut sp = MultiSpan::from_span(path_segment.ident.span);
904             sp.push_span_label(
905                 path_segment.ident.span,
906                 format!(
907                     "this call modifies {} in-place",
908                     match rcvr.kind {
909                         ExprKind::Path(QPath::Resolved(
910                             None,
911                             hir::Path { segments: [segment], .. },
912                         )) => format!("`{}`", segment.ident),
913                         _ => "its receiver".to_string(),
914                     }
915                 ),
916             );
917             sp.push_span_label(
918                 rcvr.span,
919                 "you probably want to use this value after calling the method...",
920             );
921             err.span_note(
922                 sp,
923                 &format!("method `{}` modifies its receiver in-place", path_segment.ident),
924             );
925             err.note(&format!("...instead of the `()` output of method `{}`", path_segment.ident));
926         }
927     }
928
929     pub(in super::super) fn note_need_for_fn_pointer(
930         &self,
931         err: &mut Diagnostic,
932         expected: Ty<'tcx>,
933         found: Ty<'tcx>,
934     ) {
935         let (sig, did, substs) = match (&expected.kind(), &found.kind()) {
936             (ty::FnDef(did1, substs1), ty::FnDef(did2, substs2)) => {
937                 let sig1 = self.tcx.bound_fn_sig(*did1).subst(self.tcx, substs1);
938                 let sig2 = self.tcx.bound_fn_sig(*did2).subst(self.tcx, substs2);
939                 if sig1 != sig2 {
940                     return;
941                 }
942                 err.note(
943                     "different `fn` items always have unique types, even if their signatures are \
944                      the same",
945                 );
946                 (sig1, *did1, substs1)
947             }
948             (ty::FnDef(did, substs), ty::FnPtr(sig2)) => {
949                 let sig1 = self.tcx.bound_fn_sig(*did).subst(self.tcx, substs);
950                 if sig1 != *sig2 {
951                     return;
952                 }
953                 (sig1, *did, substs)
954             }
955             _ => return,
956         };
957         err.help(&format!("change the expected type to be function pointer `{}`", sig));
958         err.help(&format!(
959             "if the expected type is due to type inference, cast the expected `fn` to a function \
960              pointer: `{} as {}`",
961             self.tcx.def_path_str_with_substs(did, substs),
962             sig
963         ));
964     }
965
966     // Instantiates the given path, which must refer to an item with the given
967     // number of type parameters and type.
968     #[instrument(skip(self, span), level = "debug")]
969     pub fn instantiate_value_path(
970         &self,
971         segments: &[hir::PathSegment<'_>],
972         self_ty: Option<RawTy<'tcx>>,
973         res: Res,
974         span: Span,
975         hir_id: hir::HirId,
976     ) -> (Ty<'tcx>, Res) {
977         let tcx = self.tcx;
978
979         let path_segs = match res {
980             Res::Local(_) | Res::SelfCtor(_) => vec![],
981             Res::Def(kind, def_id) => self.astconv().def_ids_for_value_path_segments(
982                 segments,
983                 self_ty.map(|ty| ty.raw),
984                 kind,
985                 def_id,
986                 span,
987             ),
988             _ => bug!("instantiate_value_path on {:?}", res),
989         };
990
991         let mut user_self_ty = None;
992         let mut is_alias_variant_ctor = false;
993         match res {
994             Res::Def(DefKind::Ctor(CtorOf::Variant, _), _)
995                 if let Some(self_ty) = self_ty =>
996             {
997                 let adt_def = self_ty.normalized.ty_adt_def().unwrap();
998                 user_self_ty = Some(UserSelfTy { impl_def_id: adt_def.did(), self_ty: self_ty.raw });
999                 is_alias_variant_ctor = true;
1000             }
1001             Res::Def(DefKind::AssocFn | DefKind::AssocConst, def_id) => {
1002                 let assoc_item = tcx.associated_item(def_id);
1003                 let container = assoc_item.container;
1004                 let container_id = assoc_item.container_id(tcx);
1005                 debug!(?def_id, ?container, ?container_id);
1006                 match container {
1007                     ty::TraitContainer => {
1008                         callee::check_legal_trait_for_method_call(tcx, span, None, span, container_id)
1009                     }
1010                     ty::ImplContainer => {
1011                         if segments.len() == 1 {
1012                             // `<T>::assoc` will end up here, and so
1013                             // can `T::assoc`. It this came from an
1014                             // inherent impl, we need to record the
1015                             // `T` for posterity (see `UserSelfTy` for
1016                             // details).
1017                             let self_ty = self_ty.expect("UFCS sugared assoc missing Self").raw;
1018                             user_self_ty = Some(UserSelfTy { impl_def_id: container_id, self_ty });
1019                         }
1020                     }
1021                 }
1022             }
1023             _ => {}
1024         }
1025
1026         // Now that we have categorized what space the parameters for each
1027         // segment belong to, let's sort out the parameters that the user
1028         // provided (if any) into their appropriate spaces. We'll also report
1029         // errors if type parameters are provided in an inappropriate place.
1030
1031         let generic_segs: FxHashSet<_> = path_segs.iter().map(|PathSeg(_, index)| index).collect();
1032         let generics_has_err = self.astconv().prohibit_generics(
1033             segments.iter().enumerate().filter_map(|(index, seg)| {
1034                 if !generic_segs.contains(&index) || is_alias_variant_ctor {
1035                     Some(seg)
1036                 } else {
1037                     None
1038                 }
1039             }),
1040             |_| {},
1041         );
1042
1043         if let Res::Local(hid) = res {
1044             let ty = self.local_ty(span, hid).decl_ty;
1045             let ty = self.normalize(span, ty);
1046             self.write_ty(hir_id, ty);
1047             return (ty, res);
1048         }
1049
1050         if generics_has_err {
1051             // Don't try to infer type parameters when prohibited generic arguments were given.
1052             user_self_ty = None;
1053         }
1054
1055         // Now we have to compare the types that the user *actually*
1056         // provided against the types that were *expected*. If the user
1057         // did not provide any types, then we want to substitute inference
1058         // variables. If the user provided some types, we may still need
1059         // to add defaults. If the user provided *too many* types, that's
1060         // a problem.
1061
1062         let mut infer_args_for_err = FxHashSet::default();
1063
1064         let mut explicit_late_bound = ExplicitLateBound::No;
1065         for &PathSeg(def_id, index) in &path_segs {
1066             let seg = &segments[index];
1067             let generics = tcx.generics_of(def_id);
1068
1069             // Argument-position `impl Trait` is treated as a normal generic
1070             // parameter internally, but we don't allow users to specify the
1071             // parameter's value explicitly, so we have to do some error-
1072             // checking here.
1073             let arg_count = check_generic_arg_count_for_call(
1074                 tcx,
1075                 span,
1076                 def_id,
1077                 &generics,
1078                 seg,
1079                 IsMethodCall::No,
1080             );
1081
1082             if let ExplicitLateBound::Yes = arg_count.explicit_late_bound {
1083                 explicit_late_bound = ExplicitLateBound::Yes;
1084             }
1085
1086             if let Err(GenericArgCountMismatch { reported: Some(e), .. }) = arg_count.correct {
1087                 infer_args_for_err.insert(index);
1088                 self.set_tainted_by_errors(e); // See issue #53251.
1089             }
1090         }
1091
1092         let has_self = path_segs
1093             .last()
1094             .map(|PathSeg(def_id, _)| tcx.generics_of(*def_id).has_self)
1095             .unwrap_or(false);
1096
1097         let (res, self_ctor_substs) = if let Res::SelfCtor(impl_def_id) = res {
1098             let ty = self.handle_raw_ty(span, tcx.at(span).type_of(impl_def_id));
1099             match ty.normalized.ty_adt_def() {
1100                 Some(adt_def) if adt_def.has_ctor() => {
1101                     let (ctor_kind, ctor_def_id) = adt_def.non_enum_variant().ctor.unwrap();
1102                     let new_res = Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
1103                     let user_substs = Self::user_substs_for_adt(ty);
1104                     user_self_ty = user_substs.user_self_ty;
1105                     (new_res, Some(user_substs.substs))
1106                 }
1107                 _ => {
1108                     let mut err = tcx.sess.struct_span_err(
1109                         span,
1110                         "the `Self` constructor can only be used with tuple or unit structs",
1111                     );
1112                     if let Some(adt_def) = ty.normalized.ty_adt_def() {
1113                         match adt_def.adt_kind() {
1114                             AdtKind::Enum => {
1115                                 err.help("did you mean to use one of the enum's variants?");
1116                             }
1117                             AdtKind::Struct | AdtKind::Union => {
1118                                 err.span_suggestion(
1119                                     span,
1120                                     "use curly brackets",
1121                                     "Self { /* fields */ }",
1122                                     Applicability::HasPlaceholders,
1123                                 );
1124                             }
1125                         }
1126                     }
1127                     let reported = err.emit();
1128                     return (tcx.ty_error_with_guaranteed(reported), res);
1129                 }
1130             }
1131         } else {
1132             (res, None)
1133         };
1134         let def_id = res.def_id();
1135
1136         let arg_count = GenericArgCountResult {
1137             explicit_late_bound,
1138             correct: if infer_args_for_err.is_empty() {
1139                 Ok(())
1140             } else {
1141                 Err(GenericArgCountMismatch::default())
1142             },
1143         };
1144
1145         struct CreateCtorSubstsContext<'a, 'tcx> {
1146             fcx: &'a FnCtxt<'a, 'tcx>,
1147             span: Span,
1148             path_segs: &'a [PathSeg],
1149             infer_args_for_err: &'a FxHashSet<usize>,
1150             segments: &'a [hir::PathSegment<'a>],
1151         }
1152         impl<'tcx, 'a> CreateSubstsForGenericArgsCtxt<'a, 'tcx> for CreateCtorSubstsContext<'a, 'tcx> {
1153             fn args_for_def_id(
1154                 &mut self,
1155                 def_id: DefId,
1156             ) -> (Option<&'a hir::GenericArgs<'a>>, bool) {
1157                 if let Some(&PathSeg(_, index)) =
1158                     self.path_segs.iter().find(|&PathSeg(did, _)| *did == def_id)
1159                 {
1160                     // If we've encountered an `impl Trait`-related error, we're just
1161                     // going to infer the arguments for better error messages.
1162                     if !self.infer_args_for_err.contains(&index) {
1163                         // Check whether the user has provided generic arguments.
1164                         if let Some(ref data) = self.segments[index].args {
1165                             return (Some(data), self.segments[index].infer_args);
1166                         }
1167                     }
1168                     return (None, self.segments[index].infer_args);
1169                 }
1170
1171                 (None, true)
1172             }
1173
1174             fn provided_kind(
1175                 &mut self,
1176                 param: &ty::GenericParamDef,
1177                 arg: &GenericArg<'_>,
1178             ) -> ty::GenericArg<'tcx> {
1179                 match (&param.kind, arg) {
1180                     (GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
1181                         self.fcx.astconv().ast_region_to_region(lt, Some(param)).into()
1182                     }
1183                     (GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
1184                         self.fcx.to_ty(ty).raw.into()
1185                     }
1186                     (GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => {
1187                         self.fcx.const_arg_to_const(&ct.value, param.def_id).into()
1188                     }
1189                     (GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
1190                         self.fcx.ty_infer(Some(param), inf.span).into()
1191                     }
1192                     (GenericParamDefKind::Const { .. }, GenericArg::Infer(inf)) => {
1193                         let tcx = self.fcx.tcx();
1194                         self.fcx.ct_infer(tcx.type_of(param.def_id), Some(param), inf.span).into()
1195                     }
1196                     _ => unreachable!(),
1197                 }
1198             }
1199
1200             fn inferred_kind(
1201                 &mut self,
1202                 substs: Option<&[ty::GenericArg<'tcx>]>,
1203                 param: &ty::GenericParamDef,
1204                 infer_args: bool,
1205             ) -> ty::GenericArg<'tcx> {
1206                 let tcx = self.fcx.tcx();
1207                 match param.kind {
1208                     GenericParamDefKind::Lifetime => {
1209                         self.fcx.re_infer(Some(param), self.span).unwrap().into()
1210                     }
1211                     GenericParamDefKind::Type { has_default, .. } => {
1212                         if !infer_args && has_default {
1213                             // If we have a default, then we it doesn't matter that we're not
1214                             // inferring the type arguments: we provide the default where any
1215                             // is missing.
1216                             tcx.bound_type_of(param.def_id).subst(tcx, substs.unwrap()).into()
1217                         } else {
1218                             // If no type arguments were provided, we have to infer them.
1219                             // This case also occurs as a result of some malformed input, e.g.
1220                             // a lifetime argument being given instead of a type parameter.
1221                             // Using inference instead of `Error` gives better error messages.
1222                             self.fcx.var_for_def(self.span, param)
1223                         }
1224                     }
1225                     GenericParamDefKind::Const { has_default } => {
1226                         if !infer_args && has_default {
1227                             tcx.bound_const_param_default(param.def_id)
1228                                 .subst(tcx, substs.unwrap())
1229                                 .into()
1230                         } else {
1231                             self.fcx.var_for_def(self.span, param)
1232                         }
1233                     }
1234                 }
1235             }
1236         }
1237
1238         let substs_raw = self_ctor_substs.unwrap_or_else(|| {
1239             create_substs_for_generic_args(
1240                 tcx,
1241                 def_id,
1242                 &[],
1243                 has_self,
1244                 self_ty.map(|s| s.raw),
1245                 &arg_count,
1246                 &mut CreateCtorSubstsContext {
1247                     fcx: self,
1248                     span,
1249                     path_segs: &path_segs,
1250                     infer_args_for_err: &infer_args_for_err,
1251                     segments,
1252                 },
1253             )
1254         });
1255
1256         // First, store the "user substs" for later.
1257         self.write_user_type_annotation_from_substs(hir_id, def_id, substs_raw, user_self_ty);
1258
1259         // Normalize only after registering type annotations.
1260         let substs = self.normalize(span, substs_raw);
1261
1262         self.add_required_obligations_for_hir(span, def_id, &substs, hir_id);
1263
1264         // Substitute the values for the type parameters into the type of
1265         // the referenced item.
1266         let ty = tcx.bound_type_of(def_id);
1267         assert!(!substs.has_escaping_bound_vars());
1268         assert!(!ty.0.has_escaping_bound_vars());
1269         let ty_substituted = self.normalize(span, ty.subst(tcx, substs));
1270
1271         if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty {
1272             // In the case of `Foo<T>::method` and `<Foo<T>>::method`, if `method`
1273             // is inherent, there is no `Self` parameter; instead, the impl needs
1274             // type parameters, which we can infer by unifying the provided `Self`
1275             // with the substituted impl type.
1276             // This also occurs for an enum variant on a type alias.
1277             let impl_ty = self.normalize(span, tcx.bound_type_of(impl_def_id).subst(tcx, substs));
1278             let self_ty = self.normalize(span, self_ty);
1279             match self.at(&self.misc(span), self.param_env).eq(impl_ty, self_ty) {
1280                 Ok(ok) => self.register_infer_ok_obligations(ok),
1281                 Err(_) => {
1282                     self.tcx.sess.delay_span_bug(
1283                         span,
1284                         &format!(
1285                         "instantiate_value_path: (UFCS) {:?} was a subtype of {:?} but now is not?",
1286                         self_ty,
1287                         impl_ty,
1288                     ),
1289                     );
1290                 }
1291             }
1292         }
1293
1294         debug!("instantiate_value_path: type of {:?} is {:?}", hir_id, ty_substituted);
1295         self.write_substs(hir_id, substs);
1296
1297         (ty_substituted, res)
1298     }
1299
1300     /// Add all the obligations that are required, substituting and normalized appropriately.
1301     pub(crate) fn add_required_obligations_for_hir(
1302         &self,
1303         span: Span,
1304         def_id: DefId,
1305         substs: SubstsRef<'tcx>,
1306         hir_id: hir::HirId,
1307     ) {
1308         self.add_required_obligations_with_code(span, def_id, substs, |idx, span| {
1309             if span.is_dummy() {
1310                 ObligationCauseCode::ExprItemObligation(def_id, hir_id, idx)
1311             } else {
1312                 ObligationCauseCode::ExprBindingObligation(def_id, span, hir_id, idx)
1313             }
1314         })
1315     }
1316
1317     #[instrument(level = "debug", skip(self, code, span, substs))]
1318     fn add_required_obligations_with_code(
1319         &self,
1320         span: Span,
1321         def_id: DefId,
1322         substs: SubstsRef<'tcx>,
1323         code: impl Fn(usize, Span) -> ObligationCauseCode<'tcx>,
1324     ) {
1325         let param_env = self.param_env;
1326
1327         let remap = match self.tcx.def_kind(def_id) {
1328             // Associated consts have `Self: ~const Trait` bounds that should be satisfiable when
1329             // `Self: Trait` is satisfied because it does not matter whether the impl is `const`.
1330             // Therefore we have to remap the param env here to be non-const.
1331             hir::def::DefKind::AssocConst => true,
1332             hir::def::DefKind::AssocFn
1333                 if self.tcx.def_kind(self.tcx.parent(def_id)) == hir::def::DefKind::Trait =>
1334             {
1335                 // N.B.: All callsites to this function involve checking a path expression.
1336                 //
1337                 // When instantiating a trait method as a function item, it does not actually matter whether
1338                 // the trait is `const` or not, or whether `where T: ~const Tr` needs to be satisfied as
1339                 // `const`. If we were to introduce instantiating trait methods as `const fn`s, we would
1340                 // check that after this, either via a bound `where F: ~const FnOnce` or when coercing to a
1341                 // `const fn` pointer.
1342                 //
1343                 // FIXME(fee1-dead) FIXME(const_trait_impl): update this doc when trait methods can satisfy
1344                 // `~const FnOnce` or can be coerced to `const fn` pointer.
1345                 true
1346             }
1347             _ => false,
1348         };
1349         let (bounds, _) = self.instantiate_bounds(span, def_id, &substs);
1350
1351         for mut obligation in traits::predicates_for_generics(
1352             |idx, predicate_span| {
1353                 traits::ObligationCause::new(span, self.body_id, code(idx, predicate_span))
1354             },
1355             param_env,
1356             bounds,
1357         ) {
1358             if remap {
1359                 obligation = obligation.without_const(self.tcx);
1360             }
1361             self.register_predicate(obligation);
1362         }
1363     }
1364
1365     /// Resolves `typ` by a single level if `typ` is a type variable.
1366     /// If no resolution is possible, then an error is reported.
1367     /// Numeric inference variables may be left unresolved.
1368     pub fn structurally_resolved_type(&self, sp: Span, ty: Ty<'tcx>) -> Ty<'tcx> {
1369         let ty = self.resolve_vars_with_obligations(ty);
1370         if !ty.is_ty_var() {
1371             ty
1372         } else {
1373             let e = self.tainted_by_errors().unwrap_or_else(|| {
1374                 self.err_ctxt()
1375                     .emit_inference_failure_err((**self).body_id, sp, ty.into(), E0282, true)
1376                     .emit()
1377             });
1378             let err = self.tcx.ty_error_with_guaranteed(e);
1379             self.demand_suptype(sp, err, ty);
1380             err
1381         }
1382     }
1383
1384     pub(in super::super) fn with_breakable_ctxt<F: FnOnce() -> R, R>(
1385         &self,
1386         id: hir::HirId,
1387         ctxt: BreakableCtxt<'tcx>,
1388         f: F,
1389     ) -> (BreakableCtxt<'tcx>, R) {
1390         let index;
1391         {
1392             let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
1393             index = enclosing_breakables.stack.len();
1394             enclosing_breakables.by_id.insert(id, index);
1395             enclosing_breakables.stack.push(ctxt);
1396         }
1397         let result = f();
1398         let ctxt = {
1399             let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
1400             debug_assert!(enclosing_breakables.stack.len() == index + 1);
1401             enclosing_breakables.by_id.remove(&id).expect("missing breakable context");
1402             enclosing_breakables.stack.pop().expect("missing breakable context")
1403         };
1404         (ctxt, result)
1405     }
1406
1407     /// Instantiate a QueryResponse in a probe context, without a
1408     /// good ObligationCause.
1409     pub(in super::super) fn probe_instantiate_query_response(
1410         &self,
1411         span: Span,
1412         original_values: &OriginalQueryValues<'tcx>,
1413         query_result: &Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>,
1414     ) -> InferResult<'tcx, Ty<'tcx>> {
1415         self.instantiate_query_response_and_region_obligations(
1416             &traits::ObligationCause::misc(span, self.body_id),
1417             self.param_env,
1418             original_values,
1419             query_result,
1420         )
1421     }
1422
1423     /// Returns `true` if an expression is contained inside the LHS of an assignment expression.
1424     pub(in super::super) fn expr_in_place(&self, mut expr_id: hir::HirId) -> bool {
1425         let mut contained_in_place = false;
1426
1427         while let hir::Node::Expr(parent_expr) = self.tcx.hir().get_parent(expr_id) {
1428             match &parent_expr.kind {
1429                 hir::ExprKind::Assign(lhs, ..) | hir::ExprKind::AssignOp(_, lhs, ..) => {
1430                     if lhs.hir_id == expr_id {
1431                         contained_in_place = true;
1432                         break;
1433                     }
1434                 }
1435                 _ => (),
1436             }
1437             expr_id = parent_expr.hir_id;
1438         }
1439
1440         contained_in_place
1441     }
1442 }