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