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