1use rustc_abi::{FIRST_VARIANT, FieldIdx};
9use rustc_data_structures::fx::{FxHashMap, FxHashSet};
10use rustc_data_structures::stack::ensure_sufficient_stack;
11use rustc_data_structures::unord::UnordMap;
12use rustc_errors::codes::*;
13use rustc_errors::{
14 Applicability, Diag, ErrorGuaranteed, MultiSpan, StashKey, Subdiagnostic, listify, pluralize,
15 struct_span_code_err,
16};
17use rustc_hir::def::{CtorKind, DefKind, Res};
18use rustc_hir::def_id::DefId;
19use rustc_hir::intravisit::Visitor;
20use rustc_hir::lang_items::LangItem;
21use rustc_hir::{ExprKind, HirId, QPath};
22use rustc_hir_analysis::hir_ty_lowering::{FeedConstTy, HirTyLowerer as _};
23use rustc_infer::infer;
24use rustc_infer::infer::{DefineOpaqueTypes, InferOk};
25use rustc_infer::traits::query::NoSolution;
26use rustc_middle::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase};
27use rustc_middle::ty::error::{ExpectedFound, TypeError};
28use rustc_middle::ty::{self, AdtKind, GenericArgsRef, Ty, TypeVisitableExt};
29use rustc_middle::{bug, span_bug};
30use rustc_session::errors::ExprParenthesesNeeded;
31use rustc_session::parse::feature_err;
32use rustc_span::edit_distance::find_best_match_for_name;
33use rustc_span::hygiene::DesugaringKind;
34use rustc_span::source_map::Spanned;
35use rustc_span::{Ident, Span, Symbol, kw, sym};
36use rustc_trait_selection::infer::InferCtxtExt;
37use rustc_trait_selection::traits::{self, ObligationCauseCode, ObligationCtxt};
38use tracing::{debug, instrument, trace};
39use {rustc_ast as ast, rustc_hir as hir};
40
41use crate::Expectation::{self, ExpectCastableToType, ExpectHasType, NoExpectation};
42use crate::TupleArgumentsFlag::DontTupleArguments;
43use crate::coercion::{CoerceMany, DynamicCoerceMany};
44use crate::errors::{
45 AddressOfTemporaryTaken, BaseExpressionDoubleDot, BaseExpressionDoubleDotAddExpr,
46 BaseExpressionDoubleDotEnableDefaultFieldValues, BaseExpressionDoubleDotRemove,
47 FieldMultiplySpecifiedInInitializer, FunctionalRecordUpdateOnNonStruct, HelpUseLatestEdition,
48 ReturnLikeStatementKind, ReturnStmtOutsideOfFnBody, StructExprNonExhaustive,
49 TypeMismatchFruTypo, YieldExprOutsideOfCoroutine,
50};
51use crate::{
52 BreakableCtxt, CoroutineTypes, Diverges, FnCtxt, Needs, cast, fatally_break_rust,
53 report_unexpected_variant_res, type_error_struct,
54};
55
56impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
57 pub(crate) fn check_expr_has_type_or_error(
61 &self,
62 expr: &'tcx hir::Expr<'tcx>,
63 expected_ty: Ty<'tcx>,
64 extend_err: impl FnOnce(&mut Diag<'_>),
65 ) -> Ty<'tcx> {
66 let mut ty = self.check_expr_with_expectation(expr, ExpectHasType(expected_ty));
67
68 if self.try_structurally_resolve_type(expr.span, ty).is_never()
71 && self.expr_guaranteed_to_constitute_read_for_never(expr)
72 {
73 if let Some(adjustments) = self.typeck_results.borrow().adjustments().get(expr.hir_id) {
74 let reported = self.dcx().span_delayed_bug(
75 expr.span,
76 "expression with never type wound up being adjusted",
77 );
78
79 return if let [Adjustment { kind: Adjust::NeverToAny, target }] = &adjustments[..] {
80 target.to_owned()
81 } else {
82 Ty::new_error(self.tcx(), reported)
83 };
84 }
85
86 let adj_ty = self.next_ty_var(expr.span);
87 self.apply_adjustments(
88 expr,
89 vec![Adjustment { kind: Adjust::NeverToAny, target: adj_ty }],
90 );
91 ty = adj_ty;
92 }
93
94 if let Err(mut err) = self.demand_suptype_diag(expr.span, expected_ty, ty) {
95 let _ = self.emit_type_mismatch_suggestions(
96 &mut err,
97 expr.peel_drop_temps(),
98 ty,
99 expected_ty,
100 None,
101 None,
102 );
103 extend_err(&mut err);
104 err.emit();
105 }
106 ty
107 }
108
109 pub(super) fn check_expr_coercible_to_type(
113 &self,
114 expr: &'tcx hir::Expr<'tcx>,
115 expected: Ty<'tcx>,
116 expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
117 ) -> Ty<'tcx> {
118 self.check_expr_coercible_to_type_or_error(expr, expected, expected_ty_expr, |_, _| {})
119 }
120
121 pub(crate) fn check_expr_coercible_to_type_or_error(
122 &self,
123 expr: &'tcx hir::Expr<'tcx>,
124 expected: Ty<'tcx>,
125 expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
126 extend_err: impl FnOnce(&mut Diag<'_>, Ty<'tcx>),
127 ) -> Ty<'tcx> {
128 let ty = self.check_expr_with_hint(expr, expected);
129 match self.demand_coerce_diag(expr, ty, expected, expected_ty_expr, AllowTwoPhase::No) {
131 Ok(ty) => ty,
132 Err(mut err) => {
133 extend_err(&mut err, ty);
134 err.emit();
135 expected
139 }
140 }
141 }
142
143 pub(super) fn check_expr_with_hint(
148 &self,
149 expr: &'tcx hir::Expr<'tcx>,
150 expected: Ty<'tcx>,
151 ) -> Ty<'tcx> {
152 self.check_expr_with_expectation(expr, ExpectHasType(expected))
153 }
154
155 fn check_expr_with_expectation_and_needs(
158 &self,
159 expr: &'tcx hir::Expr<'tcx>,
160 expected: Expectation<'tcx>,
161 needs: Needs,
162 ) -> Ty<'tcx> {
163 let ty = self.check_expr_with_expectation(expr, expected);
164
165 if let Needs::MutPlace = needs {
168 self.convert_place_derefs_to_mutable(expr);
169 }
170
171 ty
172 }
173
174 pub(super) fn check_expr(&self, expr: &'tcx hir::Expr<'tcx>) -> Ty<'tcx> {
176 self.check_expr_with_expectation(expr, NoExpectation)
177 }
178
179 pub(super) fn check_expr_with_needs(
182 &self,
183 expr: &'tcx hir::Expr<'tcx>,
184 needs: Needs,
185 ) -> Ty<'tcx> {
186 self.check_expr_with_expectation_and_needs(expr, NoExpectation, needs)
187 }
188
189 #[instrument(skip(self, expr), level = "debug")]
192 pub(super) fn check_expr_with_expectation(
193 &self,
194 expr: &'tcx hir::Expr<'tcx>,
195 expected: Expectation<'tcx>,
196 ) -> Ty<'tcx> {
197 self.check_expr_with_expectation_and_args(expr, expected, None)
198 }
199
200 pub(super) fn check_expr_with_expectation_and_args(
205 &self,
206 expr: &'tcx hir::Expr<'tcx>,
207 expected: Expectation<'tcx>,
208 call_expr_and_args: Option<(&'tcx hir::Expr<'tcx>, &'tcx [hir::Expr<'tcx>])>,
209 ) -> Ty<'tcx> {
210 if self.tcx().sess.verbose_internals() {
211 if let Ok(lint_str) = self.tcx.sess.source_map().span_to_snippet(expr.span) {
213 if !lint_str.contains('\n') {
214 debug!("expr text: {lint_str}");
215 } else {
216 let mut lines = lint_str.lines();
217 if let Some(line0) = lines.next() {
218 let remaining_lines = lines.count();
219 debug!("expr text: {line0}");
220 debug!("expr text: ...(and {remaining_lines} more lines)");
221 }
222 }
223 }
224 }
225
226 let is_try_block_generated_unit_expr = match expr.kind {
230 ExprKind::Call(_, [arg]) => {
231 expr.span.is_desugaring(DesugaringKind::TryBlock)
232 && arg.span.is_desugaring(DesugaringKind::TryBlock)
233 }
234 _ => false,
235 };
236
237 if !is_try_block_generated_unit_expr {
239 self.warn_if_unreachable(expr.hir_id, expr.span, "expression");
240 }
241
242 let old_diverges = self.diverges.replace(Diverges::Maybe);
245
246 if self.is_whole_body.replace(false) {
247 self.diverges.set(self.function_diverges_because_of_empty_arguments.get())
250 };
251
252 let ty = ensure_sufficient_stack(|| match &expr.kind {
253 hir::ExprKind::Path(
255 qpath @ (hir::QPath::Resolved(..) | hir::QPath::TypeRelative(..)),
256 ) => self.check_expr_path(qpath, expr, call_expr_and_args),
257 _ => self.check_expr_kind(expr, expected),
258 });
259 let ty = self.resolve_vars_if_possible(ty);
260
261 match expr.kind {
263 ExprKind::Block(..)
264 | ExprKind::If(..)
265 | ExprKind::Let(..)
266 | ExprKind::Loop(..)
267 | ExprKind::Match(..) => {}
268 ExprKind::Call(..) if expr.span.is_desugaring(DesugaringKind::TryBlock) => {}
272 ExprKind::Call(..) if expr.span.is_desugaring(DesugaringKind::Contract) => {}
274 ExprKind::Call(callee, _) => self.warn_if_unreachable(expr.hir_id, callee.span, "call"),
275 ExprKind::MethodCall(segment, ..) => {
276 self.warn_if_unreachable(expr.hir_id, segment.ident.span, "call")
277 }
278 _ => self.warn_if_unreachable(expr.hir_id, expr.span, "expression"),
279 }
280
281 if self.try_structurally_resolve_type(expr.span, ty).is_never()
286 && self.expr_guaranteed_to_constitute_read_for_never(expr)
287 {
288 self.diverges.set(self.diverges.get() | Diverges::always(expr.span));
289 }
290
291 self.write_ty(expr.hir_id, ty);
295
296 self.diverges.set(self.diverges.get() | old_diverges);
298
299 debug!("type of {} is...", self.tcx.hir().node_to_string(expr.hir_id));
300 debug!("... {:?}, expected is {:?}", ty, expected);
301
302 ty
303 }
304
305 pub(super) fn expr_guaranteed_to_constitute_read_for_never(
317 &self,
318 expr: &'tcx hir::Expr<'tcx>,
319 ) -> bool {
320 if !expr.is_syntactic_place_expr() {
326 return true;
327 }
328
329 let parent_node = self.tcx.parent_hir_node(expr.hir_id);
330 match parent_node {
331 hir::Node::Expr(parent_expr) => {
332 match parent_expr.kind {
333 ExprKind::AddrOf(..) | hir::ExprKind::Field(..) => false,
337
338 ExprKind::Type(..) | ExprKind::UnsafeBinderCast(..) => {
341 self.expr_guaranteed_to_constitute_read_for_never(expr)
342 }
343
344 ExprKind::Assign(lhs, _, _) => {
345 expr.hir_id != lhs.hir_id
347 }
348
349 ExprKind::Match(scrutinee, arms, _) => {
351 assert_eq!(scrutinee.hir_id, expr.hir_id);
352 arms.iter()
353 .all(|arm| self.pat_guaranteed_to_constitute_read_for_never(arm.pat))
354 }
355 ExprKind::Let(hir::LetExpr { init, pat, .. }) => {
356 assert_eq!(init.hir_id, expr.hir_id);
357 self.pat_guaranteed_to_constitute_read_for_never(*pat)
358 }
359
360 ExprKind::Array(_)
362 | ExprKind::Call(_, _)
363 | ExprKind::MethodCall(_, _, _, _)
364 | ExprKind::Tup(_)
365 | ExprKind::Binary(_, _, _)
366 | ExprKind::Unary(_, _)
367 | ExprKind::Cast(_, _)
368 | ExprKind::DropTemps(_)
369 | ExprKind::If(_, _, _)
370 | ExprKind::Closure(_)
371 | ExprKind::Block(_, _)
372 | ExprKind::AssignOp(_, _, _)
373 | ExprKind::Index(_, _, _)
374 | ExprKind::Break(_, _)
375 | ExprKind::Ret(_)
376 | ExprKind::Become(_)
377 | ExprKind::InlineAsm(_)
378 | ExprKind::Struct(_, _, _)
379 | ExprKind::Repeat(_, _)
380 | ExprKind::Yield(_, _) => true,
381
382 ExprKind::ConstBlock(_)
384 | ExprKind::Loop(_, _, _, _)
385 | ExprKind::Lit(_)
386 | ExprKind::Path(_)
387 | ExprKind::Continue(_)
388 | ExprKind::OffsetOf(_, _)
389 | ExprKind::Err(_) => unreachable!("no sub-expr expected for {:?}", expr.kind),
390 }
391 }
392
393 hir::Node::LetStmt(hir::LetStmt { init: Some(target), pat, .. }) => {
396 assert_eq!(target.hir_id, expr.hir_id);
397 self.pat_guaranteed_to_constitute_read_for_never(*pat)
398 }
399
400 hir::Node::Block(_)
402 | hir::Node::Arm(_)
403 | hir::Node::ExprField(_)
404 | hir::Node::AnonConst(_)
405 | hir::Node::ConstBlock(_)
406 | hir::Node::ConstArg(_)
407 | hir::Node::Stmt(_)
408 | hir::Node::Item(hir::Item {
409 kind: hir::ItemKind::Const(..) | hir::ItemKind::Static(..),
410 ..
411 })
412 | hir::Node::TraitItem(hir::TraitItem {
413 kind: hir::TraitItemKind::Const(..), ..
414 })
415 | hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. }) => true,
416
417 hir::Node::TyPat(_) | hir::Node::Pat(_) => {
418 self.dcx().span_delayed_bug(expr.span, "place expr not allowed in pattern");
419 true
420 }
421
422 hir::Node::Param(_)
424 | hir::Node::Item(_)
425 | hir::Node::ForeignItem(_)
426 | hir::Node::TraitItem(_)
427 | hir::Node::ImplItem(_)
428 | hir::Node::Variant(_)
429 | hir::Node::Field(_)
430 | hir::Node::PathSegment(_)
431 | hir::Node::Ty(_)
432 | hir::Node::AssocItemConstraint(_)
433 | hir::Node::TraitRef(_)
434 | hir::Node::PatField(_)
435 | hir::Node::PatExpr(_)
436 | hir::Node::LetStmt(_)
437 | hir::Node::Synthetic
438 | hir::Node::Err(_)
439 | hir::Node::Ctor(_)
440 | hir::Node::Lifetime(_)
441 | hir::Node::GenericParam(_)
442 | hir::Node::Crate(_)
443 | hir::Node::Infer(_)
444 | hir::Node::WherePredicate(_)
445 | hir::Node::PreciseCapturingNonLifetimeArg(_)
446 | hir::Node::OpaqueTy(_) => {
447 unreachable!("no sub-expr expected for {parent_node:?}")
448 }
449 }
450 }
451
452 pub(super) fn pat_guaranteed_to_constitute_read_for_never(&self, pat: &hir::Pat<'_>) -> bool {
458 match pat.kind {
459 hir::PatKind::Wild => false,
461
462 hir::PatKind::Guard(_, _) => true,
464
465 hir::PatKind::Or(subpats) => {
474 subpats.iter().all(|pat| self.pat_guaranteed_to_constitute_read_for_never(pat))
475 }
476
477 hir::PatKind::Never => true,
479
480 hir::PatKind::Binding(_, _, _, _)
483 | hir::PatKind::Struct(_, _, _)
484 | hir::PatKind::TupleStruct(_, _, _)
485 | hir::PatKind::Tuple(_, _)
486 | hir::PatKind::Box(_)
487 | hir::PatKind::Ref(_, _)
488 | hir::PatKind::Deref(_)
489 | hir::PatKind::Expr(_)
490 | hir::PatKind::Range(_, _, _)
491 | hir::PatKind::Slice(_, _, _)
492 | hir::PatKind::Err(_) => true,
493 }
494 }
495
496 #[instrument(skip(self, expr), level = "debug")]
497 fn check_expr_kind(
498 &self,
499 expr: &'tcx hir::Expr<'tcx>,
500 expected: Expectation<'tcx>,
501 ) -> Ty<'tcx> {
502 trace!("expr={:#?}", expr);
503
504 let tcx = self.tcx;
505 match expr.kind {
506 ExprKind::Lit(ref lit) => self.check_expr_lit(lit, expected),
507 ExprKind::Binary(op, lhs, rhs) => self.check_expr_binop(expr, op, lhs, rhs, expected),
508 ExprKind::Assign(lhs, rhs, span) => {
509 self.check_expr_assign(expr, expected, lhs, rhs, span)
510 }
511 ExprKind::AssignOp(op, lhs, rhs) => {
512 self.check_expr_binop_assign(expr, op, lhs, rhs, expected)
513 }
514 ExprKind::Unary(unop, oprnd) => self.check_expr_unop(unop, oprnd, expected, expr),
515 ExprKind::AddrOf(kind, mutbl, oprnd) => {
516 self.check_expr_addr_of(kind, mutbl, oprnd, expected, expr)
517 }
518 ExprKind::Path(QPath::LangItem(lang_item, _)) => {
519 self.check_lang_item_path(lang_item, expr)
520 }
521 ExprKind::Path(ref qpath) => self.check_expr_path(qpath, expr, None),
522 ExprKind::InlineAsm(asm) => {
523 self.deferred_asm_checks.borrow_mut().push((asm, expr.hir_id));
525 self.check_expr_asm(asm)
526 }
527 ExprKind::OffsetOf(container, fields) => {
528 self.check_expr_offset_of(container, fields, expr)
529 }
530 ExprKind::Break(destination, ref expr_opt) => {
531 self.check_expr_break(destination, expr_opt.as_deref(), expr)
532 }
533 ExprKind::Continue(destination) => {
534 if destination.target_id.is_ok() {
535 tcx.types.never
536 } else {
537 Ty::new_misc_error(tcx)
539 }
540 }
541 ExprKind::Ret(ref expr_opt) => self.check_expr_return(expr_opt.as_deref(), expr),
542 ExprKind::Become(call) => self.check_expr_become(call, expr),
543 ExprKind::Let(let_expr) => self.check_expr_let(let_expr, expr.hir_id),
544 ExprKind::Loop(body, _, source, _) => {
545 self.check_expr_loop(body, source, expected, expr)
546 }
547 ExprKind::Match(discrim, arms, match_src) => {
548 self.check_expr_match(expr, discrim, arms, expected, match_src)
549 }
550 ExprKind::Closure(closure) => self.check_expr_closure(closure, expr.span, expected),
551 ExprKind::Block(body, _) => self.check_expr_block(body, expected),
552 ExprKind::Call(callee, args) => self.check_expr_call(expr, callee, args, expected),
553 ExprKind::MethodCall(segment, receiver, args, _) => {
554 self.check_expr_method_call(expr, segment, receiver, args, expected)
555 }
556 ExprKind::Cast(e, t) => self.check_expr_cast(e, t, expr),
557 ExprKind::Type(e, t) => {
558 let ascribed_ty = self.lower_ty_saving_user_provided_ty(t);
559 let ty = self.check_expr_with_hint(e, ascribed_ty);
560 self.demand_eqtype(e.span, ascribed_ty, ty);
561 ascribed_ty
562 }
563 ExprKind::If(cond, then_expr, opt_else_expr) => {
564 self.check_expr_if(cond, then_expr, opt_else_expr, expr.span, expected)
565 }
566 ExprKind::DropTemps(e) => self.check_expr_with_expectation(e, expected),
567 ExprKind::Array(args) => self.check_expr_array(args, expected, expr),
568 ExprKind::ConstBlock(ref block) => self.check_expr_const_block(block, expected),
569 ExprKind::Repeat(element, ref count) => {
570 self.check_expr_repeat(element, count, expected, expr)
571 }
572 ExprKind::Tup(elts) => self.check_expr_tuple(elts, expected, expr),
573 ExprKind::Struct(qpath, fields, ref base_expr) => {
574 self.check_expr_struct(expr, expected, qpath, fields, base_expr)
575 }
576 ExprKind::Field(base, field) => self.check_expr_field(expr, base, field, expected),
577 ExprKind::Index(base, idx, brackets_span) => {
578 self.check_expr_index(base, idx, expr, brackets_span)
579 }
580 ExprKind::Yield(value, _) => self.check_expr_yield(value, expr),
581 ExprKind::UnsafeBinderCast(kind, inner_expr, ty) => {
582 self.check_expr_unsafe_binder_cast(expr.span, kind, inner_expr, ty, expected)
583 }
584 ExprKind::Err(guar) => Ty::new_error(tcx, guar),
585 }
586 }
587
588 fn check_expr_unop(
589 &self,
590 unop: hir::UnOp,
591 oprnd: &'tcx hir::Expr<'tcx>,
592 expected: Expectation<'tcx>,
593 expr: &'tcx hir::Expr<'tcx>,
594 ) -> Ty<'tcx> {
595 let tcx = self.tcx;
596 let expected_inner = match unop {
597 hir::UnOp::Not | hir::UnOp::Neg => expected,
598 hir::UnOp::Deref => NoExpectation,
599 };
600 let mut oprnd_t = self.check_expr_with_expectation(oprnd, expected_inner);
601
602 if !oprnd_t.references_error() {
603 oprnd_t = self.structurally_resolve_type(expr.span, oprnd_t);
604 match unop {
605 hir::UnOp::Deref => {
606 if let Some(ty) = self.lookup_derefing(expr, oprnd, oprnd_t) {
607 oprnd_t = ty;
608 } else {
609 let mut err = type_error_struct!(
610 self.dcx(),
611 expr.span,
612 oprnd_t,
613 E0614,
614 "type `{oprnd_t}` cannot be dereferenced",
615 );
616 let sp = tcx.sess.source_map().start_point(expr.span).with_parent(None);
617 if let Some(sp) =
618 tcx.sess.psess.ambiguous_block_expr_parse.borrow().get(&sp)
619 {
620 err.subdiagnostic(ExprParenthesesNeeded::surrounding(*sp));
621 }
622 oprnd_t = Ty::new_error(tcx, err.emit());
623 }
624 }
625 hir::UnOp::Not => {
626 let result = self.check_user_unop(expr, oprnd_t, unop, expected_inner);
627 if !(oprnd_t.is_integral() || *oprnd_t.kind() == ty::Bool) {
629 oprnd_t = result;
630 }
631 }
632 hir::UnOp::Neg => {
633 let result = self.check_user_unop(expr, oprnd_t, unop, expected_inner);
634 if !oprnd_t.is_numeric() {
636 oprnd_t = result;
637 }
638 }
639 }
640 }
641 oprnd_t
642 }
643
644 fn check_expr_addr_of(
645 &self,
646 kind: hir::BorrowKind,
647 mutbl: hir::Mutability,
648 oprnd: &'tcx hir::Expr<'tcx>,
649 expected: Expectation<'tcx>,
650 expr: &'tcx hir::Expr<'tcx>,
651 ) -> Ty<'tcx> {
652 let hint = expected.only_has_type(self).map_or(NoExpectation, |ty| {
653 match self.try_structurally_resolve_type(expr.span, ty).kind() {
654 ty::Ref(_, ty, _) | ty::RawPtr(ty, _) => {
655 if oprnd.is_syntactic_place_expr() {
656 ExpectHasType(*ty)
660 } else {
661 Expectation::rvalue_hint(self, *ty)
662 }
663 }
664 _ => NoExpectation,
665 }
666 });
667 let ty =
668 self.check_expr_with_expectation_and_needs(oprnd, hint, Needs::maybe_mut_place(mutbl));
669
670 match kind {
671 _ if ty.references_error() => Ty::new_misc_error(self.tcx),
672 hir::BorrowKind::Raw => {
673 self.check_named_place_expr(oprnd);
674 Ty::new_ptr(self.tcx, ty, mutbl)
675 }
676 hir::BorrowKind::Ref => {
677 let region = self.next_region_var(infer::BorrowRegion(expr.span));
692 Ty::new_ref(self.tcx, region, ty, mutbl)
693 }
694 }
695 }
696
697 fn check_named_place_expr(&self, oprnd: &'tcx hir::Expr<'tcx>) {
703 let is_named = oprnd.is_place_expr(|base| {
704 self.typeck_results
716 .borrow()
717 .adjustments()
718 .get(base.hir_id)
719 .is_some_and(|x| x.iter().any(|adj| matches!(adj.kind, Adjust::Deref(_))))
720 });
721 if !is_named {
722 self.dcx().emit_err(AddressOfTemporaryTaken { span: oprnd.span });
723 }
724 }
725
726 fn check_lang_item_path(
727 &self,
728 lang_item: hir::LangItem,
729 expr: &'tcx hir::Expr<'tcx>,
730 ) -> Ty<'tcx> {
731 self.resolve_lang_item_path(lang_item, expr.span, expr.hir_id).1
732 }
733
734 pub(crate) fn check_expr_path(
735 &self,
736 qpath: &'tcx hir::QPath<'tcx>,
737 expr: &'tcx hir::Expr<'tcx>,
738 call_expr_and_args: Option<(&'tcx hir::Expr<'tcx>, &'tcx [hir::Expr<'tcx>])>,
739 ) -> Ty<'tcx> {
740 let tcx = self.tcx;
741 let (res, opt_ty, segs) =
742 self.resolve_ty_and_res_fully_qualified_call(qpath, expr.hir_id, expr.span);
743 let ty = match res {
744 Res::Err => {
745 self.suggest_assoc_method_call(segs);
746 let e =
747 self.dcx().span_delayed_bug(qpath.span(), "`Res::Err` but no error emitted");
748 Ty::new_error(tcx, e)
749 }
750 Res::Def(DefKind::Variant, _) => {
751 let e = report_unexpected_variant_res(
752 tcx,
753 res,
754 Some(expr),
755 qpath,
756 expr.span,
757 E0533,
758 "value",
759 );
760 Ty::new_error(tcx, e)
761 }
762 _ => {
763 self.instantiate_value_path(
764 segs,
765 opt_ty,
766 res,
767 call_expr_and_args.map_or(expr.span, |(e, _)| e.span),
768 expr.span,
769 expr.hir_id,
770 )
771 .0
772 }
773 };
774
775 if let ty::FnDef(did, _) = *ty.kind() {
776 let fn_sig = ty.fn_sig(tcx);
777
778 if tcx.is_intrinsic(did, sym::transmute) {
779 let Some(from) = fn_sig.inputs().skip_binder().get(0) else {
780 span_bug!(
781 tcx.def_span(did),
782 "intrinsic fn `transmute` defined with no parameters"
783 );
784 };
785 let to = fn_sig.output().skip_binder();
786 self.deferred_transmute_checks.borrow_mut().push((*from, to, expr.hir_id));
791 }
792 if !tcx.features().unsized_fn_params() {
793 for i in 0..fn_sig.inputs().skip_binder().len() {
803 let span = call_expr_and_args
807 .and_then(|(_, args)| args.get(i))
808 .map_or(expr.span, |arg| arg.span);
809 let input = self.instantiate_binder_with_fresh_vars(
810 span,
811 infer::BoundRegionConversionTime::FnCall,
812 fn_sig.input(i),
813 );
814 self.require_type_is_sized_deferred(
815 input,
816 span,
817 ObligationCauseCode::SizedArgumentType(None),
818 );
819 }
820 }
821 let output = self.instantiate_binder_with_fresh_vars(
828 expr.span,
829 infer::BoundRegionConversionTime::FnCall,
830 fn_sig.output(),
831 );
832 self.require_type_is_sized_deferred(
833 output,
834 call_expr_and_args.map_or(expr.span, |(e, _)| e.span),
835 ObligationCauseCode::SizedCallReturnType,
836 );
837 }
838
839 let args = self.typeck_results.borrow().node_args(expr.hir_id);
842 self.add_wf_bounds(args, expr.span);
843
844 ty
845 }
846
847 fn check_expr_break(
848 &self,
849 destination: hir::Destination,
850 expr_opt: Option<&'tcx hir::Expr<'tcx>>,
851 expr: &'tcx hir::Expr<'tcx>,
852 ) -> Ty<'tcx> {
853 let tcx = self.tcx;
854 if let Ok(target_id) = destination.target_id {
855 let (e_ty, cause);
856 if let Some(e) = expr_opt {
857 let opt_coerce_to = {
860 let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
864 match enclosing_breakables.opt_find_breakable(target_id) {
865 Some(ctxt) => ctxt.coerce.as_ref().map(|coerce| coerce.expected_ty()),
866 None => {
867 return Ty::new_error_with_message(
869 tcx,
870 expr.span,
871 "break was outside loop, but no error was emitted",
872 );
873 }
874 }
875 };
876
877 let coerce_to = opt_coerce_to.unwrap_or_else(|| {
882 let guar = self.dcx().span_delayed_bug(
883 expr.span,
884 "illegal break with value found but no error reported",
885 );
886 self.set_tainted_by_errors(guar);
887 Ty::new_error(tcx, guar)
888 });
889
890 e_ty = self.check_expr_with_hint(e, coerce_to);
892 cause = self.misc(e.span);
893 } else {
894 e_ty = tcx.types.unit;
897 cause = self.misc(expr.span);
898 }
899
900 let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
904 let Some(ctxt) = enclosing_breakables.opt_find_breakable(target_id) else {
905 return Ty::new_error_with_message(
907 tcx,
908 expr.span,
909 "break was outside loop, but no error was emitted",
910 );
911 };
912
913 if let Some(ref mut coerce) = ctxt.coerce {
914 if let Some(e) = expr_opt {
915 coerce.coerce(self, &cause, e, e_ty);
916 } else {
917 assert!(e_ty.is_unit());
918 let ty = coerce.expected_ty();
919 coerce.coerce_forced_unit(
920 self,
921 &cause,
922 |mut err| {
923 self.suggest_missing_semicolon(&mut err, expr, e_ty, false);
924 self.suggest_mismatched_types_on_tail(
925 &mut err, expr, ty, e_ty, target_id,
926 );
927 let error =
928 Some(TypeError::Sorts(ExpectedFound { expected: ty, found: e_ty }));
929 self.annotate_loop_expected_due_to_inference(err, expr, error);
930 if let Some(val) =
931 self.err_ctxt().ty_kind_suggestion(self.param_env, ty)
932 {
933 err.span_suggestion_verbose(
934 expr.span.shrink_to_hi(),
935 "give the `break` a value of the expected type",
936 format!(" {val}"),
937 Applicability::HasPlaceholders,
938 );
939 }
940 },
941 false,
942 );
943 }
944 } else {
945 assert!(expr_opt.is_none() || self.tainted_by_errors().is_some());
953 }
954
955 ctxt.may_break |= !self.diverges.get().is_always();
959
960 tcx.types.never
962 } else {
963 let err = Ty::new_error_with_message(
968 self.tcx,
969 expr.span,
970 "break was outside loop, but no error was emitted",
971 );
972
973 if let Some(e) = expr_opt {
976 self.check_expr_with_hint(e, err);
977
978 if let ExprKind::Path(QPath::Resolved(_, path)) = e.kind {
981 if let [segment] = path.segments
982 && segment.ident.name == sym::rust
983 {
984 fatally_break_rust(self.tcx, expr.span);
985 }
986 }
987 }
988
989 err
991 }
992 }
993
994 fn check_expr_return(
995 &self,
996 expr_opt: Option<&'tcx hir::Expr<'tcx>>,
997 expr: &'tcx hir::Expr<'tcx>,
998 ) -> Ty<'tcx> {
999 if self.ret_coercion.is_none() {
1000 self.emit_return_outside_of_fn_body(expr, ReturnLikeStatementKind::Return);
1001
1002 if let Some(e) = expr_opt {
1003 self.check_expr(e);
1006 }
1007 } else if let Some(e) = expr_opt {
1008 if self.ret_coercion_span.get().is_none() {
1009 self.ret_coercion_span.set(Some(e.span));
1010 }
1011 self.check_return_or_body_tail(e, true);
1012 } else {
1013 let mut coercion = self.ret_coercion.as_ref().unwrap().borrow_mut();
1014 if self.ret_coercion_span.get().is_none() {
1015 self.ret_coercion_span.set(Some(expr.span));
1016 }
1017 let cause = self.cause(expr.span, ObligationCauseCode::ReturnNoExpression);
1018 if let Some((_, fn_decl)) = self.get_fn_decl(expr.hir_id) {
1019 coercion.coerce_forced_unit(
1020 self,
1021 &cause,
1022 |db| {
1023 let span = fn_decl.output.span();
1024 if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
1025 db.span_label(
1026 span,
1027 format!("expected `{snippet}` because of this return type"),
1028 );
1029 }
1030 },
1031 true,
1032 );
1033 } else {
1034 coercion.coerce_forced_unit(self, &cause, |_| (), true);
1035 }
1036 }
1037 self.tcx.types.never
1038 }
1039
1040 fn check_expr_become(
1041 &self,
1042 call: &'tcx hir::Expr<'tcx>,
1043 expr: &'tcx hir::Expr<'tcx>,
1044 ) -> Ty<'tcx> {
1045 match &self.ret_coercion {
1046 Some(ret_coercion) => {
1047 let ret_ty = ret_coercion.borrow().expected_ty();
1048 let call_expr_ty = self.check_expr_with_hint(call, ret_ty);
1049
1050 self.demand_suptype(expr.span, ret_ty, call_expr_ty);
1053 }
1054 None => {
1055 self.emit_return_outside_of_fn_body(expr, ReturnLikeStatementKind::Become);
1056
1057 self.check_expr(call);
1060 }
1061 }
1062
1063 self.tcx.types.never
1064 }
1065
1066 pub(super) fn check_return_or_body_tail(
1075 &self,
1076 return_expr: &'tcx hir::Expr<'tcx>,
1077 explicit_return: bool,
1078 ) {
1079 let ret_coercion = self.ret_coercion.as_ref().unwrap_or_else(|| {
1080 span_bug!(return_expr.span, "check_return_expr called outside fn body")
1081 });
1082
1083 let ret_ty = ret_coercion.borrow().expected_ty();
1084 let return_expr_ty = self.check_expr_with_hint(return_expr, ret_ty);
1085 let mut span = return_expr.span;
1086 let mut hir_id = return_expr.hir_id;
1087 if !explicit_return
1090 && let ExprKind::Block(body, _) = return_expr.kind
1091 && let Some(last_expr) = body.expr
1092 {
1093 span = last_expr.span;
1094 hir_id = last_expr.hir_id;
1095 }
1096 ret_coercion.borrow_mut().coerce(
1097 self,
1098 &self.cause(span, ObligationCauseCode::ReturnValue(return_expr.hir_id)),
1099 return_expr,
1100 return_expr_ty,
1101 );
1102
1103 if let Some(fn_sig) = self.body_fn_sig()
1104 && fn_sig.output().has_opaque_types()
1105 {
1106 self.select_obligations_where_possible(|errors| {
1109 self.point_at_return_for_opaque_ty_error(
1110 errors,
1111 hir_id,
1112 span,
1113 return_expr_ty,
1114 return_expr.span,
1115 );
1116 });
1117 }
1118 }
1119
1120 fn emit_return_outside_of_fn_body(&self, expr: &hir::Expr<'_>, kind: ReturnLikeStatementKind) {
1125 let mut err = ReturnStmtOutsideOfFnBody {
1126 span: expr.span,
1127 encl_body_span: None,
1128 encl_fn_span: None,
1129 statement_kind: kind,
1130 };
1131
1132 let encl_item_id = self.tcx.hir().get_parent_item(expr.hir_id);
1133
1134 if let hir::Node::Item(hir::Item {
1135 kind: hir::ItemKind::Fn { .. },
1136 span: encl_fn_span,
1137 ..
1138 })
1139 | hir::Node::TraitItem(hir::TraitItem {
1140 kind: hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(_)),
1141 span: encl_fn_span,
1142 ..
1143 })
1144 | hir::Node::ImplItem(hir::ImplItem {
1145 kind: hir::ImplItemKind::Fn(..),
1146 span: encl_fn_span,
1147 ..
1148 }) = self.tcx.hir_node_by_def_id(encl_item_id.def_id)
1149 {
1150 let encl_body_owner_id = self.tcx.hir().enclosing_body_owner(expr.hir_id);
1154
1155 assert_ne!(encl_item_id.def_id, encl_body_owner_id);
1158
1159 let encl_body = self.tcx.hir().body_owned_by(encl_body_owner_id);
1160
1161 err.encl_body_span = Some(encl_body.value.span);
1162 err.encl_fn_span = Some(*encl_fn_span);
1163 }
1164
1165 self.dcx().emit_err(err);
1166 }
1167
1168 fn point_at_return_for_opaque_ty_error(
1169 &self,
1170 errors: &mut Vec<traits::FulfillmentError<'tcx>>,
1171 hir_id: HirId,
1172 span: Span,
1173 return_expr_ty: Ty<'tcx>,
1174 return_span: Span,
1175 ) {
1176 if span == return_span {
1178 return;
1179 }
1180 for err in errors {
1181 let cause = &mut err.obligation.cause;
1182 if let ObligationCauseCode::OpaqueReturnType(None) = cause.code() {
1183 let new_cause = self.cause(
1184 cause.span,
1185 ObligationCauseCode::OpaqueReturnType(Some((return_expr_ty, hir_id))),
1186 );
1187 *cause = new_cause;
1188 }
1189 }
1190 }
1191
1192 pub(crate) fn check_lhs_assignable(
1193 &self,
1194 lhs: &'tcx hir::Expr<'tcx>,
1195 code: ErrCode,
1196 op_span: Span,
1197 adjust_err: impl FnOnce(&mut Diag<'_>),
1198 ) {
1199 if lhs.is_syntactic_place_expr() {
1200 return;
1201 }
1202
1203 let mut err = self.dcx().struct_span_err(op_span, "invalid left-hand side of assignment");
1204 err.code(code);
1205 err.span_label(lhs.span, "cannot assign to this expression");
1206
1207 self.comes_from_while_condition(lhs.hir_id, |expr| {
1208 err.span_suggestion_verbose(
1209 expr.span.shrink_to_lo(),
1210 "you might have meant to use pattern destructuring",
1211 "let ",
1212 Applicability::MachineApplicable,
1213 );
1214 });
1215 self.check_for_missing_semi(lhs, &mut err);
1216
1217 adjust_err(&mut err);
1218
1219 err.emit();
1220 }
1221
1222 pub(crate) fn check_for_missing_semi(
1224 &self,
1225 expr: &'tcx hir::Expr<'tcx>,
1226 err: &mut Diag<'_>,
1227 ) -> bool {
1228 if let hir::ExprKind::Binary(binop, lhs, rhs) = expr.kind
1229 && let hir::BinOpKind::Mul = binop.node
1230 && self.tcx.sess.source_map().is_multiline(lhs.span.between(rhs.span))
1231 && rhs.is_syntactic_place_expr()
1232 {
1233 err.span_suggestion_verbose(
1238 lhs.span.shrink_to_hi(),
1239 "you might have meant to write a semicolon here",
1240 ";",
1241 Applicability::MachineApplicable,
1242 );
1243 return true;
1244 }
1245 false
1246 }
1247
1248 pub(super) fn comes_from_while_condition(
1252 &self,
1253 original_expr_id: HirId,
1254 then: impl FnOnce(&hir::Expr<'_>),
1255 ) {
1256 let mut parent = self.tcx.parent_hir_id(original_expr_id);
1257 loop {
1258 let node = self.tcx.hir_node(parent);
1259 match node {
1260 hir::Node::Expr(hir::Expr {
1261 kind:
1262 hir::ExprKind::Loop(
1263 hir::Block {
1264 expr:
1265 Some(hir::Expr {
1266 kind:
1267 hir::ExprKind::Match(expr, ..) | hir::ExprKind::If(expr, ..),
1268 ..
1269 }),
1270 ..
1271 },
1272 _,
1273 hir::LoopSource::While,
1274 _,
1275 ),
1276 ..
1277 }) => {
1278 if self.tcx.hir().parent_id_iter(original_expr_id).any(|id| id == expr.hir_id) {
1282 then(expr);
1283 }
1284 break;
1285 }
1286 hir::Node::Item(_)
1287 | hir::Node::ImplItem(_)
1288 | hir::Node::TraitItem(_)
1289 | hir::Node::Crate(_) => break,
1290 _ => {
1291 parent = self.tcx.parent_hir_id(parent);
1292 }
1293 }
1294 }
1295 }
1296
1297 fn check_expr_if(
1300 &self,
1301 cond_expr: &'tcx hir::Expr<'tcx>,
1302 then_expr: &'tcx hir::Expr<'tcx>,
1303 opt_else_expr: Option<&'tcx hir::Expr<'tcx>>,
1304 sp: Span,
1305 orig_expected: Expectation<'tcx>,
1306 ) -> Ty<'tcx> {
1307 let cond_ty = self.check_expr_has_type_or_error(cond_expr, self.tcx.types.bool, |_| {});
1308
1309 self.warn_if_unreachable(
1310 cond_expr.hir_id,
1311 then_expr.span,
1312 "block in `if` or `while` expression",
1313 );
1314
1315 let cond_diverges = self.diverges.get();
1316 self.diverges.set(Diverges::Maybe);
1317
1318 let expected = orig_expected.try_structurally_resolve_and_adjust_for_branches(self, sp);
1319 let then_ty = self.check_expr_with_expectation(then_expr, expected);
1320 let then_diverges = self.diverges.get();
1321 self.diverges.set(Diverges::Maybe);
1322
1323 let coerce_to_ty = expected.coercion_target_type(self, sp);
1330 let mut coerce: DynamicCoerceMany<'_> = CoerceMany::new(coerce_to_ty);
1331
1332 coerce.coerce(self, &self.misc(sp), then_expr, then_ty);
1333
1334 if let Some(else_expr) = opt_else_expr {
1335 let else_ty = self.check_expr_with_expectation(else_expr, expected);
1336 let else_diverges = self.diverges.get();
1337
1338 let tail_defines_return_position_impl_trait =
1339 self.return_position_impl_trait_from_match_expectation(orig_expected);
1340 let if_cause = self.if_cause(
1341 sp,
1342 cond_expr.span,
1343 then_expr,
1344 else_expr,
1345 then_ty,
1346 else_ty,
1347 tail_defines_return_position_impl_trait,
1348 );
1349
1350 coerce.coerce(self, &if_cause, else_expr, else_ty);
1351
1352 self.diverges.set(cond_diverges | then_diverges & else_diverges);
1354 } else {
1355 self.if_fallback_coercion(sp, cond_expr, then_expr, &mut coerce);
1356
1357 self.diverges.set(cond_diverges);
1359 }
1360
1361 let result_ty = coerce.complete(self);
1362 if let Err(guar) = cond_ty.error_reported() {
1363 Ty::new_error(self.tcx, guar)
1364 } else {
1365 result_ty
1366 }
1367 }
1368
1369 fn check_expr_assign(
1372 &self,
1373 expr: &'tcx hir::Expr<'tcx>,
1374 expected: Expectation<'tcx>,
1375 lhs: &'tcx hir::Expr<'tcx>,
1376 rhs: &'tcx hir::Expr<'tcx>,
1377 span: Span,
1378 ) -> Ty<'tcx> {
1379 let expected_ty = expected.only_has_type(self);
1380 if expected_ty == Some(self.tcx.types.bool) {
1381 let guar = self.expr_assign_expected_bool_error(expr, lhs, rhs, span);
1382 return Ty::new_error(self.tcx, guar);
1383 }
1384
1385 let lhs_ty = self.check_expr_with_needs(lhs, Needs::MutPlace);
1386
1387 let suggest_deref_binop = |err: &mut Diag<'_>, rhs_ty: Ty<'tcx>| {
1388 if let Some(lhs_deref_ty) = self.deref_once_mutably_for_diagnostic(lhs_ty) {
1389 let lhs_deref_ty_is_sized = self
1392 .infcx
1393 .type_implements_trait(
1394 self.tcx.require_lang_item(LangItem::Sized, None),
1395 [lhs_deref_ty],
1396 self.param_env,
1397 )
1398 .may_apply();
1399 if lhs_deref_ty_is_sized && self.may_coerce(rhs_ty, lhs_deref_ty) {
1400 err.span_suggestion_verbose(
1401 lhs.span.shrink_to_lo(),
1402 "consider dereferencing here to assign to the mutably borrowed value",
1403 "*",
1404 Applicability::MachineApplicable,
1405 );
1406 }
1407 }
1408 };
1409
1410 let rhs_ty = self.check_expr_with_hint(rhs, lhs_ty);
1413 if let Err(mut diag) =
1414 self.demand_coerce_diag(rhs, rhs_ty, lhs_ty, Some(lhs), AllowTwoPhase::No)
1415 {
1416 suggest_deref_binop(&mut diag, rhs_ty);
1417 diag.emit();
1418 }
1419
1420 self.check_lhs_assignable(lhs, E0070, span, |err| {
1421 if let Some(rhs_ty) = self.typeck_results.borrow().expr_ty_opt(rhs) {
1422 suggest_deref_binop(err, rhs_ty);
1423 }
1424 });
1425
1426 self.require_type_is_sized(lhs_ty, lhs.span, ObligationCauseCode::AssignmentLhsSized);
1427
1428 if let Err(guar) = (lhs_ty, rhs_ty).error_reported() {
1429 Ty::new_error(self.tcx, guar)
1430 } else {
1431 self.tcx.types.unit
1432 }
1433 }
1434
1435 fn expr_assign_expected_bool_error(
1439 &self,
1440 expr: &'tcx hir::Expr<'tcx>,
1441 lhs: &'tcx hir::Expr<'tcx>,
1442 rhs: &'tcx hir::Expr<'tcx>,
1443 span: Span,
1444 ) -> ErrorGuaranteed {
1445 let actual_ty = self.tcx.types.unit;
1446 let expected_ty = self.tcx.types.bool;
1447 let mut err = self.demand_suptype_diag(expr.span, expected_ty, actual_ty).unwrap_err();
1448 let lhs_ty = self.check_expr(lhs);
1449 let rhs_ty = self.check_expr(rhs);
1450 let refs_can_coerce = |lhs: Ty<'tcx>, rhs: Ty<'tcx>| {
1451 let lhs = Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_erased, lhs.peel_refs());
1452 let rhs = Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_erased, rhs.peel_refs());
1453 self.may_coerce(rhs, lhs)
1454 };
1455 let (applicability, eq) = if self.may_coerce(rhs_ty, lhs_ty) {
1456 (Applicability::MachineApplicable, true)
1457 } else if refs_can_coerce(rhs_ty, lhs_ty) {
1458 (Applicability::MaybeIncorrect, true)
1461 } else if let ExprKind::Binary(
1462 Spanned { node: hir::BinOpKind::And | hir::BinOpKind::Or, .. },
1463 _,
1464 rhs_expr,
1465 ) = lhs.kind
1466 {
1467 let actual_lhs = self.check_expr(rhs_expr);
1470 let may_eq = self.may_coerce(rhs_ty, actual_lhs) || refs_can_coerce(rhs_ty, actual_lhs);
1471 (Applicability::MaybeIncorrect, may_eq)
1472 } else if let ExprKind::Binary(
1473 Spanned { node: hir::BinOpKind::And | hir::BinOpKind::Or, .. },
1474 lhs_expr,
1475 _,
1476 ) = rhs.kind
1477 {
1478 let actual_rhs = self.check_expr(lhs_expr);
1481 let may_eq = self.may_coerce(actual_rhs, lhs_ty) || refs_can_coerce(actual_rhs, lhs_ty);
1482 (Applicability::MaybeIncorrect, may_eq)
1483 } else {
1484 (Applicability::MaybeIncorrect, false)
1485 };
1486
1487 if !lhs.is_syntactic_place_expr()
1488 && lhs.is_approximately_pattern()
1489 && !matches!(lhs.kind, hir::ExprKind::Lit(_))
1490 {
1491 if let hir::Node::Expr(hir::Expr { kind: ExprKind::If { .. }, .. }) =
1493 self.tcx.parent_hir_node(expr.hir_id)
1494 {
1495 err.span_suggestion_verbose(
1496 expr.span.shrink_to_lo(),
1497 "you might have meant to use pattern matching",
1498 "let ",
1499 applicability,
1500 );
1501 };
1502 }
1503 if eq {
1504 err.span_suggestion_verbose(
1505 span.shrink_to_hi(),
1506 "you might have meant to compare for equality",
1507 '=',
1508 applicability,
1509 );
1510 }
1511
1512 err.emit_unless(lhs_ty.references_error() || rhs_ty.references_error())
1515 }
1516
1517 pub(super) fn check_expr_let(
1518 &self,
1519 let_expr: &'tcx hir::LetExpr<'tcx>,
1520 hir_id: HirId,
1521 ) -> Ty<'tcx> {
1522 let init = let_expr.init;
1524 self.warn_if_unreachable(init.hir_id, init.span, "block in `let` expression");
1525 self.check_decl((let_expr, hir_id).into());
1527 if let ast::Recovered::Yes(error_guaranteed) = let_expr.recovered {
1529 self.set_tainted_by_errors(error_guaranteed);
1530 Ty::new_error(self.tcx, error_guaranteed)
1531 } else {
1532 self.tcx.types.bool
1533 }
1534 }
1535
1536 fn check_expr_loop(
1537 &self,
1538 body: &'tcx hir::Block<'tcx>,
1539 source: hir::LoopSource,
1540 expected: Expectation<'tcx>,
1541 expr: &'tcx hir::Expr<'tcx>,
1542 ) -> Ty<'tcx> {
1543 let coerce = match source {
1544 hir::LoopSource::Loop => {
1546 let coerce_to = expected.coercion_target_type(self, body.span);
1547 Some(CoerceMany::new(coerce_to))
1548 }
1549
1550 hir::LoopSource::While | hir::LoopSource::ForLoop => None,
1551 };
1552
1553 let ctxt = BreakableCtxt {
1554 coerce,
1555 may_break: false, };
1557
1558 let (ctxt, ()) = self.with_breakable_ctxt(expr.hir_id, ctxt, || {
1559 self.check_block_no_value(body);
1560 });
1561
1562 if ctxt.may_break {
1563 self.diverges.set(Diverges::Maybe);
1566 } else {
1567 self.diverges.set(self.diverges.get() | Diverges::always(expr.span));
1568 }
1569
1570 if ctxt.coerce.is_none() && !ctxt.may_break {
1576 self.dcx().span_bug(body.span, "no coercion, but loop may not break");
1577 }
1578 ctxt.coerce.map(|c| c.complete(self)).unwrap_or_else(|| self.tcx.types.unit)
1579 }
1580
1581 fn check_expr_method_call(
1583 &self,
1584 expr: &'tcx hir::Expr<'tcx>,
1585 segment: &'tcx hir::PathSegment<'tcx>,
1586 rcvr: &'tcx hir::Expr<'tcx>,
1587 args: &'tcx [hir::Expr<'tcx>],
1588 expected: Expectation<'tcx>,
1589 ) -> Ty<'tcx> {
1590 let rcvr_t = self.check_expr(rcvr);
1591 let rcvr_t = self.structurally_resolve_type(rcvr.span, rcvr_t);
1593
1594 let method = match self.lookup_method(rcvr_t, segment, segment.ident.span, expr, rcvr, args)
1595 {
1596 Ok(method) => {
1597 self.write_method_call_and_enforce_effects(expr.hir_id, expr.span, method);
1600 Ok(method)
1601 }
1602 Err(error) => {
1603 if segment.ident.name == kw::Empty {
1604 span_bug!(rcvr.span, "empty method name")
1605 } else {
1606 Err(self.report_method_error(expr.hir_id, rcvr_t, error, expected, false))
1607 }
1608 }
1609 };
1610
1611 self.check_method_argument_types(
1613 segment.ident.span,
1614 expr,
1615 method,
1616 args,
1617 DontTupleArguments,
1618 expected,
1619 )
1620 }
1621
1622 fn check_expr_cast(
1623 &self,
1624 e: &'tcx hir::Expr<'tcx>,
1625 t: &'tcx hir::Ty<'tcx>,
1626 expr: &'tcx hir::Expr<'tcx>,
1627 ) -> Ty<'tcx> {
1628 let t_cast = self.lower_ty_saving_user_provided_ty(t);
1631 let t_cast = self.resolve_vars_if_possible(t_cast);
1632 let t_expr = self.check_expr_with_expectation(e, ExpectCastableToType(t_cast));
1633 let t_expr = self.resolve_vars_if_possible(t_expr);
1634
1635 if let Err(guar) = (t_expr, t_cast).error_reported() {
1637 Ty::new_error(self.tcx, guar)
1638 } else {
1639 let mut deferred_cast_checks = self.deferred_cast_checks.borrow_mut();
1641 match cast::CastCheck::new(self, e, t_expr, t_cast, t.span, expr.span) {
1642 Ok(cast_check) => {
1643 debug!(
1644 "check_expr_cast: deferring cast from {:?} to {:?}: {:?}",
1645 t_cast, t_expr, cast_check,
1646 );
1647 deferred_cast_checks.push(cast_check);
1648 t_cast
1649 }
1650 Err(guar) => Ty::new_error(self.tcx, guar),
1651 }
1652 }
1653 }
1654
1655 fn check_expr_unsafe_binder_cast(
1656 &self,
1657 span: Span,
1658 kind: hir::UnsafeBinderCastKind,
1659 inner_expr: &'tcx hir::Expr<'tcx>,
1660 hir_ty: Option<&'tcx hir::Ty<'tcx>>,
1661 expected: Expectation<'tcx>,
1662 ) -> Ty<'tcx> {
1663 match kind {
1664 hir::UnsafeBinderCastKind::Wrap => {
1665 let ascribed_ty =
1666 hir_ty.map(|hir_ty| self.lower_ty_saving_user_provided_ty(hir_ty));
1667 let expected_ty = expected.only_has_type(self);
1668 let binder_ty = match (ascribed_ty, expected_ty) {
1669 (Some(ascribed_ty), Some(expected_ty)) => {
1670 self.demand_eqtype(inner_expr.span, expected_ty, ascribed_ty);
1671 expected_ty
1672 }
1673 (Some(ty), None) | (None, Some(ty)) => ty,
1674 (None, None) => self.next_ty_var(inner_expr.span),
1678 };
1679
1680 let binder_ty = self.structurally_resolve_type(inner_expr.span, binder_ty);
1681 let hint_ty = match *binder_ty.kind() {
1682 ty::UnsafeBinder(binder) => self.instantiate_binder_with_fresh_vars(
1683 inner_expr.span,
1684 infer::BoundRegionConversionTime::HigherRankedType,
1685 binder.into(),
1686 ),
1687 ty::Error(e) => Ty::new_error(self.tcx, e),
1688 _ => {
1689 let guar = self
1690 .dcx()
1691 .struct_span_err(
1692 hir_ty.map_or(span, |hir_ty| hir_ty.span),
1693 format!(
1694 "`wrap_binder!()` can only wrap into unsafe binder, not {}",
1695 binder_ty.sort_string(self.tcx)
1696 ),
1697 )
1698 .with_note("unsafe binders are the only valid output of wrap")
1699 .emit();
1700 Ty::new_error(self.tcx, guar)
1701 }
1702 };
1703
1704 self.check_expr_has_type_or_error(inner_expr, hint_ty, |_| {});
1705
1706 binder_ty
1707 }
1708 hir::UnsafeBinderCastKind::Unwrap => {
1709 let ascribed_ty =
1710 hir_ty.map(|hir_ty| self.lower_ty_saving_user_provided_ty(hir_ty));
1711 let hint_ty = ascribed_ty.unwrap_or_else(|| self.next_ty_var(inner_expr.span));
1712 let binder_ty = self.check_expr_has_type_or_error(inner_expr, hint_ty, |_| {});
1714
1715 let binder_ty = self.structurally_resolve_type(inner_expr.span, binder_ty);
1718 match *binder_ty.kind() {
1719 ty::UnsafeBinder(binder) => self.instantiate_binder_with_fresh_vars(
1720 inner_expr.span,
1721 infer::BoundRegionConversionTime::HigherRankedType,
1722 binder.into(),
1723 ),
1724 ty::Error(e) => Ty::new_error(self.tcx, e),
1725 _ => {
1726 let guar = self
1727 .dcx()
1728 .struct_span_err(
1729 hir_ty.map_or(inner_expr.span, |hir_ty| hir_ty.span),
1730 format!(
1731 "expected unsafe binder, found {} as input of \
1732 `unwrap_binder!()`",
1733 binder_ty.sort_string(self.tcx)
1734 ),
1735 )
1736 .with_note("only an unsafe binder type can be unwrapped")
1737 .emit();
1738 Ty::new_error(self.tcx, guar)
1739 }
1740 }
1741 }
1742 }
1743 }
1744
1745 fn check_expr_array(
1746 &self,
1747 args: &'tcx [hir::Expr<'tcx>],
1748 expected: Expectation<'tcx>,
1749 expr: &'tcx hir::Expr<'tcx>,
1750 ) -> Ty<'tcx> {
1751 let element_ty = if !args.is_empty() {
1752 let coerce_to = expected
1753 .to_option(self)
1754 .and_then(|uty| match *self.try_structurally_resolve_type(expr.span, uty).kind() {
1755 ty::Array(ty, _) | ty::Slice(ty) => Some(ty),
1756 _ => None,
1757 })
1758 .unwrap_or_else(|| self.next_ty_var(expr.span));
1759 let mut coerce = CoerceMany::with_coercion_sites(coerce_to, args);
1760 assert_eq!(self.diverges.get(), Diverges::Maybe);
1761 for e in args {
1762 let e_ty = self.check_expr_with_hint(e, coerce_to);
1763 let cause = self.misc(e.span);
1764 coerce.coerce(self, &cause, e, e_ty);
1765 }
1766 coerce.complete(self)
1767 } else {
1768 self.next_ty_var(expr.span)
1769 };
1770 let array_len = args.len() as u64;
1771 self.suggest_array_len(expr, array_len);
1772 Ty::new_array(self.tcx, element_ty, array_len)
1773 }
1774
1775 fn suggest_array_len(&self, expr: &'tcx hir::Expr<'tcx>, array_len: u64) {
1776 let parent_node = self.tcx.hir().parent_iter(expr.hir_id).find(|(_, node)| {
1777 !matches!(node, hir::Node::Expr(hir::Expr { kind: hir::ExprKind::AddrOf(..), .. }))
1778 });
1779 let Some((_, hir::Node::LetStmt(hir::LetStmt { ty: Some(ty), .. }))) = parent_node else {
1780 return;
1781 };
1782 if let hir::TyKind::Array(_, ct) = ty.peel_refs().kind {
1783 let span = ct.span();
1784 self.dcx().try_steal_modify_and_emit_err(
1785 span,
1786 StashKey::UnderscoreForArrayLengths,
1787 |err| {
1788 err.span_suggestion(
1789 span,
1790 "consider specifying the array length",
1791 array_len,
1792 Applicability::MaybeIncorrect,
1793 );
1794 },
1795 );
1796 }
1797 }
1798
1799 pub(super) fn check_expr_const_block(
1800 &self,
1801 block: &'tcx hir::ConstBlock,
1802 expected: Expectation<'tcx>,
1803 ) -> Ty<'tcx> {
1804 let body = self.tcx.hir().body(block.body);
1805
1806 let def_id = block.def_id;
1808 let fcx = FnCtxt::new(self, self.param_env, def_id);
1809 crate::GatherLocalsVisitor::new(&fcx).visit_body(body);
1810
1811 let ty = fcx.check_expr_with_expectation(body.value, expected);
1812 fcx.require_type_is_sized(ty, body.value.span, ObligationCauseCode::ConstSized);
1813 fcx.write_ty(block.hir_id, ty);
1814 ty
1815 }
1816
1817 fn check_expr_repeat(
1818 &self,
1819 element: &'tcx hir::Expr<'tcx>,
1820 count: &'tcx hir::ConstArg<'tcx>,
1821 expected: Expectation<'tcx>,
1822 expr: &'tcx hir::Expr<'tcx>,
1823 ) -> Ty<'tcx> {
1824 let tcx = self.tcx;
1825 let count_span = count.span();
1826 let count = self.try_structurally_resolve_const(
1827 count_span,
1828 self.normalize(count_span, self.lower_const_arg(count, FeedConstTy::No)),
1829 );
1830
1831 if let Some(count) = count.try_to_target_usize(tcx) {
1832 self.suggest_array_len(expr, count);
1833 }
1834
1835 let uty = match expected {
1836 ExpectHasType(uty) => match *uty.kind() {
1837 ty::Array(ty, _) | ty::Slice(ty) => Some(ty),
1838 _ => None,
1839 },
1840 _ => None,
1841 };
1842
1843 let (element_ty, t) = match uty {
1844 Some(uty) => {
1845 self.check_expr_coercible_to_type(element, uty, None);
1846 (uty, uty)
1847 }
1848 None => {
1849 let ty = self.next_ty_var(element.span);
1850 let element_ty = self.check_expr_has_type_or_error(element, ty, |_| {});
1851 (element_ty, ty)
1852 }
1853 };
1854
1855 if let Err(guar) = element_ty.error_reported() {
1856 return Ty::new_error(tcx, guar);
1857 }
1858
1859 if count.try_to_target_usize(tcx).is_none_or(|x| x > 1) {
1865 self.enforce_repeat_element_needs_copy_bound(element, element_ty);
1866 }
1867
1868 let ty = Ty::new_array_with_const_len(tcx, t, count);
1869 self.register_wf_obligation(ty.into(), expr.span, ObligationCauseCode::WellFormed(None));
1870 ty
1871 }
1872
1873 fn enforce_repeat_element_needs_copy_bound(
1875 &self,
1876 element: &hir::Expr<'_>,
1877 element_ty: Ty<'tcx>,
1878 ) {
1879 let tcx = self.tcx;
1880 match &element.kind {
1882 hir::ExprKind::ConstBlock(..) => return,
1883 hir::ExprKind::Path(qpath) => {
1884 let res = self.typeck_results.borrow().qpath_res(qpath, element.hir_id);
1885 if let Res::Def(DefKind::Const | DefKind::AssocConst | DefKind::AnonConst, _) = res
1886 {
1887 return;
1888 }
1889 }
1890 _ => {}
1891 }
1892 let is_constable = match element.kind {
1896 hir::ExprKind::Call(func, _args) => match *self.node_ty(func.hir_id).kind() {
1897 ty::FnDef(def_id, _) if tcx.is_stable_const_fn(def_id) => traits::IsConstable::Fn,
1898 _ => traits::IsConstable::No,
1899 },
1900 hir::ExprKind::Path(qpath) => {
1901 match self.typeck_results.borrow().qpath_res(&qpath, element.hir_id) {
1902 Res::Def(DefKind::Ctor(_, CtorKind::Const), _) => traits::IsConstable::Ctor,
1903 _ => traits::IsConstable::No,
1904 }
1905 }
1906 _ => traits::IsConstable::No,
1907 };
1908
1909 let lang_item = self.tcx.require_lang_item(LangItem::Copy, None);
1910 let code =
1911 traits::ObligationCauseCode::RepeatElementCopy { is_constable, elt_span: element.span };
1912 self.require_type_meets(element_ty, element.span, code, lang_item);
1913 }
1914
1915 fn check_expr_tuple(
1916 &self,
1917 elts: &'tcx [hir::Expr<'tcx>],
1918 expected: Expectation<'tcx>,
1919 expr: &'tcx hir::Expr<'tcx>,
1920 ) -> Ty<'tcx> {
1921 let flds = expected.only_has_type(self).and_then(|ty| {
1922 let ty = self.try_structurally_resolve_type(expr.span, ty);
1923 match ty.kind() {
1924 ty::Tuple(flds) => Some(&flds[..]),
1925 _ => None,
1926 }
1927 });
1928
1929 let elt_ts_iter = elts.iter().enumerate().map(|(i, e)| match flds {
1930 Some(fs) if i < fs.len() => {
1931 let ety = fs[i];
1932 self.check_expr_coercible_to_type(e, ety, None);
1933 ety
1934 }
1935 _ => self.check_expr_with_expectation(e, NoExpectation),
1936 });
1937 let tuple = Ty::new_tup_from_iter(self.tcx, elt_ts_iter);
1938 if let Err(guar) = tuple.error_reported() {
1939 Ty::new_error(self.tcx, guar)
1940 } else {
1941 self.require_type_is_sized(
1942 tuple,
1943 expr.span,
1944 ObligationCauseCode::TupleInitializerSized,
1945 );
1946 tuple
1947 }
1948 }
1949
1950 fn check_expr_struct(
1951 &self,
1952 expr: &hir::Expr<'tcx>,
1953 expected: Expectation<'tcx>,
1954 qpath: &'tcx QPath<'tcx>,
1955 fields: &'tcx [hir::ExprField<'tcx>],
1956 base_expr: &'tcx hir::StructTailExpr<'tcx>,
1957 ) -> Ty<'tcx> {
1958 let (variant, adt_ty) = match self.check_struct_path(qpath, expr.hir_id) {
1960 Ok(data) => data,
1961 Err(guar) => {
1962 self.check_struct_fields_on_error(fields, base_expr);
1963 return Ty::new_error(self.tcx, guar);
1964 }
1965 };
1966
1967 let adt = adt_ty.ty_adt_def().expect("`check_struct_path` returned non-ADT type");
1969 if !adt.did().is_local() && variant.is_field_list_non_exhaustive() {
1970 self.dcx()
1971 .emit_err(StructExprNonExhaustive { span: expr.span, what: adt.variant_descr() });
1972 }
1973
1974 self.check_expr_struct_fields(
1975 adt_ty,
1976 expected,
1977 expr,
1978 qpath.span(),
1979 variant,
1980 fields,
1981 base_expr,
1982 );
1983
1984 self.require_type_is_sized(adt_ty, expr.span, ObligationCauseCode::StructInitializerSized);
1985 adt_ty
1986 }
1987
1988 fn check_expr_struct_fields(
1989 &self,
1990 adt_ty: Ty<'tcx>,
1991 expected: Expectation<'tcx>,
1992 expr: &hir::Expr<'_>,
1993 path_span: Span,
1994 variant: &'tcx ty::VariantDef,
1995 hir_fields: &'tcx [hir::ExprField<'tcx>],
1996 base_expr: &'tcx hir::StructTailExpr<'tcx>,
1997 ) {
1998 let tcx = self.tcx;
1999
2000 let adt_ty = self.try_structurally_resolve_type(path_span, adt_ty);
2001 let adt_ty_hint = expected.only_has_type(self).and_then(|expected| {
2002 self.fudge_inference_if_ok(|| {
2003 let ocx = ObligationCtxt::new(self);
2004 ocx.sup(&self.misc(path_span), self.param_env, expected, adt_ty)?;
2005 if !ocx.select_where_possible().is_empty() {
2006 return Err(TypeError::Mismatch);
2007 }
2008 Ok(self.resolve_vars_if_possible(adt_ty))
2009 })
2010 .ok()
2011 });
2012 if let Some(adt_ty_hint) = adt_ty_hint {
2013 self.demand_eqtype(path_span, adt_ty_hint, adt_ty);
2015 }
2016
2017 let ty::Adt(adt, args) = adt_ty.kind() else {
2018 span_bug!(path_span, "non-ADT passed to check_expr_struct_fields");
2019 };
2020 let adt_kind = adt.adt_kind();
2021
2022 let mut remaining_fields = variant
2023 .fields
2024 .iter_enumerated()
2025 .map(|(i, field)| (field.ident(tcx).normalize_to_macros_2_0(), (i, field)))
2026 .collect::<UnordMap<_, _>>();
2027
2028 let mut seen_fields = FxHashMap::default();
2029
2030 let mut error_happened = false;
2031
2032 if variant.fields.len() != remaining_fields.len() {
2033 let guar =
2036 self.dcx().span_delayed_bug(expr.span, "struct fields have non-unique names");
2037 self.set_tainted_by_errors(guar);
2038 error_happened = true;
2039 }
2040
2041 for (idx, field) in hir_fields.iter().enumerate() {
2043 let ident = tcx.adjust_ident(field.ident, variant.def_id);
2044 let field_type = if let Some((i, v_field)) = remaining_fields.remove(&ident) {
2045 seen_fields.insert(ident, field.span);
2046 self.write_field_index(field.hir_id, i);
2047
2048 if adt_kind != AdtKind::Enum {
2052 tcx.check_stability(v_field.did, Some(expr.hir_id), field.span, None);
2053 }
2054
2055 self.field_ty(field.span, v_field, args)
2056 } else {
2057 error_happened = true;
2058 let guar = if let Some(prev_span) = seen_fields.get(&ident) {
2059 self.dcx().emit_err(FieldMultiplySpecifiedInInitializer {
2060 span: field.ident.span,
2061 prev_span: *prev_span,
2062 ident,
2063 })
2064 } else {
2065 self.report_unknown_field(
2066 adt_ty,
2067 variant,
2068 expr,
2069 field,
2070 hir_fields,
2071 adt.variant_descr(),
2072 )
2073 };
2074
2075 Ty::new_error(tcx, guar)
2076 };
2077
2078 self.register_wf_obligation(
2082 field_type.into(),
2083 field.expr.span,
2084 ObligationCauseCode::WellFormed(None),
2085 );
2086
2087 let ty = self.check_expr_with_hint(field.expr, field_type);
2090 let diag = self.demand_coerce_diag(field.expr, ty, field_type, None, AllowTwoPhase::No);
2091
2092 if let Err(diag) = diag {
2093 if idx == hir_fields.len() - 1 {
2094 if remaining_fields.is_empty() {
2095 self.suggest_fru_from_range_and_emit(field, variant, args, diag);
2096 } else {
2097 diag.stash(field.span, StashKey::MaybeFruTypo);
2098 }
2099 } else {
2100 diag.emit();
2101 }
2102 }
2103 }
2104
2105 if adt_kind == AdtKind::Union && hir_fields.len() != 1 {
2107 struct_span_code_err!(
2108 self.dcx(),
2109 path_span,
2110 E0784,
2111 "union expressions should have exactly one field",
2112 )
2113 .emit();
2114 }
2115
2116 if error_happened {
2120 if let hir::StructTailExpr::Base(base_expr) = base_expr {
2121 self.check_expr(base_expr);
2122 }
2123 return;
2124 }
2125
2126 if let hir::StructTailExpr::DefaultFields(span) = *base_expr {
2127 let mut missing_mandatory_fields = Vec::new();
2128 let mut missing_optional_fields = Vec::new();
2129 for f in &variant.fields {
2130 let ident = self.tcx.adjust_ident(f.ident(self.tcx), variant.def_id);
2131 if let Some(_) = remaining_fields.remove(&ident) {
2132 if f.value.is_none() {
2133 missing_mandatory_fields.push(ident);
2134 } else {
2135 missing_optional_fields.push(ident);
2136 }
2137 }
2138 }
2139 if !self.tcx.features().default_field_values() {
2140 let sugg = self.tcx.crate_level_attribute_injection_span(expr.hir_id);
2141 self.dcx().emit_err(BaseExpressionDoubleDot {
2142 span: span.shrink_to_hi(),
2143 default_field_values_suggestion: if self.tcx.sess.is_nightly_build()
2146 && missing_mandatory_fields.is_empty()
2147 && !missing_optional_fields.is_empty()
2148 && sugg.is_some()
2149 {
2150 sugg
2151 } else {
2152 None
2153 },
2154 default_field_values_help: if self.tcx.sess.is_nightly_build()
2155 && missing_mandatory_fields.is_empty()
2156 && !missing_optional_fields.is_empty()
2157 && sugg.is_none()
2158 {
2159 Some(BaseExpressionDoubleDotEnableDefaultFieldValues)
2160 } else {
2161 None
2162 },
2163 add_expr: if !missing_mandatory_fields.is_empty()
2164 || !missing_optional_fields.is_empty()
2165 {
2166 Some(BaseExpressionDoubleDotAddExpr { span: span.shrink_to_hi() })
2167 } else {
2168 None
2169 },
2170 remove_dots: if missing_mandatory_fields.is_empty()
2171 && missing_optional_fields.is_empty()
2172 {
2173 Some(BaseExpressionDoubleDotRemove { span })
2174 } else {
2175 None
2176 },
2177 });
2178 return;
2179 }
2180 if variant.fields.is_empty() {
2181 let mut err = self.dcx().struct_span_err(
2182 span,
2183 format!(
2184 "`{adt_ty}` has no fields, `..` needs at least one default field in the \
2185 struct definition",
2186 ),
2187 );
2188 err.span_label(path_span, "this type has no fields");
2189 err.emit();
2190 }
2191 if !missing_mandatory_fields.is_empty() {
2192 let s = pluralize!(missing_mandatory_fields.len());
2193 let fields = listify(&missing_mandatory_fields, |f| format!("`{f}`")).unwrap();
2194 self.dcx()
2195 .struct_span_err(
2196 span.shrink_to_hi(),
2197 format!("missing mandatory field{s} {fields}"),
2198 )
2199 .emit();
2200 return;
2201 }
2202 let fru_tys = match adt_ty.kind() {
2203 ty::Adt(adt, args) if adt.is_struct() => variant
2204 .fields
2205 .iter()
2206 .map(|f| self.normalize(span, f.ty(self.tcx, args)))
2207 .collect(),
2208 ty::Adt(adt, args) if adt.is_enum() => variant
2209 .fields
2210 .iter()
2211 .map(|f| self.normalize(span, f.ty(self.tcx, args)))
2212 .collect(),
2213 _ => {
2214 self.dcx().emit_err(FunctionalRecordUpdateOnNonStruct { span });
2215 return;
2216 }
2217 };
2218 self.typeck_results.borrow_mut().fru_field_types_mut().insert(expr.hir_id, fru_tys);
2219 } else if let hir::StructTailExpr::Base(base_expr) = base_expr {
2220 let fru_tys = if self.tcx.features().type_changing_struct_update() {
2223 if adt.is_struct() {
2224 let fresh_args = self.fresh_args_for_item(base_expr.span, adt.did());
2226 let fru_tys = variant
2231 .fields
2232 .iter()
2233 .map(|f| {
2234 let fru_ty = self
2235 .normalize(expr.span, self.field_ty(base_expr.span, f, fresh_args));
2236 let ident = self.tcx.adjust_ident(f.ident(self.tcx), variant.def_id);
2237 if let Some(_) = remaining_fields.remove(&ident) {
2238 let target_ty = self.field_ty(base_expr.span, f, args);
2239 let cause = self.misc(base_expr.span);
2240 match self.at(&cause, self.param_env).sup(
2241 DefineOpaqueTypes::Yes,
2245 target_ty,
2246 fru_ty,
2247 ) {
2248 Ok(InferOk { obligations, value: () }) => {
2249 self.register_predicates(obligations)
2250 }
2251 Err(_) => {
2252 span_bug!(
2253 cause.span,
2254 "subtyping remaining fields of type changing FRU failed: {target_ty} != {fru_ty}: {}::{}",
2255 variant.name,
2256 ident.name,
2257 );
2258 }
2259 }
2260 }
2261 self.resolve_vars_if_possible(fru_ty)
2262 })
2263 .collect();
2264 let fresh_base_ty = Ty::new_adt(self.tcx, *adt, fresh_args);
2283 self.check_expr_has_type_or_error(
2284 base_expr,
2285 self.resolve_vars_if_possible(fresh_base_ty),
2286 |_| {},
2287 );
2288 fru_tys
2289 } else {
2290 self.check_expr(base_expr);
2293 self.dcx().emit_err(FunctionalRecordUpdateOnNonStruct { span: base_expr.span });
2294 return;
2295 }
2296 } else {
2297 self.check_expr_has_type_or_error(base_expr, adt_ty, |_| {
2298 let base_ty = self.typeck_results.borrow().expr_ty(*base_expr);
2299 let same_adt = matches!((adt_ty.kind(), base_ty.kind()),
2300 (ty::Adt(adt, _), ty::Adt(base_adt, _)) if adt == base_adt);
2301 if self.tcx.sess.is_nightly_build() && same_adt {
2302 feature_err(
2303 &self.tcx.sess,
2304 sym::type_changing_struct_update,
2305 base_expr.span,
2306 "type changing struct updating is experimental",
2307 )
2308 .emit();
2309 }
2310 });
2311 match adt_ty.kind() {
2312 ty::Adt(adt, args) if adt.is_struct() => variant
2313 .fields
2314 .iter()
2315 .map(|f| self.normalize(expr.span, f.ty(self.tcx, args)))
2316 .collect(),
2317 _ => {
2318 self.dcx()
2319 .emit_err(FunctionalRecordUpdateOnNonStruct { span: base_expr.span });
2320 return;
2321 }
2322 }
2323 };
2324 self.typeck_results.borrow_mut().fru_field_types_mut().insert(expr.hir_id, fru_tys);
2325 } else if adt_kind != AdtKind::Union && !remaining_fields.is_empty() {
2326 debug!(?remaining_fields);
2327 let private_fields: Vec<&ty::FieldDef> = variant
2328 .fields
2329 .iter()
2330 .filter(|field| !field.vis.is_accessible_from(tcx.parent_module(expr.hir_id), tcx))
2331 .collect();
2332
2333 if !private_fields.is_empty() {
2334 self.report_private_fields(
2335 adt_ty,
2336 path_span,
2337 expr.span,
2338 private_fields,
2339 hir_fields,
2340 );
2341 } else {
2342 self.report_missing_fields(
2343 adt_ty,
2344 path_span,
2345 expr.span,
2346 remaining_fields,
2347 variant,
2348 hir_fields,
2349 args,
2350 );
2351 }
2352 }
2353 }
2354
2355 fn check_struct_fields_on_error(
2356 &self,
2357 fields: &'tcx [hir::ExprField<'tcx>],
2358 base_expr: &'tcx hir::StructTailExpr<'tcx>,
2359 ) {
2360 for field in fields {
2361 self.check_expr(field.expr);
2362 }
2363 if let hir::StructTailExpr::Base(base) = *base_expr {
2364 self.check_expr(base);
2365 }
2366 }
2367
2368 fn report_missing_fields(
2380 &self,
2381 adt_ty: Ty<'tcx>,
2382 span: Span,
2383 full_span: Span,
2384 remaining_fields: UnordMap<Ident, (FieldIdx, &ty::FieldDef)>,
2385 variant: &'tcx ty::VariantDef,
2386 hir_fields: &'tcx [hir::ExprField<'tcx>],
2387 args: GenericArgsRef<'tcx>,
2388 ) {
2389 let len = remaining_fields.len();
2390
2391 let displayable_field_names: Vec<&str> =
2392 remaining_fields.items().map(|(ident, _)| ident.as_str()).into_sorted_stable_ord();
2393
2394 let mut truncated_fields_error = String::new();
2395 let remaining_fields_names = match &displayable_field_names[..] {
2396 [field1] => format!("`{field1}`"),
2397 [field1, field2] => format!("`{field1}` and `{field2}`"),
2398 [field1, field2, field3] => format!("`{field1}`, `{field2}` and `{field3}`"),
2399 _ => {
2400 truncated_fields_error =
2401 format!(" and {} other field{}", len - 3, pluralize!(len - 3));
2402 displayable_field_names
2403 .iter()
2404 .take(3)
2405 .map(|n| format!("`{n}`"))
2406 .collect::<Vec<_>>()
2407 .join(", ")
2408 }
2409 };
2410
2411 let mut err = struct_span_code_err!(
2412 self.dcx(),
2413 span,
2414 E0063,
2415 "missing field{} {}{} in initializer of `{}`",
2416 pluralize!(len),
2417 remaining_fields_names,
2418 truncated_fields_error,
2419 adt_ty
2420 );
2421 err.span_label(span, format!("missing {remaining_fields_names}{truncated_fields_error}"));
2422
2423 if remaining_fields.items().all(|(_, (_, field))| field.value.is_some())
2424 && self.tcx.sess.is_nightly_build()
2425 {
2426 let msg = format!(
2427 "all remaining fields have default values, {you_can} use those values with `..`",
2428 you_can = if self.tcx.features().default_field_values() {
2429 "you can"
2430 } else {
2431 "if you added `#![feature(default_field_values)]` to your crate you could"
2432 },
2433 );
2434 if let Some(hir_field) = hir_fields.last() {
2435 err.span_suggestion_verbose(
2436 hir_field.span.shrink_to_hi(),
2437 msg,
2438 ", ..".to_string(),
2439 Applicability::MachineApplicable,
2440 );
2441 } else if hir_fields.is_empty() {
2442 err.span_suggestion_verbose(
2443 span.shrink_to_hi().with_hi(full_span.hi()),
2444 msg,
2445 " { .. }".to_string(),
2446 Applicability::MachineApplicable,
2447 );
2448 }
2449 }
2450
2451 if let Some(hir_field) = hir_fields.last() {
2452 self.suggest_fru_from_range_and_emit(hir_field, variant, args, err);
2453 } else {
2454 err.emit();
2455 }
2456 }
2457
2458 fn suggest_fru_from_range_and_emit(
2461 &self,
2462 last_expr_field: &hir::ExprField<'tcx>,
2463 variant: &ty::VariantDef,
2464 args: GenericArgsRef<'tcx>,
2465 mut err: Diag<'_>,
2466 ) {
2467 if let ExprKind::Struct(QPath::LangItem(LangItem::Range, ..), [range_start, range_end], _) =
2469 last_expr_field.expr.kind
2470 && let variant_field =
2471 variant.fields.iter().find(|field| field.ident(self.tcx) == last_expr_field.ident)
2472 && let range_def_id = self.tcx.lang_items().range_struct()
2473 && variant_field
2474 .and_then(|field| field.ty(self.tcx, args).ty_adt_def())
2475 .map(|adt| adt.did())
2476 != range_def_id
2477 {
2478 let expr = self
2482 .tcx
2483 .sess
2484 .source_map()
2485 .span_to_snippet(range_end.expr.span)
2486 .ok()
2487 .filter(|s| s.len() < 25 && !s.contains(|c: char| c.is_control()));
2488
2489 let fru_span = self
2490 .tcx
2491 .sess
2492 .source_map()
2493 .span_extend_while_whitespace(range_start.span)
2494 .shrink_to_hi()
2495 .to(range_end.span);
2496
2497 err.subdiagnostic(TypeMismatchFruTypo { expr_span: range_start.span, fru_span, expr });
2498
2499 self.dcx().try_steal_replace_and_emit_err(
2501 last_expr_field.span,
2502 StashKey::MaybeFruTypo,
2503 err,
2504 );
2505 } else {
2506 err.emit();
2507 }
2508 }
2509
2510 fn report_private_fields(
2522 &self,
2523 adt_ty: Ty<'tcx>,
2524 span: Span,
2525 expr_span: Span,
2526 private_fields: Vec<&ty::FieldDef>,
2527 used_fields: &'tcx [hir::ExprField<'tcx>],
2528 ) {
2529 let mut err =
2530 self.dcx().struct_span_err(
2531 span,
2532 format!(
2533 "cannot construct `{adt_ty}` with struct literal syntax due to private fields",
2534 ),
2535 );
2536 let (used_private_fields, remaining_private_fields): (
2537 Vec<(Symbol, Span, bool)>,
2538 Vec<(Symbol, Span, bool)>,
2539 ) = private_fields
2540 .iter()
2541 .map(|field| {
2542 match used_fields.iter().find(|used_field| field.name == used_field.ident.name) {
2543 Some(used_field) => (field.name, used_field.span, true),
2544 None => (field.name, self.tcx.def_span(field.did), false),
2545 }
2546 })
2547 .partition(|field| field.2);
2548 err.span_labels(used_private_fields.iter().map(|(_, span, _)| *span), "private field");
2549 if !remaining_private_fields.is_empty() {
2550 let names = if remaining_private_fields.len() > 6 {
2551 String::new()
2552 } else {
2553 format!(
2554 "{} ",
2555 listify(&remaining_private_fields, |(name, _, _)| format!("`{name}`"))
2556 .expect("expected at least one private field to report")
2557 )
2558 };
2559 err.note(format!(
2560 "{}private field{s} {names}that {were} not provided",
2561 if used_fields.is_empty() { "" } else { "...and other " },
2562 s = pluralize!(remaining_private_fields.len()),
2563 were = pluralize!("was", remaining_private_fields.len()),
2564 ));
2565 }
2566
2567 if let ty::Adt(def, _) = adt_ty.kind() {
2568 let def_id = def.did();
2569 let mut items = self
2570 .tcx
2571 .inherent_impls(def_id)
2572 .into_iter()
2573 .flat_map(|i| self.tcx.associated_items(i).in_definition_order())
2574 .filter(|item| {
2576 matches!(item.kind, ty::AssocKind::Fn) && !item.fn_has_self_parameter
2577 })
2578 .filter_map(|item| {
2579 let fn_sig = self.tcx.fn_sig(item.def_id).skip_binder();
2581 let ret_ty = fn_sig.output();
2582 let ret_ty = self.tcx.normalize_erasing_late_bound_regions(
2583 self.typing_env(self.param_env),
2584 ret_ty,
2585 );
2586 if !self.can_eq(self.param_env, ret_ty, adt_ty) {
2587 return None;
2588 }
2589 let input_len = fn_sig.inputs().skip_binder().len();
2590 let order = !item.name.as_str().starts_with("new");
2591 Some((order, item.name, input_len))
2592 })
2593 .collect::<Vec<_>>();
2594 items.sort_by_key(|(order, _, _)| *order);
2595 let suggestion = |name, args| {
2596 format!(
2597 "::{name}({})",
2598 std::iter::repeat("_").take(args).collect::<Vec<_>>().join(", ")
2599 )
2600 };
2601 match &items[..] {
2602 [] => {}
2603 [(_, name, args)] => {
2604 err.span_suggestion_verbose(
2605 span.shrink_to_hi().with_hi(expr_span.hi()),
2606 format!("you might have meant to use the `{name}` associated function"),
2607 suggestion(name, *args),
2608 Applicability::MaybeIncorrect,
2609 );
2610 }
2611 _ => {
2612 err.span_suggestions(
2613 span.shrink_to_hi().with_hi(expr_span.hi()),
2614 "you might have meant to use an associated function to build this type",
2615 items.iter().map(|(_, name, args)| suggestion(name, *args)),
2616 Applicability::MaybeIncorrect,
2617 );
2618 }
2619 }
2620 if let Some(default_trait) = self.tcx.get_diagnostic_item(sym::Default)
2621 && self
2622 .infcx
2623 .type_implements_trait(default_trait, [adt_ty], self.param_env)
2624 .may_apply()
2625 {
2626 err.multipart_suggestion(
2627 "consider using the `Default` trait",
2628 vec![
2629 (span.shrink_to_lo(), "<".to_string()),
2630 (
2631 span.shrink_to_hi().with_hi(expr_span.hi()),
2632 " as std::default::Default>::default()".to_string(),
2633 ),
2634 ],
2635 Applicability::MaybeIncorrect,
2636 );
2637 }
2638 }
2639
2640 err.emit();
2641 }
2642
2643 fn report_unknown_field(
2644 &self,
2645 ty: Ty<'tcx>,
2646 variant: &'tcx ty::VariantDef,
2647 expr: &hir::Expr<'_>,
2648 field: &hir::ExprField<'_>,
2649 skip_fields: &[hir::ExprField<'_>],
2650 kind_name: &str,
2651 ) -> ErrorGuaranteed {
2652 if let Err(guar) = variant.has_errors() {
2654 return guar;
2655 }
2656 let mut err = self.err_ctxt().type_error_struct_with_diag(
2657 field.ident.span,
2658 |actual| match ty.kind() {
2659 ty::Adt(adt, ..) if adt.is_enum() => struct_span_code_err!(
2660 self.dcx(),
2661 field.ident.span,
2662 E0559,
2663 "{} `{}::{}` has no field named `{}`",
2664 kind_name,
2665 actual,
2666 variant.name,
2667 field.ident
2668 ),
2669 _ => struct_span_code_err!(
2670 self.dcx(),
2671 field.ident.span,
2672 E0560,
2673 "{} `{}` has no field named `{}`",
2674 kind_name,
2675 actual,
2676 field.ident
2677 ),
2678 },
2679 ty,
2680 );
2681
2682 let variant_ident_span = self.tcx.def_ident_span(variant.def_id).unwrap();
2683 match variant.ctor {
2684 Some((CtorKind::Fn, def_id)) => match ty.kind() {
2685 ty::Adt(adt, ..) if adt.is_enum() => {
2686 err.span_label(
2687 variant_ident_span,
2688 format!(
2689 "`{adt}::{variant}` defined here",
2690 adt = ty,
2691 variant = variant.name,
2692 ),
2693 );
2694 err.span_label(field.ident.span, "field does not exist");
2695 let fn_sig = self.tcx.fn_sig(def_id).instantiate_identity();
2696 let inputs = fn_sig.inputs().skip_binder();
2697 let fields = format!(
2698 "({})",
2699 inputs.iter().map(|i| format!("/* {i} */")).collect::<Vec<_>>().join(", ")
2700 );
2701 let (replace_span, sugg) = match expr.kind {
2702 hir::ExprKind::Struct(qpath, ..) => {
2703 (qpath.span().shrink_to_hi().with_hi(expr.span.hi()), fields)
2704 }
2705 _ => {
2706 (expr.span, format!("{ty}::{variant}{fields}", variant = variant.name))
2707 }
2708 };
2709 err.span_suggestion_verbose(
2710 replace_span,
2711 format!(
2712 "`{adt}::{variant}` is a tuple {kind_name}, use the appropriate syntax",
2713 adt = ty,
2714 variant = variant.name,
2715 ),
2716 sugg,
2717 Applicability::HasPlaceholders,
2718 );
2719 }
2720 _ => {
2721 err.span_label(variant_ident_span, format!("`{ty}` defined here"));
2722 err.span_label(field.ident.span, "field does not exist");
2723 let fn_sig = self.tcx.fn_sig(def_id).instantiate_identity();
2724 let inputs = fn_sig.inputs().skip_binder();
2725 let fields = format!(
2726 "({})",
2727 inputs.iter().map(|i| format!("/* {i} */")).collect::<Vec<_>>().join(", ")
2728 );
2729 err.span_suggestion_verbose(
2730 expr.span,
2731 format!("`{ty}` is a tuple {kind_name}, use the appropriate syntax",),
2732 format!("{ty}{fields}"),
2733 Applicability::HasPlaceholders,
2734 );
2735 }
2736 },
2737 _ => {
2738 let available_field_names = self.available_field_names(variant, expr, skip_fields);
2740 if let Some(field_name) =
2741 find_best_match_for_name(&available_field_names, field.ident.name, None)
2742 {
2743 err.span_label(field.ident.span, "unknown field");
2744 err.span_suggestion_verbose(
2745 field.ident.span,
2746 "a field with a similar name exists",
2747 field_name,
2748 Applicability::MaybeIncorrect,
2749 );
2750 } else {
2751 match ty.kind() {
2752 ty::Adt(adt, ..) => {
2753 if adt.is_enum() {
2754 err.span_label(
2755 field.ident.span,
2756 format!("`{}::{}` does not have this field", ty, variant.name),
2757 );
2758 } else {
2759 err.span_label(
2760 field.ident.span,
2761 format!("`{ty}` does not have this field"),
2762 );
2763 }
2764 if available_field_names.is_empty() {
2765 err.note("all struct fields are already assigned");
2766 } else {
2767 err.note(format!(
2768 "available fields are: {}",
2769 self.name_series_display(available_field_names)
2770 ));
2771 }
2772 }
2773 _ => bug!("non-ADT passed to report_unknown_field"),
2774 }
2775 };
2776 }
2777 }
2778 err.emit()
2779 }
2780
2781 fn available_field_names(
2782 &self,
2783 variant: &'tcx ty::VariantDef,
2784 expr: &hir::Expr<'_>,
2785 skip_fields: &[hir::ExprField<'_>],
2786 ) -> Vec<Symbol> {
2787 variant
2788 .fields
2789 .iter()
2790 .filter(|field| {
2791 skip_fields.iter().all(|&skip| skip.ident.name != field.name)
2792 && self.is_field_suggestable(field, expr.hir_id, expr.span)
2793 })
2794 .map(|field| field.name)
2795 .collect()
2796 }
2797
2798 fn name_series_display(&self, names: Vec<Symbol>) -> String {
2799 let limit = if names.len() == 6 { 6 } else { 5 };
2801 let mut display =
2802 names.iter().take(limit).map(|n| format!("`{n}`")).collect::<Vec<_>>().join(", ");
2803 if names.len() > limit {
2804 display = format!("{} ... and {} others", display, names.len() - limit);
2805 }
2806 display
2807 }
2808
2809 fn find_adt_field(
2813 &self,
2814 base_def: ty::AdtDef<'tcx>,
2815 ident: Ident,
2816 ) -> Option<(FieldIdx, &'tcx ty::FieldDef)> {
2817 if base_def.is_enum() {
2819 return None;
2820 }
2821
2822 for (field_idx, field) in base_def.non_enum_variant().fields.iter_enumerated() {
2823 if field.ident(self.tcx).normalize_to_macros_2_0() == ident {
2824 return Some((field_idx, field));
2826 }
2827 }
2828
2829 None
2830 }
2831
2832 fn check_expr_field(
2842 &self,
2843 expr: &'tcx hir::Expr<'tcx>,
2844 base: &'tcx hir::Expr<'tcx>,
2845 field: Ident,
2846 expected: Expectation<'tcx>,
2848 ) -> Ty<'tcx> {
2849 debug!("check_field(expr: {:?}, base: {:?}, field: {:?})", expr, base, field);
2850 let base_ty = self.check_expr(base);
2851 let base_ty = self.structurally_resolve_type(base.span, base_ty);
2852
2853 let mut private_candidate = None;
2855
2856 let mut autoderef = self.autoderef(expr.span, base_ty);
2858 while let Some((deref_base_ty, _)) = autoderef.next() {
2859 debug!("deref_base_ty: {:?}", deref_base_ty);
2860 match deref_base_ty.kind() {
2861 ty::Adt(base_def, args) if !base_def.is_enum() => {
2862 debug!("struct named {:?}", deref_base_ty);
2863 if let Err(guar) = base_def.non_enum_variant().has_errors() {
2865 return Ty::new_error(self.tcx(), guar);
2866 }
2867
2868 let fn_body_hir_id = self.tcx.local_def_id_to_hir_id(self.body_id);
2869 let (ident, def_scope) =
2870 self.tcx.adjust_ident_and_get_scope(field, base_def.did(), fn_body_hir_id);
2871
2872 if let Some((idx, field)) = self.find_adt_field(*base_def, ident) {
2873 self.write_field_index(expr.hir_id, idx);
2874
2875 let adjustments = self.adjust_steps(&autoderef);
2876 if field.vis.is_accessible_from(def_scope, self.tcx) {
2877 self.apply_adjustments(base, adjustments);
2878 self.register_predicates(autoderef.into_obligations());
2879
2880 self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None);
2881 return self.field_ty(expr.span, field, args);
2882 }
2883
2884 private_candidate = Some((adjustments, base_def.did()));
2886 }
2887 }
2888 ty::Tuple(tys) => {
2889 if let Ok(index) = field.as_str().parse::<usize>() {
2890 if field.name == sym::integer(index) {
2891 if let Some(&field_ty) = tys.get(index) {
2892 let adjustments = self.adjust_steps(&autoderef);
2893 self.apply_adjustments(base, adjustments);
2894 self.register_predicates(autoderef.into_obligations());
2895
2896 self.write_field_index(expr.hir_id, FieldIdx::from_usize(index));
2897 return field_ty;
2898 }
2899 }
2900 }
2901 }
2902 _ => {}
2903 }
2904 }
2905 self.structurally_resolve_type(autoderef.span(), autoderef.final_ty(false));
2909
2910 if let Some((adjustments, did)) = private_candidate {
2911 self.apply_adjustments(base, adjustments);
2914 let guar = self.ban_private_field_access(
2915 expr,
2916 base_ty,
2917 field,
2918 did,
2919 expected.only_has_type(self),
2920 );
2921 return Ty::new_error(self.tcx(), guar);
2922 }
2923
2924 let guar = if field.name == kw::Empty {
2925 self.dcx().span_delayed_bug(field.span, "field name with no name")
2926 } else if self.method_exists_for_diagnostic(
2927 field,
2928 base_ty,
2929 expr.hir_id,
2930 expected.only_has_type(self),
2931 ) {
2932 self.ban_take_value_of_method(expr, base_ty, field)
2934 } else if !base_ty.is_primitive_ty() {
2935 self.ban_nonexisting_field(field, base, expr, base_ty)
2936 } else {
2937 let field_name = field.to_string();
2938 let mut err = type_error_struct!(
2939 self.dcx(),
2940 field.span,
2941 base_ty,
2942 E0610,
2943 "`{base_ty}` is a primitive type and therefore doesn't have fields",
2944 );
2945 let is_valid_suffix = |field: &str| {
2946 if field == "f32" || field == "f64" {
2947 return true;
2948 }
2949 let mut chars = field.chars().peekable();
2950 match chars.peek() {
2951 Some('e') | Some('E') => {
2952 chars.next();
2953 if let Some(c) = chars.peek()
2954 && !c.is_numeric()
2955 && *c != '-'
2956 && *c != '+'
2957 {
2958 return false;
2959 }
2960 while let Some(c) = chars.peek() {
2961 if !c.is_numeric() {
2962 break;
2963 }
2964 chars.next();
2965 }
2966 }
2967 _ => (),
2968 }
2969 let suffix = chars.collect::<String>();
2970 suffix.is_empty() || suffix == "f32" || suffix == "f64"
2971 };
2972 let maybe_partial_suffix = |field: &str| -> Option<&str> {
2973 let first_chars = ['f', 'l'];
2974 if field.len() >= 1
2975 && field.to_lowercase().starts_with(first_chars)
2976 && field[1..].chars().all(|c| c.is_ascii_digit())
2977 {
2978 if field.to_lowercase().starts_with(['f']) { Some("f32") } else { Some("f64") }
2979 } else {
2980 None
2981 }
2982 };
2983 if let ty::Infer(ty::IntVar(_)) = base_ty.kind()
2984 && let ExprKind::Lit(Spanned {
2985 node: ast::LitKind::Int(_, ast::LitIntType::Unsuffixed),
2986 ..
2987 }) = base.kind
2988 && !base.span.from_expansion()
2989 {
2990 if is_valid_suffix(&field_name) {
2991 err.span_suggestion_verbose(
2992 field.span.shrink_to_lo(),
2993 "if intended to be a floating point literal, consider adding a `0` after the period",
2994 '0',
2995 Applicability::MaybeIncorrect,
2996 );
2997 } else if let Some(correct_suffix) = maybe_partial_suffix(&field_name) {
2998 err.span_suggestion_verbose(
2999 field.span,
3000 format!("if intended to be a floating point literal, consider adding a `0` after the period and a `{correct_suffix}` suffix"),
3001 format!("0{correct_suffix}"),
3002 Applicability::MaybeIncorrect,
3003 );
3004 }
3005 }
3006 err.emit()
3007 };
3008
3009 Ty::new_error(self.tcx(), guar)
3010 }
3011
3012 fn suggest_await_on_field_access(
3013 &self,
3014 err: &mut Diag<'_>,
3015 field_ident: Ident,
3016 base: &'tcx hir::Expr<'tcx>,
3017 ty: Ty<'tcx>,
3018 ) {
3019 let Some(output_ty) = self.err_ctxt().get_impl_future_output_ty(ty) else {
3020 err.span_label(field_ident.span, "unknown field");
3021 return;
3022 };
3023 let ty::Adt(def, _) = output_ty.kind() else {
3024 err.span_label(field_ident.span, "unknown field");
3025 return;
3026 };
3027 if def.is_enum() {
3029 err.span_label(field_ident.span, "unknown field");
3030 return;
3031 }
3032 if !def.non_enum_variant().fields.iter().any(|field| field.ident(self.tcx) == field_ident) {
3033 err.span_label(field_ident.span, "unknown field");
3034 return;
3035 }
3036 err.span_label(
3037 field_ident.span,
3038 "field not available in `impl Future`, but it is available in its `Output`",
3039 );
3040 match self.tcx.coroutine_kind(self.body_id) {
3041 Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) => {
3042 err.span_suggestion_verbose(
3043 base.span.shrink_to_hi(),
3044 "consider `await`ing on the `Future` to access the field",
3045 ".await",
3046 Applicability::MaybeIncorrect,
3047 );
3048 }
3049 _ => {
3050 let mut span: MultiSpan = base.span.into();
3051 span.push_span_label(self.tcx.def_span(self.body_id), "this is not `async`");
3052 err.span_note(
3053 span,
3054 "this implements `Future` and its output type has the field, \
3055 but the future cannot be awaited in a synchronous function",
3056 );
3057 }
3058 }
3059 }
3060
3061 fn ban_nonexisting_field(
3062 &self,
3063 ident: Ident,
3064 base: &'tcx hir::Expr<'tcx>,
3065 expr: &'tcx hir::Expr<'tcx>,
3066 base_ty: Ty<'tcx>,
3067 ) -> ErrorGuaranteed {
3068 debug!(
3069 "ban_nonexisting_field: field={:?}, base={:?}, expr={:?}, base_ty={:?}",
3070 ident, base, expr, base_ty
3071 );
3072 let mut err = self.no_such_field_err(ident, base_ty, base.hir_id);
3073
3074 match *base_ty.peel_refs().kind() {
3075 ty::Array(_, len) => {
3076 self.maybe_suggest_array_indexing(&mut err, base, ident, len);
3077 }
3078 ty::RawPtr(..) => {
3079 self.suggest_first_deref_field(&mut err, base, ident);
3080 }
3081 ty::Param(param_ty) => {
3082 err.span_label(ident.span, "unknown field");
3083 self.point_at_param_definition(&mut err, param_ty);
3084 }
3085 ty::Alias(ty::Opaque, _) => {
3086 self.suggest_await_on_field_access(&mut err, ident, base, base_ty.peel_refs());
3087 }
3088 _ => {
3089 err.span_label(ident.span, "unknown field");
3090 }
3091 }
3092
3093 self.suggest_fn_call(&mut err, base, base_ty, |output_ty| {
3094 if let ty::Adt(def, _) = output_ty.kind()
3095 && !def.is_enum()
3096 {
3097 def.non_enum_variant().fields.iter().any(|field| {
3098 field.ident(self.tcx) == ident
3099 && field.vis.is_accessible_from(expr.hir_id.owner.def_id, self.tcx)
3100 })
3101 } else if let ty::Tuple(tys) = output_ty.kind()
3102 && let Ok(idx) = ident.as_str().parse::<usize>()
3103 {
3104 idx < tys.len()
3105 } else {
3106 false
3107 }
3108 });
3109
3110 if ident.name == kw::Await {
3111 err.note("to `.await` a `Future`, switch to Rust 2018 or later");
3114 HelpUseLatestEdition::new().add_to_diag(&mut err);
3115 }
3116
3117 err.emit()
3118 }
3119
3120 fn ban_private_field_access(
3121 &self,
3122 expr: &hir::Expr<'tcx>,
3123 expr_t: Ty<'tcx>,
3124 field: Ident,
3125 base_did: DefId,
3126 return_ty: Option<Ty<'tcx>>,
3127 ) -> ErrorGuaranteed {
3128 let mut err = self.private_field_err(field, base_did);
3129
3130 if self.method_exists_for_diagnostic(field, expr_t, expr.hir_id, return_ty)
3132 && !self.expr_in_place(expr.hir_id)
3133 {
3134 self.suggest_method_call(
3135 &mut err,
3136 format!("a method `{field}` also exists, call it with parentheses"),
3137 field,
3138 expr_t,
3139 expr,
3140 None,
3141 );
3142 }
3143 err.emit()
3144 }
3145
3146 fn ban_take_value_of_method(
3147 &self,
3148 expr: &hir::Expr<'tcx>,
3149 expr_t: Ty<'tcx>,
3150 field: Ident,
3151 ) -> ErrorGuaranteed {
3152 let mut err = type_error_struct!(
3153 self.dcx(),
3154 field.span,
3155 expr_t,
3156 E0615,
3157 "attempted to take value of method `{field}` on type `{expr_t}`",
3158 );
3159 err.span_label(field.span, "method, not a field");
3160 let expr_is_call =
3161 if let hir::Node::Expr(hir::Expr { kind: ExprKind::Call(callee, _args), .. }) =
3162 self.tcx.parent_hir_node(expr.hir_id)
3163 {
3164 expr.hir_id == callee.hir_id
3165 } else {
3166 false
3167 };
3168 let expr_snippet =
3169 self.tcx.sess.source_map().span_to_snippet(expr.span).unwrap_or_default();
3170 let is_wrapped = expr_snippet.starts_with('(') && expr_snippet.ends_with(')');
3171 let after_open = expr.span.lo() + rustc_span::BytePos(1);
3172 let before_close = expr.span.hi() - rustc_span::BytePos(1);
3173
3174 if expr_is_call && is_wrapped {
3175 err.multipart_suggestion(
3176 "remove wrapping parentheses to call the method",
3177 vec![
3178 (expr.span.with_hi(after_open), String::new()),
3179 (expr.span.with_lo(before_close), String::new()),
3180 ],
3181 Applicability::MachineApplicable,
3182 );
3183 } else if !self.expr_in_place(expr.hir_id) {
3184 let span = if is_wrapped {
3186 expr.span.with_lo(after_open).with_hi(before_close)
3187 } else {
3188 expr.span
3189 };
3190 self.suggest_method_call(
3191 &mut err,
3192 "use parentheses to call the method",
3193 field,
3194 expr_t,
3195 expr,
3196 Some(span),
3197 );
3198 } else if let ty::RawPtr(ptr_ty, _) = expr_t.kind()
3199 && let ty::Adt(adt_def, _) = ptr_ty.kind()
3200 && let ExprKind::Field(base_expr, _) = expr.kind
3201 && let [variant] = &adt_def.variants().raw
3202 && variant.fields.iter().any(|f| f.ident(self.tcx) == field)
3203 {
3204 err.multipart_suggestion(
3205 "to access the field, dereference first",
3206 vec![
3207 (base_expr.span.shrink_to_lo(), "(*".to_string()),
3208 (base_expr.span.shrink_to_hi(), ")".to_string()),
3209 ],
3210 Applicability::MaybeIncorrect,
3211 );
3212 } else {
3213 err.help("methods are immutable and cannot be assigned to");
3214 }
3215
3216 self.dcx().try_steal_replace_and_emit_err(field.span, StashKey::GenericInFieldExpr, err)
3218 }
3219
3220 fn point_at_param_definition(&self, err: &mut Diag<'_>, param: ty::ParamTy) {
3221 let generics = self.tcx.generics_of(self.body_id);
3222 let generic_param = generics.type_param(param, self.tcx);
3223 if let ty::GenericParamDefKind::Type { synthetic: true, .. } = generic_param.kind {
3224 return;
3225 }
3226 let param_def_id = generic_param.def_id;
3227 let param_hir_id = match param_def_id.as_local() {
3228 Some(x) => self.tcx.local_def_id_to_hir_id(x),
3229 None => return,
3230 };
3231 let param_span = self.tcx.hir().span(param_hir_id);
3232 let param_name = self.tcx.hir().ty_param_name(param_def_id.expect_local());
3233
3234 err.span_label(param_span, format!("type parameter '{param_name}' declared here"));
3235 }
3236
3237 fn maybe_suggest_array_indexing(
3238 &self,
3239 err: &mut Diag<'_>,
3240 base: &hir::Expr<'_>,
3241 field: Ident,
3242 len: ty::Const<'tcx>,
3243 ) {
3244 err.span_label(field.span, "unknown field");
3245 if let (Some(len), Ok(user_index)) = (
3246 self.try_structurally_resolve_const(base.span, len).try_to_target_usize(self.tcx),
3247 field.as_str().parse::<u64>(),
3248 ) {
3249 let help = "instead of using tuple indexing, use array indexing";
3250 let applicability = if len < user_index {
3251 Applicability::MachineApplicable
3252 } else {
3253 Applicability::MaybeIncorrect
3254 };
3255 err.multipart_suggestion(
3256 help,
3257 vec![
3258 (base.span.between(field.span), "[".to_string()),
3259 (field.span.shrink_to_hi(), "]".to_string()),
3260 ],
3261 applicability,
3262 );
3263 }
3264 }
3265
3266 fn suggest_first_deref_field(&self, err: &mut Diag<'_>, base: &hir::Expr<'_>, field: Ident) {
3267 err.span_label(field.span, "unknown field");
3268 let val = if let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span)
3269 && base.len() < 20
3270 {
3271 format!("`{base}`")
3272 } else {
3273 "the value".to_string()
3274 };
3275 err.multipart_suggestion(
3276 format!("{val} is a raw pointer; try dereferencing it"),
3277 vec![
3278 (base.span.shrink_to_lo(), "(*".to_string()),
3279 (base.span.shrink_to_hi(), ")".to_string()),
3280 ],
3281 Applicability::MaybeIncorrect,
3282 );
3283 }
3284
3285 fn no_such_field_err(&self, field: Ident, expr_t: Ty<'tcx>, id: HirId) -> Diag<'_> {
3286 let span = field.span;
3287 debug!("no_such_field_err(span: {:?}, field: {:?}, expr_t: {:?})", span, field, expr_t);
3288
3289 let mut err = type_error_struct!(
3290 self.dcx(),
3291 span,
3292 expr_t,
3293 E0609,
3294 "no field `{field}` on type `{expr_t}`",
3295 );
3296
3297 let mod_id = self.tcx.parent_module(id).to_def_id();
3299 let (ty, unwrap) = if let ty::Adt(def, args) = expr_t.kind()
3300 && (self.tcx.is_diagnostic_item(sym::Result, def.did())
3301 || self.tcx.is_diagnostic_item(sym::Option, def.did()))
3302 && let Some(arg) = args.get(0)
3303 && let Some(ty) = arg.as_type()
3304 {
3305 (ty, "unwrap().")
3306 } else {
3307 (expr_t, "")
3308 };
3309 for (found_fields, args) in
3310 self.get_field_candidates_considering_privacy_for_diag(span, ty, mod_id, id)
3311 {
3312 let field_names = found_fields.iter().map(|field| field.name).collect::<Vec<_>>();
3313 let mut candidate_fields: Vec<_> = found_fields
3314 .into_iter()
3315 .filter_map(|candidate_field| {
3316 self.check_for_nested_field_satisfying_condition_for_diag(
3317 span,
3318 &|candidate_field, _| candidate_field.ident(self.tcx()) == field,
3319 candidate_field,
3320 args,
3321 vec![],
3322 mod_id,
3323 id,
3324 )
3325 })
3326 .map(|mut field_path| {
3327 field_path.pop();
3328 field_path.iter().map(|id| format!("{}.", id)).collect::<String>()
3329 })
3330 .collect::<Vec<_>>();
3331 candidate_fields.sort();
3332
3333 let len = candidate_fields.len();
3334 if len > 0 {
3335 err.span_suggestions(
3336 field.span.shrink_to_lo(),
3337 format!(
3338 "{} of the expressions' fields {} a field of the same name",
3339 if len > 1 { "some" } else { "one" },
3340 if len > 1 { "have" } else { "has" },
3341 ),
3342 candidate_fields.iter().map(|path| format!("{unwrap}{path}")),
3343 Applicability::MaybeIncorrect,
3344 );
3345 } else if let Some(field_name) =
3346 find_best_match_for_name(&field_names, field.name, None)
3347 {
3348 err.span_suggestion_verbose(
3349 field.span,
3350 "a field with a similar name exists",
3351 format!("{unwrap}{}", field_name),
3352 Applicability::MaybeIncorrect,
3353 );
3354 } else if !field_names.is_empty() {
3355 let is = if field_names.len() == 1 { " is" } else { "s are" };
3356 err.note(
3357 format!("available field{is}: {}", self.name_series_display(field_names),),
3358 );
3359 }
3360 }
3361 err
3362 }
3363
3364 fn private_field_err(&self, field: Ident, base_did: DefId) -> Diag<'_> {
3365 let struct_path = self.tcx().def_path_str(base_did);
3366 let kind_name = self.tcx().def_descr(base_did);
3367 struct_span_code_err!(
3368 self.dcx(),
3369 field.span,
3370 E0616,
3371 "field `{field}` of {kind_name} `{struct_path}` is private",
3372 )
3373 .with_span_label(field.span, "private field")
3374 }
3375
3376 pub(crate) fn get_field_candidates_considering_privacy_for_diag(
3377 &self,
3378 span: Span,
3379 base_ty: Ty<'tcx>,
3380 mod_id: DefId,
3381 hir_id: HirId,
3382 ) -> Vec<(Vec<&'tcx ty::FieldDef>, GenericArgsRef<'tcx>)> {
3383 debug!("get_field_candidates(span: {:?}, base_t: {:?}", span, base_ty);
3384
3385 let mut autoderef = self.autoderef(span, base_ty).silence_errors();
3386 let deref_chain: Vec<_> = autoderef.by_ref().collect();
3387
3388 if autoderef.reached_recursion_limit() {
3392 return vec![];
3393 }
3394
3395 deref_chain
3396 .into_iter()
3397 .filter_map(move |(base_t, _)| {
3398 match base_t.kind() {
3399 ty::Adt(base_def, args) if !base_def.is_enum() => {
3400 let tcx = self.tcx;
3401 let fields = &base_def.non_enum_variant().fields;
3402 if fields.iter().all(|field| !field.vis.is_accessible_from(mod_id, tcx)) {
3406 return None;
3407 }
3408 return Some((
3409 fields
3410 .iter()
3411 .filter(move |field| {
3412 field.vis.is_accessible_from(mod_id, tcx)
3413 && self.is_field_suggestable(field, hir_id, span)
3414 })
3415 .take(100)
3417 .collect::<Vec<_>>(),
3418 *args,
3419 ));
3420 }
3421 _ => None,
3422 }
3423 })
3424 .collect()
3425 }
3426
3427 pub(crate) fn check_for_nested_field_satisfying_condition_for_diag(
3430 &self,
3431 span: Span,
3432 matches: &impl Fn(&ty::FieldDef, Ty<'tcx>) -> bool,
3433 candidate_field: &ty::FieldDef,
3434 subst: GenericArgsRef<'tcx>,
3435 mut field_path: Vec<Ident>,
3436 mod_id: DefId,
3437 hir_id: HirId,
3438 ) -> Option<Vec<Ident>> {
3439 debug!(
3440 "check_for_nested_field_satisfying(span: {:?}, candidate_field: {:?}, field_path: {:?}",
3441 span, candidate_field, field_path
3442 );
3443
3444 if field_path.len() > 3 {
3445 None
3448 } else {
3449 field_path.push(candidate_field.ident(self.tcx).normalize_to_macros_2_0());
3450 let field_ty = candidate_field.ty(self.tcx, subst);
3451 if matches(candidate_field, field_ty) {
3452 return Some(field_path);
3453 } else {
3454 for (nested_fields, subst) in self
3455 .get_field_candidates_considering_privacy_for_diag(
3456 span, field_ty, mod_id, hir_id,
3457 )
3458 {
3459 for field in nested_fields {
3461 if let Some(field_path) = self
3462 .check_for_nested_field_satisfying_condition_for_diag(
3463 span,
3464 matches,
3465 field,
3466 subst,
3467 field_path.clone(),
3468 mod_id,
3469 hir_id,
3470 )
3471 {
3472 return Some(field_path);
3473 }
3474 }
3475 }
3476 }
3477 None
3478 }
3479 }
3480
3481 fn check_expr_index(
3482 &self,
3483 base: &'tcx hir::Expr<'tcx>,
3484 idx: &'tcx hir::Expr<'tcx>,
3485 expr: &'tcx hir::Expr<'tcx>,
3486 brackets_span: Span,
3487 ) -> Ty<'tcx> {
3488 let base_t = self.check_expr(base);
3489 let idx_t = self.check_expr(idx);
3490
3491 if base_t.references_error() {
3492 base_t
3493 } else if idx_t.references_error() {
3494 idx_t
3495 } else {
3496 let base_t = self.structurally_resolve_type(base.span, base_t);
3497 match self.lookup_indexing(expr, base, base_t, idx, idx_t) {
3498 Some((index_ty, element_ty)) => {
3499 self.demand_coerce(idx, idx_t, index_ty, None, AllowTwoPhase::No);
3501 self.select_obligations_where_possible(|errors| {
3502 self.point_at_index(errors, idx.span);
3503 });
3504 element_ty
3505 }
3506 None => {
3507 for (base_t, _) in self.autoderef(base.span, base_t).silence_errors() {
3510 if let Some((_, index_ty, element_ty)) =
3511 self.find_and_report_unsatisfied_index_impl(base, base_t)
3512 {
3513 self.demand_coerce(idx, idx_t, index_ty, None, AllowTwoPhase::No);
3514 return element_ty;
3515 }
3516 }
3517
3518 let mut err = type_error_struct!(
3519 self.dcx(),
3520 brackets_span,
3521 base_t,
3522 E0608,
3523 "cannot index into a value of type `{base_t}`",
3524 );
3525 if let ty::Tuple(types) = base_t.kind() {
3527 let mut needs_note = true;
3528 if let ExprKind::Lit(lit) = idx.kind
3531 && let ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) = lit.node
3532 && i.get()
3533 < types
3534 .len()
3535 .try_into()
3536 .expect("expected tuple index to be < usize length")
3537 {
3538 err.span_suggestion(
3539 brackets_span,
3540 "to access tuple elements, use",
3541 format!(".{i}"),
3542 Applicability::MachineApplicable,
3543 );
3544 needs_note = false;
3545 } else if let ExprKind::Path(..) = idx.peel_borrows().kind {
3546 err.span_label(
3547 idx.span,
3548 "cannot access tuple elements at a variable index",
3549 );
3550 }
3551 if needs_note {
3552 err.help(
3553 "to access tuple elements, use tuple indexing \
3554 syntax (e.g., `tuple.0`)",
3555 );
3556 }
3557 }
3558
3559 if base_t.is_raw_ptr() && idx_t.is_integral() {
3560 err.multipart_suggestion(
3561 "consider using `wrapping_add` or `add` for indexing into raw pointer",
3562 vec![
3563 (base.span.between(idx.span), ".wrapping_add(".to_owned()),
3564 (
3565 idx.span.shrink_to_hi().until(expr.span.shrink_to_hi()),
3566 ")".to_owned(),
3567 ),
3568 ],
3569 Applicability::MaybeIncorrect,
3570 );
3571 }
3572
3573 let reported = err.emit();
3574 Ty::new_error(self.tcx, reported)
3575 }
3576 }
3577 }
3578 }
3579
3580 fn find_and_report_unsatisfied_index_impl(
3588 &self,
3589 base_expr: &hir::Expr<'_>,
3590 base_ty: Ty<'tcx>,
3591 ) -> Option<(ErrorGuaranteed, Ty<'tcx>, Ty<'tcx>)> {
3592 let index_trait_def_id = self.tcx.lang_items().index_trait()?;
3593 let index_trait_output_def_id = self.tcx.get_diagnostic_item(sym::IndexOutput)?;
3594
3595 let mut relevant_impls = vec![];
3596 self.tcx.for_each_relevant_impl(index_trait_def_id, base_ty, |impl_def_id| {
3597 relevant_impls.push(impl_def_id);
3598 });
3599 let [impl_def_id] = relevant_impls[..] else {
3600 return None;
3602 };
3603
3604 self.commit_if_ok(|snapshot| {
3605 let outer_universe = self.universe();
3606
3607 let ocx = ObligationCtxt::new_with_diagnostics(self);
3608 let impl_args = self.fresh_args_for_item(base_expr.span, impl_def_id);
3609 let impl_trait_ref =
3610 self.tcx.impl_trait_ref(impl_def_id).unwrap().instantiate(self.tcx, impl_args);
3611 let cause = self.misc(base_expr.span);
3612
3613 let impl_trait_ref = ocx.normalize(&cause, self.param_env, impl_trait_ref);
3616 ocx.eq(&cause, self.param_env, base_ty, impl_trait_ref.self_ty())?;
3617
3618 ocx.register_obligations(traits::predicates_for_generics(
3622 |idx, span| {
3623 cause.clone().derived_cause(
3624 ty::Binder::dummy(ty::TraitPredicate {
3625 trait_ref: impl_trait_ref,
3626 polarity: ty::PredicatePolarity::Positive,
3627 }),
3628 |derived| {
3629 ObligationCauseCode::ImplDerived(Box::new(traits::ImplDerivedCause {
3630 derived,
3631 impl_or_alias_def_id: impl_def_id,
3632 impl_def_predicate_index: Some(idx),
3633 span,
3634 }))
3635 },
3636 )
3637 },
3638 self.param_env,
3639 self.tcx.predicates_of(impl_def_id).instantiate(self.tcx, impl_args),
3640 ));
3641
3642 let element_ty = ocx.normalize(
3645 &cause,
3646 self.param_env,
3647 Ty::new_projection_from_args(
3648 self.tcx,
3649 index_trait_output_def_id,
3650 impl_trait_ref.args,
3651 ),
3652 );
3653
3654 let true_errors = ocx.select_where_possible();
3655
3656 self.leak_check(outer_universe, Some(snapshot))?;
3660
3661 let ambiguity_errors = ocx.select_all_or_error();
3663 if true_errors.is_empty() && !ambiguity_errors.is_empty() {
3664 return Err(NoSolution);
3665 }
3666
3667 Ok::<_, NoSolution>((
3670 self.err_ctxt().report_fulfillment_errors(true_errors),
3671 impl_trait_ref.args.type_at(1),
3672 element_ty,
3673 ))
3674 })
3675 .ok()
3676 }
3677
3678 fn point_at_index(&self, errors: &mut Vec<traits::FulfillmentError<'tcx>>, span: Span) {
3679 let mut seen_preds = FxHashSet::default();
3680 errors.sort_by_key(|error| error.root_obligation.recursion_depth);
3684 for error in errors {
3685 match (
3686 error.root_obligation.predicate.kind().skip_binder(),
3687 error.obligation.predicate.kind().skip_binder(),
3688 ) {
3689 (ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)), _)
3690 if self.tcx.is_lang_item(predicate.trait_ref.def_id, LangItem::Index) =>
3691 {
3692 seen_preds.insert(error.obligation.predicate.kind().skip_binder());
3693 }
3694 (_, ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)))
3695 if self.tcx.is_diagnostic_item(sym::SliceIndex, predicate.trait_ref.def_id) =>
3696 {
3697 seen_preds.insert(error.obligation.predicate.kind().skip_binder());
3698 }
3699 (root, pred) if seen_preds.contains(&pred) || seen_preds.contains(&root) => {}
3700 _ => continue,
3701 }
3702 error.obligation.cause.span = span;
3703 }
3704 }
3705
3706 fn check_expr_yield(
3707 &self,
3708 value: &'tcx hir::Expr<'tcx>,
3709 expr: &'tcx hir::Expr<'tcx>,
3710 ) -> Ty<'tcx> {
3711 match self.coroutine_types {
3712 Some(CoroutineTypes { resume_ty, yield_ty }) => {
3713 self.check_expr_coercible_to_type(value, yield_ty, None);
3714
3715 resume_ty
3716 }
3717 _ => {
3718 self.dcx().emit_err(YieldExprOutsideOfCoroutine { span: expr.span });
3719 self.check_expr(value);
3721 self.tcx.types.unit
3722 }
3723 }
3724 }
3725
3726 fn check_expr_asm_operand(&self, expr: &'tcx hir::Expr<'tcx>, is_input: bool) {
3727 let needs = if is_input { Needs::None } else { Needs::MutPlace };
3728 let ty = self.check_expr_with_needs(expr, needs);
3729 self.require_type_is_sized(ty, expr.span, ObligationCauseCode::InlineAsmSized);
3730
3731 if !is_input && !expr.is_syntactic_place_expr() {
3732 self.dcx()
3733 .struct_span_err(expr.span, "invalid asm output")
3734 .with_span_label(expr.span, "cannot assign to this expression")
3735 .emit();
3736 }
3737
3738 if is_input {
3746 let ty = self.structurally_resolve_type(expr.span, ty);
3747 match *ty.kind() {
3748 ty::FnDef(..) => {
3749 let fnptr_ty = Ty::new_fn_ptr(self.tcx, ty.fn_sig(self.tcx));
3750 self.demand_coerce(expr, ty, fnptr_ty, None, AllowTwoPhase::No);
3751 }
3752 ty::Ref(_, base_ty, mutbl) => {
3753 let ptr_ty = Ty::new_ptr(self.tcx, base_ty, mutbl);
3754 self.demand_coerce(expr, ty, ptr_ty, None, AllowTwoPhase::No);
3755 }
3756 _ => {}
3757 }
3758 }
3759 }
3760
3761 fn check_expr_asm(&self, asm: &'tcx hir::InlineAsm<'tcx>) -> Ty<'tcx> {
3762 let mut diverge = asm.asm_macro.diverges(asm.options);
3763
3764 for (op, _op_sp) in asm.operands {
3765 match op {
3766 hir::InlineAsmOperand::In { expr, .. } => {
3767 self.check_expr_asm_operand(expr, true);
3768 }
3769 hir::InlineAsmOperand::Out { expr: Some(expr), .. }
3770 | hir::InlineAsmOperand::InOut { expr, .. } => {
3771 self.check_expr_asm_operand(expr, false);
3772 }
3773 hir::InlineAsmOperand::Out { expr: None, .. } => {}
3774 hir::InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
3775 self.check_expr_asm_operand(in_expr, true);
3776 if let Some(out_expr) = out_expr {
3777 self.check_expr_asm_operand(out_expr, false);
3778 }
3779 }
3780 hir::InlineAsmOperand::Const { .. } | hir::InlineAsmOperand::SymFn { .. } => {}
3784 hir::InlineAsmOperand::SymStatic { .. } => {}
3785 hir::InlineAsmOperand::Label { block } => {
3786 let previous_diverges = self.diverges.get();
3787
3788 let ty = self.check_expr_block(block, ExpectHasType(self.tcx.types.unit));
3790 if !ty.is_never() {
3791 self.demand_suptype(block.span, self.tcx.types.unit, ty);
3792 diverge = false;
3793 }
3794
3795 self.diverges.set(previous_diverges);
3797 }
3798 }
3799 }
3800
3801 if diverge { self.tcx.types.never } else { self.tcx.types.unit }
3802 }
3803
3804 fn check_expr_offset_of(
3805 &self,
3806 container: &'tcx hir::Ty<'tcx>,
3807 fields: &[Ident],
3808 expr: &'tcx hir::Expr<'tcx>,
3809 ) -> Ty<'tcx> {
3810 let container = self.lower_ty(container).normalized;
3811
3812 let mut field_indices = Vec::with_capacity(fields.len());
3813 let mut current_container = container;
3814 let mut fields = fields.into_iter();
3815
3816 while let Some(&field) = fields.next() {
3817 let container = self.structurally_resolve_type(expr.span, current_container);
3818
3819 match container.kind() {
3820 ty::Adt(container_def, args) if container_def.is_enum() => {
3821 let block = self.tcx.local_def_id_to_hir_id(self.body_id);
3822 let (ident, _def_scope) =
3823 self.tcx.adjust_ident_and_get_scope(field, container_def.did(), block);
3824
3825 if !self.tcx.features().offset_of_enum() {
3826 rustc_session::parse::feature_err(
3827 &self.tcx.sess,
3828 sym::offset_of_enum,
3829 ident.span,
3830 "using enums in offset_of is experimental",
3831 )
3832 .emit();
3833 }
3834
3835 let Some((index, variant)) = container_def
3836 .variants()
3837 .iter_enumerated()
3838 .find(|(_, v)| v.ident(self.tcx).normalize_to_macros_2_0() == ident)
3839 else {
3840 type_error_struct!(
3841 self.dcx(),
3842 ident.span,
3843 container,
3844 E0599,
3845 "no variant named `{ident}` found for enum `{container}`",
3846 )
3847 .with_span_label(field.span, "variant not found")
3848 .emit();
3849 break;
3850 };
3851 let Some(&subfield) = fields.next() else {
3852 type_error_struct!(
3853 self.dcx(),
3854 ident.span,
3855 container,
3856 E0795,
3857 "`{ident}` is an enum variant; expected field at end of `offset_of`",
3858 )
3859 .with_span_label(field.span, "enum variant")
3860 .emit();
3861 break;
3862 };
3863 let (subident, sub_def_scope) =
3864 self.tcx.adjust_ident_and_get_scope(subfield, variant.def_id, block);
3865
3866 let Some((subindex, field)) = variant
3867 .fields
3868 .iter_enumerated()
3869 .find(|(_, f)| f.ident(self.tcx).normalize_to_macros_2_0() == subident)
3870 else {
3871 type_error_struct!(
3872 self.dcx(),
3873 ident.span,
3874 container,
3875 E0609,
3876 "no field named `{subfield}` on enum variant `{container}::{ident}`",
3877 )
3878 .with_span_label(field.span, "this enum variant...")
3879 .with_span_label(subident.span, "...does not have this field")
3880 .emit();
3881 break;
3882 };
3883
3884 let field_ty = self.field_ty(expr.span, field, args);
3885
3886 self.require_type_is_sized(
3889 field_ty,
3890 expr.span,
3891 ObligationCauseCode::FieldSized {
3892 adt_kind: AdtKind::Enum,
3893 span: self.tcx.def_span(field.did),
3894 last: false,
3895 },
3896 );
3897
3898 if field.vis.is_accessible_from(sub_def_scope, self.tcx) {
3899 self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None);
3900 } else {
3901 self.private_field_err(ident, container_def.did()).emit();
3902 }
3903
3904 field_indices.push((index, subindex));
3907 current_container = field_ty;
3908
3909 continue;
3910 }
3911 ty::Adt(container_def, args) => {
3912 let block = self.tcx.local_def_id_to_hir_id(self.body_id);
3913 let (ident, def_scope) =
3914 self.tcx.adjust_ident_and_get_scope(field, container_def.did(), block);
3915
3916 let fields = &container_def.non_enum_variant().fields;
3917 if let Some((index, field)) = fields
3918 .iter_enumerated()
3919 .find(|(_, f)| f.ident(self.tcx).normalize_to_macros_2_0() == ident)
3920 {
3921 let field_ty = self.field_ty(expr.span, field, args);
3922
3923 if self.tcx.features().offset_of_slice() {
3924 self.require_type_has_static_alignment(field_ty, expr.span);
3925 } else {
3926 self.require_type_is_sized(
3927 field_ty,
3928 expr.span,
3929 ObligationCauseCode::Misc,
3930 );
3931 }
3932
3933 if field.vis.is_accessible_from(def_scope, self.tcx) {
3934 self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None);
3935 } else {
3936 self.private_field_err(ident, container_def.did()).emit();
3937 }
3938
3939 field_indices.push((FIRST_VARIANT, index));
3942 current_container = field_ty;
3943
3944 continue;
3945 }
3946 }
3947 ty::Tuple(tys) => {
3948 if let Ok(index) = field.as_str().parse::<usize>()
3949 && field.name == sym::integer(index)
3950 {
3951 if let Some(&field_ty) = tys.get(index) {
3952 if self.tcx.features().offset_of_slice() {
3953 self.require_type_has_static_alignment(field_ty, expr.span);
3954 } else {
3955 self.require_type_is_sized(
3956 field_ty,
3957 expr.span,
3958 ObligationCauseCode::Misc,
3959 );
3960 }
3961
3962 field_indices.push((FIRST_VARIANT, index.into()));
3963 current_container = field_ty;
3964
3965 continue;
3966 }
3967 }
3968 }
3969 _ => (),
3970 };
3971
3972 self.no_such_field_err(field, container, expr.hir_id).emit();
3973
3974 break;
3975 }
3976
3977 self.typeck_results
3978 .borrow_mut()
3979 .offset_of_data_mut()
3980 .insert(expr.hir_id, (container, field_indices));
3981
3982 self.tcx.types.usize
3983 }
3984}