rustc_hir_typeck/
expr.rs

1// ignore-tidy-filelength
2// FIXME: we should move the field error reporting code somewhere else.
3
4//! Type checking expressions.
5//!
6//! See [`rustc_hir_analysis::check`] for more context on type checking in general.
7
8use rustc_abi::{FIRST_VARIANT, FieldIdx};
9use rustc_ast::util::parser::ExprPrecedence;
10use rustc_data_structures::fx::{FxHashMap, FxHashSet};
11use rustc_data_structures::stack::ensure_sufficient_stack;
12use rustc_data_structures::unord::UnordMap;
13use rustc_errors::codes::*;
14use rustc_errors::{
15    Applicability, Diag, ErrorGuaranteed, MultiSpan, StashKey, Subdiagnostic, listify, pluralize,
16    struct_span_code_err,
17};
18use rustc_hir::attrs::AttributeKind;
19use rustc_hir::def::{CtorKind, DefKind, Res};
20use rustc_hir::def_id::DefId;
21use rustc_hir::lang_items::LangItem;
22use rustc_hir::{ExprKind, HirId, QPath, find_attr};
23use rustc_hir_analysis::NoVariantNamed;
24use rustc_hir_analysis::hir_ty_lowering::{FeedConstTy, HirTyLowerer as _};
25use rustc_infer::infer::{self, DefineOpaqueTypes, InferOk, RegionVariableOrigin};
26use rustc_infer::traits::query::NoSolution;
27use rustc_middle::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase};
28use rustc_middle::ty::error::{ExpectedFound, TypeError};
29use rustc_middle::ty::{self, AdtKind, GenericArgsRef, Ty, TypeVisitableExt};
30use rustc_middle::{bug, span_bug};
31use rustc_session::errors::ExprParenthesesNeeded;
32use rustc_session::parse::feature_err;
33use rustc_span::edit_distance::find_best_match_for_name;
34use rustc_span::hygiene::DesugaringKind;
35use rustc_span::source_map::Spanned;
36use rustc_span::{Ident, Span, Symbol, kw, sym};
37use rustc_trait_selection::infer::InferCtxtExt;
38use rustc_trait_selection::traits::{self, ObligationCauseCode, ObligationCtxt};
39use tracing::{debug, instrument, trace};
40use {rustc_ast as ast, rustc_hir as hir};
41
42use crate::Expectation::{self, ExpectCastableToType, ExpectHasType, NoExpectation};
43use crate::coercion::{CoerceMany, DynamicCoerceMany};
44use crate::errors::{
45    AddressOfTemporaryTaken, BaseExpressionDoubleDot, BaseExpressionDoubleDotAddExpr,
46    BaseExpressionDoubleDotRemove, CantDereference, FieldMultiplySpecifiedInInitializer,
47    FunctionalRecordUpdateOnNonStruct, HelpUseLatestEdition, NakedAsmOutsideNakedFn, NoFieldOnType,
48    NoFieldOnVariant, ReturnLikeStatementKind, ReturnStmtOutsideOfFnBody, StructExprNonExhaustive,
49    TypeMismatchFruTypo, YieldExprOutsideOfCoroutine,
50};
51use crate::{
52    BreakableCtxt, CoroutineTypes, Diverges, FnCtxt, GatherLocalsVisitor, Needs,
53    TupleArgumentsFlag, cast, fatally_break_rust, report_unexpected_variant_res, type_error_struct,
54};
55
56impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
57    pub(crate) fn precedence(&self, expr: &hir::Expr<'_>) -> ExprPrecedence {
58        let has_attr = |id: HirId| -> bool {
59            for attr in self.tcx.hir_attrs(id) {
60                // For the purpose of rendering suggestions, disregard attributes
61                // that originate from desugaring of any kind. For example, `x?`
62                // desugars to `#[allow(unreachable_code)] match ...`. Failing to
63                // ignore the prefix attribute in the desugaring would cause this
64                // suggestion:
65                //
66                //     let y: u32 = x?.try_into().unwrap();
67                //                    ++++++++++++++++++++
68                //
69                // to be rendered as:
70                //
71                //     let y: u32 = (x?).try_into().unwrap();
72                //                  +  +++++++++++++++++++++
73                if attr.span().desugaring_kind().is_none() {
74                    return true;
75                }
76            }
77            false
78        };
79        expr.precedence(&has_attr)
80    }
81
82    /// Check an expr with an expectation type, and also demand that the expr's
83    /// evaluated type is a subtype of the expectation at the end. This is a
84    /// *hard* requirement.
85    pub(crate) fn check_expr_has_type_or_error(
86        &self,
87        expr: &'tcx hir::Expr<'tcx>,
88        expected_ty: Ty<'tcx>,
89        extend_err: impl FnOnce(&mut Diag<'_>),
90    ) -> Ty<'tcx> {
91        let mut ty = self.check_expr_with_expectation(expr, ExpectHasType(expected_ty));
92
93        // While we don't allow *arbitrary* coercions here, we *do* allow
94        // coercions from ! to `expected`.
95        if self.try_structurally_resolve_type(expr.span, ty).is_never()
96            && self.expr_guaranteed_to_constitute_read_for_never(expr)
97        {
98            if let Some(adjustments) = self.typeck_results.borrow().adjustments().get(expr.hir_id) {
99                let reported = self.dcx().span_delayed_bug(
100                    expr.span,
101                    "expression with never type wound up being adjusted",
102                );
103
104                return if let [Adjustment { kind: Adjust::NeverToAny, target }] = &adjustments[..] {
105                    target.to_owned()
106                } else {
107                    Ty::new_error(self.tcx(), reported)
108                };
109            }
110
111            let adj_ty = self.next_ty_var(expr.span);
112            self.apply_adjustments(
113                expr,
114                vec![Adjustment { kind: Adjust::NeverToAny, target: adj_ty }],
115            );
116            ty = adj_ty;
117        }
118
119        if let Err(mut err) = self.demand_suptype_diag(expr.span, expected_ty, ty) {
120            let _ = self.emit_type_mismatch_suggestions(
121                &mut err,
122                expr.peel_drop_temps(),
123                ty,
124                expected_ty,
125                None,
126                None,
127            );
128            extend_err(&mut err);
129            err.emit();
130        }
131        ty
132    }
133
134    /// Check an expr with an expectation type, and also demand that the expr's
135    /// evaluated type is a coercible to the expectation at the end. This is a
136    /// *hard* requirement.
137    pub(super) fn check_expr_coercible_to_type(
138        &self,
139        expr: &'tcx hir::Expr<'tcx>,
140        expected: Ty<'tcx>,
141        expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
142    ) -> Ty<'tcx> {
143        self.check_expr_coercible_to_type_or_error(expr, expected, expected_ty_expr, |_, _| {})
144    }
145
146    pub(crate) fn check_expr_coercible_to_type_or_error(
147        &self,
148        expr: &'tcx hir::Expr<'tcx>,
149        expected: Ty<'tcx>,
150        expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
151        extend_err: impl FnOnce(&mut Diag<'_>, Ty<'tcx>),
152    ) -> Ty<'tcx> {
153        let ty = self.check_expr_with_hint(expr, expected);
154        // checks don't need two phase
155        match self.demand_coerce_diag(expr, ty, expected, expected_ty_expr, AllowTwoPhase::No) {
156            Ok(ty) => ty,
157            Err(mut err) => {
158                extend_err(&mut err, ty);
159                err.emit();
160                // Return the original type instead of an error type here, otherwise the type of `x` in
161                // `let x: u32 = ();` will be a type error, causing all subsequent usages of `x` to not
162                // report errors, even though `x` is definitely `u32`.
163                expected
164            }
165        }
166    }
167
168    /// Check an expr with an expectation type. Don't actually enforce that expectation
169    /// is related to the expr's evaluated type via subtyping or coercion. This is
170    /// usually called because we want to do that subtype/coerce call manually for better
171    /// diagnostics.
172    pub(super) fn check_expr_with_hint(
173        &self,
174        expr: &'tcx hir::Expr<'tcx>,
175        expected: Ty<'tcx>,
176    ) -> Ty<'tcx> {
177        self.check_expr_with_expectation(expr, ExpectHasType(expected))
178    }
179
180    /// Check an expr with an expectation type, and also [`Needs`] which will
181    /// prompt typeck to convert any implicit immutable derefs to mutable derefs.
182    fn check_expr_with_expectation_and_needs(
183        &self,
184        expr: &'tcx hir::Expr<'tcx>,
185        expected: Expectation<'tcx>,
186        needs: Needs,
187    ) -> Ty<'tcx> {
188        let ty = self.check_expr_with_expectation(expr, expected);
189
190        // If the expression is used in a place whether mutable place is required
191        // e.g. LHS of assignment, perform the conversion.
192        if let Needs::MutPlace = needs {
193            self.convert_place_derefs_to_mutable(expr);
194        }
195
196        ty
197    }
198
199    /// Check an expr with no expectations.
200    pub(super) fn check_expr(&self, expr: &'tcx hir::Expr<'tcx>) -> Ty<'tcx> {
201        self.check_expr_with_expectation(expr, NoExpectation)
202    }
203
204    /// Check an expr with no expectations, but with [`Needs`] which will
205    /// prompt typeck to convert any implicit immutable derefs to mutable derefs.
206    pub(super) fn check_expr_with_needs(
207        &self,
208        expr: &'tcx hir::Expr<'tcx>,
209        needs: Needs,
210    ) -> Ty<'tcx> {
211        self.check_expr_with_expectation_and_needs(expr, NoExpectation, needs)
212    }
213
214    /// Check an expr with an expectation type which may be used to eagerly
215    /// guide inference when evaluating that expr.
216    #[instrument(skip(self, expr), level = "debug")]
217    pub(super) fn check_expr_with_expectation(
218        &self,
219        expr: &'tcx hir::Expr<'tcx>,
220        expected: Expectation<'tcx>,
221    ) -> Ty<'tcx> {
222        self.check_expr_with_expectation_and_args(expr, expected, None)
223    }
224
225    /// Same as [`Self::check_expr_with_expectation`], but allows us to pass in
226    /// the arguments of a [`ExprKind::Call`] when evaluating its callee that
227    /// is an [`ExprKind::Path`]. We use this to refine the spans for certain
228    /// well-formedness guarantees for the path expr.
229    pub(super) fn check_expr_with_expectation_and_args(
230        &self,
231        expr: &'tcx hir::Expr<'tcx>,
232        expected: Expectation<'tcx>,
233        call_expr_and_args: Option<(&'tcx hir::Expr<'tcx>, &'tcx [hir::Expr<'tcx>])>,
234    ) -> Ty<'tcx> {
235        if self.tcx().sess.verbose_internals() {
236            // make this code only run with -Zverbose-internals because it is probably slow
237            if let Ok(lint_str) = self.tcx.sess.source_map().span_to_snippet(expr.span) {
238                if !lint_str.contains('\n') {
239                    debug!("expr text: {lint_str}");
240                } else {
241                    let mut lines = lint_str.lines();
242                    if let Some(line0) = lines.next() {
243                        let remaining_lines = lines.count();
244                        debug!("expr text: {line0}");
245                        debug!("expr text: ...(and {remaining_lines} more lines)");
246                    }
247                }
248            }
249        }
250
251        // True if `expr` is a `Try::from_ok(())` that is a result of desugaring a try block
252        // without the final expr (e.g. `try { return; }`). We don't want to generate an
253        // unreachable_code lint for it since warnings for autogenerated code are confusing.
254        let is_try_block_generated_unit_expr = match expr.kind {
255            ExprKind::Call(_, [arg]) => {
256                expr.span.is_desugaring(DesugaringKind::TryBlock)
257                    && arg.span.is_desugaring(DesugaringKind::TryBlock)
258            }
259            _ => false,
260        };
261
262        // Warn for expressions after diverging siblings.
263        if !is_try_block_generated_unit_expr {
264            self.warn_if_unreachable(expr.hir_id, expr.span, "expression");
265        }
266
267        // Whether a past expression diverges doesn't affect typechecking of this expression, so we
268        // reset `diverges` while checking `expr`.
269        let old_diverges = self.diverges.replace(Diverges::Maybe);
270
271        if self.is_whole_body.replace(false) {
272            // If this expression is the whole body and the function diverges because of its
273            // arguments, we check this here to ensure the body is considered to diverge.
274            self.diverges.set(self.function_diverges_because_of_empty_arguments.get())
275        };
276
277        let ty = ensure_sufficient_stack(|| match &expr.kind {
278            // Intercept the callee path expr and give it better spans.
279            hir::ExprKind::Path(
280                qpath @ (hir::QPath::Resolved(..) | hir::QPath::TypeRelative(..)),
281            ) => self.check_expr_path(qpath, expr, call_expr_and_args),
282            _ => self.check_expr_kind(expr, expected),
283        });
284        let ty = self.resolve_vars_if_possible(ty);
285
286        // Warn for non-block expressions with diverging children.
287        match expr.kind {
288            ExprKind::Block(..)
289            | ExprKind::If(..)
290            | ExprKind::Let(..)
291            | ExprKind::Loop(..)
292            | ExprKind::Match(..) => {}
293            // Do not warn on `as` casts from never to any,
294            // they are sometimes required to appeal typeck.
295            ExprKind::Cast(_, _) => {}
296            // If `expr` is a result of desugaring the try block and is an ok-wrapped
297            // diverging expression (e.g. it arose from desugaring of `try { return }`),
298            // we skip issuing a warning because it is autogenerated code.
299            ExprKind::Call(..) if expr.span.is_desugaring(DesugaringKind::TryBlock) => {}
300            // Likewise, do not lint unreachable code injected via contracts desugaring.
301            ExprKind::Call(..) if expr.span.is_desugaring(DesugaringKind::Contract) => {}
302            ExprKind::Call(callee, _) => self.warn_if_unreachable(expr.hir_id, callee.span, "call"),
303            ExprKind::MethodCall(segment, ..) => {
304                self.warn_if_unreachable(expr.hir_id, segment.ident.span, "call")
305            }
306            _ => self.warn_if_unreachable(expr.hir_id, expr.span, "expression"),
307        }
308
309        // Any expression that produces a value of type `!` must have diverged,
310        // unless it's a place expression that isn't being read from, in which case
311        // diverging would be unsound since we may never actually read the `!`.
312        // e.g. `let _ = *never_ptr;` with `never_ptr: *const !`.
313        if self.try_structurally_resolve_type(expr.span, ty).is_never()
314            && self.expr_guaranteed_to_constitute_read_for_never(expr)
315        {
316            self.diverges.set(self.diverges.get() | Diverges::always(expr.span));
317        }
318
319        // Record the type, which applies it effects.
320        // We need to do this after the warning above, so that
321        // we don't warn for the diverging expression itself.
322        self.write_ty(expr.hir_id, ty);
323
324        // Combine the diverging and has_error flags.
325        self.diverges.set(self.diverges.get() | old_diverges);
326
327        debug!("type of {} is...", self.tcx.hir_id_to_string(expr.hir_id));
328        debug!("... {:?}, expected is {:?}", ty, expected);
329
330        ty
331    }
332
333    /// Whether this expression constitutes a read of value of the type that
334    /// it evaluates to.
335    ///
336    /// This is used to determine if we should consider the block to diverge
337    /// if the expression evaluates to `!`, and if we should insert a `NeverToAny`
338    /// coercion for values of type `!`.
339    ///
340    /// This function generally returns `false` if the expression is a place
341    /// expression and the *parent* expression is the scrutinee of a match or
342    /// the pointee of an `&` addr-of expression, since both of those parent
343    /// expressions take a *place* and not a value.
344    pub(super) fn expr_guaranteed_to_constitute_read_for_never(
345        &self,
346        expr: &'tcx hir::Expr<'tcx>,
347    ) -> bool {
348        // We only care about place exprs. Anything else returns an immediate
349        // which would constitute a read. We don't care about distinguishing
350        // "syntactic" place exprs since if the base of a field projection is
351        // not a place then it would've been UB to read from it anyways since
352        // that constitutes a read.
353        if !expr.is_syntactic_place_expr() {
354            return true;
355        }
356
357        let parent_node = self.tcx.parent_hir_node(expr.hir_id);
358        match parent_node {
359            hir::Node::Expr(parent_expr) => {
360                match parent_expr.kind {
361                    // Addr-of, field projections, and LHS of assignment don't constitute reads.
362                    // Assignment does call `drop_in_place`, though, but its safety
363                    // requirements are not the same.
364                    ExprKind::AddrOf(..) | hir::ExprKind::Field(..) => false,
365
366                    // Place-preserving expressions only constitute reads if their
367                    // parent expression constitutes a read.
368                    ExprKind::Type(..) | ExprKind::UnsafeBinderCast(..) => {
369                        self.expr_guaranteed_to_constitute_read_for_never(expr)
370                    }
371
372                    ExprKind::Assign(lhs, _, _) => {
373                        // Only the LHS does not constitute a read
374                        expr.hir_id != lhs.hir_id
375                    }
376
377                    // See note on `PatKind::Or` below for why this is `all`.
378                    ExprKind::Match(scrutinee, arms, _) => {
379                        assert_eq!(scrutinee.hir_id, expr.hir_id);
380                        arms.iter()
381                            .all(|arm| self.pat_guaranteed_to_constitute_read_for_never(arm.pat))
382                    }
383                    ExprKind::Let(hir::LetExpr { init, pat, .. }) => {
384                        assert_eq!(init.hir_id, expr.hir_id);
385                        self.pat_guaranteed_to_constitute_read_for_never(*pat)
386                    }
387
388                    // Any expression child of these expressions constitute reads.
389                    ExprKind::Array(_)
390                    | ExprKind::Call(_, _)
391                    | ExprKind::Use(_, _)
392                    | ExprKind::MethodCall(_, _, _, _)
393                    | ExprKind::Tup(_)
394                    | ExprKind::Binary(_, _, _)
395                    | ExprKind::Unary(_, _)
396                    | ExprKind::Cast(_, _)
397                    | ExprKind::DropTemps(_)
398                    | ExprKind::If(_, _, _)
399                    | ExprKind::Closure(_)
400                    | ExprKind::Block(_, _)
401                    | ExprKind::AssignOp(_, _, _)
402                    | ExprKind::Index(_, _, _)
403                    | ExprKind::Break(_, _)
404                    | ExprKind::Ret(_)
405                    | ExprKind::Become(_)
406                    | ExprKind::InlineAsm(_)
407                    | ExprKind::Struct(_, _, _)
408                    | ExprKind::Repeat(_, _)
409                    | ExprKind::Yield(_, _) => true,
410
411                    // These expressions have no (direct) sub-exprs.
412                    ExprKind::ConstBlock(_)
413                    | ExprKind::Loop(_, _, _, _)
414                    | ExprKind::Lit(_)
415                    | ExprKind::Path(_)
416                    | ExprKind::Continue(_)
417                    | ExprKind::OffsetOf(_, _)
418                    | ExprKind::Err(_) => unreachable!("no sub-expr expected for {:?}", expr.kind),
419                }
420            }
421
422            // If we have a subpattern that performs a read, we want to consider this
423            // to diverge for compatibility to support something like `let x: () = *never_ptr;`.
424            hir::Node::LetStmt(hir::LetStmt { init: Some(target), pat, .. }) => {
425                assert_eq!(target.hir_id, expr.hir_id);
426                self.pat_guaranteed_to_constitute_read_for_never(*pat)
427            }
428
429            // These nodes (if they have a sub-expr) do constitute a read.
430            hir::Node::Block(_)
431            | hir::Node::Arm(_)
432            | hir::Node::ExprField(_)
433            | hir::Node::AnonConst(_)
434            | hir::Node::ConstBlock(_)
435            | hir::Node::ConstArg(_)
436            | hir::Node::Stmt(_)
437            | hir::Node::Item(hir::Item {
438                kind: hir::ItemKind::Const(..) | hir::ItemKind::Static(..),
439                ..
440            })
441            | hir::Node::TraitItem(hir::TraitItem {
442                kind: hir::TraitItemKind::Const(..), ..
443            })
444            | hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. }) => true,
445
446            hir::Node::TyPat(_) | hir::Node::Pat(_) => {
447                self.dcx().span_delayed_bug(expr.span, "place expr not allowed in pattern");
448                true
449            }
450
451            // These nodes do not have direct sub-exprs.
452            hir::Node::Param(_)
453            | hir::Node::Item(_)
454            | hir::Node::ForeignItem(_)
455            | hir::Node::TraitItem(_)
456            | hir::Node::ImplItem(_)
457            | hir::Node::Variant(_)
458            | hir::Node::Field(_)
459            | hir::Node::PathSegment(_)
460            | hir::Node::Ty(_)
461            | hir::Node::AssocItemConstraint(_)
462            | hir::Node::TraitRef(_)
463            | hir::Node::PatField(_)
464            | hir::Node::PatExpr(_)
465            | hir::Node::LetStmt(_)
466            | hir::Node::Synthetic
467            | hir::Node::Err(_)
468            | hir::Node::Ctor(_)
469            | hir::Node::Lifetime(_)
470            | hir::Node::GenericParam(_)
471            | hir::Node::Crate(_)
472            | hir::Node::Infer(_)
473            | hir::Node::WherePredicate(_)
474            | hir::Node::PreciseCapturingNonLifetimeArg(_)
475            | hir::Node::OpaqueTy(_) => {
476                unreachable!("no sub-expr expected for {parent_node:?}")
477            }
478        }
479    }
480
481    /// Whether this pattern constitutes a read of value of the scrutinee that
482    /// it is matching against. This is used to determine whether we should
483    /// perform `NeverToAny` coercions.
484    ///
485    /// See above for the nuances of what happens when this returns true.
486    pub(super) fn pat_guaranteed_to_constitute_read_for_never(&self, pat: &hir::Pat<'_>) -> bool {
487        match pat.kind {
488            // Does not constitute a read.
489            hir::PatKind::Wild => false,
490
491            // Might not constitute a read, since the condition might be false.
492            hir::PatKind::Guard(_, _) => true,
493
494            // This is unnecessarily restrictive when the pattern that doesn't
495            // constitute a read is unreachable.
496            //
497            // For example `match *never_ptr { value => {}, _ => {} }` or
498            // `match *never_ptr { _ if false => {}, value => {} }`.
499            //
500            // It is however fine to be restrictive here; only returning `true`
501            // can lead to unsoundness.
502            hir::PatKind::Or(subpats) => {
503                subpats.iter().all(|pat| self.pat_guaranteed_to_constitute_read_for_never(pat))
504            }
505
506            // Does constitute a read, since it is equivalent to a discriminant read.
507            hir::PatKind::Never => true,
508
509            // All of these constitute a read, or match on something that isn't `!`,
510            // which would require a `NeverToAny` coercion.
511            hir::PatKind::Missing
512            | hir::PatKind::Binding(_, _, _, _)
513            | hir::PatKind::Struct(_, _, _)
514            | hir::PatKind::TupleStruct(_, _, _)
515            | hir::PatKind::Tuple(_, _)
516            | hir::PatKind::Box(_)
517            | hir::PatKind::Ref(_, _)
518            | hir::PatKind::Deref(_)
519            | hir::PatKind::Expr(_)
520            | hir::PatKind::Range(_, _, _)
521            | hir::PatKind::Slice(_, _, _)
522            | hir::PatKind::Err(_) => true,
523        }
524    }
525
526    #[instrument(skip(self, expr), level = "debug")]
527    fn check_expr_kind(
528        &self,
529        expr: &'tcx hir::Expr<'tcx>,
530        expected: Expectation<'tcx>,
531    ) -> Ty<'tcx> {
532        trace!("expr={:#?}", expr);
533
534        let tcx = self.tcx;
535        match expr.kind {
536            ExprKind::Lit(ref lit) => self.check_expr_lit(lit, expected),
537            ExprKind::Binary(op, lhs, rhs) => self.check_expr_binop(expr, op, lhs, rhs, expected),
538            ExprKind::Assign(lhs, rhs, span) => {
539                self.check_expr_assign(expr, expected, lhs, rhs, span)
540            }
541            ExprKind::AssignOp(op, lhs, rhs) => {
542                self.check_expr_assign_op(expr, op, lhs, rhs, expected)
543            }
544            ExprKind::Unary(unop, oprnd) => self.check_expr_unop(unop, oprnd, expected, expr),
545            ExprKind::AddrOf(kind, mutbl, oprnd) => {
546                self.check_expr_addr_of(kind, mutbl, oprnd, expected, expr)
547            }
548            ExprKind::Path(QPath::LangItem(lang_item, _)) => {
549                self.check_lang_item_path(lang_item, expr)
550            }
551            ExprKind::Path(ref qpath) => self.check_expr_path(qpath, expr, None),
552            ExprKind::InlineAsm(asm) => {
553                // We defer some asm checks as we may not have resolved the input and output types yet (they may still be infer vars).
554                self.deferred_asm_checks.borrow_mut().push((asm, expr.hir_id));
555                self.check_expr_asm(asm, expr.span)
556            }
557            ExprKind::OffsetOf(container, fields) => {
558                self.check_expr_offset_of(container, fields, expr)
559            }
560            ExprKind::Break(destination, ref expr_opt) => {
561                self.check_expr_break(destination, expr_opt.as_deref(), expr)
562            }
563            ExprKind::Continue(destination) => self.check_expr_continue(destination, expr),
564            ExprKind::Ret(ref expr_opt) => self.check_expr_return(expr_opt.as_deref(), expr),
565            ExprKind::Become(call) => self.check_expr_become(call, expr),
566            ExprKind::Let(let_expr) => self.check_expr_let(let_expr, expr.hir_id),
567            ExprKind::Loop(body, _, source, _) => {
568                self.check_expr_loop(body, source, expected, expr)
569            }
570            ExprKind::Match(discrim, arms, match_src) => {
571                self.check_expr_match(expr, discrim, arms, expected, match_src)
572            }
573            ExprKind::Closure(closure) => self.check_expr_closure(closure, expr.span, expected),
574            ExprKind::Block(body, _) => self.check_expr_block(body, expected),
575            ExprKind::Call(callee, args) => self.check_expr_call(expr, callee, args, expected),
576            ExprKind::Use(used_expr, _) => self.check_expr_use(used_expr, expected),
577            ExprKind::MethodCall(segment, receiver, args, _) => {
578                self.check_expr_method_call(expr, segment, receiver, args, expected)
579            }
580            ExprKind::Cast(e, t) => self.check_expr_cast(e, t, expr),
581            ExprKind::Type(e, t) => {
582                let ascribed_ty = self.lower_ty_saving_user_provided_ty(t);
583                let ty = self.check_expr_with_hint(e, ascribed_ty);
584                self.demand_eqtype(e.span, ascribed_ty, ty);
585                ascribed_ty
586            }
587            ExprKind::If(cond, then_expr, opt_else_expr) => {
588                self.check_expr_if(expr.hir_id, cond, then_expr, opt_else_expr, expr.span, expected)
589            }
590            ExprKind::DropTemps(e) => self.check_expr_with_expectation(e, expected),
591            ExprKind::Array(args) => self.check_expr_array(args, expected, expr),
592            ExprKind::ConstBlock(ref block) => self.check_expr_const_block(block, expected),
593            ExprKind::Repeat(element, ref count) => {
594                self.check_expr_repeat(element, count, expected, expr)
595            }
596            ExprKind::Tup(elts) => self.check_expr_tuple(elts, expected, expr),
597            ExprKind::Struct(qpath, fields, ref base_expr) => {
598                self.check_expr_struct(expr, expected, qpath, fields, base_expr)
599            }
600            ExprKind::Field(base, field) => self.check_expr_field(expr, base, field, expected),
601            ExprKind::Index(base, idx, brackets_span) => {
602                self.check_expr_index(base, idx, expr, brackets_span)
603            }
604            ExprKind::Yield(value, _) => self.check_expr_yield(value, expr),
605            ExprKind::UnsafeBinderCast(kind, inner_expr, ty) => {
606                self.check_expr_unsafe_binder_cast(expr.span, kind, inner_expr, ty, expected)
607            }
608            ExprKind::Err(guar) => Ty::new_error(tcx, guar),
609        }
610    }
611
612    fn check_expr_unop(
613        &self,
614        unop: hir::UnOp,
615        oprnd: &'tcx hir::Expr<'tcx>,
616        expected: Expectation<'tcx>,
617        expr: &'tcx hir::Expr<'tcx>,
618    ) -> Ty<'tcx> {
619        let tcx = self.tcx;
620        let expected_inner = match unop {
621            hir::UnOp::Not | hir::UnOp::Neg => expected,
622            hir::UnOp::Deref => NoExpectation,
623        };
624        let mut oprnd_t = self.check_expr_with_expectation(oprnd, expected_inner);
625
626        if !oprnd_t.references_error() {
627            oprnd_t = self.structurally_resolve_type(expr.span, oprnd_t);
628            match unop {
629                hir::UnOp::Deref => {
630                    if let Some(ty) = self.lookup_derefing(expr, oprnd, oprnd_t) {
631                        oprnd_t = ty;
632                    } else {
633                        let mut err =
634                            self.dcx().create_err(CantDereference { span: expr.span, ty: oprnd_t });
635                        let sp = tcx.sess.source_map().start_point(expr.span).with_parent(None);
636                        if let Some(sp) =
637                            tcx.sess.psess.ambiguous_block_expr_parse.borrow().get(&sp)
638                        {
639                            err.subdiagnostic(ExprParenthesesNeeded::surrounding(*sp));
640                        }
641                        oprnd_t = Ty::new_error(tcx, err.emit());
642                    }
643                }
644                hir::UnOp::Not => {
645                    let result = self.check_user_unop(expr, oprnd_t, unop, expected_inner);
646                    // If it's builtin, we can reuse the type, this helps inference.
647                    if !(oprnd_t.is_integral() || *oprnd_t.kind() == ty::Bool) {
648                        oprnd_t = result;
649                    }
650                }
651                hir::UnOp::Neg => {
652                    let result = self.check_user_unop(expr, oprnd_t, unop, expected_inner);
653                    // If it's builtin, we can reuse the type, this helps inference.
654                    if !oprnd_t.is_numeric() {
655                        oprnd_t = result;
656                    }
657                }
658            }
659        }
660        oprnd_t
661    }
662
663    fn check_expr_addr_of(
664        &self,
665        kind: hir::BorrowKind,
666        mutbl: hir::Mutability,
667        oprnd: &'tcx hir::Expr<'tcx>,
668        expected: Expectation<'tcx>,
669        expr: &'tcx hir::Expr<'tcx>,
670    ) -> Ty<'tcx> {
671        let hint = expected.only_has_type(self).map_or(NoExpectation, |ty| {
672            match self.try_structurally_resolve_type(expr.span, ty).kind() {
673                ty::Ref(_, ty, _) | ty::RawPtr(ty, _) => {
674                    if oprnd.is_syntactic_place_expr() {
675                        // Places may legitimately have unsized types.
676                        // For example, dereferences of a wide pointer and
677                        // the last field of a struct can be unsized.
678                        ExpectHasType(*ty)
679                    } else {
680                        Expectation::rvalue_hint(self, *ty)
681                    }
682                }
683                _ => NoExpectation,
684            }
685        });
686        let ty =
687            self.check_expr_with_expectation_and_needs(oprnd, hint, Needs::maybe_mut_place(mutbl));
688        if let Err(guar) = ty.error_reported() {
689            return Ty::new_error(self.tcx, guar);
690        }
691
692        match kind {
693            hir::BorrowKind::Raw => {
694                self.check_named_place_expr(oprnd);
695                Ty::new_ptr(self.tcx, ty, mutbl)
696            }
697            hir::BorrowKind::Ref | hir::BorrowKind::Pin => {
698                // Note: at this point, we cannot say what the best lifetime
699                // is to use for resulting pointer. We want to use the
700                // shortest lifetime possible so as to avoid spurious borrowck
701                // errors. Moreover, the longest lifetime will depend on the
702                // precise details of the value whose address is being taken
703                // (and how long it is valid), which we don't know yet until
704                // type inference is complete.
705                //
706                // Therefore, here we simply generate a region variable. The
707                // region inferencer will then select a suitable value.
708                // Finally, borrowck will infer the value of the region again,
709                // this time with enough precision to check that the value
710                // whose address was taken can actually be made to live as long
711                // as it needs to live.
712                let region = self.next_region_var(RegionVariableOrigin::BorrowRegion(expr.span));
713                match kind {
714                    hir::BorrowKind::Ref => Ty::new_ref(self.tcx, region, ty, mutbl),
715                    hir::BorrowKind::Pin => Ty::new_pinned_ref(self.tcx, region, ty, mutbl),
716                    _ => unreachable!(),
717                }
718            }
719        }
720    }
721
722    /// Does this expression refer to a place that either:
723    /// * Is based on a local or static.
724    /// * Contains a dereference
725    /// Note that the adjustments for the children of `expr` should already
726    /// have been resolved.
727    fn check_named_place_expr(&self, oprnd: &'tcx hir::Expr<'tcx>) {
728        let is_named = oprnd.is_place_expr(|base| {
729            // Allow raw borrows if there are any deref adjustments.
730            //
731            // const VAL: (i32,) = (0,);
732            // const REF: &(i32,) = &(0,);
733            //
734            // &raw const VAL.0;            // ERROR
735            // &raw const REF.0;            // OK, same as &raw const (*REF).0;
736            //
737            // This is maybe too permissive, since it allows
738            // `let u = &raw const Box::new((1,)).0`, which creates an
739            // immediately dangling raw pointer.
740            self.typeck_results
741                .borrow()
742                .adjustments()
743                .get(base.hir_id)
744                .is_some_and(|x| x.iter().any(|adj| matches!(adj.kind, Adjust::Deref(_))))
745        });
746        if !is_named {
747            self.dcx().emit_err(AddressOfTemporaryTaken { span: oprnd.span });
748        }
749    }
750
751    fn check_lang_item_path(
752        &self,
753        lang_item: hir::LangItem,
754        expr: &'tcx hir::Expr<'tcx>,
755    ) -> Ty<'tcx> {
756        self.resolve_lang_item_path(lang_item, expr.span, expr.hir_id).1
757    }
758
759    pub(crate) fn check_expr_path(
760        &self,
761        qpath: &'tcx hir::QPath<'tcx>,
762        expr: &'tcx hir::Expr<'tcx>,
763        call_expr_and_args: Option<(&'tcx hir::Expr<'tcx>, &'tcx [hir::Expr<'tcx>])>,
764    ) -> Ty<'tcx> {
765        let tcx = self.tcx;
766        let (res, opt_ty, segs) =
767            self.resolve_ty_and_res_fully_qualified_call(qpath, expr.hir_id, expr.span);
768        let ty = match res {
769            Res::Err => {
770                self.suggest_assoc_method_call(segs);
771                let e =
772                    self.dcx().span_delayed_bug(qpath.span(), "`Res::Err` but no error emitted");
773                Ty::new_error(tcx, e)
774            }
775            Res::Def(DefKind::Variant, _) => {
776                let e = report_unexpected_variant_res(
777                    tcx,
778                    res,
779                    Some(expr),
780                    qpath,
781                    expr.span,
782                    E0533,
783                    "value",
784                );
785                Ty::new_error(tcx, e)
786            }
787            _ => {
788                self.instantiate_value_path(
789                    segs,
790                    opt_ty,
791                    res,
792                    call_expr_and_args.map_or(expr.span, |(e, _)| e.span),
793                    expr.span,
794                    expr.hir_id,
795                )
796                .0
797            }
798        };
799
800        if let ty::FnDef(did, _) = *ty.kind() {
801            let fn_sig = ty.fn_sig(tcx);
802
803            if tcx.is_intrinsic(did, sym::transmute) {
804                let Some(from) = fn_sig.inputs().skip_binder().get(0) else {
805                    span_bug!(
806                        tcx.def_span(did),
807                        "intrinsic fn `transmute` defined with no parameters"
808                    );
809                };
810                let to = fn_sig.output().skip_binder();
811                // We defer the transmute to the end of typeck, once all inference vars have
812                // been resolved or we errored. This is important as we can only check transmute
813                // on concrete types, but the output type may not be known yet (it would only
814                // be known if explicitly specified via turbofish).
815                self.deferred_transmute_checks.borrow_mut().push((*from, to, expr.hir_id));
816            }
817            if !tcx.features().unsized_fn_params() {
818                // We want to remove some Sized bounds from std functions,
819                // but don't want to expose the removal to stable Rust.
820                // i.e., we don't want to allow
821                //
822                // ```rust
823                // drop as fn(str);
824                // ```
825                //
826                // to work in stable even if the Sized bound on `drop` is relaxed.
827                for i in 0..fn_sig.inputs().skip_binder().len() {
828                    // We just want to check sizedness, so instead of introducing
829                    // placeholder lifetimes with probing, we just replace higher lifetimes
830                    // with fresh vars.
831                    let span = call_expr_and_args
832                        .and_then(|(_, args)| args.get(i))
833                        .map_or(expr.span, |arg| arg.span);
834                    let input = self.instantiate_binder_with_fresh_vars(
835                        span,
836                        infer::BoundRegionConversionTime::FnCall,
837                        fn_sig.input(i),
838                    );
839                    self.require_type_is_sized_deferred(
840                        input,
841                        span,
842                        ObligationCauseCode::SizedArgumentType(None),
843                    );
844                }
845            }
846            // Here we want to prevent struct constructors from returning unsized types,
847            // which can happen with fn pointer coercion on stable.
848            // Also, as we just want to check sizedness, instead of introducing
849            // placeholder lifetimes with probing, we just replace higher lifetimes
850            // with fresh vars.
851            let output = self.instantiate_binder_with_fresh_vars(
852                expr.span,
853                infer::BoundRegionConversionTime::FnCall,
854                fn_sig.output(),
855            );
856            self.require_type_is_sized_deferred(
857                output,
858                call_expr_and_args.map_or(expr.span, |(e, _)| e.span),
859                ObligationCauseCode::SizedCallReturnType,
860            );
861        }
862
863        // We always require that the type provided as the value for
864        // a type parameter outlives the moment of instantiation.
865        let args = self.typeck_results.borrow().node_args(expr.hir_id);
866        self.add_wf_bounds(args, expr.span);
867
868        ty
869    }
870
871    fn check_expr_break(
872        &self,
873        destination: hir::Destination,
874        expr_opt: Option<&'tcx hir::Expr<'tcx>>,
875        expr: &'tcx hir::Expr<'tcx>,
876    ) -> Ty<'tcx> {
877        let tcx = self.tcx;
878        if let Ok(target_id) = destination.target_id {
879            let (e_ty, cause);
880            if let Some(e) = expr_opt {
881                // If this is a break with a value, we need to type-check
882                // the expression. Get an expected type from the loop context.
883                let opt_coerce_to = {
884                    // We should release `enclosing_breakables` before the `check_expr_with_hint`
885                    // below, so can't move this block of code to the enclosing scope and share
886                    // `ctxt` with the second `enclosing_breakables` borrow below.
887                    let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
888                    match enclosing_breakables.opt_find_breakable(target_id) {
889                        Some(ctxt) => ctxt.coerce.as_ref().map(|coerce| coerce.expected_ty()),
890                        None => {
891                            // Avoid ICE when `break` is inside a closure (#65383).
892                            return Ty::new_error_with_message(
893                                tcx,
894                                expr.span,
895                                "break was outside loop, but no error was emitted",
896                            );
897                        }
898                    }
899                };
900
901                // If the loop context is not a `loop { }`, then break with
902                // a value is illegal, and `opt_coerce_to` will be `None`.
903                // Set expectation to error in that case and set tainted
904                // by error (#114529)
905                let coerce_to = opt_coerce_to.unwrap_or_else(|| {
906                    let guar = self.dcx().span_delayed_bug(
907                        expr.span,
908                        "illegal break with value found but no error reported",
909                    );
910                    self.set_tainted_by_errors(guar);
911                    Ty::new_error(tcx, guar)
912                });
913
914                // Recurse without `enclosing_breakables` borrowed.
915                e_ty = self.check_expr_with_hint(e, coerce_to);
916                cause = self.misc(e.span);
917            } else {
918                // Otherwise, this is a break *without* a value. That's
919                // always legal, and is equivalent to `break ()`.
920                e_ty = tcx.types.unit;
921                cause = self.misc(expr.span);
922            }
923
924            // Now that we have type-checked `expr_opt`, borrow
925            // the `enclosing_loops` field and let's coerce the
926            // type of `expr_opt` into what is expected.
927            let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
928            let Some(ctxt) = enclosing_breakables.opt_find_breakable(target_id) else {
929                // Avoid ICE when `break` is inside a closure (#65383).
930                return Ty::new_error_with_message(
931                    tcx,
932                    expr.span,
933                    "break was outside loop, but no error was emitted",
934                );
935            };
936
937            if let Some(ref mut coerce) = ctxt.coerce {
938                if let Some(e) = expr_opt {
939                    coerce.coerce(self, &cause, e, e_ty);
940                } else {
941                    assert!(e_ty.is_unit());
942                    let ty = coerce.expected_ty();
943                    coerce.coerce_forced_unit(
944                        self,
945                        &cause,
946                        |mut err| {
947                            self.suggest_missing_semicolon(&mut err, expr, e_ty, false, false);
948                            self.suggest_mismatched_types_on_tail(
949                                &mut err, expr, ty, e_ty, target_id,
950                            );
951                            let error =
952                                Some(TypeError::Sorts(ExpectedFound { expected: ty, found: e_ty }));
953                            self.annotate_loop_expected_due_to_inference(err, expr, error);
954                            if let Some(val) =
955                                self.err_ctxt().ty_kind_suggestion(self.param_env, ty)
956                            {
957                                err.span_suggestion_verbose(
958                                    expr.span.shrink_to_hi(),
959                                    "give the `break` a value of the expected type",
960                                    format!(" {val}"),
961                                    Applicability::HasPlaceholders,
962                                );
963                            }
964                        },
965                        false,
966                    );
967                }
968            } else {
969                // If `ctxt.coerce` is `None`, we can just ignore
970                // the type of the expression. This is because
971                // either this was a break *without* a value, in
972                // which case it is always a legal type (`()`), or
973                // else an error would have been flagged by the
974                // `loops` pass for using break with an expression
975                // where you are not supposed to.
976                assert!(expr_opt.is_none() || self.tainted_by_errors().is_some());
977            }
978
979            // If we encountered a `break`, then (no surprise) it may be possible to break from the
980            // loop... unless the value being returned from the loop diverges itself, e.g.
981            // `break return 5` or `break loop {}`.
982            ctxt.may_break |= !self.diverges.get().is_always();
983
984            // the type of a `break` is always `!`, since it diverges
985            tcx.types.never
986        } else {
987            // Otherwise, we failed to find the enclosing loop;
988            // this can only happen if the `break` was not
989            // inside a loop at all, which is caught by the
990            // loop-checking pass.
991            let err = Ty::new_error_with_message(
992                self.tcx,
993                expr.span,
994                "break was outside loop, but no error was emitted",
995            );
996
997            // We still need to assign a type to the inner expression to
998            // prevent the ICE in #43162.
999            if let Some(e) = expr_opt {
1000                self.check_expr_with_hint(e, err);
1001
1002                // ... except when we try to 'break rust;'.
1003                // ICE this expression in particular (see #43162).
1004                if let ExprKind::Path(QPath::Resolved(_, path)) = e.kind {
1005                    if let [segment] = path.segments
1006                        && segment.ident.name == sym::rust
1007                    {
1008                        fatally_break_rust(self.tcx, expr.span);
1009                    }
1010                }
1011            }
1012
1013            // There was an error; make type-check fail.
1014            err
1015        }
1016    }
1017
1018    fn check_expr_continue(
1019        &self,
1020        destination: hir::Destination,
1021        expr: &'tcx hir::Expr<'tcx>,
1022    ) -> Ty<'tcx> {
1023        if let Ok(target_id) = destination.target_id {
1024            if let hir::Node::Expr(hir::Expr { kind: ExprKind::Loop(..), .. }) =
1025                self.tcx.hir_node(target_id)
1026            {
1027                self.tcx.types.never
1028            } else {
1029                // Liveness linting assumes `continue`s all point to loops. We'll report an error
1030                // in `check_mod_loops`, but make sure we don't run liveness (#113379, #121623).
1031                let guar = self.dcx().span_delayed_bug(
1032                    expr.span,
1033                    "found `continue` not pointing to loop, but no error reported",
1034                );
1035                Ty::new_error(self.tcx, guar)
1036            }
1037        } else {
1038            // There was an error; make type-check fail.
1039            Ty::new_misc_error(self.tcx)
1040        }
1041    }
1042
1043    fn check_expr_return(
1044        &self,
1045        expr_opt: Option<&'tcx hir::Expr<'tcx>>,
1046        expr: &'tcx hir::Expr<'tcx>,
1047    ) -> Ty<'tcx> {
1048        if self.ret_coercion.is_none() {
1049            self.emit_return_outside_of_fn_body(expr, ReturnLikeStatementKind::Return);
1050
1051            if let Some(e) = expr_opt {
1052                // We still have to type-check `e` (issue #86188), but calling
1053                // `check_return_expr` only works inside fn bodies.
1054                self.check_expr(e);
1055            }
1056        } else if let Some(e) = expr_opt {
1057            if self.ret_coercion_span.get().is_none() {
1058                self.ret_coercion_span.set(Some(e.span));
1059            }
1060            self.check_return_or_body_tail(e, true);
1061        } else {
1062            let mut coercion = self.ret_coercion.as_ref().unwrap().borrow_mut();
1063            if self.ret_coercion_span.get().is_none() {
1064                self.ret_coercion_span.set(Some(expr.span));
1065            }
1066            let cause = self.cause(expr.span, ObligationCauseCode::ReturnNoExpression);
1067            if let Some((_, fn_decl)) = self.get_fn_decl(expr.hir_id) {
1068                coercion.coerce_forced_unit(
1069                    self,
1070                    &cause,
1071                    |db| {
1072                        let span = fn_decl.output.span();
1073                        if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
1074                            db.span_label(
1075                                span,
1076                                format!("expected `{snippet}` because of this return type"),
1077                            );
1078                        }
1079                    },
1080                    true,
1081                );
1082            } else {
1083                coercion.coerce_forced_unit(self, &cause, |_| (), true);
1084            }
1085        }
1086        self.tcx.types.never
1087    }
1088
1089    fn check_expr_become(
1090        &self,
1091        call: &'tcx hir::Expr<'tcx>,
1092        expr: &'tcx hir::Expr<'tcx>,
1093    ) -> Ty<'tcx> {
1094        match &self.ret_coercion {
1095            Some(ret_coercion) => {
1096                let ret_ty = ret_coercion.borrow().expected_ty();
1097                let call_expr_ty = self.check_expr_with_hint(call, ret_ty);
1098
1099                // N.B. don't coerce here, as tail calls can't support most/all coercions
1100                // FIXME(explicit_tail_calls): add a diagnostic note that `become` doesn't allow coercions
1101                self.demand_suptype(expr.span, ret_ty, call_expr_ty);
1102            }
1103            None => {
1104                self.emit_return_outside_of_fn_body(expr, ReturnLikeStatementKind::Become);
1105
1106                // Fallback to simply type checking `call` without hint/demanding the right types.
1107                // Best effort to highlight more errors.
1108                self.check_expr(call);
1109            }
1110        }
1111
1112        self.tcx.types.never
1113    }
1114
1115    /// Check an expression that _is being returned_.
1116    /// For example, this is called with `return_expr: $expr` when `return $expr`
1117    /// is encountered.
1118    ///
1119    /// Note that this function must only be called in function bodies.
1120    ///
1121    /// `explicit_return` is `true` if we're checking an explicit `return expr`,
1122    /// and `false` if we're checking a trailing expression.
1123    pub(super) fn check_return_or_body_tail(
1124        &self,
1125        return_expr: &'tcx hir::Expr<'tcx>,
1126        explicit_return: bool,
1127    ) {
1128        let ret_coercion = self.ret_coercion.as_ref().unwrap_or_else(|| {
1129            span_bug!(return_expr.span, "check_return_expr called outside fn body")
1130        });
1131
1132        let ret_ty = ret_coercion.borrow().expected_ty();
1133        let return_expr_ty = self.check_expr_with_hint(return_expr, ret_ty);
1134        let mut span = return_expr.span;
1135        let mut hir_id = return_expr.hir_id;
1136        // Use the span of the trailing expression for our cause,
1137        // not the span of the entire function
1138        if !explicit_return
1139            && let ExprKind::Block(body, _) = return_expr.kind
1140            && let Some(last_expr) = body.expr
1141        {
1142            span = last_expr.span;
1143            hir_id = last_expr.hir_id;
1144        }
1145        ret_coercion.borrow_mut().coerce(
1146            self,
1147            &self.cause(span, ObligationCauseCode::ReturnValue(return_expr.hir_id)),
1148            return_expr,
1149            return_expr_ty,
1150        );
1151
1152        if let Some(fn_sig) = self.body_fn_sig()
1153            && fn_sig.output().has_opaque_types()
1154        {
1155            // Point any obligations that were registered due to opaque type
1156            // inference at the return expression.
1157            self.select_obligations_where_possible(|errors| {
1158                self.point_at_return_for_opaque_ty_error(
1159                    errors,
1160                    hir_id,
1161                    span,
1162                    return_expr_ty,
1163                    return_expr.span,
1164                );
1165            });
1166        }
1167    }
1168
1169    /// Emit an error because `return` or `become` is used outside of a function body.
1170    ///
1171    /// `expr` is the `return` (`become`) "statement", `kind` is the kind of the statement
1172    /// either `Return` or `Become`.
1173    fn emit_return_outside_of_fn_body(&self, expr: &hir::Expr<'_>, kind: ReturnLikeStatementKind) {
1174        let mut err = ReturnStmtOutsideOfFnBody {
1175            span: expr.span,
1176            encl_body_span: None,
1177            encl_fn_span: None,
1178            statement_kind: kind,
1179        };
1180
1181        let encl_item_id = self.tcx.hir_get_parent_item(expr.hir_id);
1182
1183        if let hir::Node::Item(hir::Item {
1184            kind: hir::ItemKind::Fn { .. },
1185            span: encl_fn_span,
1186            ..
1187        })
1188        | hir::Node::TraitItem(hir::TraitItem {
1189            kind: hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(_)),
1190            span: encl_fn_span,
1191            ..
1192        })
1193        | hir::Node::ImplItem(hir::ImplItem {
1194            kind: hir::ImplItemKind::Fn(..),
1195            span: encl_fn_span,
1196            ..
1197        }) = self.tcx.hir_node_by_def_id(encl_item_id.def_id)
1198        {
1199            // We are inside a function body, so reporting "return statement
1200            // outside of function body" needs an explanation.
1201
1202            let encl_body_owner_id = self.tcx.hir_enclosing_body_owner(expr.hir_id);
1203
1204            // If this didn't hold, we would not have to report an error in
1205            // the first place.
1206            assert_ne!(encl_item_id.def_id, encl_body_owner_id);
1207
1208            let encl_body = self.tcx.hir_body_owned_by(encl_body_owner_id);
1209
1210            err.encl_body_span = Some(encl_body.value.span);
1211            err.encl_fn_span = Some(*encl_fn_span);
1212        }
1213
1214        self.dcx().emit_err(err);
1215    }
1216
1217    fn point_at_return_for_opaque_ty_error(
1218        &self,
1219        errors: &mut Vec<traits::FulfillmentError<'tcx>>,
1220        hir_id: HirId,
1221        span: Span,
1222        return_expr_ty: Ty<'tcx>,
1223        return_span: Span,
1224    ) {
1225        // Don't point at the whole block if it's empty
1226        if span == return_span {
1227            return;
1228        }
1229        for err in errors {
1230            let cause = &mut err.obligation.cause;
1231            if let ObligationCauseCode::OpaqueReturnType(None) = cause.code() {
1232                let new_cause = self.cause(
1233                    cause.span,
1234                    ObligationCauseCode::OpaqueReturnType(Some((return_expr_ty, hir_id))),
1235                );
1236                *cause = new_cause;
1237            }
1238        }
1239    }
1240
1241    pub(crate) fn check_lhs_assignable(
1242        &self,
1243        lhs: &'tcx hir::Expr<'tcx>,
1244        code: ErrCode,
1245        op_span: Span,
1246        adjust_err: impl FnOnce(&mut Diag<'_>),
1247    ) {
1248        if lhs.is_syntactic_place_expr() {
1249            return;
1250        }
1251
1252        let mut err = self.dcx().struct_span_err(op_span, "invalid left-hand side of assignment");
1253        err.code(code);
1254        err.span_label(lhs.span, "cannot assign to this expression");
1255
1256        self.comes_from_while_condition(lhs.hir_id, |expr| {
1257            err.span_suggestion_verbose(
1258                expr.span.shrink_to_lo(),
1259                "you might have meant to use pattern destructuring",
1260                "let ",
1261                Applicability::MachineApplicable,
1262            );
1263        });
1264        self.check_for_missing_semi(lhs, &mut err);
1265
1266        adjust_err(&mut err);
1267
1268        err.emit();
1269    }
1270
1271    /// Check if the expression that could not be assigned to was a typoed expression that
1272    pub(crate) fn check_for_missing_semi(
1273        &self,
1274        expr: &'tcx hir::Expr<'tcx>,
1275        err: &mut Diag<'_>,
1276    ) -> bool {
1277        if let hir::ExprKind::Binary(binop, lhs, rhs) = expr.kind
1278            && let hir::BinOpKind::Mul = binop.node
1279            && self.tcx.sess.source_map().is_multiline(lhs.span.between(rhs.span))
1280            && rhs.is_syntactic_place_expr()
1281        {
1282            //      v missing semicolon here
1283            // foo()
1284            // *bar = baz;
1285            // (#80446).
1286            err.span_suggestion_verbose(
1287                lhs.span.shrink_to_hi(),
1288                "you might have meant to write a semicolon here",
1289                ";",
1290                Applicability::MachineApplicable,
1291            );
1292            return true;
1293        }
1294        false
1295    }
1296
1297    // Check if an expression `original_expr_id` comes from the condition of a while loop,
1298    /// as opposed from the body of a while loop, which we can naively check by iterating
1299    /// parents until we find a loop...
1300    pub(super) fn comes_from_while_condition(
1301        &self,
1302        original_expr_id: HirId,
1303        then: impl FnOnce(&hir::Expr<'_>),
1304    ) {
1305        let mut parent = self.tcx.parent_hir_id(original_expr_id);
1306        loop {
1307            let node = self.tcx.hir_node(parent);
1308            match node {
1309                hir::Node::Expr(hir::Expr {
1310                    kind:
1311                        hir::ExprKind::Loop(
1312                            hir::Block {
1313                                expr:
1314                                    Some(hir::Expr {
1315                                        kind:
1316                                            hir::ExprKind::Match(expr, ..) | hir::ExprKind::If(expr, ..),
1317                                        ..
1318                                    }),
1319                                ..
1320                            },
1321                            _,
1322                            hir::LoopSource::While,
1323                            _,
1324                        ),
1325                    ..
1326                }) => {
1327                    // Check if our original expression is a child of the condition of a while loop.
1328                    // If it is, then we have a situation like `while Some(0) = value.get(0) {`,
1329                    // where `while let` was more likely intended.
1330                    if self.tcx.hir_parent_id_iter(original_expr_id).any(|id| id == expr.hir_id) {
1331                        then(expr);
1332                    }
1333                    break;
1334                }
1335                hir::Node::Item(_)
1336                | hir::Node::ImplItem(_)
1337                | hir::Node::TraitItem(_)
1338                | hir::Node::Crate(_) => break,
1339                _ => {
1340                    parent = self.tcx.parent_hir_id(parent);
1341                }
1342            }
1343        }
1344    }
1345
1346    // A generic function for checking the 'then' and 'else' clauses in an 'if'
1347    // or 'if-else' expression.
1348    fn check_expr_if(
1349        &self,
1350        expr_id: HirId,
1351        cond_expr: &'tcx hir::Expr<'tcx>,
1352        then_expr: &'tcx hir::Expr<'tcx>,
1353        opt_else_expr: Option<&'tcx hir::Expr<'tcx>>,
1354        sp: Span,
1355        orig_expected: Expectation<'tcx>,
1356    ) -> Ty<'tcx> {
1357        let cond_ty = self.check_expr_has_type_or_error(cond_expr, self.tcx.types.bool, |_| {});
1358
1359        self.warn_if_unreachable(
1360            cond_expr.hir_id,
1361            then_expr.span,
1362            "block in `if` or `while` expression",
1363        );
1364
1365        let cond_diverges = self.diverges.get();
1366        self.diverges.set(Diverges::Maybe);
1367
1368        let expected = orig_expected.try_structurally_resolve_and_adjust_for_branches(self, sp);
1369        let then_ty = self.check_expr_with_expectation(then_expr, expected);
1370        let then_diverges = self.diverges.get();
1371        self.diverges.set(Diverges::Maybe);
1372
1373        // We've already taken the expected type's preferences
1374        // into account when typing the `then` branch. To figure
1375        // out the initial shot at a LUB, we thus only consider
1376        // `expected` if it represents a *hard* constraint
1377        // (`only_has_type`); otherwise, we just go with a
1378        // fresh type variable.
1379        let coerce_to_ty = expected.coercion_target_type(self, sp);
1380        let mut coerce: DynamicCoerceMany<'_> = CoerceMany::new(coerce_to_ty);
1381
1382        coerce.coerce(self, &self.misc(sp), then_expr, then_ty);
1383
1384        if let Some(else_expr) = opt_else_expr {
1385            let else_ty = self.check_expr_with_expectation(else_expr, expected);
1386            let else_diverges = self.diverges.get();
1387
1388            let tail_defines_return_position_impl_trait =
1389                self.return_position_impl_trait_from_match_expectation(orig_expected);
1390            let if_cause =
1391                self.if_cause(expr_id, else_expr, tail_defines_return_position_impl_trait);
1392
1393            coerce.coerce(self, &if_cause, else_expr, else_ty);
1394
1395            // We won't diverge unless both branches do (or the condition does).
1396            self.diverges.set(cond_diverges | then_diverges & else_diverges);
1397        } else {
1398            self.if_fallback_coercion(sp, cond_expr, then_expr, &mut coerce);
1399
1400            // If the condition is false we can't diverge.
1401            self.diverges.set(cond_diverges);
1402        }
1403
1404        let result_ty = coerce.complete(self);
1405        if let Err(guar) = cond_ty.error_reported() {
1406            Ty::new_error(self.tcx, guar)
1407        } else {
1408            result_ty
1409        }
1410    }
1411
1412    /// Type check assignment expression `expr` of form `lhs = rhs`.
1413    /// The expected type is `()` and is passed to the function for the purposes of diagnostics.
1414    fn check_expr_assign(
1415        &self,
1416        expr: &'tcx hir::Expr<'tcx>,
1417        expected: Expectation<'tcx>,
1418        lhs: &'tcx hir::Expr<'tcx>,
1419        rhs: &'tcx hir::Expr<'tcx>,
1420        span: Span,
1421    ) -> Ty<'tcx> {
1422        let expected_ty = expected.only_has_type(self);
1423        if expected_ty == Some(self.tcx.types.bool) {
1424            let guar = self.expr_assign_expected_bool_error(expr, lhs, rhs, span);
1425            return Ty::new_error(self.tcx, guar);
1426        }
1427
1428        let lhs_ty = self.check_expr_with_needs(lhs, Needs::MutPlace);
1429
1430        let suggest_deref_binop = |err: &mut Diag<'_>, rhs_ty: Ty<'tcx>| {
1431            if let Some(lhs_deref_ty) = self.deref_once_mutably_for_diagnostic(lhs_ty) {
1432                // Can only assign if the type is sized, so if `DerefMut` yields a type that is
1433                // unsized, do not suggest dereferencing it.
1434                let lhs_deref_ty_is_sized = self
1435                    .infcx
1436                    .type_implements_trait(
1437                        self.tcx.require_lang_item(LangItem::Sized, span),
1438                        [lhs_deref_ty],
1439                        self.param_env,
1440                    )
1441                    .may_apply();
1442                if lhs_deref_ty_is_sized && self.may_coerce(rhs_ty, lhs_deref_ty) {
1443                    err.span_suggestion_verbose(
1444                        lhs.span.shrink_to_lo(),
1445                        "consider dereferencing here to assign to the mutably borrowed value",
1446                        "*",
1447                        Applicability::MachineApplicable,
1448                    );
1449                }
1450            }
1451        };
1452
1453        // This is (basically) inlined `check_expr_coercible_to_type`, but we want
1454        // to suggest an additional fixup here in `suggest_deref_binop`.
1455        let rhs_ty = self.check_expr_with_hint(rhs, lhs_ty);
1456        if let Err(mut diag) =
1457            self.demand_coerce_diag(rhs, rhs_ty, lhs_ty, Some(lhs), AllowTwoPhase::No)
1458        {
1459            suggest_deref_binop(&mut diag, rhs_ty);
1460            diag.emit();
1461        }
1462
1463        self.check_lhs_assignable(lhs, E0070, span, |err| {
1464            if let Some(rhs_ty) = self.typeck_results.borrow().expr_ty_opt(rhs) {
1465                suggest_deref_binop(err, rhs_ty);
1466            }
1467        });
1468
1469        self.require_type_is_sized(lhs_ty, lhs.span, ObligationCauseCode::AssignmentLhsSized);
1470
1471        if let Err(guar) = (lhs_ty, rhs_ty).error_reported() {
1472            Ty::new_error(self.tcx, guar)
1473        } else {
1474            self.tcx.types.unit
1475        }
1476    }
1477
1478    /// The expected type is `bool` but this will result in `()` so we can reasonably
1479    /// say that the user intended to write `lhs == rhs` instead of `lhs = rhs`.
1480    /// The likely cause of this is `if foo = bar { .. }`.
1481    fn expr_assign_expected_bool_error(
1482        &self,
1483        expr: &'tcx hir::Expr<'tcx>,
1484        lhs: &'tcx hir::Expr<'tcx>,
1485        rhs: &'tcx hir::Expr<'tcx>,
1486        span: Span,
1487    ) -> ErrorGuaranteed {
1488        let actual_ty = self.tcx.types.unit;
1489        let expected_ty = self.tcx.types.bool;
1490        let mut err = self.demand_suptype_diag(expr.span, expected_ty, actual_ty).unwrap_err();
1491        let lhs_ty = self.check_expr(lhs);
1492        let rhs_ty = self.check_expr(rhs);
1493        let refs_can_coerce = |lhs: Ty<'tcx>, rhs: Ty<'tcx>| {
1494            let lhs = Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_erased, lhs.peel_refs());
1495            let rhs = Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_erased, rhs.peel_refs());
1496            self.may_coerce(rhs, lhs)
1497        };
1498        let (applicability, eq) = if self.may_coerce(rhs_ty, lhs_ty) {
1499            (Applicability::MachineApplicable, true)
1500        } else if refs_can_coerce(rhs_ty, lhs_ty) {
1501            // The lhs and rhs are likely missing some references in either side. Subsequent
1502            // suggestions will show up.
1503            (Applicability::MaybeIncorrect, true)
1504        } else if let ExprKind::Binary(
1505            Spanned { node: hir::BinOpKind::And | hir::BinOpKind::Or, .. },
1506            _,
1507            rhs_expr,
1508        ) = lhs.kind
1509        {
1510            // if x == 1 && y == 2 { .. }
1511            //                 +
1512            let actual_lhs = self.check_expr(rhs_expr);
1513            let may_eq = self.may_coerce(rhs_ty, actual_lhs) || refs_can_coerce(rhs_ty, actual_lhs);
1514            (Applicability::MaybeIncorrect, may_eq)
1515        } else if let ExprKind::Binary(
1516            Spanned { node: hir::BinOpKind::And | hir::BinOpKind::Or, .. },
1517            lhs_expr,
1518            _,
1519        ) = rhs.kind
1520        {
1521            // if x == 1 && y == 2 { .. }
1522            //       +
1523            let actual_rhs = self.check_expr(lhs_expr);
1524            let may_eq = self.may_coerce(actual_rhs, lhs_ty) || refs_can_coerce(actual_rhs, lhs_ty);
1525            (Applicability::MaybeIncorrect, may_eq)
1526        } else {
1527            (Applicability::MaybeIncorrect, false)
1528        };
1529
1530        if !lhs.is_syntactic_place_expr()
1531            && lhs.is_approximately_pattern()
1532            && !matches!(lhs.kind, hir::ExprKind::Lit(_))
1533        {
1534            // Do not suggest `if let x = y` as `==` is way more likely to be the intention.
1535            if let hir::Node::Expr(hir::Expr { kind: ExprKind::If { .. }, .. }) =
1536                self.tcx.parent_hir_node(expr.hir_id)
1537            {
1538                err.span_suggestion_verbose(
1539                    expr.span.shrink_to_lo(),
1540                    "you might have meant to use pattern matching",
1541                    "let ",
1542                    applicability,
1543                );
1544            };
1545        }
1546        if eq {
1547            err.span_suggestion_verbose(
1548                span.shrink_to_hi(),
1549                "you might have meant to compare for equality",
1550                '=',
1551                applicability,
1552            );
1553        }
1554
1555        // If the assignment expression itself is ill-formed, don't
1556        // bother emitting another error
1557        err.emit_unless_delay(lhs_ty.references_error() || rhs_ty.references_error())
1558    }
1559
1560    pub(super) fn check_expr_let(
1561        &self,
1562        let_expr: &'tcx hir::LetExpr<'tcx>,
1563        hir_id: HirId,
1564    ) -> Ty<'tcx> {
1565        GatherLocalsVisitor::gather_from_let_expr(self, let_expr, hir_id);
1566
1567        // for let statements, this is done in check_stmt
1568        let init = let_expr.init;
1569        self.warn_if_unreachable(init.hir_id, init.span, "block in `let` expression");
1570
1571        // otherwise check exactly as a let statement
1572        self.check_decl((let_expr, hir_id).into());
1573
1574        // but return a bool, for this is a boolean expression
1575        if let ast::Recovered::Yes(error_guaranteed) = let_expr.recovered {
1576            self.set_tainted_by_errors(error_guaranteed);
1577            Ty::new_error(self.tcx, error_guaranteed)
1578        } else {
1579            self.tcx.types.bool
1580        }
1581    }
1582
1583    fn check_expr_loop(
1584        &self,
1585        body: &'tcx hir::Block<'tcx>,
1586        source: hir::LoopSource,
1587        expected: Expectation<'tcx>,
1588        expr: &'tcx hir::Expr<'tcx>,
1589    ) -> Ty<'tcx> {
1590        let coerce = match source {
1591            // you can only use break with a value from a normal `loop { }`
1592            hir::LoopSource::Loop => {
1593                let coerce_to = expected.coercion_target_type(self, body.span);
1594                Some(CoerceMany::new(coerce_to))
1595            }
1596
1597            hir::LoopSource::While | hir::LoopSource::ForLoop => None,
1598        };
1599
1600        let ctxt = BreakableCtxt {
1601            coerce,
1602            may_break: false, // Will get updated if/when we find a `break`.
1603        };
1604
1605        let (ctxt, ()) = self.with_breakable_ctxt(expr.hir_id, ctxt, || {
1606            self.check_block_no_value(body);
1607        });
1608
1609        if ctxt.may_break {
1610            // No way to know whether it's diverging because
1611            // of a `break` or an outer `break` or `return`.
1612            self.diverges.set(Diverges::Maybe);
1613        } else {
1614            self.diverges.set(self.diverges.get() | Diverges::always(expr.span));
1615        }
1616
1617        // If we permit break with a value, then result type is
1618        // the LUB of the breaks (possibly ! if none); else, it
1619        // is nil. This makes sense because infinite loops
1620        // (which would have type !) are only possible iff we
1621        // permit break with a value.
1622        if ctxt.coerce.is_none() && !ctxt.may_break {
1623            self.dcx().span_bug(body.span, "no coercion, but loop may not break");
1624        }
1625        ctxt.coerce.map(|c| c.complete(self)).unwrap_or_else(|| self.tcx.types.unit)
1626    }
1627
1628    /// Checks a method call.
1629    fn check_expr_method_call(
1630        &self,
1631        expr: &'tcx hir::Expr<'tcx>,
1632        segment: &'tcx hir::PathSegment<'tcx>,
1633        rcvr: &'tcx hir::Expr<'tcx>,
1634        args: &'tcx [hir::Expr<'tcx>],
1635        expected: Expectation<'tcx>,
1636    ) -> Ty<'tcx> {
1637        let rcvr_t = self.check_expr(rcvr);
1638        let rcvr_t = self.try_structurally_resolve_type(rcvr.span, rcvr_t);
1639
1640        match self.lookup_method(rcvr_t, segment, segment.ident.span, expr, rcvr, args) {
1641            Ok(method) => {
1642                self.write_method_call_and_enforce_effects(expr.hir_id, expr.span, method);
1643
1644                self.check_argument_types(
1645                    segment.ident.span,
1646                    expr,
1647                    &method.sig.inputs()[1..],
1648                    method.sig.output(),
1649                    expected,
1650                    args,
1651                    method.sig.c_variadic,
1652                    TupleArgumentsFlag::DontTupleArguments,
1653                    Some(method.def_id),
1654                );
1655
1656                self.check_call_abi(method.sig.abi, expr.span);
1657
1658                method.sig.output()
1659            }
1660            Err(error) => {
1661                let guar = self.report_method_error(expr.hir_id, rcvr_t, error, expected, false);
1662
1663                let err_inputs = self.err_args(args.len(), guar);
1664                let err_output = Ty::new_error(self.tcx, guar);
1665
1666                self.check_argument_types(
1667                    segment.ident.span,
1668                    expr,
1669                    &err_inputs,
1670                    err_output,
1671                    NoExpectation,
1672                    args,
1673                    false,
1674                    TupleArgumentsFlag::DontTupleArguments,
1675                    None,
1676                );
1677
1678                err_output
1679            }
1680        }
1681    }
1682
1683    /// Checks use `x.use`.
1684    fn check_expr_use(
1685        &self,
1686        used_expr: &'tcx hir::Expr<'tcx>,
1687        expected: Expectation<'tcx>,
1688    ) -> Ty<'tcx> {
1689        self.check_expr_with_expectation(used_expr, expected)
1690    }
1691
1692    fn check_expr_cast(
1693        &self,
1694        e: &'tcx hir::Expr<'tcx>,
1695        t: &'tcx hir::Ty<'tcx>,
1696        expr: &'tcx hir::Expr<'tcx>,
1697    ) -> Ty<'tcx> {
1698        // Find the type of `e`. Supply hints based on the type we are casting to,
1699        // if appropriate.
1700        let t_cast = self.lower_ty_saving_user_provided_ty(t);
1701        let t_cast = self.resolve_vars_if_possible(t_cast);
1702        let t_expr = self.check_expr_with_expectation(e, ExpectCastableToType(t_cast));
1703        let t_expr = self.resolve_vars_if_possible(t_expr);
1704
1705        // Eagerly check for some obvious errors.
1706        if let Err(guar) = (t_expr, t_cast).error_reported() {
1707            Ty::new_error(self.tcx, guar)
1708        } else {
1709            // Defer other checks until we're done type checking.
1710            let mut deferred_cast_checks = self.deferred_cast_checks.borrow_mut();
1711            match cast::CastCheck::new(self, e, t_expr, t_cast, t.span, expr.span) {
1712                Ok(cast_check) => {
1713                    debug!(
1714                        "check_expr_cast: deferring cast from {:?} to {:?}: {:?}",
1715                        t_cast, t_expr, cast_check,
1716                    );
1717                    deferred_cast_checks.push(cast_check);
1718                    t_cast
1719                }
1720                Err(guar) => Ty::new_error(self.tcx, guar),
1721            }
1722        }
1723    }
1724
1725    fn check_expr_unsafe_binder_cast(
1726        &self,
1727        span: Span,
1728        kind: ast::UnsafeBinderCastKind,
1729        inner_expr: &'tcx hir::Expr<'tcx>,
1730        hir_ty: Option<&'tcx hir::Ty<'tcx>>,
1731        expected: Expectation<'tcx>,
1732    ) -> Ty<'tcx> {
1733        match kind {
1734            ast::UnsafeBinderCastKind::Wrap => {
1735                let ascribed_ty =
1736                    hir_ty.map(|hir_ty| self.lower_ty_saving_user_provided_ty(hir_ty));
1737                let expected_ty = expected.only_has_type(self);
1738                let binder_ty = match (ascribed_ty, expected_ty) {
1739                    (Some(ascribed_ty), Some(expected_ty)) => {
1740                        self.demand_eqtype(inner_expr.span, expected_ty, ascribed_ty);
1741                        expected_ty
1742                    }
1743                    (Some(ty), None) | (None, Some(ty)) => ty,
1744                    // This will always cause a structural resolve error, but we do it
1745                    // so we don't need to manually report an E0282 both on this codepath
1746                    // and in the others; it all happens in `structurally_resolve_type`.
1747                    (None, None) => self.next_ty_var(inner_expr.span),
1748                };
1749
1750                let binder_ty = self.structurally_resolve_type(inner_expr.span, binder_ty);
1751                let hint_ty = match *binder_ty.kind() {
1752                    ty::UnsafeBinder(binder) => self.instantiate_binder_with_fresh_vars(
1753                        inner_expr.span,
1754                        infer::BoundRegionConversionTime::HigherRankedType,
1755                        binder.into(),
1756                    ),
1757                    ty::Error(e) => Ty::new_error(self.tcx, e),
1758                    _ => {
1759                        let guar = self
1760                            .dcx()
1761                            .struct_span_err(
1762                                hir_ty.map_or(span, |hir_ty| hir_ty.span),
1763                                format!(
1764                                    "`wrap_binder!()` can only wrap into unsafe binder, not {}",
1765                                    binder_ty.sort_string(self.tcx)
1766                                ),
1767                            )
1768                            .with_note("unsafe binders are the only valid output of wrap")
1769                            .emit();
1770                        Ty::new_error(self.tcx, guar)
1771                    }
1772                };
1773
1774                self.check_expr_has_type_or_error(inner_expr, hint_ty, |_| {});
1775
1776                binder_ty
1777            }
1778            ast::UnsafeBinderCastKind::Unwrap => {
1779                let ascribed_ty =
1780                    hir_ty.map(|hir_ty| self.lower_ty_saving_user_provided_ty(hir_ty));
1781                let hint_ty = ascribed_ty.unwrap_or_else(|| self.next_ty_var(inner_expr.span));
1782                // FIXME(unsafe_binders): coerce here if needed?
1783                let binder_ty = self.check_expr_has_type_or_error(inner_expr, hint_ty, |_| {});
1784
1785                // Unwrap the binder. This will be ambiguous if it's an infer var, and will error
1786                // if it's not an unsafe binder.
1787                let binder_ty = self.structurally_resolve_type(inner_expr.span, binder_ty);
1788                match *binder_ty.kind() {
1789                    ty::UnsafeBinder(binder) => self.instantiate_binder_with_fresh_vars(
1790                        inner_expr.span,
1791                        infer::BoundRegionConversionTime::HigherRankedType,
1792                        binder.into(),
1793                    ),
1794                    ty::Error(e) => Ty::new_error(self.tcx, e),
1795                    _ => {
1796                        let guar = self
1797                            .dcx()
1798                            .struct_span_err(
1799                                hir_ty.map_or(inner_expr.span, |hir_ty| hir_ty.span),
1800                                format!(
1801                                    "expected unsafe binder, found {} as input of \
1802                                    `unwrap_binder!()`",
1803                                    binder_ty.sort_string(self.tcx)
1804                                ),
1805                            )
1806                            .with_note("only an unsafe binder type can be unwrapped")
1807                            .emit();
1808                        Ty::new_error(self.tcx, guar)
1809                    }
1810                }
1811            }
1812        }
1813    }
1814
1815    fn check_expr_array(
1816        &self,
1817        args: &'tcx [hir::Expr<'tcx>],
1818        expected: Expectation<'tcx>,
1819        expr: &'tcx hir::Expr<'tcx>,
1820    ) -> Ty<'tcx> {
1821        let element_ty = if !args.is_empty() {
1822            // This shouldn't happen unless there's another error
1823            // (e.g., never patterns in inappropriate contexts).
1824            if self.diverges.get() != Diverges::Maybe {
1825                self.dcx()
1826                    .struct_span_err(expr.span, "unexpected divergence state in checking array")
1827                    .delay_as_bug();
1828            }
1829
1830            let coerce_to = expected
1831                .to_option(self)
1832                .and_then(|uty| self.try_structurally_resolve_type(expr.span, uty).builtin_index())
1833                .unwrap_or_else(|| self.next_ty_var(expr.span));
1834            let mut coerce = CoerceMany::with_coercion_sites(coerce_to, args);
1835
1836            for e in args {
1837                let e_ty = self.check_expr_with_hint(e, coerce_to);
1838                let cause = self.misc(e.span);
1839                coerce.coerce(self, &cause, e, e_ty);
1840            }
1841            coerce.complete(self)
1842        } else {
1843            self.next_ty_var(expr.span)
1844        };
1845        let array_len = args.len() as u64;
1846        self.suggest_array_len(expr, array_len);
1847        Ty::new_array(self.tcx, element_ty, array_len)
1848    }
1849
1850    fn suggest_array_len(&self, expr: &'tcx hir::Expr<'tcx>, array_len: u64) {
1851        let parent_node = self.tcx.hir_parent_iter(expr.hir_id).find(|(_, node)| {
1852            !matches!(node, hir::Node::Expr(hir::Expr { kind: hir::ExprKind::AddrOf(..), .. }))
1853        });
1854        let Some((_, hir::Node::LetStmt(hir::LetStmt { ty: Some(ty), .. }))) = parent_node else {
1855            return;
1856        };
1857        if let hir::TyKind::Array(_, ct) = ty.peel_refs().kind {
1858            let span = ct.span();
1859            self.dcx().try_steal_modify_and_emit_err(
1860                span,
1861                StashKey::UnderscoreForArrayLengths,
1862                |err| {
1863                    err.span_suggestion(
1864                        span,
1865                        "consider specifying the array length",
1866                        array_len,
1867                        Applicability::MaybeIncorrect,
1868                    );
1869                },
1870            );
1871        }
1872    }
1873
1874    pub(super) fn check_expr_const_block(
1875        &self,
1876        block: &'tcx hir::ConstBlock,
1877        expected: Expectation<'tcx>,
1878    ) -> Ty<'tcx> {
1879        let body = self.tcx.hir_body(block.body);
1880
1881        // Create a new function context.
1882        let def_id = block.def_id;
1883        let fcx = FnCtxt::new(self, self.param_env, def_id);
1884
1885        let ty = fcx.check_expr_with_expectation(body.value, expected);
1886        fcx.require_type_is_sized(ty, body.value.span, ObligationCauseCode::SizedConstOrStatic);
1887        fcx.write_ty(block.hir_id, ty);
1888        ty
1889    }
1890
1891    fn check_expr_repeat(
1892        &self,
1893        element: &'tcx hir::Expr<'tcx>,
1894        count: &'tcx hir::ConstArg<'tcx>,
1895        expected: Expectation<'tcx>,
1896        expr: &'tcx hir::Expr<'tcx>,
1897    ) -> Ty<'tcx> {
1898        let tcx = self.tcx;
1899        let count_span = count.span();
1900        let count = self.try_structurally_resolve_const(
1901            count_span,
1902            self.normalize(count_span, self.lower_const_arg(count, FeedConstTy::No)),
1903        );
1904
1905        if let Some(count) = count.try_to_target_usize(tcx) {
1906            self.suggest_array_len(expr, count);
1907        }
1908
1909        let uty = match expected {
1910            ExpectHasType(uty) => uty.builtin_index(),
1911            _ => None,
1912        };
1913
1914        let (element_ty, t) = match uty {
1915            Some(uty) => {
1916                self.check_expr_coercible_to_type(element, uty, None);
1917                (uty, uty)
1918            }
1919            None => {
1920                let ty = self.next_ty_var(element.span);
1921                let element_ty = self.check_expr_has_type_or_error(element, ty, |_| {});
1922                (element_ty, ty)
1923            }
1924        };
1925
1926        if let Err(guar) = element_ty.error_reported() {
1927            return Ty::new_error(tcx, guar);
1928        }
1929
1930        // We defer checking whether the element type is `Copy` as it is possible to have
1931        // an inference variable as a repeat count and it seems unlikely that `Copy` would
1932        // have inference side effects required for type checking to succeed.
1933        self.deferred_repeat_expr_checks.borrow_mut().push((element, element_ty, count));
1934
1935        let ty = Ty::new_array_with_const_len(tcx, t, count);
1936        self.register_wf_obligation(ty.into(), expr.span, ObligationCauseCode::WellFormed(None));
1937        ty
1938    }
1939
1940    fn check_expr_tuple(
1941        &self,
1942        elts: &'tcx [hir::Expr<'tcx>],
1943        expected: Expectation<'tcx>,
1944        expr: &'tcx hir::Expr<'tcx>,
1945    ) -> Ty<'tcx> {
1946        let flds = expected.only_has_type(self).and_then(|ty| {
1947            let ty = self.try_structurally_resolve_type(expr.span, ty);
1948            match ty.kind() {
1949                ty::Tuple(flds) => Some(&flds[..]),
1950                _ => None,
1951            }
1952        });
1953
1954        let elt_ts_iter = elts.iter().enumerate().map(|(i, e)| match flds {
1955            Some(fs) if i < fs.len() => {
1956                let ety = fs[i];
1957                self.check_expr_coercible_to_type(e, ety, None);
1958                ety
1959            }
1960            _ => self.check_expr_with_expectation(e, NoExpectation),
1961        });
1962        let tuple = Ty::new_tup_from_iter(self.tcx, elt_ts_iter);
1963        if let Err(guar) = tuple.error_reported() {
1964            Ty::new_error(self.tcx, guar)
1965        } else {
1966            self.require_type_is_sized(
1967                tuple,
1968                expr.span,
1969                ObligationCauseCode::TupleInitializerSized,
1970            );
1971            tuple
1972        }
1973    }
1974
1975    fn check_expr_struct(
1976        &self,
1977        expr: &hir::Expr<'tcx>,
1978        expected: Expectation<'tcx>,
1979        qpath: &'tcx QPath<'tcx>,
1980        fields: &'tcx [hir::ExprField<'tcx>],
1981        base_expr: &'tcx hir::StructTailExpr<'tcx>,
1982    ) -> Ty<'tcx> {
1983        // Find the relevant variant
1984        let (variant, adt_ty) = match self.check_struct_path(qpath, expr.hir_id) {
1985            Ok(data) => data,
1986            Err(guar) => {
1987                self.check_struct_fields_on_error(fields, base_expr);
1988                return Ty::new_error(self.tcx, guar);
1989            }
1990        };
1991
1992        // Prohibit struct expressions when non-exhaustive flag is set.
1993        let adt = adt_ty.ty_adt_def().expect("`check_struct_path` returned non-ADT type");
1994        if variant.field_list_has_applicable_non_exhaustive() {
1995            self.dcx()
1996                .emit_err(StructExprNonExhaustive { span: expr.span, what: adt.variant_descr() });
1997        }
1998
1999        self.check_expr_struct_fields(
2000            adt_ty,
2001            expected,
2002            expr,
2003            qpath.span(),
2004            variant,
2005            fields,
2006            base_expr,
2007        );
2008
2009        self.require_type_is_sized(adt_ty, expr.span, ObligationCauseCode::StructInitializerSized);
2010        adt_ty
2011    }
2012
2013    fn check_expr_struct_fields(
2014        &self,
2015        adt_ty: Ty<'tcx>,
2016        expected: Expectation<'tcx>,
2017        expr: &hir::Expr<'_>,
2018        path_span: Span,
2019        variant: &'tcx ty::VariantDef,
2020        hir_fields: &'tcx [hir::ExprField<'tcx>],
2021        base_expr: &'tcx hir::StructTailExpr<'tcx>,
2022    ) {
2023        let tcx = self.tcx;
2024
2025        let adt_ty = self.try_structurally_resolve_type(path_span, adt_ty);
2026        let adt_ty_hint = expected.only_has_type(self).and_then(|expected| {
2027            self.fudge_inference_if_ok(|| {
2028                let ocx = ObligationCtxt::new(self);
2029                ocx.sup(&self.misc(path_span), self.param_env, expected, adt_ty)?;
2030                if !ocx.try_evaluate_obligations().is_empty() {
2031                    return Err(TypeError::Mismatch);
2032                }
2033                Ok(self.resolve_vars_if_possible(adt_ty))
2034            })
2035            .ok()
2036        });
2037        if let Some(adt_ty_hint) = adt_ty_hint {
2038            // re-link the variables that the fudging above can create.
2039            self.demand_eqtype(path_span, adt_ty_hint, adt_ty);
2040        }
2041
2042        let ty::Adt(adt, args) = adt_ty.kind() else {
2043            span_bug!(path_span, "non-ADT passed to check_expr_struct_fields");
2044        };
2045        let adt_kind = adt.adt_kind();
2046
2047        let mut remaining_fields = variant
2048            .fields
2049            .iter_enumerated()
2050            .map(|(i, field)| (field.ident(tcx).normalize_to_macros_2_0(), (i, field)))
2051            .collect::<UnordMap<_, _>>();
2052
2053        let mut seen_fields = FxHashMap::default();
2054
2055        let mut error_happened = false;
2056
2057        if variant.fields.len() != remaining_fields.len() {
2058            // Some field is defined more than once. Make sure we don't try to
2059            // instantiate this struct in static/const context.
2060            let guar =
2061                self.dcx().span_delayed_bug(expr.span, "struct fields have non-unique names");
2062            self.set_tainted_by_errors(guar);
2063            error_happened = true;
2064        }
2065
2066        // Type-check each field.
2067        for (idx, field) in hir_fields.iter().enumerate() {
2068            let ident = tcx.adjust_ident(field.ident, variant.def_id);
2069            let field_type = if let Some((i, v_field)) = remaining_fields.remove(&ident) {
2070                seen_fields.insert(ident, field.span);
2071                self.write_field_index(field.hir_id, i);
2072
2073                // We don't look at stability attributes on
2074                // struct-like enums (yet...), but it's definitely not
2075                // a bug to have constructed one.
2076                if adt_kind != AdtKind::Enum {
2077                    tcx.check_stability(v_field.did, Some(field.hir_id), field.span, None);
2078                }
2079
2080                self.field_ty(field.span, v_field, args)
2081            } else {
2082                error_happened = true;
2083                let guar = if let Some(prev_span) = seen_fields.get(&ident) {
2084                    self.dcx().emit_err(FieldMultiplySpecifiedInInitializer {
2085                        span: field.ident.span,
2086                        prev_span: *prev_span,
2087                        ident,
2088                    })
2089                } else {
2090                    self.report_unknown_field(
2091                        adt_ty,
2092                        variant,
2093                        expr,
2094                        field,
2095                        hir_fields,
2096                        adt.variant_descr(),
2097                    )
2098                };
2099
2100                Ty::new_error(tcx, guar)
2101            };
2102
2103            // Check that the expected field type is WF. Otherwise, we emit no use-site error
2104            // in the case of coercions for non-WF fields, which leads to incorrect error
2105            // tainting. See issue #126272.
2106            self.register_wf_obligation(
2107                field_type.into(),
2108                field.expr.span,
2109                ObligationCauseCode::WellFormed(None),
2110            );
2111
2112            // Make sure to give a type to the field even if there's
2113            // an error, so we can continue type-checking.
2114            let ty = self.check_expr_with_hint(field.expr, field_type);
2115            let diag = self.demand_coerce_diag(field.expr, ty, field_type, None, AllowTwoPhase::No);
2116
2117            if let Err(diag) = diag {
2118                if idx == hir_fields.len() - 1 {
2119                    if remaining_fields.is_empty() {
2120                        self.suggest_fru_from_range_and_emit(field, variant, args, diag);
2121                    } else {
2122                        diag.stash(field.span, StashKey::MaybeFruTypo);
2123                    }
2124                } else {
2125                    diag.emit();
2126                }
2127            }
2128        }
2129
2130        // Make sure the programmer specified correct number of fields.
2131        if adt_kind == AdtKind::Union && hir_fields.len() != 1 {
2132            struct_span_code_err!(
2133                self.dcx(),
2134                path_span,
2135                E0784,
2136                "union expressions should have exactly one field",
2137            )
2138            .emit();
2139        }
2140
2141        // If check_expr_struct_fields hit an error, do not attempt to populate
2142        // the fields with the base_expr. This could cause us to hit errors later
2143        // when certain fields are assumed to exist that in fact do not.
2144        if error_happened {
2145            if let hir::StructTailExpr::Base(base_expr) = base_expr {
2146                self.check_expr(base_expr);
2147            }
2148            return;
2149        }
2150
2151        if let hir::StructTailExpr::DefaultFields(span) = *base_expr {
2152            let mut missing_mandatory_fields = Vec::new();
2153            let mut missing_optional_fields = Vec::new();
2154            for f in &variant.fields {
2155                let ident = self.tcx.adjust_ident(f.ident(self.tcx), variant.def_id);
2156                if let Some(_) = remaining_fields.remove(&ident) {
2157                    if f.value.is_none() {
2158                        missing_mandatory_fields.push(ident);
2159                    } else {
2160                        missing_optional_fields.push(ident);
2161                    }
2162                }
2163            }
2164            if !self.tcx.features().default_field_values() {
2165                let sugg = self.tcx.crate_level_attribute_injection_span();
2166                self.dcx().emit_err(BaseExpressionDoubleDot {
2167                    span: span.shrink_to_hi(),
2168                    // We only mention enabling the feature if this is a nightly rustc *and* the
2169                    // expression would make sense with the feature enabled.
2170                    default_field_values_suggestion: if self.tcx.sess.is_nightly_build()
2171                        && missing_mandatory_fields.is_empty()
2172                        && !missing_optional_fields.is_empty()
2173                    {
2174                        Some(sugg)
2175                    } else {
2176                        None
2177                    },
2178                    add_expr: if !missing_mandatory_fields.is_empty()
2179                        || !missing_optional_fields.is_empty()
2180                    {
2181                        Some(BaseExpressionDoubleDotAddExpr { span: span.shrink_to_hi() })
2182                    } else {
2183                        None
2184                    },
2185                    remove_dots: if missing_mandatory_fields.is_empty()
2186                        && missing_optional_fields.is_empty()
2187                    {
2188                        Some(BaseExpressionDoubleDotRemove { span })
2189                    } else {
2190                        None
2191                    },
2192                });
2193                return;
2194            }
2195            if variant.fields.is_empty() {
2196                let mut err = self.dcx().struct_span_err(
2197                    span,
2198                    format!(
2199                        "`{adt_ty}` has no fields, `..` needs at least one default field in the \
2200                         struct definition",
2201                    ),
2202                );
2203                err.span_label(path_span, "this type has no fields");
2204                err.emit();
2205            }
2206            if !missing_mandatory_fields.is_empty() {
2207                let s = pluralize!(missing_mandatory_fields.len());
2208                let fields = listify(&missing_mandatory_fields, |f| format!("`{f}`")).unwrap();
2209                self.dcx()
2210                    .struct_span_err(
2211                        span.shrink_to_lo(),
2212                        format!("missing field{s} {fields} in initializer"),
2213                    )
2214                    .with_span_label(
2215                        span.shrink_to_lo(),
2216                        "fields that do not have a defaulted value must be provided explicitly",
2217                    )
2218                    .emit();
2219                return;
2220            }
2221            let fru_tys = match adt_ty.kind() {
2222                ty::Adt(adt, args) if adt.is_struct() => variant
2223                    .fields
2224                    .iter()
2225                    .map(|f| self.normalize(span, f.ty(self.tcx, args)))
2226                    .collect(),
2227                ty::Adt(adt, args) if adt.is_enum() => variant
2228                    .fields
2229                    .iter()
2230                    .map(|f| self.normalize(span, f.ty(self.tcx, args)))
2231                    .collect(),
2232                _ => {
2233                    self.dcx().emit_err(FunctionalRecordUpdateOnNonStruct { span });
2234                    return;
2235                }
2236            };
2237            self.typeck_results.borrow_mut().fru_field_types_mut().insert(expr.hir_id, fru_tys);
2238        } else if let hir::StructTailExpr::Base(base_expr) = base_expr {
2239            // FIXME: We are currently creating two branches here in order to maintain
2240            // consistency. But they should be merged as much as possible.
2241            let fru_tys = if self.tcx.features().type_changing_struct_update() {
2242                if adt.is_struct() {
2243                    // Make some fresh generic parameters for our ADT type.
2244                    let fresh_args = self.fresh_args_for_item(base_expr.span, adt.did());
2245                    // We do subtyping on the FRU fields first, so we can
2246                    // learn exactly what types we expect the base expr
2247                    // needs constrained to be compatible with the struct
2248                    // type we expect from the expectation value.
2249                    let fru_tys = variant
2250                        .fields
2251                        .iter()
2252                        .map(|f| {
2253                            let fru_ty = self
2254                                .normalize(expr.span, self.field_ty(base_expr.span, f, fresh_args));
2255                            let ident = self.tcx.adjust_ident(f.ident(self.tcx), variant.def_id);
2256                            if let Some(_) = remaining_fields.remove(&ident) {
2257                                let target_ty = self.field_ty(base_expr.span, f, args);
2258                                let cause = self.misc(base_expr.span);
2259                                match self.at(&cause, self.param_env).sup(
2260                                    // We're already using inference variables for any params, and don't allow converting
2261                                    // between different structs, so there is no way this ever actually defines an opaque type.
2262                                    // Thus choosing `Yes` is fine.
2263                                    DefineOpaqueTypes::Yes,
2264                                    target_ty,
2265                                    fru_ty,
2266                                ) {
2267                                    Ok(InferOk { obligations, value: () }) => {
2268                                        self.register_predicates(obligations)
2269                                    }
2270                                    Err(_) => {
2271                                        span_bug!(
2272                                            cause.span,
2273                                            "subtyping remaining fields of type changing FRU failed: {target_ty} != {fru_ty}: {}::{}",
2274                                            variant.name,
2275                                            ident.name,
2276                                        );
2277                                    }
2278                                }
2279                            }
2280                            self.resolve_vars_if_possible(fru_ty)
2281                        })
2282                        .collect();
2283                    // The use of fresh args that we have subtyped against
2284                    // our base ADT type's fields allows us to guide inference
2285                    // along so that, e.g.
2286                    // ```
2287                    // MyStruct<'a, F1, F2, const C: usize> {
2288                    //     f: F1,
2289                    //     // Other fields that reference `'a`, `F2`, and `C`
2290                    // }
2291                    //
2292                    // let x = MyStruct {
2293                    //    f: 1usize,
2294                    //    ..other_struct
2295                    // };
2296                    // ```
2297                    // will have the `other_struct` expression constrained to
2298                    // `MyStruct<'a, _, F2, C>`, as opposed to just `_`...
2299                    // This is important to allow coercions to happen in
2300                    // `other_struct` itself. See `coerce-in-base-expr.rs`.
2301                    let fresh_base_ty = Ty::new_adt(self.tcx, *adt, fresh_args);
2302                    self.check_expr_has_type_or_error(
2303                        base_expr,
2304                        self.resolve_vars_if_possible(fresh_base_ty),
2305                        |_| {},
2306                    );
2307                    fru_tys
2308                } else {
2309                    // Check the base_expr, regardless of a bad expected adt_ty, so we can get
2310                    // type errors on that expression, too.
2311                    self.check_expr(base_expr);
2312                    self.dcx().emit_err(FunctionalRecordUpdateOnNonStruct { span: base_expr.span });
2313                    return;
2314                }
2315            } else {
2316                self.check_expr_has_type_or_error(base_expr, adt_ty, |_| {
2317                    let base_ty = self.typeck_results.borrow().expr_ty(*base_expr);
2318                    let same_adt = matches!((adt_ty.kind(), base_ty.kind()),
2319                        (ty::Adt(adt, _), ty::Adt(base_adt, _)) if adt == base_adt);
2320                    if self.tcx.sess.is_nightly_build() && same_adt {
2321                        feature_err(
2322                            &self.tcx.sess,
2323                            sym::type_changing_struct_update,
2324                            base_expr.span,
2325                            "type changing struct updating is experimental",
2326                        )
2327                        .emit();
2328                    }
2329                });
2330                match adt_ty.kind() {
2331                    ty::Adt(adt, args) if adt.is_struct() => variant
2332                        .fields
2333                        .iter()
2334                        .map(|f| self.normalize(expr.span, f.ty(self.tcx, args)))
2335                        .collect(),
2336                    _ => {
2337                        self.dcx()
2338                            .emit_err(FunctionalRecordUpdateOnNonStruct { span: base_expr.span });
2339                        return;
2340                    }
2341                }
2342            };
2343            self.typeck_results.borrow_mut().fru_field_types_mut().insert(expr.hir_id, fru_tys);
2344        } else if adt_kind != AdtKind::Union && !remaining_fields.is_empty() {
2345            debug!(?remaining_fields);
2346            let private_fields: Vec<&ty::FieldDef> = variant
2347                .fields
2348                .iter()
2349                .filter(|field| !field.vis.is_accessible_from(tcx.parent_module(expr.hir_id), tcx))
2350                .collect();
2351
2352            if !private_fields.is_empty() {
2353                self.report_private_fields(
2354                    adt_ty,
2355                    path_span,
2356                    expr.span,
2357                    private_fields,
2358                    hir_fields,
2359                );
2360            } else {
2361                self.report_missing_fields(
2362                    adt_ty,
2363                    path_span,
2364                    expr.span,
2365                    remaining_fields,
2366                    variant,
2367                    hir_fields,
2368                    args,
2369                );
2370            }
2371        }
2372    }
2373
2374    fn check_struct_fields_on_error(
2375        &self,
2376        fields: &'tcx [hir::ExprField<'tcx>],
2377        base_expr: &'tcx hir::StructTailExpr<'tcx>,
2378    ) {
2379        for field in fields {
2380            self.check_expr(field.expr);
2381        }
2382        if let hir::StructTailExpr::Base(base) = *base_expr {
2383            self.check_expr(base);
2384        }
2385    }
2386
2387    /// Report an error for a struct field expression when there are fields which aren't provided.
2388    ///
2389    /// ```text
2390    /// error: missing field `you_can_use_this_field` in initializer of `foo::Foo`
2391    ///  --> src/main.rs:8:5
2392    ///   |
2393    /// 8 |     foo::Foo {};
2394    ///   |     ^^^^^^^^ missing `you_can_use_this_field`
2395    ///
2396    /// error: aborting due to 1 previous error
2397    /// ```
2398    fn report_missing_fields(
2399        &self,
2400        adt_ty: Ty<'tcx>,
2401        span: Span,
2402        full_span: Span,
2403        remaining_fields: UnordMap<Ident, (FieldIdx, &ty::FieldDef)>,
2404        variant: &'tcx ty::VariantDef,
2405        hir_fields: &'tcx [hir::ExprField<'tcx>],
2406        args: GenericArgsRef<'tcx>,
2407    ) {
2408        let len = remaining_fields.len();
2409
2410        let displayable_field_names: Vec<&str> =
2411            remaining_fields.items().map(|(ident, _)| ident.as_str()).into_sorted_stable_ord();
2412
2413        let mut truncated_fields_error = String::new();
2414        let remaining_fields_names = match &displayable_field_names[..] {
2415            [field1] => format!("`{field1}`"),
2416            [field1, field2] => format!("`{field1}` and `{field2}`"),
2417            [field1, field2, field3] => format!("`{field1}`, `{field2}` and `{field3}`"),
2418            _ => {
2419                truncated_fields_error =
2420                    format!(" and {} other field{}", len - 3, pluralize!(len - 3));
2421                displayable_field_names
2422                    .iter()
2423                    .take(3)
2424                    .map(|n| format!("`{n}`"))
2425                    .collect::<Vec<_>>()
2426                    .join(", ")
2427            }
2428        };
2429
2430        let mut err = struct_span_code_err!(
2431            self.dcx(),
2432            span,
2433            E0063,
2434            "missing field{} {}{} in initializer of `{}`",
2435            pluralize!(len),
2436            remaining_fields_names,
2437            truncated_fields_error,
2438            adt_ty
2439        );
2440        err.span_label(span, format!("missing {remaining_fields_names}{truncated_fields_error}"));
2441
2442        if remaining_fields.items().all(|(_, (_, field))| field.value.is_some())
2443            && self.tcx.sess.is_nightly_build()
2444        {
2445            let msg = format!(
2446                "all remaining fields have default values, {you_can} use those values with `..`",
2447                you_can = if self.tcx.features().default_field_values() {
2448                    "you can"
2449                } else {
2450                    "if you added `#![feature(default_field_values)]` to your crate you could"
2451                },
2452            );
2453            if let Some(hir_field) = hir_fields.last() {
2454                err.span_suggestion_verbose(
2455                    hir_field.span.shrink_to_hi(),
2456                    msg,
2457                    ", ..".to_string(),
2458                    Applicability::MachineApplicable,
2459                );
2460            } else if hir_fields.is_empty() {
2461                err.span_suggestion_verbose(
2462                    span.shrink_to_hi().with_hi(full_span.hi()),
2463                    msg,
2464                    " { .. }".to_string(),
2465                    Applicability::MachineApplicable,
2466                );
2467            }
2468        }
2469
2470        if let Some(hir_field) = hir_fields.last() {
2471            self.suggest_fru_from_range_and_emit(hir_field, variant, args, err);
2472        } else {
2473            err.emit();
2474        }
2475    }
2476
2477    /// If the last field is a range literal, but it isn't supposed to be, then they probably
2478    /// meant to use functional update syntax.
2479    fn suggest_fru_from_range_and_emit(
2480        &self,
2481        last_expr_field: &hir::ExprField<'tcx>,
2482        variant: &ty::VariantDef,
2483        args: GenericArgsRef<'tcx>,
2484        mut err: Diag<'_>,
2485    ) {
2486        // I don't use 'is_range_literal' because only double-sided, half-open ranges count.
2487        if let ExprKind::Struct(QPath::LangItem(LangItem::Range, ..), [range_start, range_end], _) =
2488            last_expr_field.expr.kind
2489            && let variant_field =
2490                variant.fields.iter().find(|field| field.ident(self.tcx) == last_expr_field.ident)
2491            && let range_def_id = self.tcx.lang_items().range_struct()
2492            && variant_field
2493                .and_then(|field| field.ty(self.tcx, args).ty_adt_def())
2494                .map(|adt| adt.did())
2495                != range_def_id
2496        {
2497            // Use a (somewhat arbitrary) filtering heuristic to avoid printing
2498            // expressions that are either too long, or have control character
2499            // such as newlines in them.
2500            let expr = self
2501                .tcx
2502                .sess
2503                .source_map()
2504                .span_to_snippet(range_end.expr.span)
2505                .ok()
2506                .filter(|s| s.len() < 25 && !s.contains(|c: char| c.is_control()));
2507
2508            let fru_span = self
2509                .tcx
2510                .sess
2511                .source_map()
2512                .span_extend_while_whitespace(range_start.span)
2513                .shrink_to_hi()
2514                .to(range_end.span);
2515
2516            err.subdiagnostic(TypeMismatchFruTypo { expr_span: range_start.span, fru_span, expr });
2517
2518            // Suppress any range expr type mismatches
2519            self.dcx().try_steal_replace_and_emit_err(
2520                last_expr_field.span,
2521                StashKey::MaybeFruTypo,
2522                err,
2523            );
2524        } else {
2525            err.emit();
2526        }
2527    }
2528
2529    /// Report an error for a struct field expression when there are invisible fields.
2530    ///
2531    /// ```text
2532    /// error: cannot construct `Foo` with struct literal syntax due to private fields
2533    ///  --> src/main.rs:8:5
2534    ///   |
2535    /// 8 |     foo::Foo {};
2536    ///   |     ^^^^^^^^
2537    ///
2538    /// error: aborting due to 1 previous error
2539    /// ```
2540    fn report_private_fields(
2541        &self,
2542        adt_ty: Ty<'tcx>,
2543        span: Span,
2544        expr_span: Span,
2545        private_fields: Vec<&ty::FieldDef>,
2546        used_fields: &'tcx [hir::ExprField<'tcx>],
2547    ) {
2548        let mut err =
2549            self.dcx().struct_span_err(
2550                span,
2551                format!(
2552                    "cannot construct `{adt_ty}` with struct literal syntax due to private fields",
2553                ),
2554            );
2555        let (used_private_fields, remaining_private_fields): (
2556            Vec<(Symbol, Span, bool)>,
2557            Vec<(Symbol, Span, bool)>,
2558        ) = private_fields
2559            .iter()
2560            .map(|field| {
2561                match used_fields.iter().find(|used_field| field.name == used_field.ident.name) {
2562                    Some(used_field) => (field.name, used_field.span, true),
2563                    None => (field.name, self.tcx.def_span(field.did), false),
2564                }
2565            })
2566            .partition(|field| field.2);
2567        err.span_labels(used_private_fields.iter().map(|(_, span, _)| *span), "private field");
2568        if !remaining_private_fields.is_empty() {
2569            let names = if remaining_private_fields.len() > 6 {
2570                String::new()
2571            } else {
2572                format!(
2573                    "{} ",
2574                    listify(&remaining_private_fields, |(name, _, _)| format!("`{name}`"))
2575                        .expect("expected at least one private field to report")
2576                )
2577            };
2578            err.note(format!(
2579                "{}private field{s} {names}that {were} not provided",
2580                if used_fields.is_empty() { "" } else { "...and other " },
2581                s = pluralize!(remaining_private_fields.len()),
2582                were = pluralize!("was", remaining_private_fields.len()),
2583            ));
2584        }
2585
2586        if let ty::Adt(def, _) = adt_ty.kind() {
2587            let def_id = def.did();
2588            let mut items = self
2589                .tcx
2590                .inherent_impls(def_id)
2591                .into_iter()
2592                .flat_map(|i| self.tcx.associated_items(i).in_definition_order())
2593                // Only assoc fn with no receivers.
2594                .filter(|item| item.is_fn() && !item.is_method())
2595                .filter_map(|item| {
2596                    // Only assoc fns that return `Self`
2597                    let fn_sig = self
2598                        .tcx
2599                        .fn_sig(item.def_id)
2600                        .instantiate(self.tcx, self.fresh_args_for_item(span, item.def_id));
2601                    let ret_ty = self.tcx.instantiate_bound_regions_with_erased(fn_sig.output());
2602                    if !self.can_eq(self.param_env, ret_ty, adt_ty) {
2603                        return None;
2604                    }
2605                    let input_len = fn_sig.inputs().skip_binder().len();
2606                    let name = item.name();
2607                    let order = !name.as_str().starts_with("new");
2608                    Some((order, name, input_len))
2609                })
2610                .collect::<Vec<_>>();
2611            items.sort_by_key(|(order, _, _)| *order);
2612            let suggestion = |name, args| {
2613                format!(
2614                    "::{name}({})",
2615                    std::iter::repeat_n("_", args).collect::<Vec<_>>().join(", ")
2616                )
2617            };
2618            match &items[..] {
2619                [] => {}
2620                [(_, name, args)] => {
2621                    err.span_suggestion_verbose(
2622                        span.shrink_to_hi().with_hi(expr_span.hi()),
2623                        format!("you might have meant to use the `{name}` associated function"),
2624                        suggestion(name, *args),
2625                        Applicability::MaybeIncorrect,
2626                    );
2627                }
2628                _ => {
2629                    err.span_suggestions(
2630                        span.shrink_to_hi().with_hi(expr_span.hi()),
2631                        "you might have meant to use an associated function to build this type",
2632                        items.iter().map(|(_, name, args)| suggestion(name, *args)),
2633                        Applicability::MaybeIncorrect,
2634                    );
2635                }
2636            }
2637            if let Some(default_trait) = self.tcx.get_diagnostic_item(sym::Default)
2638                && self
2639                    .infcx
2640                    .type_implements_trait(default_trait, [adt_ty], self.param_env)
2641                    .may_apply()
2642            {
2643                err.multipart_suggestion(
2644                    "consider using the `Default` trait",
2645                    vec![
2646                        (span.shrink_to_lo(), "<".to_string()),
2647                        (
2648                            span.shrink_to_hi().with_hi(expr_span.hi()),
2649                            " as std::default::Default>::default()".to_string(),
2650                        ),
2651                    ],
2652                    Applicability::MaybeIncorrect,
2653                );
2654            }
2655        }
2656
2657        err.emit();
2658    }
2659
2660    fn report_unknown_field(
2661        &self,
2662        ty: Ty<'tcx>,
2663        variant: &'tcx ty::VariantDef,
2664        expr: &hir::Expr<'_>,
2665        field: &hir::ExprField<'_>,
2666        skip_fields: &[hir::ExprField<'_>],
2667        kind_name: &str,
2668    ) -> ErrorGuaranteed {
2669        // we don't care to report errors for a struct if the struct itself is tainted
2670        if let Err(guar) = variant.has_errors() {
2671            return guar;
2672        }
2673        let mut err = self.err_ctxt().type_error_struct_with_diag(
2674            field.ident.span,
2675            |actual| match ty.kind() {
2676                ty::Adt(adt, ..) if adt.is_enum() => struct_span_code_err!(
2677                    self.dcx(),
2678                    field.ident.span,
2679                    E0559,
2680                    "{} `{}::{}` has no field named `{}`",
2681                    kind_name,
2682                    actual,
2683                    variant.name,
2684                    field.ident
2685                ),
2686                _ => struct_span_code_err!(
2687                    self.dcx(),
2688                    field.ident.span,
2689                    E0560,
2690                    "{} `{}` has no field named `{}`",
2691                    kind_name,
2692                    actual,
2693                    field.ident
2694                ),
2695            },
2696            ty,
2697        );
2698
2699        let variant_ident_span = self.tcx.def_ident_span(variant.def_id).unwrap();
2700        match variant.ctor {
2701            Some((CtorKind::Fn, def_id)) => match ty.kind() {
2702                ty::Adt(adt, ..) if adt.is_enum() => {
2703                    err.span_label(
2704                        variant_ident_span,
2705                        format!(
2706                            "`{adt}::{variant}` defined here",
2707                            adt = ty,
2708                            variant = variant.name,
2709                        ),
2710                    );
2711                    err.span_label(field.ident.span, "field does not exist");
2712                    let fn_sig = self.tcx.fn_sig(def_id).instantiate_identity();
2713                    let inputs = fn_sig.inputs().skip_binder();
2714                    let fields = format!(
2715                        "({})",
2716                        inputs.iter().map(|i| format!("/* {i} */")).collect::<Vec<_>>().join(", ")
2717                    );
2718                    let (replace_span, sugg) = match expr.kind {
2719                        hir::ExprKind::Struct(qpath, ..) => {
2720                            (qpath.span().shrink_to_hi().with_hi(expr.span.hi()), fields)
2721                        }
2722                        _ => {
2723                            (expr.span, format!("{ty}::{variant}{fields}", variant = variant.name))
2724                        }
2725                    };
2726                    err.span_suggestion_verbose(
2727                        replace_span,
2728                        format!(
2729                            "`{adt}::{variant}` is a tuple {kind_name}, use the appropriate syntax",
2730                            adt = ty,
2731                            variant = variant.name,
2732                        ),
2733                        sugg,
2734                        Applicability::HasPlaceholders,
2735                    );
2736                }
2737                _ => {
2738                    err.span_label(variant_ident_span, format!("`{ty}` defined here"));
2739                    err.span_label(field.ident.span, "field does not exist");
2740                    let fn_sig = self.tcx.fn_sig(def_id).instantiate_identity();
2741                    let inputs = fn_sig.inputs().skip_binder();
2742                    let fields = format!(
2743                        "({})",
2744                        inputs.iter().map(|i| format!("/* {i} */")).collect::<Vec<_>>().join(", ")
2745                    );
2746                    err.span_suggestion_verbose(
2747                        expr.span,
2748                        format!("`{ty}` is a tuple {kind_name}, use the appropriate syntax",),
2749                        format!("{ty}{fields}"),
2750                        Applicability::HasPlaceholders,
2751                    );
2752                }
2753            },
2754            _ => {
2755                // prevent all specified fields from being suggested
2756                let available_field_names = self.available_field_names(variant, expr, skip_fields);
2757                if let Some(field_name) =
2758                    find_best_match_for_name(&available_field_names, field.ident.name, None)
2759                    && !(field.ident.name.as_str().parse::<usize>().is_ok()
2760                        && field_name.as_str().parse::<usize>().is_ok())
2761                {
2762                    err.span_label(field.ident.span, "unknown field");
2763                    err.span_suggestion_verbose(
2764                        field.ident.span,
2765                        "a field with a similar name exists",
2766                        field_name,
2767                        Applicability::MaybeIncorrect,
2768                    );
2769                } else {
2770                    match ty.kind() {
2771                        ty::Adt(adt, ..) => {
2772                            if adt.is_enum() {
2773                                err.span_label(
2774                                    field.ident.span,
2775                                    format!("`{}::{}` does not have this field", ty, variant.name),
2776                                );
2777                            } else {
2778                                err.span_label(
2779                                    field.ident.span,
2780                                    format!("`{ty}` does not have this field"),
2781                                );
2782                            }
2783                            if available_field_names.is_empty() {
2784                                err.note("all struct fields are already assigned");
2785                            } else {
2786                                err.note(format!(
2787                                    "available fields are: {}",
2788                                    self.name_series_display(available_field_names)
2789                                ));
2790                            }
2791                        }
2792                        _ => bug!("non-ADT passed to report_unknown_field"),
2793                    }
2794                };
2795            }
2796        }
2797        err.emit()
2798    }
2799
2800    fn available_field_names(
2801        &self,
2802        variant: &'tcx ty::VariantDef,
2803        expr: &hir::Expr<'_>,
2804        skip_fields: &[hir::ExprField<'_>],
2805    ) -> Vec<Symbol> {
2806        variant
2807            .fields
2808            .iter()
2809            .filter(|field| {
2810                skip_fields.iter().all(|&skip| skip.ident.name != field.name)
2811                    && self.is_field_suggestable(field, expr.hir_id, expr.span)
2812            })
2813            .map(|field| field.name)
2814            .collect()
2815    }
2816
2817    fn name_series_display(&self, names: Vec<Symbol>) -> String {
2818        // dynamic limit, to never omit just one field
2819        let limit = if names.len() == 6 { 6 } else { 5 };
2820        let mut display =
2821            names.iter().take(limit).map(|n| format!("`{n}`")).collect::<Vec<_>>().join(", ");
2822        if names.len() > limit {
2823            display = format!("{} ... and {} others", display, names.len() - limit);
2824        }
2825        display
2826    }
2827
2828    /// Find the position of a field named `ident` in `base_def`, accounting for unnammed fields.
2829    /// Return whether such a field has been found. The path to it is stored in `nested_fields`.
2830    /// `ident` must have been adjusted beforehand.
2831    fn find_adt_field(
2832        &self,
2833        base_def: ty::AdtDef<'tcx>,
2834        ident: Ident,
2835    ) -> Option<(FieldIdx, &'tcx ty::FieldDef)> {
2836        // No way to find a field in an enum.
2837        if base_def.is_enum() {
2838            return None;
2839        }
2840
2841        for (field_idx, field) in base_def.non_enum_variant().fields.iter_enumerated() {
2842            if field.ident(self.tcx).normalize_to_macros_2_0() == ident {
2843                // We found the field we wanted.
2844                return Some((field_idx, field));
2845            }
2846        }
2847
2848        None
2849    }
2850
2851    /// Check field access expressions, this works for both structs and tuples.
2852    /// Returns the Ty of the field.
2853    ///
2854    /// ```ignore (illustrative)
2855    /// base.field
2856    /// ^^^^^^^^^^ expr
2857    /// ^^^^       base
2858    ///      ^^^^^ field
2859    /// ```
2860    fn check_expr_field(
2861        &self,
2862        expr: &'tcx hir::Expr<'tcx>,
2863        base: &'tcx hir::Expr<'tcx>,
2864        field: Ident,
2865        // The expected type hint of the field.
2866        expected: Expectation<'tcx>,
2867    ) -> Ty<'tcx> {
2868        debug!("check_field(expr: {:?}, base: {:?}, field: {:?})", expr, base, field);
2869        let base_ty = self.check_expr(base);
2870        let base_ty = self.structurally_resolve_type(base.span, base_ty);
2871
2872        // Whether we are trying to access a private field. Used for error reporting.
2873        let mut private_candidate = None;
2874
2875        // Field expressions automatically deref
2876        let mut autoderef = self.autoderef(expr.span, base_ty);
2877        while let Some((deref_base_ty, _)) = autoderef.next() {
2878            debug!("deref_base_ty: {:?}", deref_base_ty);
2879            match deref_base_ty.kind() {
2880                ty::Adt(base_def, args) if !base_def.is_enum() => {
2881                    debug!("struct named {:?}", deref_base_ty);
2882                    // we don't care to report errors for a struct if the struct itself is tainted
2883                    if let Err(guar) = base_def.non_enum_variant().has_errors() {
2884                        return Ty::new_error(self.tcx(), guar);
2885                    }
2886
2887                    let fn_body_hir_id = self.tcx.local_def_id_to_hir_id(self.body_id);
2888                    let (ident, def_scope) =
2889                        self.tcx.adjust_ident_and_get_scope(field, base_def.did(), fn_body_hir_id);
2890
2891                    if let Some((idx, field)) = self.find_adt_field(*base_def, ident) {
2892                        self.write_field_index(expr.hir_id, idx);
2893
2894                        let adjustments = self.adjust_steps(&autoderef);
2895                        if field.vis.is_accessible_from(def_scope, self.tcx) {
2896                            self.apply_adjustments(base, adjustments);
2897                            self.register_predicates(autoderef.into_obligations());
2898
2899                            self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None);
2900                            return self.field_ty(expr.span, field, args);
2901                        }
2902
2903                        // The field is not accessible, fall through to error reporting.
2904                        private_candidate = Some((adjustments, base_def.did()));
2905                    }
2906                }
2907                ty::Tuple(tys) => {
2908                    if let Ok(index) = field.as_str().parse::<usize>() {
2909                        if field.name == sym::integer(index) {
2910                            if let Some(&field_ty) = tys.get(index) {
2911                                let adjustments = self.adjust_steps(&autoderef);
2912                                self.apply_adjustments(base, adjustments);
2913                                self.register_predicates(autoderef.into_obligations());
2914
2915                                self.write_field_index(expr.hir_id, FieldIdx::from_usize(index));
2916                                return field_ty;
2917                            }
2918                        }
2919                    }
2920                }
2921                _ => {}
2922            }
2923        }
2924        // We failed to check the expression, report an error.
2925
2926        // Emits an error if we deref an infer variable, like calling `.field` on a base type
2927        // of `&_`. We can also use this to suppress unnecessary "missing field" errors that
2928        // will follow ambiguity errors.
2929        let final_ty = self.structurally_resolve_type(autoderef.span(), autoderef.final_ty());
2930        if let ty::Error(_) = final_ty.kind() {
2931            return final_ty;
2932        }
2933
2934        if let Some((adjustments, did)) = private_candidate {
2935            // (#90483) apply adjustments to avoid ExprUseVisitor from
2936            // creating erroneous projection.
2937            self.apply_adjustments(base, adjustments);
2938            let guar = self.ban_private_field_access(
2939                expr,
2940                base_ty,
2941                field,
2942                did,
2943                expected.only_has_type(self),
2944            );
2945            return Ty::new_error(self.tcx(), guar);
2946        }
2947
2948        let guar = if self.method_exists_for_diagnostic(
2949            field,
2950            base_ty,
2951            expr.hir_id,
2952            expected.only_has_type(self),
2953        ) {
2954            // If taking a method instead of calling it
2955            self.ban_take_value_of_method(expr, base_ty, field)
2956        } else if !base_ty.is_primitive_ty() {
2957            self.ban_nonexisting_field(field, base, expr, base_ty)
2958        } else {
2959            let field_name = field.to_string();
2960            let mut err = type_error_struct!(
2961                self.dcx(),
2962                field.span,
2963                base_ty,
2964                E0610,
2965                "`{base_ty}` is a primitive type and therefore doesn't have fields",
2966            );
2967            let is_valid_suffix = |field: &str| {
2968                if field == "f32" || field == "f64" {
2969                    return true;
2970                }
2971                let mut chars = field.chars().peekable();
2972                match chars.peek() {
2973                    Some('e') | Some('E') => {
2974                        chars.next();
2975                        if let Some(c) = chars.peek()
2976                            && !c.is_numeric()
2977                            && *c != '-'
2978                            && *c != '+'
2979                        {
2980                            return false;
2981                        }
2982                        while let Some(c) = chars.peek() {
2983                            if !c.is_numeric() {
2984                                break;
2985                            }
2986                            chars.next();
2987                        }
2988                    }
2989                    _ => (),
2990                }
2991                let suffix = chars.collect::<String>();
2992                suffix.is_empty() || suffix == "f32" || suffix == "f64"
2993            };
2994            let maybe_partial_suffix = |field: &str| -> Option<&str> {
2995                let first_chars = ['f', 'l'];
2996                if field.len() >= 1
2997                    && field.to_lowercase().starts_with(first_chars)
2998                    && field[1..].chars().all(|c| c.is_ascii_digit())
2999                {
3000                    if field.to_lowercase().starts_with(['f']) { Some("f32") } else { Some("f64") }
3001                } else {
3002                    None
3003                }
3004            };
3005            if let ty::Infer(ty::IntVar(_)) = base_ty.kind()
3006                && let ExprKind::Lit(Spanned {
3007                    node: ast::LitKind::Int(_, ast::LitIntType::Unsuffixed),
3008                    ..
3009                }) = base.kind
3010                && !base.span.from_expansion()
3011            {
3012                if is_valid_suffix(&field_name) {
3013                    err.span_suggestion_verbose(
3014                        field.span.shrink_to_lo(),
3015                        "if intended to be a floating point literal, consider adding a `0` after the period",
3016                        '0',
3017                        Applicability::MaybeIncorrect,
3018                    );
3019                } else if let Some(correct_suffix) = maybe_partial_suffix(&field_name) {
3020                    err.span_suggestion_verbose(
3021                        field.span,
3022                        format!("if intended to be a floating point literal, consider adding a `0` after the period and a `{correct_suffix}` suffix"),
3023                        format!("0{correct_suffix}"),
3024                        Applicability::MaybeIncorrect,
3025                    );
3026                }
3027            }
3028            err.emit()
3029        };
3030
3031        Ty::new_error(self.tcx(), guar)
3032    }
3033
3034    fn suggest_await_on_field_access(
3035        &self,
3036        err: &mut Diag<'_>,
3037        field_ident: Ident,
3038        base: &'tcx hir::Expr<'tcx>,
3039        ty: Ty<'tcx>,
3040    ) {
3041        let Some(output_ty) = self.err_ctxt().get_impl_future_output_ty(ty) else {
3042            err.span_label(field_ident.span, "unknown field");
3043            return;
3044        };
3045        let ty::Adt(def, _) = output_ty.kind() else {
3046            err.span_label(field_ident.span, "unknown field");
3047            return;
3048        };
3049        // no field access on enum type
3050        if def.is_enum() {
3051            err.span_label(field_ident.span, "unknown field");
3052            return;
3053        }
3054        if !def.non_enum_variant().fields.iter().any(|field| field.ident(self.tcx) == field_ident) {
3055            err.span_label(field_ident.span, "unknown field");
3056            return;
3057        }
3058        err.span_label(
3059            field_ident.span,
3060            "field not available in `impl Future`, but it is available in its `Output`",
3061        );
3062        match self.tcx.coroutine_kind(self.body_id) {
3063            Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) => {
3064                err.span_suggestion_verbose(
3065                    base.span.shrink_to_hi(),
3066                    "consider `await`ing on the `Future` to access the field",
3067                    ".await",
3068                    Applicability::MaybeIncorrect,
3069                );
3070            }
3071            _ => {
3072                let mut span: MultiSpan = base.span.into();
3073                span.push_span_label(self.tcx.def_span(self.body_id), "this is not `async`");
3074                err.span_note(
3075                    span,
3076                    "this implements `Future` and its output type has the field, \
3077                    but the future cannot be awaited in a synchronous function",
3078                );
3079            }
3080        }
3081    }
3082
3083    fn ban_nonexisting_field(
3084        &self,
3085        ident: Ident,
3086        base: &'tcx hir::Expr<'tcx>,
3087        expr: &'tcx hir::Expr<'tcx>,
3088        base_ty: Ty<'tcx>,
3089    ) -> ErrorGuaranteed {
3090        debug!(
3091            "ban_nonexisting_field: field={:?}, base={:?}, expr={:?}, base_ty={:?}",
3092            ident, base, expr, base_ty
3093        );
3094        let mut err = self.no_such_field_err(ident, base_ty, expr);
3095
3096        match *base_ty.peel_refs().kind() {
3097            ty::Array(_, len) => {
3098                self.maybe_suggest_array_indexing(&mut err, base, ident, len);
3099            }
3100            ty::RawPtr(..) => {
3101                self.suggest_first_deref_field(&mut err, base, ident);
3102            }
3103            ty::Param(param_ty) => {
3104                err.span_label(ident.span, "unknown field");
3105                self.point_at_param_definition(&mut err, param_ty);
3106            }
3107            ty::Alias(ty::Opaque, _) => {
3108                self.suggest_await_on_field_access(&mut err, ident, base, base_ty.peel_refs());
3109            }
3110            _ => {
3111                err.span_label(ident.span, "unknown field");
3112            }
3113        }
3114
3115        self.suggest_fn_call(&mut err, base, base_ty, |output_ty| {
3116            if let ty::Adt(def, _) = output_ty.kind()
3117                && !def.is_enum()
3118            {
3119                def.non_enum_variant().fields.iter().any(|field| {
3120                    field.ident(self.tcx) == ident
3121                        && field.vis.is_accessible_from(expr.hir_id.owner.def_id, self.tcx)
3122                })
3123            } else if let ty::Tuple(tys) = output_ty.kind()
3124                && let Ok(idx) = ident.as_str().parse::<usize>()
3125            {
3126                idx < tys.len()
3127            } else {
3128                false
3129            }
3130        });
3131
3132        if ident.name == kw::Await {
3133            // We know by construction that `<expr>.await` is either on Rust 2015
3134            // or results in `ExprKind::Await`. Suggest switching the edition to 2018.
3135            err.note("to `.await` a `Future`, switch to Rust 2018 or later");
3136            HelpUseLatestEdition::new().add_to_diag(&mut err);
3137        }
3138
3139        err.emit()
3140    }
3141
3142    fn ban_private_field_access(
3143        &self,
3144        expr: &hir::Expr<'tcx>,
3145        expr_t: Ty<'tcx>,
3146        field: Ident,
3147        base_did: DefId,
3148        return_ty: Option<Ty<'tcx>>,
3149    ) -> ErrorGuaranteed {
3150        let mut err = self.private_field_err(field, base_did);
3151
3152        // Also check if an accessible method exists, which is often what is meant.
3153        if self.method_exists_for_diagnostic(field, expr_t, expr.hir_id, return_ty)
3154            && !self.expr_in_place(expr.hir_id)
3155        {
3156            self.suggest_method_call(
3157                &mut err,
3158                format!("a method `{field}` also exists, call it with parentheses"),
3159                field,
3160                expr_t,
3161                expr,
3162                None,
3163            );
3164        }
3165        err.emit()
3166    }
3167
3168    fn ban_take_value_of_method(
3169        &self,
3170        expr: &hir::Expr<'tcx>,
3171        expr_t: Ty<'tcx>,
3172        field: Ident,
3173    ) -> ErrorGuaranteed {
3174        let mut err = type_error_struct!(
3175            self.dcx(),
3176            field.span,
3177            expr_t,
3178            E0615,
3179            "attempted to take value of method `{field}` on type `{expr_t}`",
3180        );
3181        err.span_label(field.span, "method, not a field");
3182        let expr_is_call =
3183            if let hir::Node::Expr(hir::Expr { kind: ExprKind::Call(callee, _args), .. }) =
3184                self.tcx.parent_hir_node(expr.hir_id)
3185            {
3186                expr.hir_id == callee.hir_id
3187            } else {
3188                false
3189            };
3190        let expr_snippet =
3191            self.tcx.sess.source_map().span_to_snippet(expr.span).unwrap_or_default();
3192        let is_wrapped = expr_snippet.starts_with('(') && expr_snippet.ends_with(')');
3193        let after_open = expr.span.lo() + rustc_span::BytePos(1);
3194        let before_close = expr.span.hi() - rustc_span::BytePos(1);
3195
3196        if expr_is_call && is_wrapped {
3197            err.multipart_suggestion(
3198                "remove wrapping parentheses to call the method",
3199                vec![
3200                    (expr.span.with_hi(after_open), String::new()),
3201                    (expr.span.with_lo(before_close), String::new()),
3202                ],
3203                Applicability::MachineApplicable,
3204            );
3205        } else if !self.expr_in_place(expr.hir_id) {
3206            // Suggest call parentheses inside the wrapping parentheses
3207            let span = if is_wrapped {
3208                expr.span.with_lo(after_open).with_hi(before_close)
3209            } else {
3210                expr.span
3211            };
3212            self.suggest_method_call(
3213                &mut err,
3214                "use parentheses to call the method",
3215                field,
3216                expr_t,
3217                expr,
3218                Some(span),
3219            );
3220        } else if let ty::RawPtr(ptr_ty, _) = expr_t.kind()
3221            && let ty::Adt(adt_def, _) = ptr_ty.kind()
3222            && let ExprKind::Field(base_expr, _) = expr.kind
3223            && let [variant] = &adt_def.variants().raw
3224            && variant.fields.iter().any(|f| f.ident(self.tcx) == field)
3225        {
3226            err.multipart_suggestion(
3227                "to access the field, dereference first",
3228                vec![
3229                    (base_expr.span.shrink_to_lo(), "(*".to_string()),
3230                    (base_expr.span.shrink_to_hi(), ")".to_string()),
3231                ],
3232                Applicability::MaybeIncorrect,
3233            );
3234        } else {
3235            err.help("methods are immutable and cannot be assigned to");
3236        }
3237
3238        // See `StashKey::GenericInFieldExpr` for more info
3239        self.dcx().try_steal_replace_and_emit_err(field.span, StashKey::GenericInFieldExpr, err)
3240    }
3241
3242    fn point_at_param_definition(&self, err: &mut Diag<'_>, param: ty::ParamTy) {
3243        let generics = self.tcx.generics_of(self.body_id);
3244        let generic_param = generics.type_param(param, self.tcx);
3245        if let ty::GenericParamDefKind::Type { synthetic: true, .. } = generic_param.kind {
3246            return;
3247        }
3248        let param_def_id = generic_param.def_id;
3249        let param_hir_id = match param_def_id.as_local() {
3250            Some(x) => self.tcx.local_def_id_to_hir_id(x),
3251            None => return,
3252        };
3253        let param_span = self.tcx.hir_span(param_hir_id);
3254        let param_name = self.tcx.hir_ty_param_name(param_def_id.expect_local());
3255
3256        err.span_label(param_span, format!("type parameter '{param_name}' declared here"));
3257    }
3258
3259    fn maybe_suggest_array_indexing(
3260        &self,
3261        err: &mut Diag<'_>,
3262        base: &hir::Expr<'_>,
3263        field: Ident,
3264        len: ty::Const<'tcx>,
3265    ) {
3266        err.span_label(field.span, "unknown field");
3267        if let (Some(len), Ok(user_index)) = (
3268            self.try_structurally_resolve_const(base.span, len).try_to_target_usize(self.tcx),
3269            field.as_str().parse::<u64>(),
3270        ) {
3271            let help = "instead of using tuple indexing, use array indexing";
3272            let applicability = if len < user_index {
3273                Applicability::MachineApplicable
3274            } else {
3275                Applicability::MaybeIncorrect
3276            };
3277            err.multipart_suggestion(
3278                help,
3279                vec![
3280                    (base.span.between(field.span), "[".to_string()),
3281                    (field.span.shrink_to_hi(), "]".to_string()),
3282                ],
3283                applicability,
3284            );
3285        }
3286    }
3287
3288    fn suggest_first_deref_field(&self, err: &mut Diag<'_>, base: &hir::Expr<'_>, field: Ident) {
3289        err.span_label(field.span, "unknown field");
3290        let val = if let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span)
3291            && base.len() < 20
3292        {
3293            format!("`{base}`")
3294        } else {
3295            "the value".to_string()
3296        };
3297        err.multipart_suggestion(
3298            format!("{val} is a raw pointer; try dereferencing it"),
3299            vec![
3300                (base.span.shrink_to_lo(), "(*".into()),
3301                (base.span.between(field.span), format!(").")),
3302            ],
3303            Applicability::MaybeIncorrect,
3304        );
3305    }
3306
3307    fn no_such_field_err(
3308        &self,
3309        field: Ident,
3310        base_ty: Ty<'tcx>,
3311        expr: &hir::Expr<'tcx>,
3312    ) -> Diag<'_> {
3313        let span = field.span;
3314        debug!("no_such_field_err(span: {:?}, field: {:?}, expr_t: {:?})", span, field, base_ty);
3315
3316        let mut err = self.dcx().create_err(NoFieldOnType { span, ty: base_ty, field });
3317        if base_ty.references_error() {
3318            err.downgrade_to_delayed_bug();
3319        }
3320
3321        if let Some(within_macro_span) = span.within_macro(expr.span, self.tcx.sess.source_map()) {
3322            err.span_label(within_macro_span, "due to this macro variable");
3323        }
3324
3325        // try to add a suggestion in case the field is a nested field of a field of the Adt
3326        let mod_id = self.tcx.parent_module(expr.hir_id).to_def_id();
3327        let (ty, unwrap) = if let ty::Adt(def, args) = base_ty.kind()
3328            && (self.tcx.is_diagnostic_item(sym::Result, def.did())
3329                || self.tcx.is_diagnostic_item(sym::Option, def.did()))
3330            && let Some(arg) = args.get(0)
3331            && let Some(ty) = arg.as_type()
3332        {
3333            (ty, "unwrap().")
3334        } else {
3335            (base_ty, "")
3336        };
3337        for found_fields in
3338            self.get_field_candidates_considering_privacy_for_diag(span, ty, mod_id, expr.hir_id)
3339        {
3340            let field_names = found_fields.iter().map(|field| field.0.name).collect::<Vec<_>>();
3341            let mut candidate_fields: Vec<_> = found_fields
3342                .into_iter()
3343                .filter_map(|candidate_field| {
3344                    self.check_for_nested_field_satisfying_condition_for_diag(
3345                        span,
3346                        &|candidate_field, _| candidate_field == field,
3347                        candidate_field,
3348                        vec![],
3349                        mod_id,
3350                        expr.hir_id,
3351                    )
3352                })
3353                .map(|mut field_path| {
3354                    field_path.pop();
3355                    field_path.iter().map(|id| format!("{}.", id)).collect::<String>()
3356                })
3357                .collect::<Vec<_>>();
3358            candidate_fields.sort();
3359
3360            let len = candidate_fields.len();
3361            // Don't suggest `.field` if the base expr is from a different
3362            // syntax context than the field.
3363            if len > 0 && expr.span.eq_ctxt(field.span) {
3364                err.span_suggestions(
3365                    field.span.shrink_to_lo(),
3366                    format!(
3367                        "{} of the expressions' fields {} a field of the same name",
3368                        if len > 1 { "some" } else { "one" },
3369                        if len > 1 { "have" } else { "has" },
3370                    ),
3371                    candidate_fields.iter().map(|path| format!("{unwrap}{path}")),
3372                    Applicability::MaybeIncorrect,
3373                );
3374            } else if let Some(field_name) =
3375                find_best_match_for_name(&field_names, field.name, None)
3376                && !(field.name.as_str().parse::<usize>().is_ok()
3377                    && field_name.as_str().parse::<usize>().is_ok())
3378            {
3379                err.span_suggestion_verbose(
3380                    field.span,
3381                    "a field with a similar name exists",
3382                    format!("{unwrap}{}", field_name),
3383                    Applicability::MaybeIncorrect,
3384                );
3385            } else if !field_names.is_empty() {
3386                let is = if field_names.len() == 1 { " is" } else { "s are" };
3387                err.note(
3388                    format!("available field{is}: {}", self.name_series_display(field_names),),
3389                );
3390            }
3391        }
3392        err
3393    }
3394
3395    fn private_field_err(&self, field: Ident, base_did: DefId) -> Diag<'_> {
3396        let struct_path = self.tcx().def_path_str(base_did);
3397        let kind_name = self.tcx().def_descr(base_did);
3398        struct_span_code_err!(
3399            self.dcx(),
3400            field.span,
3401            E0616,
3402            "field `{field}` of {kind_name} `{struct_path}` is private",
3403        )
3404        .with_span_label(field.span, "private field")
3405    }
3406
3407    pub(crate) fn get_field_candidates_considering_privacy_for_diag(
3408        &self,
3409        span: Span,
3410        base_ty: Ty<'tcx>,
3411        mod_id: DefId,
3412        hir_id: HirId,
3413    ) -> Vec<Vec<(Ident, Ty<'tcx>)>> {
3414        debug!("get_field_candidates(span: {:?}, base_t: {:?}", span, base_ty);
3415
3416        let mut autoderef = self.autoderef(span, base_ty).silence_errors();
3417        let deref_chain: Vec<_> = autoderef.by_ref().collect();
3418
3419        // Don't probe if we hit the recursion limit, since it may result in
3420        // quadratic blowup if we then try to further deref the results of this
3421        // function. This is a best-effort method, after all.
3422        if autoderef.reached_recursion_limit() {
3423            return vec![];
3424        }
3425
3426        deref_chain
3427            .into_iter()
3428            .filter_map(move |(base_t, _)| {
3429                match base_t.kind() {
3430                    ty::Adt(base_def, args) if !base_def.is_enum() => {
3431                        let tcx = self.tcx;
3432                        let fields = &base_def.non_enum_variant().fields;
3433                        // Some struct, e.g. some that impl `Deref`, have all private fields
3434                        // because you're expected to deref them to access the _real_ fields.
3435                        // This, for example, will help us suggest accessing a field through a `Box<T>`.
3436                        if fields.iter().all(|field| !field.vis.is_accessible_from(mod_id, tcx)) {
3437                            return None;
3438                        }
3439                        return Some(
3440                            fields
3441                                .iter()
3442                                .filter(move |field| {
3443                                    field.vis.is_accessible_from(mod_id, tcx)
3444                                        && self.is_field_suggestable(field, hir_id, span)
3445                                })
3446                                // For compile-time reasons put a limit on number of fields we search
3447                                .take(100)
3448                                .map(|field_def| {
3449                                    (
3450                                        field_def.ident(self.tcx).normalize_to_macros_2_0(),
3451                                        field_def.ty(self.tcx, args),
3452                                    )
3453                                })
3454                                .collect::<Vec<_>>(),
3455                        );
3456                    }
3457                    ty::Tuple(types) => {
3458                        return Some(
3459                            types
3460                                .iter()
3461                                .enumerate()
3462                                // For compile-time reasons put a limit on number of fields we search
3463                                .take(100)
3464                                .map(|(i, ty)| (Ident::from_str(&i.to_string()), ty))
3465                                .collect::<Vec<_>>(),
3466                        );
3467                    }
3468                    _ => None,
3469                }
3470            })
3471            .collect()
3472    }
3473
3474    /// This method is called after we have encountered a missing field error to recursively
3475    /// search for the field
3476    #[instrument(skip(self, matches, mod_id, hir_id), level = "debug")]
3477    pub(crate) fn check_for_nested_field_satisfying_condition_for_diag(
3478        &self,
3479        span: Span,
3480        matches: &impl Fn(Ident, Ty<'tcx>) -> bool,
3481        (candidate_name, candidate_ty): (Ident, Ty<'tcx>),
3482        mut field_path: Vec<Ident>,
3483        mod_id: DefId,
3484        hir_id: HirId,
3485    ) -> Option<Vec<Ident>> {
3486        if field_path.len() > 3 {
3487            // For compile-time reasons and to avoid infinite recursion we only check for fields
3488            // up to a depth of three
3489            return None;
3490        }
3491        field_path.push(candidate_name);
3492        if matches(candidate_name, candidate_ty) {
3493            return Some(field_path);
3494        }
3495        for nested_fields in self.get_field_candidates_considering_privacy_for_diag(
3496            span,
3497            candidate_ty,
3498            mod_id,
3499            hir_id,
3500        ) {
3501            // recursively search fields of `candidate_field` if it's a ty::Adt
3502            for field in nested_fields {
3503                if let Some(field_path) = self.check_for_nested_field_satisfying_condition_for_diag(
3504                    span,
3505                    matches,
3506                    field,
3507                    field_path.clone(),
3508                    mod_id,
3509                    hir_id,
3510                ) {
3511                    return Some(field_path);
3512                }
3513            }
3514        }
3515        None
3516    }
3517
3518    fn check_expr_index(
3519        &self,
3520        base: &'tcx hir::Expr<'tcx>,
3521        idx: &'tcx hir::Expr<'tcx>,
3522        expr: &'tcx hir::Expr<'tcx>,
3523        brackets_span: Span,
3524    ) -> Ty<'tcx> {
3525        let base_t = self.check_expr(base);
3526        let idx_t = self.check_expr(idx);
3527
3528        if base_t.references_error() {
3529            base_t
3530        } else if idx_t.references_error() {
3531            idx_t
3532        } else {
3533            let base_t = self.structurally_resolve_type(base.span, base_t);
3534            match self.lookup_indexing(expr, base, base_t, idx, idx_t) {
3535                Some((index_ty, element_ty)) => {
3536                    // two-phase not needed because index_ty is never mutable
3537                    self.demand_coerce(idx, idx_t, index_ty, None, AllowTwoPhase::No);
3538                    self.select_obligations_where_possible(|errors| {
3539                        self.point_at_index(errors, idx.span);
3540                    });
3541                    element_ty
3542                }
3543                None => {
3544                    // Attempt to *shallowly* search for an impl which matches,
3545                    // but has nested obligations which are unsatisfied.
3546                    for (base_t, _) in self.autoderef(base.span, base_t).silence_errors() {
3547                        if let Some((_, index_ty, element_ty)) =
3548                            self.find_and_report_unsatisfied_index_impl(base, base_t)
3549                        {
3550                            self.demand_coerce(idx, idx_t, index_ty, None, AllowTwoPhase::No);
3551                            return element_ty;
3552                        }
3553                    }
3554
3555                    let mut err = type_error_struct!(
3556                        self.dcx(),
3557                        brackets_span,
3558                        base_t,
3559                        E0608,
3560                        "cannot index into a value of type `{base_t}`",
3561                    );
3562                    // Try to give some advice about indexing tuples.
3563                    if let ty::Tuple(types) = base_t.kind() {
3564                        err.help(
3565                            "tuples are indexed with a dot and a literal index: `tuple.0`, `tuple.1`, etc.",
3566                        );
3567                        // If index is an unsuffixed integer, show the fixed expression:
3568                        if let ExprKind::Lit(lit) = idx.kind
3569                            && let ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) = lit.node
3570                            && i.get() < types.len().try_into().expect("tuple length fits in u128")
3571                        {
3572                            err.span_suggestion(
3573                                brackets_span,
3574                                format!("to access tuple element `{i}`, use"),
3575                                format!(".{i}"),
3576                                Applicability::MachineApplicable,
3577                            );
3578                        }
3579                    }
3580
3581                    if base_t.is_raw_ptr() && idx_t.is_integral() {
3582                        err.multipart_suggestion(
3583                            "consider using `wrapping_add` or `add` for indexing into raw pointer",
3584                            vec![
3585                                (base.span.between(idx.span), ".wrapping_add(".to_owned()),
3586                                (
3587                                    idx.span.shrink_to_hi().until(expr.span.shrink_to_hi()),
3588                                    ")".to_owned(),
3589                                ),
3590                            ],
3591                            Applicability::MaybeIncorrect,
3592                        );
3593                    }
3594
3595                    let reported = err.emit();
3596                    Ty::new_error(self.tcx, reported)
3597                }
3598            }
3599        }
3600    }
3601
3602    /// Try to match an implementation of `Index` against a self type, and report
3603    /// the unsatisfied predicates that result from confirming this impl.
3604    ///
3605    /// Given an index expression, sometimes the `Self` type shallowly but does not
3606    /// deeply satisfy an impl predicate. Instead of simply saying that the type
3607    /// does not support being indexed, we want to point out exactly what nested
3608    /// predicates cause this to be, so that the user can add them to fix their code.
3609    fn find_and_report_unsatisfied_index_impl(
3610        &self,
3611        base_expr: &hir::Expr<'_>,
3612        base_ty: Ty<'tcx>,
3613    ) -> Option<(ErrorGuaranteed, Ty<'tcx>, Ty<'tcx>)> {
3614        let index_trait_def_id = self.tcx.lang_items().index_trait()?;
3615        let index_trait_output_def_id = self.tcx.get_diagnostic_item(sym::IndexOutput)?;
3616
3617        let mut relevant_impls = vec![];
3618        self.tcx.for_each_relevant_impl(index_trait_def_id, base_ty, |impl_def_id| {
3619            relevant_impls.push(impl_def_id);
3620        });
3621        let [impl_def_id] = relevant_impls[..] else {
3622            // Only report unsatisfied impl predicates if there's one impl
3623            return None;
3624        };
3625
3626        self.commit_if_ok(|snapshot| {
3627            let outer_universe = self.universe();
3628
3629            let ocx = ObligationCtxt::new_with_diagnostics(self);
3630            let impl_args = self.fresh_args_for_item(base_expr.span, impl_def_id);
3631            let impl_trait_ref =
3632                self.tcx.impl_trait_ref(impl_def_id).unwrap().instantiate(self.tcx, impl_args);
3633            let cause = self.misc(base_expr.span);
3634
3635            // Match the impl self type against the base ty. If this fails,
3636            // we just skip this impl, since it's not particularly useful.
3637            let impl_trait_ref = ocx.normalize(&cause, self.param_env, impl_trait_ref);
3638            ocx.eq(&cause, self.param_env, base_ty, impl_trait_ref.self_ty())?;
3639
3640            // Register the impl's predicates. One of these predicates
3641            // must be unsatisfied, or else we wouldn't have gotten here
3642            // in the first place.
3643            ocx.register_obligations(traits::predicates_for_generics(
3644                |idx, span| {
3645                    cause.clone().derived_cause(
3646                        ty::Binder::dummy(ty::TraitPredicate {
3647                            trait_ref: impl_trait_ref,
3648                            polarity: ty::PredicatePolarity::Positive,
3649                        }),
3650                        |derived| {
3651                            ObligationCauseCode::ImplDerived(Box::new(traits::ImplDerivedCause {
3652                                derived,
3653                                impl_or_alias_def_id: impl_def_id,
3654                                impl_def_predicate_index: Some(idx),
3655                                span,
3656                            }))
3657                        },
3658                    )
3659                },
3660                self.param_env,
3661                self.tcx.predicates_of(impl_def_id).instantiate(self.tcx, impl_args),
3662            ));
3663
3664            // Normalize the output type, which we can use later on as the
3665            // return type of the index expression...
3666            let element_ty = ocx.normalize(
3667                &cause,
3668                self.param_env,
3669                Ty::new_projection_from_args(
3670                    self.tcx,
3671                    index_trait_output_def_id,
3672                    impl_trait_ref.args,
3673                ),
3674            );
3675
3676            let true_errors = ocx.try_evaluate_obligations();
3677
3678            // Do a leak check -- we can't really report a useful error here,
3679            // but it at least avoids an ICE when the error has to do with higher-ranked
3680            // lifetimes.
3681            self.leak_check(outer_universe, Some(snapshot))?;
3682
3683            // Bail if we have ambiguity errors, which we can't report in a useful way.
3684            let ambiguity_errors = ocx.evaluate_obligations_error_on_ambiguity();
3685            if true_errors.is_empty() && !ambiguity_errors.is_empty() {
3686                return Err(NoSolution);
3687            }
3688
3689            // There should be at least one error reported. If not, we
3690            // will still delay a span bug in `report_fulfillment_errors`.
3691            Ok::<_, NoSolution>((
3692                self.err_ctxt().report_fulfillment_errors(true_errors),
3693                impl_trait_ref.args.type_at(1),
3694                element_ty,
3695            ))
3696        })
3697        .ok()
3698    }
3699
3700    fn point_at_index(&self, errors: &mut Vec<traits::FulfillmentError<'tcx>>, span: Span) {
3701        let mut seen_preds = FxHashSet::default();
3702        // We re-sort here so that the outer most root obligations comes first, as we have the
3703        // subsequent weird logic to identify *every* relevant obligation for proper deduplication
3704        // of diagnostics.
3705        errors.sort_by_key(|error| error.root_obligation.recursion_depth);
3706        for error in errors {
3707            match (
3708                error.root_obligation.predicate.kind().skip_binder(),
3709                error.obligation.predicate.kind().skip_binder(),
3710            ) {
3711                (ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)), _)
3712                    if self.tcx.is_lang_item(predicate.trait_ref.def_id, LangItem::Index) =>
3713                {
3714                    seen_preds.insert(error.obligation.predicate.kind().skip_binder());
3715                }
3716                (_, ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)))
3717                    if self.tcx.is_diagnostic_item(sym::SliceIndex, predicate.trait_ref.def_id) =>
3718                {
3719                    seen_preds.insert(error.obligation.predicate.kind().skip_binder());
3720                }
3721                (root, pred) if seen_preds.contains(&pred) || seen_preds.contains(&root) => {}
3722                _ => continue,
3723            }
3724            error.obligation.cause.span = span;
3725        }
3726    }
3727
3728    fn check_expr_yield(
3729        &self,
3730        value: &'tcx hir::Expr<'tcx>,
3731        expr: &'tcx hir::Expr<'tcx>,
3732    ) -> Ty<'tcx> {
3733        match self.coroutine_types {
3734            Some(CoroutineTypes { resume_ty, yield_ty }) => {
3735                self.check_expr_coercible_to_type(value, yield_ty, None);
3736
3737                resume_ty
3738            }
3739            _ => {
3740                self.dcx().emit_err(YieldExprOutsideOfCoroutine { span: expr.span });
3741                // Avoid expressions without types during writeback (#78653).
3742                self.check_expr(value);
3743                self.tcx.types.unit
3744            }
3745        }
3746    }
3747
3748    fn check_expr_asm_operand(&self, expr: &'tcx hir::Expr<'tcx>, is_input: bool) {
3749        let needs = if is_input { Needs::None } else { Needs::MutPlace };
3750        let ty = self.check_expr_with_needs(expr, needs);
3751        self.require_type_is_sized(ty, expr.span, ObligationCauseCode::InlineAsmSized);
3752
3753        if !is_input && !expr.is_syntactic_place_expr() {
3754            self.dcx()
3755                .struct_span_err(expr.span, "invalid asm output")
3756                .with_span_label(expr.span, "cannot assign to this expression")
3757                .emit();
3758        }
3759
3760        // If this is an input value, we require its type to be fully resolved
3761        // at this point. This allows us to provide helpful coercions which help
3762        // pass the type candidate list in a later pass.
3763        //
3764        // We don't require output types to be resolved at this point, which
3765        // allows them to be inferred based on how they are used later in the
3766        // function.
3767        if is_input {
3768            let ty = self.structurally_resolve_type(expr.span, ty);
3769            match *ty.kind() {
3770                ty::FnDef(..) => {
3771                    let fnptr_ty = Ty::new_fn_ptr(self.tcx, ty.fn_sig(self.tcx));
3772                    self.demand_coerce(expr, ty, fnptr_ty, None, AllowTwoPhase::No);
3773                }
3774                ty::Ref(_, base_ty, mutbl) => {
3775                    let ptr_ty = Ty::new_ptr(self.tcx, base_ty, mutbl);
3776                    self.demand_coerce(expr, ty, ptr_ty, None, AllowTwoPhase::No);
3777                }
3778                _ => {}
3779            }
3780        }
3781    }
3782
3783    fn check_expr_asm(&self, asm: &'tcx hir::InlineAsm<'tcx>, span: Span) -> Ty<'tcx> {
3784        if let rustc_ast::AsmMacro::NakedAsm = asm.asm_macro {
3785            if !find_attr!(self.tcx.get_all_attrs(self.body_id), AttributeKind::Naked(..)) {
3786                self.tcx.dcx().emit_err(NakedAsmOutsideNakedFn { span });
3787            }
3788        }
3789
3790        let mut diverge = asm.asm_macro.diverges(asm.options);
3791
3792        for (op, _op_sp) in asm.operands {
3793            match *op {
3794                hir::InlineAsmOperand::In { expr, .. } => {
3795                    self.check_expr_asm_operand(expr, true);
3796                }
3797                hir::InlineAsmOperand::Out { expr: Some(expr), .. }
3798                | hir::InlineAsmOperand::InOut { expr, .. } => {
3799                    self.check_expr_asm_operand(expr, false);
3800                }
3801                hir::InlineAsmOperand::Out { expr: None, .. } => {}
3802                hir::InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
3803                    self.check_expr_asm_operand(in_expr, true);
3804                    if let Some(out_expr) = out_expr {
3805                        self.check_expr_asm_operand(out_expr, false);
3806                    }
3807                }
3808                hir::InlineAsmOperand::Const { ref anon_const } => {
3809                    self.check_expr_const_block(anon_const, Expectation::NoExpectation);
3810                }
3811                hir::InlineAsmOperand::SymFn { expr } => {
3812                    self.check_expr(expr);
3813                }
3814                hir::InlineAsmOperand::SymStatic { .. } => {}
3815                hir::InlineAsmOperand::Label { block } => {
3816                    let previous_diverges = self.diverges.get();
3817
3818                    // The label blocks should have unit return value or diverge.
3819                    let ty = self.check_expr_block(block, ExpectHasType(self.tcx.types.unit));
3820                    if !ty.is_never() {
3821                        self.demand_suptype(block.span, self.tcx.types.unit, ty);
3822                        diverge = false;
3823                    }
3824
3825                    // We need this to avoid false unreachable warning when a label diverges.
3826                    self.diverges.set(previous_diverges);
3827                }
3828            }
3829        }
3830
3831        if diverge { self.tcx.types.never } else { self.tcx.types.unit }
3832    }
3833
3834    fn check_expr_offset_of(
3835        &self,
3836        container: &'tcx hir::Ty<'tcx>,
3837        fields: &[Ident],
3838        expr: &'tcx hir::Expr<'tcx>,
3839    ) -> Ty<'tcx> {
3840        let container = self.lower_ty(container).normalized;
3841
3842        let mut field_indices = Vec::with_capacity(fields.len());
3843        let mut current_container = container;
3844        let mut fields = fields.into_iter();
3845
3846        while let Some(&field) = fields.next() {
3847            let container = self.structurally_resolve_type(expr.span, current_container);
3848
3849            match container.kind() {
3850                ty::Adt(container_def, args) if container_def.is_enum() => {
3851                    let block = self.tcx.local_def_id_to_hir_id(self.body_id);
3852                    let (ident, _def_scope) =
3853                        self.tcx.adjust_ident_and_get_scope(field, container_def.did(), block);
3854
3855                    if !self.tcx.features().offset_of_enum() {
3856                        rustc_session::parse::feature_err(
3857                            &self.tcx.sess,
3858                            sym::offset_of_enum,
3859                            ident.span,
3860                            "using enums in offset_of is experimental",
3861                        )
3862                        .emit();
3863                    }
3864
3865                    let Some((index, variant)) = container_def
3866                        .variants()
3867                        .iter_enumerated()
3868                        .find(|(_, v)| v.ident(self.tcx).normalize_to_macros_2_0() == ident)
3869                    else {
3870                        self.dcx()
3871                            .create_err(NoVariantNamed { span: ident.span, ident, ty: container })
3872                            .with_span_label(field.span, "variant not found")
3873                            .emit_unless_delay(container.references_error());
3874                        break;
3875                    };
3876                    let Some(&subfield) = fields.next() else {
3877                        type_error_struct!(
3878                            self.dcx(),
3879                            ident.span,
3880                            container,
3881                            E0795,
3882                            "`{ident}` is an enum variant; expected field at end of `offset_of`",
3883                        )
3884                        .with_span_label(field.span, "enum variant")
3885                        .emit();
3886                        break;
3887                    };
3888                    let (subident, sub_def_scope) =
3889                        self.tcx.adjust_ident_and_get_scope(subfield, variant.def_id, block);
3890
3891                    let Some((subindex, field)) = variant
3892                        .fields
3893                        .iter_enumerated()
3894                        .find(|(_, f)| f.ident(self.tcx).normalize_to_macros_2_0() == subident)
3895                    else {
3896                        self.dcx()
3897                            .create_err(NoFieldOnVariant {
3898                                span: ident.span,
3899                                container,
3900                                ident,
3901                                field: subfield,
3902                                enum_span: field.span,
3903                                field_span: subident.span,
3904                            })
3905                            .emit_unless_delay(container.references_error());
3906                        break;
3907                    };
3908
3909                    let field_ty = self.field_ty(expr.span, field, args);
3910
3911                    // Enums are anyway always sized. But just to safeguard against future
3912                    // language extensions, let's double-check.
3913                    self.require_type_is_sized(
3914                        field_ty,
3915                        expr.span,
3916                        ObligationCauseCode::FieldSized {
3917                            adt_kind: AdtKind::Enum,
3918                            span: self.tcx.def_span(field.did),
3919                            last: false,
3920                        },
3921                    );
3922
3923                    if field.vis.is_accessible_from(sub_def_scope, self.tcx) {
3924                        self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None);
3925                    } else {
3926                        self.private_field_err(ident, container_def.did()).emit();
3927                    }
3928
3929                    // Save the index of all fields regardless of their visibility in case
3930                    // of error recovery.
3931                    field_indices.push((index, subindex));
3932                    current_container = field_ty;
3933
3934                    continue;
3935                }
3936                ty::Adt(container_def, args) => {
3937                    let block = self.tcx.local_def_id_to_hir_id(self.body_id);
3938                    let (ident, def_scope) =
3939                        self.tcx.adjust_ident_and_get_scope(field, container_def.did(), block);
3940
3941                    let fields = &container_def.non_enum_variant().fields;
3942                    if let Some((index, field)) = fields
3943                        .iter_enumerated()
3944                        .find(|(_, f)| f.ident(self.tcx).normalize_to_macros_2_0() == ident)
3945                    {
3946                        let field_ty = self.field_ty(expr.span, field, args);
3947
3948                        if self.tcx.features().offset_of_slice() {
3949                            self.require_type_has_static_alignment(field_ty, expr.span);
3950                        } else {
3951                            self.require_type_is_sized(
3952                                field_ty,
3953                                expr.span,
3954                                ObligationCauseCode::Misc,
3955                            );
3956                        }
3957
3958                        if field.vis.is_accessible_from(def_scope, self.tcx) {
3959                            self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None);
3960                        } else {
3961                            self.private_field_err(ident, container_def.did()).emit();
3962                        }
3963
3964                        // Save the index of all fields regardless of their visibility in case
3965                        // of error recovery.
3966                        field_indices.push((FIRST_VARIANT, index));
3967                        current_container = field_ty;
3968
3969                        continue;
3970                    }
3971                }
3972                ty::Tuple(tys) => {
3973                    if let Ok(index) = field.as_str().parse::<usize>()
3974                        && field.name == sym::integer(index)
3975                    {
3976                        if let Some(&field_ty) = tys.get(index) {
3977                            if self.tcx.features().offset_of_slice() {
3978                                self.require_type_has_static_alignment(field_ty, expr.span);
3979                            } else {
3980                                self.require_type_is_sized(
3981                                    field_ty,
3982                                    expr.span,
3983                                    ObligationCauseCode::Misc,
3984                                );
3985                            }
3986
3987                            field_indices.push((FIRST_VARIANT, index.into()));
3988                            current_container = field_ty;
3989
3990                            continue;
3991                        }
3992                    }
3993                }
3994                _ => (),
3995            };
3996
3997            self.no_such_field_err(field, container, expr).emit();
3998
3999            break;
4000        }
4001
4002        self.typeck_results
4003            .borrow_mut()
4004            .offset_of_data_mut()
4005            .insert(expr.hir_id, (container, field_indices));
4006
4007        self.tcx.types.usize
4008    }
4009}