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