Skip to main content

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::def::{CtorKind, DefKind, Res};
19use rustc_hir::def_id::DefId;
20use rustc_hir::lang_items::LangItem;
21use rustc_hir::{ExprKind, HirId, QPath, find_attr, is_range_literal};
22use rustc_hir_analysis::NoVariantNamed;
23use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer as _;
24use rustc_infer::infer::{self, DefineOpaqueTypes, InferOk, RegionVariableOrigin};
25use rustc_infer::traits::query::NoSolution;
26use rustc_middle::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase};
27use rustc_middle::ty::error::{ExpectedFound, TypeError};
28use rustc_middle::ty::{self, AdtKind, GenericArgsRef, Ty, TypeVisitableExt};
29use rustc_middle::{bug, span_bug};
30use rustc_session::errors::ExprParenthesesNeeded;
31use rustc_session::parse::feature_err;
32use rustc_span::edit_distance::find_best_match_for_name;
33use rustc_span::hygiene::DesugaringKind;
34use rustc_span::source_map::Spanned;
35use rustc_span::{Ident, Span, Symbol, kw, sym};
36use rustc_trait_selection::infer::InferCtxtExt;
37use rustc_trait_selection::traits::{self, ObligationCauseCode, ObligationCtxt};
38use tracing::{debug, instrument, trace};
39use {rustc_ast as ast, rustc_hir as hir};
40
41use crate::Expectation::{self, ExpectCastableToType, ExpectHasType, NoExpectation};
42use crate::coercion::CoerceMany;
43use crate::errors::{
44    AddressOfTemporaryTaken, BaseExpressionDoubleDot, BaseExpressionDoubleDotAddExpr,
45    BaseExpressionDoubleDotRemove, CantDereference, FieldMultiplySpecifiedInInitializer,
46    FunctionalRecordUpdateOnNonStruct, HelpUseLatestEdition, NakedAsmOutsideNakedFn, NoFieldOnType,
47    NoFieldOnVariant, ReturnLikeStatementKind, ReturnStmtOutsideOfFnBody, StructExprNonExhaustive,
48    TypeMismatchFruTypo, YieldExprOutsideOfCoroutine,
49};
50use crate::op::contains_let_in_chain;
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
80        // Special case: range expressions are desugared to struct literals in HIR,
81        // so they would normally return `Unambiguous` precedence in expr.precedence.
82        // we should return `Range` precedence for correct parenthesization in suggestions.
83        if is_range_literal(expr) {
84            return ExprPrecedence::Range;
85        }
86
87        expr.precedence(&has_attr)
88    }
89
90    /// Check an expr with an expectation type, and also demand that the expr's
91    /// evaluated type is a subtype of the expectation at the end. This is a
92    /// *hard* requirement.
93    pub(crate) fn check_expr_has_type_or_error(
94        &self,
95        expr: &'tcx hir::Expr<'tcx>,
96        expected_ty: Ty<'tcx>,
97        extend_err: impl FnOnce(&mut Diag<'_>),
98    ) -> Ty<'tcx> {
99        let mut ty = self.check_expr_with_expectation(expr, ExpectHasType(expected_ty));
100
101        // While we don't allow *arbitrary* coercions here, we *do* allow
102        // coercions from ! to `expected`.
103        if self.try_structurally_resolve_type(expr.span, ty).is_never()
104            && self.tcx.expr_guaranteed_to_constitute_read_for_never(expr)
105        {
106            if let Some(adjustments) = self.typeck_results.borrow().adjustments().get(expr.hir_id) {
107                let reported = self.dcx().span_delayed_bug(
108                    expr.span,
109                    "expression with never type wound up being adjusted",
110                );
111
112                return if let [Adjustment { kind: Adjust::NeverToAny, target }] = &adjustments[..] {
113                    target.to_owned()
114                } else {
115                    Ty::new_error(self.tcx(), reported)
116                };
117            }
118
119            let adj_ty = self.next_ty_var(expr.span);
120            self.apply_adjustments(
121                expr,
122                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [Adjustment { kind: Adjust::NeverToAny, target: adj_ty }]))vec![Adjustment { kind: Adjust::NeverToAny, target: adj_ty }],
123            );
124            ty = adj_ty;
125        }
126
127        if let Err(mut err) = self.demand_suptype_diag(expr.span, expected_ty, ty) {
128            let _ = self.emit_type_mismatch_suggestions(
129                &mut err,
130                expr.peel_drop_temps(),
131                ty,
132                expected_ty,
133                None,
134                None,
135            );
136            extend_err(&mut err);
137            err.emit();
138        }
139        ty
140    }
141
142    /// Check an expr with an expectation type, and also demand that the expr's
143    /// evaluated type is a coercible to the expectation at the end. This is a
144    /// *hard* requirement.
145    pub(super) fn check_expr_coercible_to_type(
146        &self,
147        expr: &'tcx hir::Expr<'tcx>,
148        expected: Ty<'tcx>,
149        expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
150    ) -> Ty<'tcx> {
151        self.check_expr_coercible_to_type_or_error(expr, expected, expected_ty_expr, |_, _| {})
152    }
153
154    pub(crate) fn check_expr_coercible_to_type_or_error(
155        &self,
156        expr: &'tcx hir::Expr<'tcx>,
157        expected: Ty<'tcx>,
158        expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
159        extend_err: impl FnOnce(&mut Diag<'_>, Ty<'tcx>),
160    ) -> Ty<'tcx> {
161        let ty = self.check_expr_with_hint(expr, expected);
162        // checks don't need two phase
163        match self.demand_coerce_diag(expr, ty, expected, expected_ty_expr, AllowTwoPhase::No) {
164            Ok(ty) => ty,
165            Err(mut err) => {
166                extend_err(&mut err, ty);
167                err.emit();
168                // Return the original type instead of an error type here, otherwise the type of `x` in
169                // `let x: u32 = ();` will be a type error, causing all subsequent usages of `x` to not
170                // report errors, even though `x` is definitely `u32`.
171                expected
172            }
173        }
174    }
175
176    /// Check an expr with an expectation type. Don't actually enforce that expectation
177    /// is related to the expr's evaluated type via subtyping or coercion. This is
178    /// usually called because we want to do that subtype/coerce call manually for better
179    /// diagnostics.
180    pub(super) fn check_expr_with_hint(
181        &self,
182        expr: &'tcx hir::Expr<'tcx>,
183        expected: Ty<'tcx>,
184    ) -> Ty<'tcx> {
185        self.check_expr_with_expectation(expr, ExpectHasType(expected))
186    }
187
188    /// Check an expr with an expectation type, and also [`Needs`] which will
189    /// prompt typeck to convert any implicit immutable derefs to mutable derefs.
190    fn check_expr_with_expectation_and_needs(
191        &self,
192        expr: &'tcx hir::Expr<'tcx>,
193        expected: Expectation<'tcx>,
194        needs: Needs,
195    ) -> Ty<'tcx> {
196        let ty = self.check_expr_with_expectation(expr, expected);
197
198        // If the expression is used in a place whether mutable place is required
199        // e.g. LHS of assignment, perform the conversion.
200        if let Needs::MutPlace = needs {
201            self.convert_place_derefs_to_mutable(expr);
202        }
203
204        ty
205    }
206
207    /// Check an expr with no expectations.
208    pub(super) fn check_expr(&self, expr: &'tcx hir::Expr<'tcx>) -> Ty<'tcx> {
209        self.check_expr_with_expectation(expr, NoExpectation)
210    }
211
212    /// Check an expr with no expectations, but with [`Needs`] which will
213    /// prompt typeck to convert any implicit immutable derefs to mutable derefs.
214    pub(super) fn check_expr_with_needs(
215        &self,
216        expr: &'tcx hir::Expr<'tcx>,
217        needs: Needs,
218    ) -> Ty<'tcx> {
219        self.check_expr_with_expectation_and_needs(expr, NoExpectation, needs)
220    }
221
222    /// Check an expr with an expectation type which may be used to eagerly
223    /// guide inference when evaluating that expr.
224    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("check_expr_with_expectation",
                                    "rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
                                    ::tracing_core::__macro_support::Option::Some(224u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
                                    ::tracing_core::field::FieldSet::new(&["expected"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expected)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: Ty<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        { self.check_expr_with_expectation_and_args(expr, expected, None) }
    }
}#[instrument(skip(self, expr), level = "debug")]
225    pub(super) fn check_expr_with_expectation(
226        &self,
227        expr: &'tcx hir::Expr<'tcx>,
228        expected: Expectation<'tcx>,
229    ) -> Ty<'tcx> {
230        self.check_expr_with_expectation_and_args(expr, expected, None)
231    }
232
233    /// Same as [`Self::check_expr_with_expectation`], but allows us to pass in
234    /// the arguments of a [`ExprKind::Call`] when evaluating its callee that
235    /// is an [`ExprKind::Path`]. We use this to refine the spans for certain
236    /// well-formedness guarantees for the path expr.
237    pub(super) fn check_expr_with_expectation_and_args(
238        &self,
239        expr: &'tcx hir::Expr<'tcx>,
240        expected: Expectation<'tcx>,
241        call_expr_and_args: Option<(&'tcx hir::Expr<'tcx>, &'tcx [hir::Expr<'tcx>])>,
242    ) -> Ty<'tcx> {
243        if self.tcx().sess.verbose_internals() {
244            // make this code only run with -Zverbose-internals because it is probably slow
245            if let Ok(lint_str) = self.tcx.sess.source_map().span_to_snippet(expr.span) {
246                if !lint_str.contains('\n') {
247                    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/expr.rs:247",
                        "rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
                        ::tracing_core::__macro_support::Option::Some(247u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("expr text: {0}",
                                                    lint_str) as &dyn Value))])
            });
    } else { ; }
};debug!("expr text: {lint_str}");
248                } else {
249                    let mut lines = lint_str.lines();
250                    if let Some(line0) = lines.next() {
251                        let remaining_lines = lines.count();
252                        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/expr.rs:252",
                        "rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
                        ::tracing_core::__macro_support::Option::Some(252u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("expr text: {0}",
                                                    line0) as &dyn Value))])
            });
    } else { ; }
};debug!("expr text: {line0}");
253                        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/expr.rs:253",
                        "rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
                        ::tracing_core::__macro_support::Option::Some(253u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("expr text: ...(and {0} more lines)",
                                                    remaining_lines) as &dyn Value))])
            });
    } else { ; }
};debug!("expr text: ...(and {remaining_lines} more lines)");
254                    }
255                }
256            }
257        }
258
259        // True if `expr` is a `Try::from_ok(())` that is a result of desugaring a try block
260        // without the final expr (e.g. `try { return; }`). We don't want to generate an
261        // unreachable_code lint for it since warnings for autogenerated code are confusing.
262        let is_try_block_generated_unit_expr = match expr.kind {
263            ExprKind::Call(_, [arg]) => {
264                expr.span.is_desugaring(DesugaringKind::TryBlock)
265                    && arg.span.is_desugaring(DesugaringKind::TryBlock)
266            }
267            _ => false,
268        };
269
270        // Warn for expressions after diverging siblings.
271        if !is_try_block_generated_unit_expr {
272            self.warn_if_unreachable(expr.hir_id, expr.span, "expression");
273        }
274
275        // Whether a past expression diverges doesn't affect typechecking of this expression, so we
276        // reset `diverges` while checking `expr`.
277        let old_diverges = self.diverges.replace(Diverges::Maybe);
278
279        if self.is_whole_body.replace(false) {
280            // If this expression is the whole body and the function diverges because of its
281            // arguments, we check this here to ensure the body is considered to diverge.
282            self.diverges.set(self.function_diverges_because_of_empty_arguments.get())
283        };
284
285        let ty = ensure_sufficient_stack(|| match &expr.kind {
286            // Intercept the callee path expr and give it better spans.
287            hir::ExprKind::Path(
288                qpath @ (hir::QPath::Resolved(..) | hir::QPath::TypeRelative(..)),
289            ) => self.check_expr_path(qpath, expr, call_expr_and_args),
290            _ => self.check_expr_kind(expr, expected),
291        });
292        let ty = self.resolve_vars_if_possible(ty);
293
294        // Warn for non-block expressions with diverging children.
295        match expr.kind {
296            ExprKind::Block(..)
297            | ExprKind::If(..)
298            | ExprKind::Let(..)
299            | ExprKind::Loop(..)
300            | ExprKind::Match(..) => {}
301            // Do not warn on `as` casts from never to any,
302            // they are sometimes required to appeal typeck.
303            ExprKind::Cast(_, _) => {}
304            // If `expr` is a result of desugaring the try block and is an ok-wrapped
305            // diverging expression (e.g. it arose from desugaring of `try { return }`),
306            // we skip issuing a warning because it is autogenerated code.
307            ExprKind::Call(..) if expr.span.is_desugaring(DesugaringKind::TryBlock) => {}
308            // Likewise, do not lint unreachable code injected via contracts desugaring.
309            ExprKind::Call(..) if expr.span.is_desugaring(DesugaringKind::Contract) => {}
310            ExprKind::Call(callee, _) => self.warn_if_unreachable(expr.hir_id, callee.span, "call"),
311            ExprKind::MethodCall(segment, ..) => {
312                self.warn_if_unreachable(expr.hir_id, segment.ident.span, "call")
313            }
314            _ => self.warn_if_unreachable(expr.hir_id, expr.span, "expression"),
315        }
316
317        // Any expression that produces a value of type `!` must have diverged,
318        // unless it's a place expression that isn't being read from, in which case
319        // diverging would be unsound since we may never actually read the `!`.
320        // e.g. `let _ = *never_ptr;` with `never_ptr: *const !`.
321        if self.try_structurally_resolve_type(expr.span, ty).is_never()
322            && self.tcx.expr_guaranteed_to_constitute_read_for_never(expr)
323        {
324            self.diverges.set(self.diverges.get() | Diverges::always(expr.span));
325        }
326
327        // Record the type, which applies it effects.
328        // We need to do this after the warning above, so that
329        // we don't warn for the diverging expression itself.
330        self.write_ty(expr.hir_id, ty);
331
332        // Combine the diverging and has_error flags.
333        self.diverges.set(self.diverges.get() | old_diverges);
334
335        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/expr.rs:335",
                        "rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
                        ::tracing_core::__macro_support::Option::Some(335u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("type of {0} is...",
                                                    self.tcx.hir_id_to_string(expr.hir_id)) as &dyn Value))])
            });
    } else { ; }
};debug!("type of {} is...", self.tcx.hir_id_to_string(expr.hir_id));
336        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/expr.rs:336",
                        "rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
                        ::tracing_core::__macro_support::Option::Some(336u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("... {0:?}, expected is {1:?}",
                                                    ty, expected) as &dyn Value))])
            });
    } else { ; }
};debug!("... {:?}, expected is {:?}", ty, expected);
337
338        ty
339    }
340
341    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("check_expr_kind",
                                    "rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
                                    ::tracing_core::__macro_support::Option::Some(341u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
                                    ::tracing_core::field::FieldSet::new(&["expected"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expected)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

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

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: Option<Vec<Ident>> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            if field_path.len() > 3 { return None; }
            field_path.push(candidate_name);
            if matches(candidate_name, candidate_ty) {
                return Some(field_path);
            }
            for nested_fields in
                self.get_field_candidates_considering_privacy_for_diag(span,
                    candidate_ty, mod_id, hir_id) {
                for field in nested_fields {
                    if let Some(field_path) =
                            self.check_for_nested_field_satisfying_condition_for_diag(span,
                                matches, field, field_path.clone(), mod_id, hir_id) {
                        return Some(field_path);
                    }
                }
            }
            None
        }
    }
}#[instrument(skip(self, matches, mod_id, hir_id), level = "debug")]
3370    pub(crate) fn check_for_nested_field_satisfying_condition_for_diag(
3371        &self,
3372        span: Span,
3373        matches: &impl Fn(Ident, Ty<'tcx>) -> bool,
3374        (candidate_name, candidate_ty): (Ident, Ty<'tcx>),
3375        mut field_path: Vec<Ident>,
3376        mod_id: DefId,
3377        hir_id: HirId,
3378    ) -> Option<Vec<Ident>> {
3379        if field_path.len() > 3 {
3380            // For compile-time reasons and to avoid infinite recursion we only check for fields
3381            // up to a depth of three
3382            return None;
3383        }
3384        field_path.push(candidate_name);
3385        if matches(candidate_name, candidate_ty) {
3386            return Some(field_path);
3387        }
3388        for nested_fields in self.get_field_candidates_considering_privacy_for_diag(
3389            span,
3390            candidate_ty,
3391            mod_id,
3392            hir_id,
3393        ) {
3394            // recursively search fields of `candidate_field` if it's a ty::Adt
3395            for field in nested_fields {
3396                if let Some(field_path) = self.check_for_nested_field_satisfying_condition_for_diag(
3397                    span,
3398                    matches,
3399                    field,
3400                    field_path.clone(),
3401                    mod_id,
3402                    hir_id,
3403                ) {
3404                    return Some(field_path);
3405                }
3406            }
3407        }
3408        None
3409    }
3410
3411    fn check_expr_index(
3412        &self,
3413        base: &'tcx hir::Expr<'tcx>,
3414        idx: &'tcx hir::Expr<'tcx>,
3415        expr: &'tcx hir::Expr<'tcx>,
3416        brackets_span: Span,
3417    ) -> Ty<'tcx> {
3418        let base_t = self.check_expr(base);
3419        let idx_t = self.check_expr(idx);
3420
3421        if base_t.references_error() {
3422            base_t
3423        } else if idx_t.references_error() {
3424            idx_t
3425        } else {
3426            let base_t = self.structurally_resolve_type(base.span, base_t);
3427            match self.lookup_indexing(expr, base, base_t, idx, idx_t) {
3428                Some((index_ty, element_ty)) => {
3429                    // two-phase not needed because index_ty is never mutable
3430                    self.demand_coerce(idx, idx_t, index_ty, None, AllowTwoPhase::No);
3431                    self.select_obligations_where_possible(|errors| {
3432                        self.point_at_index(errors, idx.span);
3433                    });
3434                    element_ty
3435                }
3436                None => {
3437                    // Attempt to *shallowly* search for an impl which matches,
3438                    // but has nested obligations which are unsatisfied.
3439                    for (base_t, _) in self.autoderef(base.span, base_t).silence_errors() {
3440                        if let Some((_, index_ty, element_ty)) =
3441                            self.find_and_report_unsatisfied_index_impl(base, base_t)
3442                        {
3443                            self.demand_coerce(idx, idx_t, index_ty, None, AllowTwoPhase::No);
3444                            return element_ty;
3445                        }
3446                    }
3447
3448                    let mut err = {
    let mut err =
        {
            self.dcx().struct_span_err(brackets_span,
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("cannot index into a value of type `{0}`",
                                    base_t))
                        })).with_code(E0608)
        };
    if base_t.references_error() { err.downgrade_to_delayed_bug(); }
    err
}type_error_struct!(
3449                        self.dcx(),
3450                        brackets_span,
3451                        base_t,
3452                        E0608,
3453                        "cannot index into a value of type `{base_t}`",
3454                    );
3455                    // Try to give some advice about indexing tuples.
3456                    if let ty::Tuple(types) = base_t.kind() {
3457                        err.help(
3458                            "tuples are indexed with a dot and a literal index: `tuple.0`, `tuple.1`, etc.",
3459                        );
3460                        // If index is an unsuffixed integer, show the fixed expression:
3461                        if let ExprKind::Lit(lit) = idx.kind
3462                            && let ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) = lit.node
3463                            && i.get() < types.len().try_into().expect("tuple length fits in u128")
3464                        {
3465                            err.span_suggestion(
3466                                brackets_span,
3467                                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("to access tuple element `{0}`, use",
                i))
    })format!("to access tuple element `{i}`, use"),
3468                                ::alloc::__export::must_use({ ::alloc::fmt::format(format_args!(".{0}", i)) })format!(".{i}"),
3469                                Applicability::MachineApplicable,
3470                            );
3471                        }
3472                    }
3473
3474                    if base_t.is_raw_ptr() && idx_t.is_integral() {
3475                        err.multipart_suggestion(
3476                            "consider using `wrapping_add` or `add` for indexing into raw pointer",
3477                            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(base.span.between(idx.span), ".wrapping_add(".to_owned()),
                (idx.span.shrink_to_hi().until(expr.span.shrink_to_hi()),
                    ")".to_owned())]))vec![
3478                                (base.span.between(idx.span), ".wrapping_add(".to_owned()),
3479                                (
3480                                    idx.span.shrink_to_hi().until(expr.span.shrink_to_hi()),
3481                                    ")".to_owned(),
3482                                ),
3483                            ],
3484                            Applicability::MaybeIncorrect,
3485                        );
3486                    }
3487
3488                    let reported = err.emit();
3489                    Ty::new_error(self.tcx, reported)
3490                }
3491            }
3492        }
3493    }
3494
3495    /// Try to match an implementation of `Index` against a self type, and report
3496    /// the unsatisfied predicates that result from confirming this impl.
3497    ///
3498    /// Given an index expression, sometimes the `Self` type shallowly but does not
3499    /// deeply satisfy an impl predicate. Instead of simply saying that the type
3500    /// does not support being indexed, we want to point out exactly what nested
3501    /// predicates cause this to be, so that the user can add them to fix their code.
3502    fn find_and_report_unsatisfied_index_impl(
3503        &self,
3504        base_expr: &hir::Expr<'_>,
3505        base_ty: Ty<'tcx>,
3506    ) -> Option<(ErrorGuaranteed, Ty<'tcx>, Ty<'tcx>)> {
3507        let index_trait_def_id = self.tcx.lang_items().index_trait()?;
3508        let index_trait_output_def_id = self.tcx.get_diagnostic_item(sym::IndexOutput)?;
3509
3510        let mut relevant_impls = ::alloc::vec::Vec::new()vec![];
3511        self.tcx.for_each_relevant_impl(index_trait_def_id, base_ty, |impl_def_id| {
3512            relevant_impls.push(impl_def_id);
3513        });
3514        let [impl_def_id] = relevant_impls[..] else {
3515            // Only report unsatisfied impl predicates if there's one impl
3516            return None;
3517        };
3518
3519        self.commit_if_ok(|snapshot| {
3520            let outer_universe = self.universe();
3521
3522            let ocx = ObligationCtxt::new_with_diagnostics(self);
3523            let impl_args = self.fresh_args_for_item(base_expr.span, impl_def_id);
3524            let impl_trait_ref =
3525                self.tcx.impl_trait_ref(impl_def_id).instantiate(self.tcx, impl_args);
3526            let cause = self.misc(base_expr.span);
3527
3528            // Match the impl self type against the base ty. If this fails,
3529            // we just skip this impl, since it's not particularly useful.
3530            let impl_trait_ref = ocx.normalize(&cause, self.param_env, impl_trait_ref);
3531            ocx.eq(&cause, self.param_env, base_ty, impl_trait_ref.self_ty())?;
3532
3533            // Register the impl's predicates. One of these predicates
3534            // must be unsatisfied, or else we wouldn't have gotten here
3535            // in the first place.
3536            ocx.register_obligations(traits::predicates_for_generics(
3537                |idx, span| {
3538                    cause.clone().derived_cause(
3539                        ty::Binder::dummy(ty::TraitPredicate {
3540                            trait_ref: impl_trait_ref,
3541                            polarity: ty::PredicatePolarity::Positive,
3542                        }),
3543                        |derived| {
3544                            ObligationCauseCode::ImplDerived(Box::new(traits::ImplDerivedCause {
3545                                derived,
3546                                impl_or_alias_def_id: impl_def_id,
3547                                impl_def_predicate_index: Some(idx),
3548                                span,
3549                            }))
3550                        },
3551                    )
3552                },
3553                self.param_env,
3554                self.tcx.predicates_of(impl_def_id).instantiate(self.tcx, impl_args),
3555            ));
3556
3557            // Normalize the output type, which we can use later on as the
3558            // return type of the index expression...
3559            let element_ty = ocx.normalize(
3560                &cause,
3561                self.param_env,
3562                Ty::new_projection_from_args(
3563                    self.tcx,
3564                    index_trait_output_def_id,
3565                    impl_trait_ref.args,
3566                ),
3567            );
3568
3569            let true_errors = ocx.try_evaluate_obligations();
3570
3571            // Do a leak check -- we can't really report a useful error here,
3572            // but it at least avoids an ICE when the error has to do with higher-ranked
3573            // lifetimes.
3574            self.leak_check(outer_universe, Some(snapshot))?;
3575
3576            // Bail if we have ambiguity errors, which we can't report in a useful way.
3577            let ambiguity_errors = ocx.evaluate_obligations_error_on_ambiguity();
3578            if true_errors.is_empty() && !ambiguity_errors.is_empty() {
3579                return Err(NoSolution);
3580            }
3581
3582            // There should be at least one error reported. If not, we
3583            // will still delay a span bug in `report_fulfillment_errors`.
3584            Ok::<_, NoSolution>((
3585                self.err_ctxt().report_fulfillment_errors(true_errors),
3586                impl_trait_ref.args.type_at(1),
3587                element_ty,
3588            ))
3589        })
3590        .ok()
3591    }
3592
3593    fn point_at_index(&self, errors: &mut Vec<traits::FulfillmentError<'tcx>>, span: Span) {
3594        let mut seen_preds = FxHashSet::default();
3595        // We re-sort here so that the outer most root obligations comes first, as we have the
3596        // subsequent weird logic to identify *every* relevant obligation for proper deduplication
3597        // of diagnostics.
3598        errors.sort_by_key(|error| error.root_obligation.recursion_depth);
3599        for error in errors {
3600            match (
3601                error.root_obligation.predicate.kind().skip_binder(),
3602                error.obligation.predicate.kind().skip_binder(),
3603            ) {
3604                (ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)), _)
3605                    if self.tcx.is_lang_item(predicate.trait_ref.def_id, LangItem::Index) =>
3606                {
3607                    seen_preds.insert(error.obligation.predicate.kind().skip_binder());
3608                }
3609                (_, ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)))
3610                    if self.tcx.is_diagnostic_item(sym::SliceIndex, predicate.trait_ref.def_id) =>
3611                {
3612                    seen_preds.insert(error.obligation.predicate.kind().skip_binder());
3613                }
3614                (root, pred) if seen_preds.contains(&pred) || seen_preds.contains(&root) => {}
3615                _ => continue,
3616            }
3617            error.obligation.cause.span = span;
3618        }
3619    }
3620
3621    fn check_expr_yield(
3622        &self,
3623        value: &'tcx hir::Expr<'tcx>,
3624        expr: &'tcx hir::Expr<'tcx>,
3625    ) -> Ty<'tcx> {
3626        match self.coroutine_types {
3627            Some(CoroutineTypes { resume_ty, yield_ty }) => {
3628                self.check_expr_coercible_to_type(value, yield_ty, None);
3629
3630                resume_ty
3631            }
3632            _ => {
3633                self.dcx().emit_err(YieldExprOutsideOfCoroutine { span: expr.span });
3634                // Avoid expressions without types during writeback (#78653).
3635                self.check_expr(value);
3636                self.tcx.types.unit
3637            }
3638        }
3639    }
3640
3641    fn check_expr_asm_operand(&self, expr: &'tcx hir::Expr<'tcx>, is_input: bool) {
3642        let needs = if is_input { Needs::None } else { Needs::MutPlace };
3643        let ty = self.check_expr_with_needs(expr, needs);
3644        self.require_type_is_sized(ty, expr.span, ObligationCauseCode::InlineAsmSized);
3645
3646        if !is_input && !expr.is_syntactic_place_expr() {
3647            self.dcx()
3648                .struct_span_err(expr.span, "invalid asm output")
3649                .with_span_label(expr.span, "cannot assign to this expression")
3650                .emit();
3651        }
3652
3653        // If this is an input value, we require its type to be fully resolved
3654        // at this point. This allows us to provide helpful coercions which help
3655        // pass the type candidate list in a later pass.
3656        //
3657        // We don't require output types to be resolved at this point, which
3658        // allows them to be inferred based on how they are used later in the
3659        // function.
3660        if is_input {
3661            let ty = self.structurally_resolve_type(expr.span, ty);
3662            match *ty.kind() {
3663                ty::FnDef(..) => {
3664                    let fnptr_ty = Ty::new_fn_ptr(self.tcx, ty.fn_sig(self.tcx));
3665                    self.demand_coerce(expr, ty, fnptr_ty, None, AllowTwoPhase::No);
3666                }
3667                ty::Ref(_, base_ty, mutbl) => {
3668                    let ptr_ty = Ty::new_ptr(self.tcx, base_ty, mutbl);
3669                    self.demand_coerce(expr, ty, ptr_ty, None, AllowTwoPhase::No);
3670                }
3671                _ => {}
3672            }
3673        }
3674    }
3675
3676    fn check_expr_asm(&self, asm: &'tcx hir::InlineAsm<'tcx>, span: Span) -> Ty<'tcx> {
3677        if let rustc_ast::AsmMacro::NakedAsm = asm.asm_macro {
3678            if !{

        #[allow(deprecated)]
        {
            {
                'done:
                    {
                    for i in self.tcx.get_all_attrs(self.body_id) {
                        #[allow(unused_imports)]
                        use rustc_hir::attrs::AttributeKind::*;
                        let i: &rustc_hir::Attribute = i;
                        match i {
                            rustc_hir::Attribute::Parsed(Naked(..)) => {
                                break 'done Some(());
                            }
                            rustc_hir::Attribute::Unparsed(..) =>
                                {}
                                #[deny(unreachable_patterns)]
                                _ => {}
                        }
                    }
                    None
                }
            }
        }
    }.is_some()find_attr!(self.tcx, self.body_id, Naked(..)) {
3679                self.tcx.dcx().emit_err(NakedAsmOutsideNakedFn { span });
3680            }
3681        }
3682
3683        let mut diverge = asm.asm_macro.diverges(asm.options);
3684
3685        for (op, _op_sp) in asm.operands {
3686            match *op {
3687                hir::InlineAsmOperand::In { expr, .. } => {
3688                    self.check_expr_asm_operand(expr, true);
3689                }
3690                hir::InlineAsmOperand::Out { expr: Some(expr), .. }
3691                | hir::InlineAsmOperand::InOut { expr, .. } => {
3692                    self.check_expr_asm_operand(expr, false);
3693                }
3694                hir::InlineAsmOperand::Out { expr: None, .. } => {}
3695                hir::InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
3696                    self.check_expr_asm_operand(in_expr, true);
3697                    if let Some(out_expr) = out_expr {
3698                        self.check_expr_asm_operand(out_expr, false);
3699                    }
3700                }
3701                hir::InlineAsmOperand::Const { ref anon_const } => {
3702                    self.check_expr_const_block(anon_const, Expectation::NoExpectation);
3703                }
3704                hir::InlineAsmOperand::SymFn { expr } => {
3705                    self.check_expr(expr);
3706                }
3707                hir::InlineAsmOperand::SymStatic { .. } => {}
3708                hir::InlineAsmOperand::Label { block } => {
3709                    let previous_diverges = self.diverges.get();
3710
3711                    // The label blocks should have unit return value or diverge.
3712                    let ty = self.check_expr_block(block, ExpectHasType(self.tcx.types.unit));
3713                    if !ty.is_never() {
3714                        self.demand_suptype(block.span, self.tcx.types.unit, ty);
3715                        diverge = false;
3716                    }
3717
3718                    // We need this to avoid false unreachable warning when a label diverges.
3719                    self.diverges.set(previous_diverges);
3720                }
3721            }
3722        }
3723
3724        if diverge { self.tcx.types.never } else { self.tcx.types.unit }
3725    }
3726
3727    fn check_expr_offset_of(
3728        &self,
3729        container: &'tcx hir::Ty<'tcx>,
3730        fields: &[Ident],
3731        expr: &'tcx hir::Expr<'tcx>,
3732    ) -> Ty<'tcx> {
3733        let mut current_container = self.lower_ty(container).normalized;
3734        let mut field_indices = Vec::with_capacity(fields.len());
3735        let mut fields = fields.into_iter();
3736
3737        while let Some(&field) = fields.next() {
3738            let container = self.structurally_resolve_type(expr.span, current_container);
3739
3740            match container.kind() {
3741                ty::Adt(container_def, args) if container_def.is_enum() => {
3742                    let block = self.tcx.local_def_id_to_hir_id(self.body_id);
3743                    let (ident, _def_scope) =
3744                        self.tcx.adjust_ident_and_get_scope(field, container_def.did(), block);
3745
3746                    if !self.tcx.features().offset_of_enum() {
3747                        rustc_session::parse::feature_err(
3748                            &self.tcx.sess,
3749                            sym::offset_of_enum,
3750                            ident.span,
3751                            "using enums in offset_of is experimental",
3752                        )
3753                        .emit();
3754                    }
3755
3756                    let Some((index, variant)) = container_def
3757                        .variants()
3758                        .iter_enumerated()
3759                        .find(|(_, v)| v.ident(self.tcx).normalize_to_macros_2_0() == ident)
3760                    else {
3761                        self.dcx()
3762                            .create_err(NoVariantNamed { span: ident.span, ident, ty: container })
3763                            .with_span_label(field.span, "variant not found")
3764                            .emit_unless_delay(container.references_error());
3765                        break;
3766                    };
3767                    let Some(&subfield) = fields.next() else {
3768                        {
    let mut err =
        {
            self.dcx().struct_span_err(ident.span,
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("`{0}` is an enum variant; expected field at end of `offset_of`",
                                    ident))
                        })).with_code(E0795)
        };
    if container.references_error() { err.downgrade_to_delayed_bug(); }
    err
}type_error_struct!(
3769                            self.dcx(),
3770                            ident.span,
3771                            container,
3772                            E0795,
3773                            "`{ident}` is an enum variant; expected field at end of `offset_of`",
3774                        )
3775                        .with_span_label(field.span, "enum variant")
3776                        .emit();
3777                        break;
3778                    };
3779                    let (subident, sub_def_scope) =
3780                        self.tcx.adjust_ident_and_get_scope(subfield, variant.def_id, block);
3781
3782                    let Some((subindex, field)) = variant
3783                        .fields
3784                        .iter_enumerated()
3785                        .find(|(_, f)| f.ident(self.tcx).normalize_to_macros_2_0() == subident)
3786                    else {
3787                        self.dcx()
3788                            .create_err(NoFieldOnVariant {
3789                                span: ident.span,
3790                                container,
3791                                ident,
3792                                field: subfield,
3793                                enum_span: field.span,
3794                                field_span: subident.span,
3795                            })
3796                            .emit_unless_delay(container.references_error());
3797                        break;
3798                    };
3799
3800                    let field_ty = self.field_ty(expr.span, field, args);
3801
3802                    // Enums are anyway always sized. But just to safeguard against future
3803                    // language extensions, let's double-check.
3804                    self.require_type_is_sized(
3805                        field_ty,
3806                        expr.span,
3807                        ObligationCauseCode::FieldSized {
3808                            adt_kind: AdtKind::Enum,
3809                            span: self.tcx.def_span(field.did),
3810                            last: false,
3811                        },
3812                    );
3813
3814                    if field.vis.is_accessible_from(sub_def_scope, self.tcx) {
3815                        self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None);
3816                    } else {
3817                        self.private_field_err(ident, container_def.did()).emit();
3818                    }
3819
3820                    // Save the index of all fields regardless of their visibility in case
3821                    // of error recovery.
3822                    field_indices.push((current_container, index, subindex));
3823                    current_container = field_ty;
3824
3825                    continue;
3826                }
3827                ty::Adt(container_def, args) => {
3828                    let block = self.tcx.local_def_id_to_hir_id(self.body_id);
3829                    let (ident, def_scope) =
3830                        self.tcx.adjust_ident_and_get_scope(field, container_def.did(), block);
3831
3832                    let fields = &container_def.non_enum_variant().fields;
3833                    if let Some((index, field)) = fields
3834                        .iter_enumerated()
3835                        .find(|(_, f)| f.ident(self.tcx).normalize_to_macros_2_0() == ident)
3836                    {
3837                        let field_ty = self.field_ty(expr.span, field, args);
3838
3839                        if self.tcx.features().offset_of_slice() {
3840                            self.require_type_has_static_alignment(field_ty, expr.span);
3841                        } else {
3842                            self.require_type_is_sized(
3843                                field_ty,
3844                                expr.span,
3845                                ObligationCauseCode::Misc,
3846                            );
3847                        }
3848
3849                        if field.vis.is_accessible_from(def_scope, self.tcx) {
3850                            self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None);
3851                        } else {
3852                            self.private_field_err(ident, container_def.did()).emit();
3853                        }
3854
3855                        // Save the index of all fields regardless of their visibility in case
3856                        // of error recovery.
3857                        field_indices.push((current_container, FIRST_VARIANT, index));
3858                        current_container = field_ty;
3859
3860                        continue;
3861                    }
3862                }
3863                ty::Tuple(tys) => {
3864                    if let Ok(index) = field.as_str().parse::<usize>()
3865                        && field.name == sym::integer(index)
3866                    {
3867                        if let Some(&field_ty) = tys.get(index) {
3868                            if self.tcx.features().offset_of_slice() {
3869                                self.require_type_has_static_alignment(field_ty, expr.span);
3870                            } else {
3871                                self.require_type_is_sized(
3872                                    field_ty,
3873                                    expr.span,
3874                                    ObligationCauseCode::Misc,
3875                                );
3876                            }
3877
3878                            field_indices.push((current_container, FIRST_VARIANT, index.into()));
3879                            current_container = field_ty;
3880
3881                            continue;
3882                        }
3883                    }
3884                }
3885                _ => (),
3886            };
3887
3888            self.no_such_field_err(field, container, expr).emit();
3889
3890            break;
3891        }
3892
3893        self.typeck_results.borrow_mut().offset_of_data_mut().insert(expr.hir_id, field_indices);
3894
3895        self.tcx.types.usize
3896    }
3897}