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