rustc_hir_typeck/fn_ctxt/
_impl.rs

1use std::collections::hash_map::Entry;
2use std::slice;
3
4use rustc_abi::FieldIdx;
5use rustc_data_structures::fx::FxHashSet;
6use rustc_errors::{Applicability, Diag, ErrorGuaranteed, MultiSpan};
7use rustc_hir::def::{CtorOf, DefKind, Res};
8use rustc_hir::def_id::DefId;
9use rustc_hir::intravisit::VisitorExt;
10use rustc_hir::lang_items::LangItem;
11use rustc_hir::{self as hir, AmbigArg, ExprKind, GenericArg, HirId, Node, QPath, intravisit};
12use rustc_hir_analysis::hir_ty_lowering::errors::GenericsArgsErrExtend;
13use rustc_hir_analysis::hir_ty_lowering::generics::{
14    check_generic_arg_count_for_call, lower_generic_args,
15};
16use rustc_hir_analysis::hir_ty_lowering::{
17    ExplicitLateBound, FeedConstTy, GenericArgCountMismatch, GenericArgCountResult,
18    GenericArgsLowerer, GenericPathSegment, HirTyLowerer, IsMethodCall, RegionInferReason,
19};
20use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryResponse};
21use rustc_infer::infer::{DefineOpaqueTypes, InferResult};
22use rustc_lint::builtin::SELF_CONSTRUCTOR_FROM_OUTER_ITEM;
23use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
24use rustc_middle::ty::{
25    self, AdtKind, CanonicalUserType, GenericArgsRef, GenericParamDefKind, IsIdentity,
26    SizedTraitKind, Ty, TyCtxt, TypeFoldable, TypeVisitable, TypeVisitableExt, UserArgs,
27    UserSelfTy,
28};
29use rustc_middle::{bug, span_bug};
30use rustc_session::lint;
31use rustc_span::Span;
32use rustc_span::def_id::LocalDefId;
33use rustc_span::hygiene::DesugaringKind;
34use rustc_trait_selection::error_reporting::infer::need_type_info::TypeAnnotationNeeded;
35use rustc_trait_selection::traits::{
36    self, NormalizeExt, ObligationCauseCode, StructurallyNormalizeExt,
37};
38use tracing::{debug, instrument};
39
40use crate::callee::{self, DeferredCallResolution};
41use crate::errors::{self, CtorIsPrivate};
42use crate::method::{self, MethodCallee};
43use crate::{BreakableCtxt, Diverges, Expectation, FnCtxt, LoweredTy, rvalue_scopes};
44
45impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
46    /// Produces warning on the given node, if the current point in the
47    /// function is unreachable, and there hasn't been another warning.
48    pub(crate) fn warn_if_unreachable(&self, id: HirId, span: Span, kind: &str) {
49        let Diverges::Always { span: orig_span, custom_note } = self.diverges.get() else {
50            return;
51        };
52
53        match span.desugaring_kind() {
54            // Don't lint if the result of an async block or async function is `!`.
55            // This does not affect the unreachable lints *within* the body.
56            Some(DesugaringKind::Async) => return,
57
58            // Don't lint *within* the `.await` operator, since that's all just desugaring
59            // junk. We only want to lint if there is a subsequent expression after the
60            // `.await` operator.
61            Some(DesugaringKind::Await) => return,
62
63            _ => {}
64        }
65
66        // Don't warn twice.
67        self.diverges.set(Diverges::WarnedAlways);
68
69        debug!("warn_if_unreachable: id={:?} span={:?} kind={}", id, span, kind);
70
71        let msg = format!("unreachable {kind}");
72        self.tcx().node_span_lint(lint::builtin::UNREACHABLE_CODE, id, span, |lint| {
73            lint.primary_message(msg.clone());
74            lint.span_label(span, msg).span_label(
75                orig_span,
76                custom_note.unwrap_or("any code following this expression is unreachable"),
77            );
78        })
79    }
80
81    /// Resolves type and const variables in `t` if possible. Unlike the infcx
82    /// version (resolve_vars_if_possible), this version will
83    /// also select obligations if it seems useful, in an effort
84    /// to get more type information.
85    // FIXME(-Znext-solver): A lot of the calls to this method should
86    // probably be `try_structurally_resolve_type` or `structurally_resolve_type` instead.
87    #[instrument(skip(self), level = "debug", ret)]
88    pub(crate) fn resolve_vars_with_obligations<T: TypeFoldable<TyCtxt<'tcx>>>(
89        &self,
90        mut t: T,
91    ) -> T {
92        // No Infer()? Nothing needs doing.
93        if !t.has_non_region_infer() {
94            debug!("no inference var, nothing needs doing");
95            return t;
96        }
97
98        // If `t` is a type variable, see whether we already know what it is.
99        t = self.resolve_vars_if_possible(t);
100        if !t.has_non_region_infer() {
101            debug!(?t);
102            return t;
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(|_| {});
110        self.resolve_vars_if_possible(t)
111    }
112
113    pub(crate) 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(crate) 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    fn tag(&self) -> String {
131        format!("{self:p}")
132    }
133
134    pub(crate) fn local_ty(&self, span: Span, nid: HirId) -> Ty<'tcx> {
135        self.locals.borrow().get(&nid).cloned().unwrap_or_else(|| {
136            span_bug!(span, "no type for local variable {}", self.tcx.hir_id_to_string(nid))
137        })
138    }
139
140    #[inline]
141    pub(crate) fn write_ty(&self, id: HirId, ty: Ty<'tcx>) {
142        debug!("write_ty({:?}, {:?}) in fcx {}", id, self.resolve_vars_if_possible(ty), self.tag());
143        let mut typeck = self.typeck_results.borrow_mut();
144        let mut node_ty = typeck.node_types_mut();
145
146        if let Some(prev) = node_ty.insert(id, ty) {
147            if prev.references_error() {
148                node_ty.insert(id, prev);
149            } else if !ty.references_error() {
150                // Could change this to a bug, but there's lots of diagnostic code re-lowering
151                // or re-typechecking nodes that were already typecked.
152                // Lots of that diagnostics code relies on subtle effects of re-lowering, so we'll
153                // let it keep doing that and just ensure that compilation won't succeed.
154                self.dcx().span_delayed_bug(
155                    self.tcx.hir_span(id),
156                    format!("`{prev}` overridden by `{ty}` for {id:?} in {:?}", self.body_id),
157                );
158            }
159        }
160
161        if let Err(e) = ty.error_reported() {
162            self.set_tainted_by_errors(e);
163        }
164    }
165
166    pub(crate) fn write_field_index(&self, hir_id: HirId, index: FieldIdx) {
167        self.typeck_results.borrow_mut().field_indices_mut().insert(hir_id, index);
168    }
169
170    #[instrument(level = "debug", skip(self))]
171    pub(crate) fn write_resolution(
172        &self,
173        hir_id: HirId,
174        r: Result<(DefKind, DefId), ErrorGuaranteed>,
175    ) {
176        self.typeck_results.borrow_mut().type_dependent_defs_mut().insert(hir_id, r);
177    }
178
179    #[instrument(level = "debug", skip(self))]
180    pub(crate) fn write_method_call_and_enforce_effects(
181        &self,
182        hir_id: HirId,
183        span: Span,
184        method: MethodCallee<'tcx>,
185    ) {
186        self.enforce_context_effects(Some(hir_id), span, method.def_id, method.args);
187        self.write_resolution(hir_id, Ok((DefKind::AssocFn, method.def_id)));
188        self.write_args(hir_id, method.args);
189    }
190
191    fn write_args(&self, node_id: HirId, args: GenericArgsRef<'tcx>) {
192        if !args.is_empty() {
193            debug!("write_args({:?}, {:?}) in fcx {}", node_id, args, self.tag());
194
195            self.typeck_results.borrow_mut().node_args_mut().insert(node_id, args);
196        }
197    }
198
199    /// Given the args that we just converted from the HIR, try to
200    /// canonicalize them and store them as user-given parameters
201    /// (i.e., parameters that must be respected by the NLL check).
202    ///
203    /// This should be invoked **before any unifications have
204    /// occurred**, so that annotations like `Vec<_>` are preserved
205    /// properly.
206    #[instrument(skip(self), level = "debug")]
207    pub(crate) fn write_user_type_annotation_from_args(
208        &self,
209        hir_id: HirId,
210        def_id: DefId,
211        args: GenericArgsRef<'tcx>,
212        user_self_ty: Option<UserSelfTy<'tcx>>,
213    ) {
214        debug!("fcx {}", self.tag());
215
216        // Don't write user type annotations for const param types, since we give them
217        // identity args just so that we can trivially substitute their `EarlyBinder`.
218        // We enforce that they match their type in MIR later on.
219        if matches!(self.tcx.def_kind(def_id), DefKind::ConstParam) {
220            return;
221        }
222
223        if Self::can_contain_user_lifetime_bounds((args, user_self_ty)) {
224            let canonicalized = self.canonicalize_user_type_annotation(ty::UserType::new(
225                ty::UserTypeKind::TypeOf(def_id, UserArgs { args, user_self_ty }),
226            ));
227            debug!(?canonicalized);
228            self.write_user_type_annotation(hir_id, canonicalized);
229        }
230    }
231
232    #[instrument(skip(self), level = "debug")]
233    pub(crate) fn write_user_type_annotation(
234        &self,
235        hir_id: HirId,
236        canonical_user_type_annotation: CanonicalUserType<'tcx>,
237    ) {
238        debug!("fcx {}", self.tag());
239
240        // FIXME: is_identity being on `UserType` and not `Canonical<UserType>` is awkward
241        if !canonical_user_type_annotation.is_identity() {
242            self.typeck_results
243                .borrow_mut()
244                .user_provided_types_mut()
245                .insert(hir_id, canonical_user_type_annotation);
246        } else {
247            debug!("skipping identity args");
248        }
249    }
250
251    #[instrument(skip(self, expr), level = "debug")]
252    pub(crate) fn apply_adjustments(&self, expr: &hir::Expr<'_>, adj: Vec<Adjustment<'tcx>>) {
253        debug!("expr = {:#?}", expr);
254
255        if adj.is_empty() {
256            return;
257        }
258
259        let mut expr_ty = self.typeck_results.borrow().expr_ty_adjusted(expr);
260
261        for a in &adj {
262            match a.kind {
263                Adjust::NeverToAny => {
264                    if a.target.is_ty_var() {
265                        self.diverging_type_vars.borrow_mut().insert(a.target);
266                        debug!("apply_adjustments: adding `{:?}` as diverging type var", a.target);
267                    }
268                }
269                Adjust::Deref(Some(overloaded_deref)) => {
270                    self.enforce_context_effects(
271                        None,
272                        expr.span,
273                        overloaded_deref.method_call(self.tcx),
274                        self.tcx.mk_args(&[expr_ty.into()]),
275                    );
276                }
277                Adjust::Deref(None) => {
278                    // FIXME(const_trait_impl): We *could* enforce `&T: [const] Deref` here.
279                }
280                Adjust::Pointer(_pointer_coercion) => {
281                    // FIXME(const_trait_impl): We should probably enforce these.
282                }
283                Adjust::ReborrowPin(_mutability) => {
284                    // FIXME(const_trait_impl): We could enforce these; they correspond to
285                    // `&mut T: DerefMut` tho, so it's kinda moot.
286                }
287                Adjust::Borrow(_) => {
288                    // No effects to enforce here.
289                }
290            }
291
292            expr_ty = a.target;
293        }
294
295        let autoborrow_mut = adj.iter().any(|adj| {
296            matches!(
297                adj,
298                &Adjustment {
299                    kind: Adjust::Borrow(AutoBorrow::Ref(AutoBorrowMutability::Mut { .. })),
300                    ..
301                }
302            )
303        });
304
305        match self.typeck_results.borrow_mut().adjustments_mut().entry(expr.hir_id) {
306            Entry::Vacant(entry) => {
307                entry.insert(adj);
308            }
309            Entry::Occupied(mut entry) => {
310                debug!(" - composing on top of {:?}", entry.get());
311                match (&mut entry.get_mut()[..], &adj[..]) {
312                    (
313                        [Adjustment { kind: Adjust::NeverToAny, target }],
314                        &[.., Adjustment { target: new_target, .. }],
315                    ) => {
316                        // NeverToAny coercion can target any type, so instead of adding a new
317                        // adjustment on top we can change the target.
318                        //
319                        // This is required for things like `a == a` (where `a: !`) to produce
320                        // valid MIR -- we need borrow adjustment from things like `==` to change
321                        // the type to `&!` (or `&()` depending on the fallback). This might be
322                        // relevant even in unreachable code.
323                        *target = new_target;
324                    }
325
326                    (
327                        &mut [
328                            Adjustment { kind: Adjust::Deref(_), .. },
329                            Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(..)), .. },
330                        ],
331                        &[
332                            Adjustment { kind: Adjust::Deref(_), .. },
333                            .., // Any following adjustments are allowed.
334                        ],
335                    ) => {
336                        // A reborrow has no effect before a dereference, so we can safely replace adjustments.
337                        *entry.get_mut() = adj;
338                    }
339
340                    _ => {
341                        // FIXME: currently we never try to compose autoderefs
342                        // and ReifyFnPointer/UnsafeFnPointer, but we could.
343                        self.dcx().span_delayed_bug(
344                            expr.span,
345                            format!(
346                                "while adjusting {:?}, can't compose {:?} and {:?}",
347                                expr,
348                                entry.get(),
349                                adj
350                            ),
351                        );
352
353                        *entry.get_mut() = adj;
354                    }
355                }
356            }
357        }
358
359        // If there is an mutable auto-borrow, it is equivalent to `&mut <expr>`.
360        // In this case implicit use of `Deref` and `Index` within `<expr>` should
361        // instead be `DerefMut` and `IndexMut`, so fix those up.
362        if autoborrow_mut {
363            self.convert_place_derefs_to_mutable(expr);
364        }
365    }
366
367    /// Instantiates and normalizes the bounds for a given item
368    pub(crate) fn instantiate_bounds(
369        &self,
370        span: Span,
371        def_id: DefId,
372        args: GenericArgsRef<'tcx>,
373    ) -> ty::InstantiatedPredicates<'tcx> {
374        let bounds = self.tcx.predicates_of(def_id);
375        let result = bounds.instantiate(self.tcx, args);
376        let result = self.normalize(span, result);
377        debug!("instantiate_bounds(bounds={:?}, args={:?}) = {:?}", bounds, args, result);
378        result
379    }
380
381    pub(crate) fn normalize<T>(&self, span: Span, value: T) -> T
382    where
383        T: TypeFoldable<TyCtxt<'tcx>>,
384    {
385        self.register_infer_ok_obligations(
386            self.at(&self.misc(span), self.param_env).normalize(value),
387        )
388    }
389
390    pub(crate) fn require_type_meets(
391        &self,
392        ty: Ty<'tcx>,
393        span: Span,
394        code: traits::ObligationCauseCode<'tcx>,
395        def_id: DefId,
396    ) {
397        self.register_bound(ty, def_id, self.cause(span, code));
398    }
399
400    pub(crate) fn require_type_is_sized(
401        &self,
402        ty: Ty<'tcx>,
403        span: Span,
404        code: traits::ObligationCauseCode<'tcx>,
405    ) {
406        if !ty.references_error() {
407            let lang_item = self.tcx.require_lang_item(LangItem::Sized, span);
408            self.require_type_meets(ty, span, code, lang_item);
409        }
410    }
411
412    pub(crate) fn require_type_is_sized_deferred(
413        &self,
414        ty: Ty<'tcx>,
415        span: Span,
416        code: traits::ObligationCauseCode<'tcx>,
417    ) {
418        if !ty.references_error() {
419            self.deferred_sized_obligations.borrow_mut().push((ty, span, code));
420        }
421    }
422
423    pub(crate) fn require_type_has_static_alignment(&self, ty: Ty<'tcx>, span: Span) {
424        if !ty.references_error() {
425            let tail = self.tcx.struct_tail_raw(
426                ty,
427                |ty| {
428                    if self.next_trait_solver() {
429                        self.try_structurally_resolve_type(span, ty)
430                    } else {
431                        self.normalize(span, ty)
432                    }
433                },
434                || {},
435            );
436            // Sized types have static alignment, and so do slices.
437            if tail.has_trivial_sizedness(self.tcx, SizedTraitKind::Sized)
438                || matches!(tail.kind(), ty::Slice(..))
439            {
440                // Nothing else is required here.
441            } else {
442                // We can't be sure, let's required full `Sized`.
443                let lang_item = self.tcx.require_lang_item(LangItem::Sized, span);
444                self.require_type_meets(ty, span, ObligationCauseCode::Misc, lang_item);
445            }
446        }
447    }
448
449    pub(crate) fn register_bound(
450        &self,
451        ty: Ty<'tcx>,
452        def_id: DefId,
453        cause: traits::ObligationCause<'tcx>,
454    ) {
455        if !ty.references_error() {
456            self.fulfillment_cx.borrow_mut().register_bound(
457                self,
458                self.param_env,
459                ty,
460                def_id,
461                cause,
462            );
463        }
464    }
465
466    pub(crate) fn lower_ty(&self, hir_ty: &hir::Ty<'tcx>) -> LoweredTy<'tcx> {
467        let ty = self.lowerer().lower_ty(hir_ty);
468        self.register_wf_obligation(ty.into(), hir_ty.span, ObligationCauseCode::WellFormed(None));
469        LoweredTy::from_raw(self, hir_ty.span, ty)
470    }
471
472    /// Walk a `hir_ty` and collect any clauses that may have come from a type
473    /// within the `hir_ty`. These clauses will be canonicalized with a user type
474    /// annotation so that we can enforce these bounds in borrowck, too.
475    pub(crate) fn collect_impl_trait_clauses_from_hir_ty(
476        &self,
477        hir_ty: &'tcx hir::Ty<'tcx>,
478    ) -> ty::Clauses<'tcx> {
479        struct CollectClauses<'a, 'tcx> {
480            clauses: Vec<ty::Clause<'tcx>>,
481            fcx: &'a FnCtxt<'a, 'tcx>,
482        }
483
484        impl<'tcx> intravisit::Visitor<'tcx> for CollectClauses<'_, 'tcx> {
485            fn visit_ty(&mut self, ty: &'tcx hir::Ty<'tcx, AmbigArg>) {
486                if let Some(clauses) = self.fcx.trait_ascriptions.borrow().get(&ty.hir_id.local_id)
487                {
488                    self.clauses.extend(clauses.iter().cloned());
489                }
490                intravisit::walk_ty(self, ty)
491            }
492        }
493
494        let mut clauses = CollectClauses { clauses: vec![], fcx: self };
495        clauses.visit_ty_unambig(hir_ty);
496        self.tcx.mk_clauses(&clauses.clauses)
497    }
498
499    #[instrument(level = "debug", skip_all)]
500    pub(crate) fn lower_ty_saving_user_provided_ty(&self, hir_ty: &'tcx hir::Ty<'tcx>) -> Ty<'tcx> {
501        let ty = self.lower_ty(hir_ty);
502        debug!(?ty);
503
504        if Self::can_contain_user_lifetime_bounds(ty.raw) {
505            let c_ty = self.canonicalize_response(ty::UserType::new(ty::UserTypeKind::Ty(ty.raw)));
506            debug!(?c_ty);
507            self.typeck_results.borrow_mut().user_provided_types_mut().insert(hir_ty.hir_id, c_ty);
508        }
509
510        ty.normalized
511    }
512
513    pub(super) fn user_args_for_adt(ty: LoweredTy<'tcx>) -> UserArgs<'tcx> {
514        match (ty.raw.kind(), ty.normalized.kind()) {
515            (ty::Adt(_, args), _) => UserArgs { args, user_self_ty: None },
516            (_, ty::Adt(adt, args)) => UserArgs {
517                args,
518                user_self_ty: Some(UserSelfTy { impl_def_id: adt.did(), self_ty: ty.raw }),
519            },
520            _ => bug!("non-adt type {:?}", ty),
521        }
522    }
523
524    pub(crate) fn lower_const_arg(
525        &self,
526        const_arg: &'tcx hir::ConstArg<'tcx>,
527        feed: FeedConstTy<'_, 'tcx>,
528    ) -> ty::Const<'tcx> {
529        let ct = self.lowerer().lower_const_arg(const_arg, feed);
530        self.register_wf_obligation(
531            ct.into(),
532            self.tcx.hir_span(const_arg.hir_id),
533            ObligationCauseCode::WellFormed(None),
534        );
535        ct
536    }
537
538    // If the type given by the user has free regions, save it for later, since
539    // NLL would like to enforce those. Also pass in types that involve
540    // projections, since those can resolve to `'static` bounds (modulo #54940,
541    // which hopefully will be fixed by the time you see this comment, dear
542    // reader, although I have my doubts). Also pass in types with inference
543    // types, because they may be repeated. Other sorts of things are already
544    // sufficiently enforced with erased regions. =)
545    fn can_contain_user_lifetime_bounds<T>(t: T) -> bool
546    where
547        T: TypeVisitable<TyCtxt<'tcx>>,
548    {
549        t.has_free_regions() || t.has_aliases() || t.has_infer_types()
550    }
551
552    pub(crate) fn node_ty(&self, id: HirId) -> Ty<'tcx> {
553        match self.typeck_results.borrow().node_types().get(id) {
554            Some(&t) => t,
555            None if let Some(e) = self.tainted_by_errors() => Ty::new_error(self.tcx, e),
556            None => {
557                bug!("no type for node {} in fcx {}", self.tcx.hir_id_to_string(id), self.tag());
558            }
559        }
560    }
561
562    pub(crate) fn node_ty_opt(&self, id: HirId) -> Option<Ty<'tcx>> {
563        match self.typeck_results.borrow().node_types().get(id) {
564            Some(&t) => Some(t),
565            None if let Some(e) = self.tainted_by_errors() => Some(Ty::new_error(self.tcx, e)),
566            None => None,
567        }
568    }
569
570    /// Registers an obligation for checking later, during regionck, that `arg` is well-formed.
571    pub(crate) fn register_wf_obligation(
572        &self,
573        term: ty::Term<'tcx>,
574        span: Span,
575        code: traits::ObligationCauseCode<'tcx>,
576    ) {
577        // WF obligations never themselves fail, so no real need to give a detailed cause:
578        let cause = self.cause(span, code);
579        self.register_predicate(traits::Obligation::new(
580            self.tcx,
581            cause,
582            self.param_env,
583            ty::ClauseKind::WellFormed(term),
584        ));
585    }
586
587    /// Registers obligations that all `args` are well-formed.
588    pub(crate) fn add_wf_bounds(&self, args: GenericArgsRef<'tcx>, span: Span) {
589        for term in args.iter().filter_map(ty::GenericArg::as_term) {
590            self.register_wf_obligation(term, span, ObligationCauseCode::WellFormed(None));
591        }
592    }
593
594    // FIXME(arielb1): use this instead of field.ty everywhere
595    // Only for fields! Returns <none> for methods>
596    // Indifferent to privacy flags
597    pub(crate) fn field_ty(
598        &self,
599        span: Span,
600        field: &'tcx ty::FieldDef,
601        args: GenericArgsRef<'tcx>,
602    ) -> Ty<'tcx> {
603        self.normalize(span, field.ty(self.tcx, args))
604    }
605
606    pub(crate) fn resolve_rvalue_scopes(&self, def_id: DefId) {
607        let scope_tree = self.tcx.region_scope_tree(def_id);
608        let rvalue_scopes = { rvalue_scopes::resolve_rvalue_scopes(self, scope_tree, def_id) };
609        let mut typeck_results = self.typeck_results.borrow_mut();
610        typeck_results.rvalue_scopes = rvalue_scopes;
611    }
612
613    /// Unify the inference variables corresponding to coroutine witnesses, and save all the
614    /// predicates that were stalled on those inference variables.
615    ///
616    /// This process allows to conservatively save all predicates that do depend on the coroutine
617    /// interior types, for later processing by `check_coroutine_obligations`.
618    ///
619    /// We must not attempt to select obligations after this method has run, or risk query cycle
620    /// ICE.
621    #[instrument(level = "debug", skip(self))]
622    pub(crate) fn resolve_coroutine_interiors(&self) {
623        // Try selecting all obligations that are not blocked on inference variables.
624        // Once we start unifying coroutine witnesses, trying to select obligations on them will
625        // trigger query cycle ICEs, as doing so requires MIR.
626        self.select_obligations_where_possible(|_| {});
627
628        let ty::TypingMode::Analysis { defining_opaque_types_and_generators } = self.typing_mode()
629        else {
630            bug!();
631        };
632
633        if defining_opaque_types_and_generators
634            .iter()
635            .any(|def_id| self.tcx.is_coroutine(def_id.to_def_id()))
636        {
637            self.typeck_results.borrow_mut().coroutine_stalled_predicates.extend(
638                self.fulfillment_cx
639                    .borrow_mut()
640                    .drain_stalled_obligations_for_coroutines(&self.infcx)
641                    .into_iter()
642                    .map(|o| (o.predicate, o.cause)),
643            );
644        }
645    }
646
647    #[instrument(skip(self), level = "debug")]
648    pub(crate) fn report_ambiguity_errors(&self) {
649        let mut errors = self.fulfillment_cx.borrow_mut().collect_remaining_errors(self);
650
651        if !errors.is_empty() {
652            self.adjust_fulfillment_errors_for_expr_obligation(&mut errors);
653            self.err_ctxt().report_fulfillment_errors(errors);
654        }
655    }
656
657    /// Select as many obligations as we can at present.
658    pub(crate) fn select_obligations_where_possible(
659        &self,
660        mutate_fulfillment_errors: impl Fn(&mut Vec<traits::FulfillmentError<'tcx>>),
661    ) {
662        let mut result = self.fulfillment_cx.borrow_mut().select_where_possible(self);
663        if !result.is_empty() {
664            mutate_fulfillment_errors(&mut result);
665            self.adjust_fulfillment_errors_for_expr_obligation(&mut result);
666            self.err_ctxt().report_fulfillment_errors(result);
667        }
668    }
669
670    /// For the overloaded place expressions (`*x`, `x[3]`), the trait
671    /// returns a type of `&T`, but the actual type we assign to the
672    /// *expression* is `T`. So this function just peels off the return
673    /// type by one layer to yield `T`.
674    pub(crate) fn make_overloaded_place_return_type(&self, method: MethodCallee<'tcx>) -> Ty<'tcx> {
675        // extract method return type, which will be &T;
676        let ret_ty = method.sig.output();
677
678        // method returns &T, but the type as visible to user is T, so deref
679        ret_ty.builtin_deref(true).unwrap()
680    }
681
682    pub(crate) fn type_var_is_sized(&self, self_ty: ty::TyVid) -> bool {
683        let sized_did = self.tcx.lang_items().sized_trait();
684        self.obligations_for_self_ty(self_ty).into_iter().any(|obligation| {
685            match obligation.predicate.kind().skip_binder() {
686                ty::PredicateKind::Clause(ty::ClauseKind::Trait(data)) => {
687                    Some(data.def_id()) == sized_did
688                }
689                _ => false,
690            }
691        })
692    }
693
694    pub(crate) fn err_args(&self, len: usize, guar: ErrorGuaranteed) -> Vec<Ty<'tcx>> {
695        let ty_error = Ty::new_error(self.tcx, guar);
696        vec![ty_error; len]
697    }
698
699    pub(crate) fn resolve_lang_item_path(
700        &self,
701        lang_item: hir::LangItem,
702        span: Span,
703        hir_id: HirId,
704    ) -> (Res, Ty<'tcx>) {
705        let def_id = self.tcx.require_lang_item(lang_item, span);
706        let def_kind = self.tcx.def_kind(def_id);
707
708        let item_ty = if let DefKind::Variant = def_kind {
709            self.tcx.type_of(self.tcx.parent(def_id))
710        } else {
711            self.tcx.type_of(def_id)
712        };
713        let args = self.fresh_args_for_item(span, def_id);
714        let ty = item_ty.instantiate(self.tcx, args);
715
716        self.write_args(hir_id, args);
717        self.write_resolution(hir_id, Ok((def_kind, def_id)));
718
719        let code = match lang_item {
720            hir::LangItem::IntoFutureIntoFuture => {
721                if let hir::Node::Expr(into_future_call) = self.tcx.parent_hir_node(hir_id)
722                    && let hir::ExprKind::Call(_, [arg0]) = &into_future_call.kind
723                {
724                    Some(ObligationCauseCode::AwaitableExpr(arg0.hir_id))
725                } else {
726                    None
727                }
728            }
729            hir::LangItem::IteratorNext | hir::LangItem::IntoIterIntoIter => {
730                Some(ObligationCauseCode::ForLoopIterator)
731            }
732            hir::LangItem::TryTraitFromOutput
733            | hir::LangItem::TryTraitFromResidual
734            | hir::LangItem::TryTraitBranch => Some(ObligationCauseCode::QuestionMark),
735            _ => None,
736        };
737        if let Some(code) = code {
738            self.add_required_obligations_with_code(span, def_id, args, move |_, _| code.clone());
739        } else {
740            self.add_required_obligations_for_hir(span, def_id, args, hir_id);
741        }
742
743        (Res::Def(def_kind, def_id), ty)
744    }
745
746    /// Resolves an associated value path into a base type and associated constant, or method
747    /// resolution. The newly resolved definition is written into `type_dependent_defs`.
748    #[instrument(level = "trace", skip(self), ret)]
749    pub(crate) fn resolve_ty_and_res_fully_qualified_call(
750        &self,
751        qpath: &'tcx QPath<'tcx>,
752        hir_id: HirId,
753        span: Span,
754    ) -> (Res, Option<LoweredTy<'tcx>>, &'tcx [hir::PathSegment<'tcx>]) {
755        let (ty, qself, item_segment) = match *qpath {
756            QPath::Resolved(ref opt_qself, path) => {
757                return (
758                    path.res,
759                    opt_qself.as_ref().map(|qself| self.lower_ty(qself)),
760                    path.segments,
761                );
762            }
763            QPath::TypeRelative(ref qself, ref segment) => {
764                // Don't use `self.lower_ty`, since this will register a WF obligation.
765                // If we're trying to call a nonexistent method on a trait
766                // (e.g. `MyTrait::missing_method`), then resolution will
767                // give us a `QPath::TypeRelative` with a trait object as
768                // `qself`. In that case, we want to avoid registering a WF obligation
769                // for `dyn MyTrait`, since we don't actually need the trait
770                // to be dyn-compatible.
771                // We manually call `register_wf_obligation` in the success path
772                // below.
773                let ty = self.lowerer().lower_ty(qself);
774                (LoweredTy::from_raw(self, span, ty), qself, segment)
775            }
776            QPath::LangItem(..) => {
777                bug!("`resolve_ty_and_res_fully_qualified_call` called on `LangItem`")
778            }
779        };
780
781        self.register_wf_obligation(
782            ty.raw.into(),
783            qself.span,
784            ObligationCauseCode::WellFormed(None),
785        );
786        self.select_obligations_where_possible(|_| {});
787
788        if let Some(&cached_result) = self.typeck_results.borrow().type_dependent_defs().get(hir_id)
789        {
790            // Return directly on cache hit. This is useful to avoid doubly reporting
791            // errors with default match binding modes. See #44614.
792            let def = cached_result.map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id));
793            return (def, Some(ty), slice::from_ref(&**item_segment));
794        }
795        let item_name = item_segment.ident;
796        let result = self
797            .resolve_fully_qualified_call(span, item_name, ty.normalized, qself.span, hir_id)
798            .or_else(|error| {
799                let guar = self
800                    .dcx()
801                    .span_delayed_bug(span, "method resolution should've emitted an error");
802                let result = match error {
803                    method::MethodError::PrivateMatch(kind, def_id, _) => Ok((kind, def_id)),
804                    _ => Err(guar),
805                };
806
807                let trait_missing_method =
808                    matches!(error, method::MethodError::NoMatch(_)) && ty.normalized.is_trait();
809                self.report_method_error(
810                    hir_id,
811                    ty.normalized,
812                    error,
813                    Expectation::NoExpectation,
814                    trait_missing_method && span.edition().at_least_rust_2021(), // emits missing method for trait only after edition 2021
815                );
816
817                result
818            });
819
820        // Write back the new resolution.
821        self.write_resolution(hir_id, result);
822        (
823            result.map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id)),
824            Some(ty),
825            slice::from_ref(&**item_segment),
826        )
827    }
828
829    /// Given a `HirId`, return the `HirId` of the enclosing function and its `FnDecl`.
830    pub(crate) fn get_fn_decl(
831        &self,
832        blk_id: HirId,
833    ) -> Option<(LocalDefId, &'tcx hir::FnDecl<'tcx>)> {
834        // Get enclosing Fn, if it is a function or a trait method, unless there's a `loop` or
835        // `while` before reaching it, as block tail returns are not available in them.
836        self.tcx.hir_get_fn_id_for_return_block(blk_id).and_then(|item_id| {
837            match self.tcx.hir_node(item_id) {
838                Node::Item(&hir::Item {
839                    kind: hir::ItemKind::Fn { sig, .. }, owner_id, ..
840                }) => Some((owner_id.def_id, sig.decl)),
841                Node::TraitItem(&hir::TraitItem {
842                    kind: hir::TraitItemKind::Fn(ref sig, ..),
843                    owner_id,
844                    ..
845                }) => Some((owner_id.def_id, sig.decl)),
846                Node::ImplItem(&hir::ImplItem {
847                    kind: hir::ImplItemKind::Fn(ref sig, ..),
848                    owner_id,
849                    ..
850                }) => Some((owner_id.def_id, sig.decl)),
851                Node::Expr(&hir::Expr {
852                    hir_id,
853                    kind: hir::ExprKind::Closure(&hir::Closure { def_id, kind, fn_decl, .. }),
854                    ..
855                }) => {
856                    match kind {
857                        hir::ClosureKind::CoroutineClosure(_) => {
858                            // FIXME(async_closures): Implement this.
859                            return None;
860                        }
861                        hir::ClosureKind::Closure => Some((def_id, fn_decl)),
862                        hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
863                            _,
864                            hir::CoroutineSource::Fn,
865                        )) => {
866                            let (sig, owner_id) = match self.tcx.parent_hir_node(hir_id) {
867                                Node::Item(&hir::Item {
868                                    kind: hir::ItemKind::Fn { ref sig, .. },
869                                    owner_id,
870                                    ..
871                                }) => (sig, owner_id),
872                                Node::TraitItem(&hir::TraitItem {
873                                    kind: hir::TraitItemKind::Fn(ref sig, ..),
874                                    owner_id,
875                                    ..
876                                }) => (sig, owner_id),
877                                Node::ImplItem(&hir::ImplItem {
878                                    kind: hir::ImplItemKind::Fn(ref sig, ..),
879                                    owner_id,
880                                    ..
881                                }) => (sig, owner_id),
882                                _ => return None,
883                            };
884                            Some((owner_id.def_id, sig.decl))
885                        }
886                        _ => None,
887                    }
888                }
889                _ => None,
890            }
891        })
892    }
893
894    pub(crate) fn note_internal_mutation_in_method(
895        &self,
896        err: &mut Diag<'_>,
897        expr: &hir::Expr<'_>,
898        expected: Option<Ty<'tcx>>,
899        found: Ty<'tcx>,
900    ) {
901        if found != self.tcx.types.unit {
902            return;
903        }
904
905        let ExprKind::MethodCall(path_segment, rcvr, ..) = expr.kind else {
906            return;
907        };
908
909        let rcvr_has_the_expected_type = self
910            .typeck_results
911            .borrow()
912            .expr_ty_adjusted_opt(rcvr)
913            .zip(expected)
914            .is_some_and(|(ty, expected_ty)| expected_ty.peel_refs() == ty.peel_refs());
915
916        let prev_call_mutates_and_returns_unit = || {
917            self.typeck_results
918                .borrow()
919                .type_dependent_def_id(expr.hir_id)
920                .map(|def_id| self.tcx.fn_sig(def_id).skip_binder().skip_binder())
921                .and_then(|sig| sig.inputs_and_output.split_last())
922                .is_some_and(|(output, inputs)| {
923                    output.is_unit()
924                        && inputs
925                            .get(0)
926                            .and_then(|self_ty| self_ty.ref_mutability())
927                            .is_some_and(rustc_ast::Mutability::is_mut)
928                })
929        };
930
931        if !(rcvr_has_the_expected_type || prev_call_mutates_and_returns_unit()) {
932            return;
933        }
934
935        let mut sp = MultiSpan::from_span(path_segment.ident.span);
936        sp.push_span_label(
937            path_segment.ident.span,
938            format!(
939                "this call modifies {} in-place",
940                match rcvr.kind {
941                    ExprKind::Path(QPath::Resolved(
942                        None,
943                        hir::Path { segments: [segment], .. },
944                    )) => format!("`{}`", segment.ident),
945                    _ => "its receiver".to_string(),
946                }
947            ),
948        );
949
950        let modifies_rcvr_note =
951            format!("method `{}` modifies its receiver in-place", path_segment.ident);
952        if rcvr_has_the_expected_type {
953            sp.push_span_label(
954                rcvr.span,
955                "you probably want to use this value after calling the method...",
956            );
957            err.span_note(sp, modifies_rcvr_note);
958            err.note(format!("...instead of the `()` output of method `{}`", path_segment.ident));
959        } else if let ExprKind::MethodCall(..) = rcvr.kind {
960            err.span_note(
961                sp,
962                modifies_rcvr_note + ", it is not meant to be used in method chains.",
963            );
964        } else {
965            err.span_note(sp, modifies_rcvr_note);
966        }
967    }
968
969    // Instantiates the given path, which must refer to an item with the given
970    // number of type parameters and type.
971    #[instrument(skip(self, span), level = "debug")]
972    pub(crate) fn instantiate_value_path(
973        &self,
974        segments: &'tcx [hir::PathSegment<'tcx>],
975        self_ty: Option<LoweredTy<'tcx>>,
976        res: Res,
977        span: Span,
978        path_span: Span,
979        hir_id: HirId,
980    ) -> (Ty<'tcx>, Res) {
981        let tcx = self.tcx;
982
983        let generic_segments = match res {
984            Res::Local(_) | Res::SelfCtor(_) => vec![],
985            Res::Def(kind, def_id) => self.lowerer().probe_generic_path_segments(
986                segments,
987                self_ty.map(|ty| ty.raw),
988                kind,
989                def_id,
990                span,
991            ),
992            Res::Err => {
993                return (
994                    Ty::new_error(
995                        tcx,
996                        tcx.dcx().span_delayed_bug(span, "could not resolve path {:?}"),
997                    ),
998                    res,
999                );
1000            }
1001            _ => bug!("instantiate_value_path on {:?}", res),
1002        };
1003
1004        let mut user_self_ty = None;
1005        let mut is_alias_variant_ctor = false;
1006        let mut err_extend = GenericsArgsErrExtend::None;
1007        match res {
1008            Res::Def(DefKind::Ctor(CtorOf::Variant, _), _) if let Some(self_ty) = self_ty => {
1009                let adt_def = self_ty.normalized.ty_adt_def().unwrap();
1010                user_self_ty =
1011                    Some(UserSelfTy { impl_def_id: adt_def.did(), self_ty: self_ty.raw });
1012                is_alias_variant_ctor = true;
1013                err_extend = GenericsArgsErrExtend::DefVariant(segments);
1014            }
1015            Res::Def(DefKind::Ctor(CtorOf::Variant, _), _) => {
1016                err_extend = GenericsArgsErrExtend::DefVariant(segments);
1017            }
1018            Res::Def(DefKind::AssocFn | DefKind::AssocConst, def_id) => {
1019                let assoc_item = tcx.associated_item(def_id);
1020                let container = assoc_item.container;
1021                let container_id = assoc_item.container_id(tcx);
1022                debug!(?def_id, ?container, ?container_id);
1023                match container {
1024                    ty::AssocContainer::Trait => {
1025                        if let Err(e) = callee::check_legal_trait_for_method_call(
1026                            tcx,
1027                            path_span,
1028                            None,
1029                            span,
1030                            container_id,
1031                            self.body_id.to_def_id(),
1032                        ) {
1033                            self.set_tainted_by_errors(e);
1034                        }
1035                    }
1036                    ty::AssocContainer::InherentImpl | ty::AssocContainer::TraitImpl(_) => {
1037                        if segments.len() == 1 {
1038                            // `<T>::assoc` will end up here, and so
1039                            // can `T::assoc`. If this came from an
1040                            // inherent impl, we need to record the
1041                            // `T` for posterity (see `UserSelfTy` for
1042                            // details).
1043                            let self_ty = self_ty.expect("UFCS sugared assoc missing Self").raw;
1044                            user_self_ty = Some(UserSelfTy { impl_def_id: container_id, self_ty });
1045                        }
1046                    }
1047                }
1048            }
1049            _ => {}
1050        }
1051
1052        // Now that we have categorized what space the parameters for each
1053        // segment belong to, let's sort out the parameters that the user
1054        // provided (if any) into their appropriate spaces. We'll also report
1055        // errors if type parameters are provided in an inappropriate place.
1056
1057        let indices: FxHashSet<_> =
1058            generic_segments.iter().map(|GenericPathSegment(_, index)| index).collect();
1059        let generics_err = self.lowerer().prohibit_generic_args(
1060            segments.iter().enumerate().filter_map(|(index, seg)| {
1061                if !indices.contains(&index) || is_alias_variant_ctor { Some(seg) } else { None }
1062            }),
1063            err_extend,
1064        );
1065
1066        if let Res::Local(hid) = res {
1067            let ty = self.local_ty(span, hid);
1068            let ty = self.normalize(span, ty);
1069            return (ty, res);
1070        }
1071
1072        if let Err(_) = generics_err {
1073            // Don't try to infer type parameters when prohibited generic arguments were given.
1074            user_self_ty = None;
1075        }
1076
1077        // Now we have to compare the types that the user *actually*
1078        // provided against the types that were *expected*. If the user
1079        // did not provide any types, then we want to instantiate inference
1080        // variables. If the user provided some types, we may still need
1081        // to add defaults. If the user provided *too many* types, that's
1082        // a problem.
1083
1084        let mut infer_args_for_err = None;
1085
1086        let mut explicit_late_bound = ExplicitLateBound::No;
1087        for &GenericPathSegment(def_id, index) in &generic_segments {
1088            let seg = &segments[index];
1089            let generics = tcx.generics_of(def_id);
1090
1091            // Argument-position `impl Trait` is treated as a normal generic
1092            // parameter internally, but we don't allow users to specify the
1093            // parameter's value explicitly, so we have to do some error-
1094            // checking here.
1095            let arg_count =
1096                check_generic_arg_count_for_call(self, def_id, generics, seg, IsMethodCall::No);
1097
1098            if let ExplicitLateBound::Yes = arg_count.explicit_late_bound {
1099                explicit_late_bound = ExplicitLateBound::Yes;
1100            }
1101
1102            if let Err(GenericArgCountMismatch { reported, .. }) = arg_count.correct {
1103                infer_args_for_err
1104                    .get_or_insert_with(|| (reported, FxHashSet::default()))
1105                    .1
1106                    .insert(index);
1107                self.set_tainted_by_errors(reported); // See issue #53251.
1108            }
1109        }
1110
1111        let has_self = generic_segments
1112            .last()
1113            .is_some_and(|GenericPathSegment(def_id, _)| tcx.generics_of(*def_id).has_self);
1114
1115        let (res, implicit_args) = if let Res::Def(DefKind::ConstParam, def) = res {
1116            // types of const parameters are somewhat special as they are part of
1117            // the same environment as the const parameter itself. this means that
1118            // unlike most paths `type-of(N)` can return a type naming parameters
1119            // introduced by the containing item, rather than provided through `N`.
1120            //
1121            // for example given `<T, const M: usize, const N: [T; M]>` and some
1122            // `let a = N;` expression. The path to `N` would wind up with no args
1123            // (as it has no args), but instantiating the early binder on `typeof(N)`
1124            // requires providing generic arguments for `[T, M, N]`.
1125            (res, Some(ty::GenericArgs::identity_for_item(tcx, tcx.parent(def))))
1126        } else if let Res::SelfCtor(impl_def_id) = res {
1127            let ty = LoweredTy::from_raw(
1128                self,
1129                span,
1130                tcx.at(span).type_of(impl_def_id).instantiate_identity(),
1131            );
1132
1133            // Firstly, check that this SelfCtor even comes from the item we're currently
1134            // typechecking. This can happen because we never validated the resolution of
1135            // SelfCtors, and when we started doing so, we noticed regressions. After
1136            // sufficiently long time, we can remove this check and turn it into a hard
1137            // error in `validate_res_from_ribs` -- it's just difficult to tell whether the
1138            // self type has any generic types during rustc_resolve, which is what we use
1139            // to determine if this is a hard error or warning.
1140            if std::iter::successors(Some(self.body_id.to_def_id()), |def_id| {
1141                self.tcx.generics_of(def_id).parent
1142            })
1143            .all(|def_id| def_id != impl_def_id)
1144            {
1145                let sugg = ty.normalized.ty_adt_def().map(|def| errors::ReplaceWithName {
1146                    span: path_span,
1147                    name: self.tcx.item_name(def.did()).to_ident_string(),
1148                });
1149                if ty.raw.has_param() {
1150                    let guar = self.dcx().emit_err(errors::SelfCtorFromOuterItem {
1151                        span: path_span,
1152                        impl_span: tcx.def_span(impl_def_id),
1153                        sugg,
1154                    });
1155                    return (Ty::new_error(self.tcx, guar), res);
1156                } else {
1157                    self.tcx.emit_node_span_lint(
1158                        SELF_CONSTRUCTOR_FROM_OUTER_ITEM,
1159                        hir_id,
1160                        path_span,
1161                        errors::SelfCtorFromOuterItemLint {
1162                            impl_span: tcx.def_span(impl_def_id),
1163                            sugg,
1164                        },
1165                    );
1166                }
1167            }
1168
1169            match ty.normalized.ty_adt_def() {
1170                Some(adt_def) if adt_def.has_ctor() => {
1171                    let (ctor_kind, ctor_def_id) = adt_def.non_enum_variant().ctor.unwrap();
1172                    // Check the visibility of the ctor.
1173                    let vis = tcx.visibility(ctor_def_id);
1174                    if !vis.is_accessible_from(tcx.parent_module(hir_id).to_def_id(), tcx) {
1175                        self.dcx()
1176                            .emit_err(CtorIsPrivate { span, def: tcx.def_path_str(adt_def.did()) });
1177                    }
1178                    let new_res = Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
1179                    let user_args = Self::user_args_for_adt(ty);
1180                    user_self_ty = user_args.user_self_ty;
1181                    (new_res, Some(user_args.args))
1182                }
1183                _ => {
1184                    let mut err = self.dcx().struct_span_err(
1185                        span,
1186                        "the `Self` constructor can only be used with tuple or unit structs",
1187                    );
1188                    if let Some(adt_def) = ty.normalized.ty_adt_def() {
1189                        match adt_def.adt_kind() {
1190                            AdtKind::Enum => {
1191                                err.help("did you mean to use one of the enum's variants?");
1192                            }
1193                            AdtKind::Struct | AdtKind::Union => {
1194                                err.span_suggestion(
1195                                    span,
1196                                    "use curly brackets",
1197                                    "Self { /* fields */ }",
1198                                    Applicability::HasPlaceholders,
1199                                );
1200                            }
1201                        }
1202                    }
1203                    let reported = err.emit();
1204                    return (Ty::new_error(tcx, reported), res);
1205                }
1206            }
1207        } else {
1208            (res, None)
1209        };
1210        let def_id = res.def_id();
1211
1212        let (correct, infer_args_for_err) = match infer_args_for_err {
1213            Some((reported, args)) => {
1214                (Err(GenericArgCountMismatch { reported, invalid_args: vec![] }), args)
1215            }
1216            None => (Ok(()), Default::default()),
1217        };
1218
1219        let arg_count = GenericArgCountResult { explicit_late_bound, correct };
1220
1221        struct CtorGenericArgsCtxt<'a, 'tcx> {
1222            fcx: &'a FnCtxt<'a, 'tcx>,
1223            span: Span,
1224            generic_segments: &'a [GenericPathSegment],
1225            infer_args_for_err: &'a FxHashSet<usize>,
1226            segments: &'tcx [hir::PathSegment<'tcx>],
1227        }
1228        impl<'a, 'tcx> GenericArgsLowerer<'a, 'tcx> for CtorGenericArgsCtxt<'a, 'tcx> {
1229            fn args_for_def_id(
1230                &mut self,
1231                def_id: DefId,
1232            ) -> (Option<&'a hir::GenericArgs<'tcx>>, bool) {
1233                if let Some(&GenericPathSegment(_, index)) =
1234                    self.generic_segments.iter().find(|&GenericPathSegment(did, _)| *did == def_id)
1235                {
1236                    // If we've encountered an `impl Trait`-related error, we're just
1237                    // going to infer the arguments for better error messages.
1238                    if !self.infer_args_for_err.contains(&index) {
1239                        // Check whether the user has provided generic arguments.
1240                        if let Some(data) = self.segments[index].args {
1241                            return (Some(data), self.segments[index].infer_args);
1242                        }
1243                    }
1244                    return (None, self.segments[index].infer_args);
1245                }
1246
1247                (None, true)
1248            }
1249
1250            fn provided_kind(
1251                &mut self,
1252                preceding_args: &[ty::GenericArg<'tcx>],
1253                param: &ty::GenericParamDef,
1254                arg: &GenericArg<'tcx>,
1255            ) -> ty::GenericArg<'tcx> {
1256                match (&param.kind, arg) {
1257                    (GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => self
1258                        .fcx
1259                        .lowerer()
1260                        .lower_lifetime(lt, RegionInferReason::Param(param))
1261                        .into(),
1262                    (GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
1263                        // We handle the ambig portions of `Ty` in match arm below
1264                        self.fcx.lower_ty(ty.as_unambig_ty()).raw.into()
1265                    }
1266                    (GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
1267                        self.fcx.lower_ty(&inf.to_ty()).raw.into()
1268                    }
1269                    (GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => self
1270                        .fcx
1271                        // Ambiguous parts of `ConstArg` are handled in the match arms below
1272                        .lower_const_arg(
1273                            ct.as_unambig_ct(),
1274                            FeedConstTy::Param(param.def_id, preceding_args),
1275                        )
1276                        .into(),
1277                    (&GenericParamDefKind::Const { .. }, GenericArg::Infer(inf)) => {
1278                        self.fcx.ct_infer(Some(param), inf.span).into()
1279                    }
1280                    _ => unreachable!(),
1281                }
1282            }
1283
1284            fn inferred_kind(
1285                &mut self,
1286                preceding_args: &[ty::GenericArg<'tcx>],
1287                param: &ty::GenericParamDef,
1288                infer_args: bool,
1289            ) -> ty::GenericArg<'tcx> {
1290                let tcx = self.fcx.tcx();
1291                if !infer_args && let Some(default) = param.default_value(tcx) {
1292                    // If we have a default, then it doesn't matter that we're not inferring
1293                    // the type/const arguments: We provide the default where any is missing.
1294                    return default.instantiate(tcx, preceding_args);
1295                }
1296                // If no type/const arguments were provided, we have to infer them.
1297                // This case also occurs as a result of some malformed input, e.g.,
1298                // a lifetime argument being given instead of a type/const parameter.
1299                // Using inference instead of `Error` gives better error messages.
1300                self.fcx.var_for_def(self.span, param)
1301            }
1302        }
1303
1304        let args_raw = implicit_args.unwrap_or_else(|| {
1305            lower_generic_args(
1306                self,
1307                def_id,
1308                &[],
1309                has_self,
1310                self_ty.map(|s| s.raw),
1311                &arg_count,
1312                &mut CtorGenericArgsCtxt {
1313                    fcx: self,
1314                    span,
1315                    generic_segments: &generic_segments,
1316                    infer_args_for_err: &infer_args_for_err,
1317                    segments,
1318                },
1319            )
1320        });
1321
1322        // First, store the "user args" for later.
1323        self.write_user_type_annotation_from_args(hir_id, def_id, args_raw, user_self_ty);
1324
1325        // Normalize only after registering type annotations.
1326        let args = self.normalize(span, args_raw);
1327
1328        self.add_required_obligations_for_hir(span, def_id, args, hir_id);
1329
1330        // Instantiate the values for the type parameters into the type of
1331        // the referenced item.
1332        let ty = tcx.type_of(def_id);
1333        assert!(!args.has_escaping_bound_vars());
1334        assert!(!ty.skip_binder().has_escaping_bound_vars());
1335        let ty_instantiated = self.normalize(span, ty.instantiate(tcx, args));
1336
1337        if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty {
1338            // In the case of `Foo<T>::method` and `<Foo<T>>::method`, if `method`
1339            // is inherent, there is no `Self` parameter; instead, the impl needs
1340            // type parameters, which we can infer by unifying the provided `Self`
1341            // with the instantiated impl type.
1342            // This also occurs for an enum variant on a type alias.
1343            let impl_ty = self.normalize(span, tcx.type_of(impl_def_id).instantiate(tcx, args));
1344            let self_ty = self.normalize(span, self_ty);
1345            match self.at(&self.misc(span), self.param_env).eq(
1346                DefineOpaqueTypes::Yes,
1347                impl_ty,
1348                self_ty,
1349            ) {
1350                Ok(ok) => self.register_infer_ok_obligations(ok),
1351                Err(_) => {
1352                    self.dcx().span_bug(
1353                        span,
1354                        format!(
1355                            "instantiate_value_path: (UFCS) {self_ty:?} was a subtype of {impl_ty:?} but now is not?",
1356                        ),
1357                    );
1358                }
1359            }
1360        }
1361
1362        debug!("instantiate_value_path: type of {:?} is {:?}", hir_id, ty_instantiated);
1363        self.write_args(hir_id, args);
1364
1365        (ty_instantiated, res)
1366    }
1367
1368    /// Add all the obligations that are required, instantiated and normalized appropriately.
1369    pub(crate) fn add_required_obligations_for_hir(
1370        &self,
1371        span: Span,
1372        def_id: DefId,
1373        args: GenericArgsRef<'tcx>,
1374        hir_id: HirId,
1375    ) {
1376        self.add_required_obligations_with_code(span, def_id, args, |idx, span| {
1377            ObligationCauseCode::WhereClauseInExpr(def_id, span, hir_id, idx)
1378        })
1379    }
1380
1381    #[instrument(level = "debug", skip(self, code, span, args))]
1382    fn add_required_obligations_with_code(
1383        &self,
1384        span: Span,
1385        def_id: DefId,
1386        args: GenericArgsRef<'tcx>,
1387        code: impl Fn(usize, Span) -> ObligationCauseCode<'tcx>,
1388    ) {
1389        let param_env = self.param_env;
1390
1391        let bounds = self.instantiate_bounds(span, def_id, args);
1392
1393        for obligation in traits::predicates_for_generics(
1394            |idx, predicate_span| self.cause(span, code(idx, predicate_span)),
1395            param_env,
1396            bounds,
1397        ) {
1398            self.register_predicate(obligation);
1399        }
1400    }
1401
1402    /// Try to resolve `ty` to a structural type, normalizing aliases.
1403    ///
1404    /// In case there is still ambiguity, the returned type may be an inference
1405    /// variable. This is different from `structurally_resolve_type` which errors
1406    /// in this case.
1407    #[instrument(level = "debug", skip(self, sp), ret)]
1408    pub(crate) fn try_structurally_resolve_type(&self, sp: Span, ty: Ty<'tcx>) -> Ty<'tcx> {
1409        if self.next_trait_solver()
1410            && let ty::Alias(..) = ty.kind()
1411        {
1412            // We need to use a separate variable here as otherwise the temporary for
1413            // `self.fulfillment_cx.borrow_mut()` is alive in the `Err` branch, resulting
1414            // in a reentrant borrow, causing an ICE.
1415            let result = self
1416                .at(&self.misc(sp), self.param_env)
1417                .structurally_normalize_ty(ty, &mut **self.fulfillment_cx.borrow_mut());
1418            match result {
1419                Ok(normalized_ty) => normalized_ty,
1420                Err(errors) => {
1421                    let guar = self.err_ctxt().report_fulfillment_errors(errors);
1422                    return Ty::new_error(self.tcx, guar);
1423                }
1424            }
1425        } else {
1426            self.resolve_vars_with_obligations(ty)
1427        }
1428    }
1429
1430    #[instrument(level = "debug", skip(self, sp), ret)]
1431    pub(crate) fn try_structurally_resolve_const(
1432        &self,
1433        sp: Span,
1434        ct: ty::Const<'tcx>,
1435    ) -> ty::Const<'tcx> {
1436        let ct = self.resolve_vars_with_obligations(ct);
1437
1438        if self.next_trait_solver()
1439            && let ty::ConstKind::Unevaluated(..) = ct.kind()
1440        {
1441            // We need to use a separate variable here as otherwise the temporary for
1442            // `self.fulfillment_cx.borrow_mut()` is alive in the `Err` branch, resulting
1443            // in a reentrant borrow, causing an ICE.
1444            let result = self
1445                .at(&self.misc(sp), self.param_env)
1446                .structurally_normalize_const(ct, &mut **self.fulfillment_cx.borrow_mut());
1447            match result {
1448                Ok(normalized_ct) => normalized_ct,
1449                Err(errors) => {
1450                    let guar = self.err_ctxt().report_fulfillment_errors(errors);
1451                    return ty::Const::new_error(self.tcx, guar);
1452                }
1453            }
1454        } else if self.tcx.features().generic_const_exprs() {
1455            rustc_trait_selection::traits::evaluate_const(&self.infcx, ct, self.param_env)
1456        } else {
1457            ct
1458        }
1459    }
1460
1461    /// Resolves `ty` by a single level if `ty` is a type variable.
1462    ///
1463    /// When the new solver is enabled, this will also attempt to normalize
1464    /// the type if it's a projection (note that it will not deeply normalize
1465    /// projections within the type, just the outermost layer of the type).
1466    ///
1467    /// If no resolution is possible, then an error is reported.
1468    /// Numeric inference variables may be left unresolved.
1469    pub(crate) fn structurally_resolve_type(&self, sp: Span, ty: Ty<'tcx>) -> Ty<'tcx> {
1470        let ty = self.try_structurally_resolve_type(sp, ty);
1471
1472        if !ty.is_ty_var() {
1473            ty
1474        } else {
1475            let e = self.tainted_by_errors().unwrap_or_else(|| {
1476                self.err_ctxt()
1477                    .emit_inference_failure_err(
1478                        self.body_id,
1479                        sp,
1480                        ty.into(),
1481                        TypeAnnotationNeeded::E0282,
1482                        true,
1483                    )
1484                    .emit()
1485            });
1486            let err = Ty::new_error(self.tcx, e);
1487            self.demand_suptype(sp, err, ty);
1488            err
1489        }
1490    }
1491
1492    pub(crate) fn structurally_resolve_const(
1493        &self,
1494        sp: Span,
1495        ct: ty::Const<'tcx>,
1496    ) -> ty::Const<'tcx> {
1497        let ct = self.try_structurally_resolve_const(sp, ct);
1498
1499        if !ct.is_ct_infer() {
1500            ct
1501        } else {
1502            let e = self.tainted_by_errors().unwrap_or_else(|| {
1503                self.err_ctxt()
1504                    .emit_inference_failure_err(
1505                        self.body_id,
1506                        sp,
1507                        ct.into(),
1508                        TypeAnnotationNeeded::E0282,
1509                        true,
1510                    )
1511                    .emit()
1512            });
1513            // FIXME: Infer `?ct = {const error}`?
1514            ty::Const::new_error(self.tcx, e)
1515        }
1516    }
1517
1518    pub(crate) fn with_breakable_ctxt<F: FnOnce() -> R, R>(
1519        &self,
1520        id: HirId,
1521        ctxt: BreakableCtxt<'tcx>,
1522        f: F,
1523    ) -> (BreakableCtxt<'tcx>, R) {
1524        let index;
1525        {
1526            let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
1527            index = enclosing_breakables.stack.len();
1528            enclosing_breakables.by_id.insert(id, index);
1529            enclosing_breakables.stack.push(ctxt);
1530        }
1531        let result = f();
1532        let ctxt = {
1533            let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
1534            debug_assert!(enclosing_breakables.stack.len() == index + 1);
1535            // FIXME(#120456) - is `swap_remove` correct?
1536            enclosing_breakables.by_id.swap_remove(&id).expect("missing breakable context");
1537            enclosing_breakables.stack.pop().expect("missing breakable context")
1538        };
1539        (ctxt, result)
1540    }
1541
1542    /// Instantiate a QueryResponse in a probe context, without a
1543    /// good ObligationCause.
1544    pub(crate) fn probe_instantiate_query_response(
1545        &self,
1546        span: Span,
1547        original_values: &OriginalQueryValues<'tcx>,
1548        query_result: &Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>,
1549    ) -> InferResult<'tcx, Ty<'tcx>> {
1550        self.instantiate_query_response_and_region_obligations(
1551            &self.misc(span),
1552            self.param_env,
1553            original_values,
1554            query_result,
1555        )
1556    }
1557
1558    /// Returns `true` if an expression is contained inside the LHS of an assignment expression.
1559    pub(crate) fn expr_in_place(&self, mut expr_id: HirId) -> bool {
1560        let mut contained_in_place = false;
1561
1562        while let hir::Node::Expr(parent_expr) = self.tcx.parent_hir_node(expr_id) {
1563            match &parent_expr.kind {
1564                hir::ExprKind::Assign(lhs, ..) | hir::ExprKind::AssignOp(_, lhs, ..) => {
1565                    if lhs.hir_id == expr_id {
1566                        contained_in_place = true;
1567                        break;
1568                    }
1569                }
1570                _ => (),
1571            }
1572            expr_id = parent_expr.hir_id;
1573        }
1574
1575        contained_in_place
1576    }
1577}