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