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