rustc_hir_typeck/
expr.rs

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