rustc_hir_typeck/
expr.rs

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