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::lang_items::LangItem;
20use rustc_hir::{ExprKind, HirId, QPath};
21use rustc_hir_analysis::NoVariantNamed;
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::coercion::{CoerceMany, DynamicCoerceMany};
43use crate::errors::{
44 AddressOfTemporaryTaken, BaseExpressionDoubleDot, BaseExpressionDoubleDotAddExpr,
45 BaseExpressionDoubleDotEnableDefaultFieldValues, BaseExpressionDoubleDotRemove,
46 CantDereference, FieldMultiplySpecifiedInInitializer, FunctionalRecordUpdateOnNonStruct,
47 HelpUseLatestEdition, NoFieldOnType, NoFieldOnVariant, ReturnLikeStatementKind,
48 ReturnStmtOutsideOfFnBody, StructExprNonExhaustive, TypeMismatchFruTypo,
49 YieldExprOutsideOfCoroutine,
50};
51use crate::{
52 BreakableCtxt, CoroutineTypes, Diverges, FnCtxt, GatherLocalsVisitor, Needs,
53 TupleArgumentsFlag, cast, fatally_break_rust, report_unexpected_variant_res, type_error_struct,
54};
55
56impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
57 pub(crate) fn 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_id_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::Use(_, _)
364 | ExprKind::MethodCall(_, _, _, _)
365 | ExprKind::Tup(_)
366 | ExprKind::Binary(_, _, _)
367 | ExprKind::Unary(_, _)
368 | ExprKind::Cast(_, _)
369 | ExprKind::DropTemps(_)
370 | ExprKind::If(_, _, _)
371 | ExprKind::Closure(_)
372 | ExprKind::Block(_, _)
373 | ExprKind::AssignOp(_, _, _)
374 | ExprKind::Index(_, _, _)
375 | ExprKind::Break(_, _)
376 | ExprKind::Ret(_)
377 | ExprKind::Become(_)
378 | ExprKind::InlineAsm(_)
379 | ExprKind::Struct(_, _, _)
380 | ExprKind::Repeat(_, _)
381 | ExprKind::Yield(_, _) => true,
382
383 ExprKind::ConstBlock(_)
385 | ExprKind::Loop(_, _, _, _)
386 | ExprKind::Lit(_)
387 | ExprKind::Path(_)
388 | ExprKind::Continue(_)
389 | ExprKind::OffsetOf(_, _)
390 | ExprKind::Err(_) => unreachable!("no sub-expr expected for {:?}", expr.kind),
391 }
392 }
393
394 hir::Node::LetStmt(hir::LetStmt { init: Some(target), pat, .. }) => {
397 assert_eq!(target.hir_id, expr.hir_id);
398 self.pat_guaranteed_to_constitute_read_for_never(*pat)
399 }
400
401 hir::Node::Block(_)
403 | hir::Node::Arm(_)
404 | hir::Node::ExprField(_)
405 | hir::Node::AnonConst(_)
406 | hir::Node::ConstBlock(_)
407 | hir::Node::ConstArg(_)
408 | hir::Node::Stmt(_)
409 | hir::Node::Item(hir::Item {
410 kind: hir::ItemKind::Const(..) | hir::ItemKind::Static(..),
411 ..
412 })
413 | hir::Node::TraitItem(hir::TraitItem {
414 kind: hir::TraitItemKind::Const(..), ..
415 })
416 | hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. }) => true,
417
418 hir::Node::TyPat(_) | hir::Node::Pat(_) => {
419 self.dcx().span_delayed_bug(expr.span, "place expr not allowed in pattern");
420 true
421 }
422
423 hir::Node::Param(_)
425 | hir::Node::Item(_)
426 | hir::Node::ForeignItem(_)
427 | hir::Node::TraitItem(_)
428 | hir::Node::ImplItem(_)
429 | hir::Node::Variant(_)
430 | hir::Node::Field(_)
431 | hir::Node::PathSegment(_)
432 | hir::Node::Ty(_)
433 | hir::Node::AssocItemConstraint(_)
434 | hir::Node::TraitRef(_)
435 | hir::Node::PatField(_)
436 | hir::Node::PatExpr(_)
437 | hir::Node::LetStmt(_)
438 | hir::Node::Synthetic
439 | hir::Node::Err(_)
440 | hir::Node::Ctor(_)
441 | hir::Node::Lifetime(_)
442 | hir::Node::GenericParam(_)
443 | hir::Node::Crate(_)
444 | hir::Node::Infer(_)
445 | hir::Node::WherePredicate(_)
446 | hir::Node::PreciseCapturingNonLifetimeArg(_)
447 | hir::Node::OpaqueTy(_) => {
448 unreachable!("no sub-expr expected for {parent_node:?}")
449 }
450 }
451 }
452
453 pub(super) fn pat_guaranteed_to_constitute_read_for_never(&self, pat: &hir::Pat<'_>) -> bool {
459 match pat.kind {
460 hir::PatKind::Wild => false,
462
463 hir::PatKind::Guard(_, _) => true,
465
466 hir::PatKind::Or(subpats) => {
475 subpats.iter().all(|pat| self.pat_guaranteed_to_constitute_read_for_never(pat))
476 }
477
478 hir::PatKind::Never => true,
480
481 hir::PatKind::Missing
484 | hir::PatKind::Binding(_, _, _, _)
485 | hir::PatKind::Struct(_, _, _)
486 | hir::PatKind::TupleStruct(_, _, _)
487 | hir::PatKind::Tuple(_, _)
488 | hir::PatKind::Box(_)
489 | hir::PatKind::Ref(_, _)
490 | hir::PatKind::Deref(_)
491 | hir::PatKind::Expr(_)
492 | hir::PatKind::Range(_, _, _)
493 | hir::PatKind::Slice(_, _, _)
494 | hir::PatKind::Err(_) => true,
495 }
496 }
497
498 #[instrument(skip(self, expr), level = "debug")]
499 fn check_expr_kind(
500 &self,
501 expr: &'tcx hir::Expr<'tcx>,
502 expected: Expectation<'tcx>,
503 ) -> Ty<'tcx> {
504 trace!("expr={:#?}", expr);
505
506 let tcx = self.tcx;
507 match expr.kind {
508 ExprKind::Lit(ref lit) => self.check_expr_lit(lit, expected),
509 ExprKind::Binary(op, lhs, rhs) => self.check_expr_binop(expr, op, lhs, rhs, expected),
510 ExprKind::Assign(lhs, rhs, span) => {
511 self.check_expr_assign(expr, expected, lhs, rhs, span)
512 }
513 ExprKind::AssignOp(op, lhs, rhs) => {
514 self.check_expr_assign_op(expr, op, lhs, rhs, expected)
515 }
516 ExprKind::Unary(unop, oprnd) => self.check_expr_unop(unop, oprnd, expected, expr),
517 ExprKind::AddrOf(kind, mutbl, oprnd) => {
518 self.check_expr_addr_of(kind, mutbl, oprnd, expected, expr)
519 }
520 ExprKind::Path(QPath::LangItem(lang_item, _)) => {
521 self.check_lang_item_path(lang_item, expr)
522 }
523 ExprKind::Path(ref qpath) => self.check_expr_path(qpath, expr, None),
524 ExprKind::InlineAsm(asm) => {
525 self.deferred_asm_checks.borrow_mut().push((asm, expr.hir_id));
527 self.check_expr_asm(asm)
528 }
529 ExprKind::OffsetOf(container, fields) => {
530 self.check_expr_offset_of(container, fields, expr)
531 }
532 ExprKind::Break(destination, ref expr_opt) => {
533 self.check_expr_break(destination, expr_opt.as_deref(), expr)
534 }
535 ExprKind::Continue(destination) => self.check_expr_continue(destination, expr),
536 ExprKind::Ret(ref expr_opt) => self.check_expr_return(expr_opt.as_deref(), expr),
537 ExprKind::Become(call) => self.check_expr_become(call, expr),
538 ExprKind::Let(let_expr) => self.check_expr_let(let_expr, expr.hir_id),
539 ExprKind::Loop(body, _, source, _) => {
540 self.check_expr_loop(body, source, expected, expr)
541 }
542 ExprKind::Match(discrim, arms, match_src) => {
543 self.check_expr_match(expr, discrim, arms, expected, match_src)
544 }
545 ExprKind::Closure(closure) => self.check_expr_closure(closure, expr.span, expected),
546 ExprKind::Block(body, _) => self.check_expr_block(body, expected),
547 ExprKind::Call(callee, args) => self.check_expr_call(expr, callee, args, expected),
548 ExprKind::Use(used_expr, _) => self.check_expr_use(used_expr, expected),
549 ExprKind::MethodCall(segment, receiver, args, _) => {
550 self.check_expr_method_call(expr, segment, receiver, args, expected)
551 }
552 ExprKind::Cast(e, t) => self.check_expr_cast(e, t, expr),
553 ExprKind::Type(e, t) => {
554 let ascribed_ty = self.lower_ty_saving_user_provided_ty(t);
555 let ty = self.check_expr_with_hint(e, ascribed_ty);
556 self.demand_eqtype(e.span, ascribed_ty, ty);
557 ascribed_ty
558 }
559 ExprKind::If(cond, then_expr, opt_else_expr) => {
560 self.check_expr_if(cond, then_expr, opt_else_expr, expr.span, expected)
561 }
562 ExprKind::DropTemps(e) => self.check_expr_with_expectation(e, expected),
563 ExprKind::Array(args) => self.check_expr_array(args, expected, expr),
564 ExprKind::ConstBlock(ref block) => self.check_expr_const_block(block, expected),
565 ExprKind::Repeat(element, ref count) => {
566 self.check_expr_repeat(element, count, expected, expr)
567 }
568 ExprKind::Tup(elts) => self.check_expr_tuple(elts, expected, expr),
569 ExprKind::Struct(qpath, fields, ref base_expr) => {
570 self.check_expr_struct(expr, expected, qpath, fields, base_expr)
571 }
572 ExprKind::Field(base, field) => self.check_expr_field(expr, base, field, expected),
573 ExprKind::Index(base, idx, brackets_span) => {
574 self.check_expr_index(base, idx, expr, brackets_span)
575 }
576 ExprKind::Yield(value, _) => self.check_expr_yield(value, expr),
577 ExprKind::UnsafeBinderCast(kind, inner_expr, ty) => {
578 self.check_expr_unsafe_binder_cast(expr.span, kind, inner_expr, ty, expected)
579 }
580 ExprKind::Err(guar) => Ty::new_error(tcx, guar),
581 }
582 }
583
584 fn check_expr_unop(
585 &self,
586 unop: hir::UnOp,
587 oprnd: &'tcx hir::Expr<'tcx>,
588 expected: Expectation<'tcx>,
589 expr: &'tcx hir::Expr<'tcx>,
590 ) -> Ty<'tcx> {
591 let tcx = self.tcx;
592 let expected_inner = match unop {
593 hir::UnOp::Not | hir::UnOp::Neg => expected,
594 hir::UnOp::Deref => NoExpectation,
595 };
596 let mut oprnd_t = self.check_expr_with_expectation(oprnd, expected_inner);
597
598 if !oprnd_t.references_error() {
599 oprnd_t = self.structurally_resolve_type(expr.span, oprnd_t);
600 match unop {
601 hir::UnOp::Deref => {
602 if let Some(ty) = self.lookup_derefing(expr, oprnd, oprnd_t) {
603 oprnd_t = ty;
604 } else {
605 let mut err =
606 self.dcx().create_err(CantDereference { span: expr.span, ty: oprnd_t });
607 let sp = tcx.sess.source_map().start_point(expr.span).with_parent(None);
608 if let Some(sp) =
609 tcx.sess.psess.ambiguous_block_expr_parse.borrow().get(&sp)
610 {
611 err.subdiagnostic(ExprParenthesesNeeded::surrounding(*sp));
612 }
613 oprnd_t = Ty::new_error(tcx, err.emit());
614 }
615 }
616 hir::UnOp::Not => {
617 let result = self.check_user_unop(expr, oprnd_t, unop, expected_inner);
618 if !(oprnd_t.is_integral() || *oprnd_t.kind() == ty::Bool) {
620 oprnd_t = result;
621 }
622 }
623 hir::UnOp::Neg => {
624 let result = self.check_user_unop(expr, oprnd_t, unop, expected_inner);
625 if !oprnd_t.is_numeric() {
627 oprnd_t = result;
628 }
629 }
630 }
631 }
632 oprnd_t
633 }
634
635 fn check_expr_addr_of(
636 &self,
637 kind: hir::BorrowKind,
638 mutbl: hir::Mutability,
639 oprnd: &'tcx hir::Expr<'tcx>,
640 expected: Expectation<'tcx>,
641 expr: &'tcx hir::Expr<'tcx>,
642 ) -> Ty<'tcx> {
643 let hint = expected.only_has_type(self).map_or(NoExpectation, |ty| {
644 match self.try_structurally_resolve_type(expr.span, ty).kind() {
645 ty::Ref(_, ty, _) | ty::RawPtr(ty, _) => {
646 if oprnd.is_syntactic_place_expr() {
647 ExpectHasType(*ty)
651 } else {
652 Expectation::rvalue_hint(self, *ty)
653 }
654 }
655 _ => NoExpectation,
656 }
657 });
658 let ty =
659 self.check_expr_with_expectation_and_needs(oprnd, hint, Needs::maybe_mut_place(mutbl));
660
661 match kind {
662 _ if ty.references_error() => Ty::new_misc_error(self.tcx),
663 hir::BorrowKind::Raw => {
664 self.check_named_place_expr(oprnd);
665 Ty::new_ptr(self.tcx, ty, mutbl)
666 }
667 hir::BorrowKind::Ref => {
668 let region = self.next_region_var(infer::BorrowRegion(expr.span));
683 Ty::new_ref(self.tcx, region, ty, mutbl)
684 }
685 }
686 }
687
688 fn check_named_place_expr(&self, oprnd: &'tcx hir::Expr<'tcx>) {
694 let is_named = oprnd.is_place_expr(|base| {
695 self.typeck_results
707 .borrow()
708 .adjustments()
709 .get(base.hir_id)
710 .is_some_and(|x| x.iter().any(|adj| matches!(adj.kind, Adjust::Deref(_))))
711 });
712 if !is_named {
713 self.dcx().emit_err(AddressOfTemporaryTaken { span: oprnd.span });
714 }
715 }
716
717 fn check_lang_item_path(
718 &self,
719 lang_item: hir::LangItem,
720 expr: &'tcx hir::Expr<'tcx>,
721 ) -> Ty<'tcx> {
722 self.resolve_lang_item_path(lang_item, expr.span, expr.hir_id).1
723 }
724
725 pub(crate) fn check_expr_path(
726 &self,
727 qpath: &'tcx hir::QPath<'tcx>,
728 expr: &'tcx hir::Expr<'tcx>,
729 call_expr_and_args: Option<(&'tcx hir::Expr<'tcx>, &'tcx [hir::Expr<'tcx>])>,
730 ) -> Ty<'tcx> {
731 let tcx = self.tcx;
732 let (res, opt_ty, segs) =
733 self.resolve_ty_and_res_fully_qualified_call(qpath, expr.hir_id, expr.span);
734 let ty = match res {
735 Res::Err => {
736 self.suggest_assoc_method_call(segs);
737 let e =
738 self.dcx().span_delayed_bug(qpath.span(), "`Res::Err` but no error emitted");
739 Ty::new_error(tcx, e)
740 }
741 Res::Def(DefKind::Variant, _) => {
742 let e = report_unexpected_variant_res(
743 tcx,
744 res,
745 Some(expr),
746 qpath,
747 expr.span,
748 E0533,
749 "value",
750 );
751 Ty::new_error(tcx, e)
752 }
753 _ => {
754 self.instantiate_value_path(
755 segs,
756 opt_ty,
757 res,
758 call_expr_and_args.map_or(expr.span, |(e, _)| e.span),
759 expr.span,
760 expr.hir_id,
761 )
762 .0
763 }
764 };
765
766 if let ty::FnDef(did, _) = *ty.kind() {
767 let fn_sig = ty.fn_sig(tcx);
768
769 if tcx.is_intrinsic(did, sym::transmute) {
770 let Some(from) = fn_sig.inputs().skip_binder().get(0) else {
771 span_bug!(
772 tcx.def_span(did),
773 "intrinsic fn `transmute` defined with no parameters"
774 );
775 };
776 let to = fn_sig.output().skip_binder();
777 self.deferred_transmute_checks.borrow_mut().push((*from, to, expr.hir_id));
782 }
783 if !tcx.features().unsized_fn_params() {
784 for i in 0..fn_sig.inputs().skip_binder().len() {
794 let span = call_expr_and_args
798 .and_then(|(_, args)| args.get(i))
799 .map_or(expr.span, |arg| arg.span);
800 let input = self.instantiate_binder_with_fresh_vars(
801 span,
802 infer::BoundRegionConversionTime::FnCall,
803 fn_sig.input(i),
804 );
805 self.require_type_is_sized_deferred(
806 input,
807 span,
808 ObligationCauseCode::SizedArgumentType(None),
809 );
810 }
811 }
812 let output = self.instantiate_binder_with_fresh_vars(
819 expr.span,
820 infer::BoundRegionConversionTime::FnCall,
821 fn_sig.output(),
822 );
823 self.require_type_is_sized_deferred(
824 output,
825 call_expr_and_args.map_or(expr.span, |(e, _)| e.span),
826 ObligationCauseCode::SizedCallReturnType,
827 );
828 }
829
830 let args = self.typeck_results.borrow().node_args(expr.hir_id);
833 self.add_wf_bounds(args, expr.span);
834
835 ty
836 }
837
838 fn check_expr_break(
839 &self,
840 destination: hir::Destination,
841 expr_opt: Option<&'tcx hir::Expr<'tcx>>,
842 expr: &'tcx hir::Expr<'tcx>,
843 ) -> Ty<'tcx> {
844 let tcx = self.tcx;
845 if let Ok(target_id) = destination.target_id {
846 let (e_ty, cause);
847 if let Some(e) = expr_opt {
848 let opt_coerce_to = {
851 let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
855 match enclosing_breakables.opt_find_breakable(target_id) {
856 Some(ctxt) => ctxt.coerce.as_ref().map(|coerce| coerce.expected_ty()),
857 None => {
858 return Ty::new_error_with_message(
860 tcx,
861 expr.span,
862 "break was outside loop, but no error was emitted",
863 );
864 }
865 }
866 };
867
868 let coerce_to = opt_coerce_to.unwrap_or_else(|| {
873 let guar = self.dcx().span_delayed_bug(
874 expr.span,
875 "illegal break with value found but no error reported",
876 );
877 self.set_tainted_by_errors(guar);
878 Ty::new_error(tcx, guar)
879 });
880
881 e_ty = self.check_expr_with_hint(e, coerce_to);
883 cause = self.misc(e.span);
884 } else {
885 e_ty = tcx.types.unit;
888 cause = self.misc(expr.span);
889 }
890
891 let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
895 let Some(ctxt) = enclosing_breakables.opt_find_breakable(target_id) else {
896 return Ty::new_error_with_message(
898 tcx,
899 expr.span,
900 "break was outside loop, but no error was emitted",
901 );
902 };
903
904 if let Some(ref mut coerce) = ctxt.coerce {
905 if let Some(e) = expr_opt {
906 coerce.coerce(self, &cause, e, e_ty);
907 } else {
908 assert!(e_ty.is_unit());
909 let ty = coerce.expected_ty();
910 coerce.coerce_forced_unit(
911 self,
912 &cause,
913 |mut err| {
914 self.suggest_missing_semicolon(&mut err, expr, e_ty, false, false);
915 self.suggest_mismatched_types_on_tail(
916 &mut err, expr, ty, e_ty, target_id,
917 );
918 let error =
919 Some(TypeError::Sorts(ExpectedFound { expected: ty, found: e_ty }));
920 self.annotate_loop_expected_due_to_inference(err, expr, error);
921 if let Some(val) =
922 self.err_ctxt().ty_kind_suggestion(self.param_env, ty)
923 {
924 err.span_suggestion_verbose(
925 expr.span.shrink_to_hi(),
926 "give the `break` a value of the expected type",
927 format!(" {val}"),
928 Applicability::HasPlaceholders,
929 );
930 }
931 },
932 false,
933 );
934 }
935 } else {
936 assert!(expr_opt.is_none() || self.tainted_by_errors().is_some());
944 }
945
946 ctxt.may_break |= !self.diverges.get().is_always();
950
951 tcx.types.never
953 } else {
954 let err = Ty::new_error_with_message(
959 self.tcx,
960 expr.span,
961 "break was outside loop, but no error was emitted",
962 );
963
964 if let Some(e) = expr_opt {
967 self.check_expr_with_hint(e, err);
968
969 if let ExprKind::Path(QPath::Resolved(_, path)) = e.kind {
972 if let [segment] = path.segments
973 && segment.ident.name == sym::rust
974 {
975 fatally_break_rust(self.tcx, expr.span);
976 }
977 }
978 }
979
980 err
982 }
983 }
984
985 fn check_expr_continue(
986 &self,
987 destination: hir::Destination,
988 expr: &'tcx hir::Expr<'tcx>,
989 ) -> Ty<'tcx> {
990 if let Ok(target_id) = destination.target_id {
991 if let hir::Node::Expr(hir::Expr { kind: ExprKind::Loop(..), .. }) =
992 self.tcx.hir_node(target_id)
993 {
994 self.tcx.types.never
995 } else {
996 let guar = self.dcx().span_delayed_bug(
999 expr.span,
1000 "found `continue` not pointing to loop, but no error reported",
1001 );
1002 Ty::new_error(self.tcx, guar)
1003 }
1004 } else {
1005 Ty::new_misc_error(self.tcx)
1007 }
1008 }
1009
1010 fn check_expr_return(
1011 &self,
1012 expr_opt: Option<&'tcx hir::Expr<'tcx>>,
1013 expr: &'tcx hir::Expr<'tcx>,
1014 ) -> Ty<'tcx> {
1015 if self.ret_coercion.is_none() {
1016 self.emit_return_outside_of_fn_body(expr, ReturnLikeStatementKind::Return);
1017
1018 if let Some(e) = expr_opt {
1019 self.check_expr(e);
1022 }
1023 } else if let Some(e) = expr_opt {
1024 if self.ret_coercion_span.get().is_none() {
1025 self.ret_coercion_span.set(Some(e.span));
1026 }
1027 self.check_return_or_body_tail(e, true);
1028 } else {
1029 let mut coercion = self.ret_coercion.as_ref().unwrap().borrow_mut();
1030 if self.ret_coercion_span.get().is_none() {
1031 self.ret_coercion_span.set(Some(expr.span));
1032 }
1033 let cause = self.cause(expr.span, ObligationCauseCode::ReturnNoExpression);
1034 if let Some((_, fn_decl)) = self.get_fn_decl(expr.hir_id) {
1035 coercion.coerce_forced_unit(
1036 self,
1037 &cause,
1038 |db| {
1039 let span = fn_decl.output.span();
1040 if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
1041 db.span_label(
1042 span,
1043 format!("expected `{snippet}` because of this return type"),
1044 );
1045 }
1046 },
1047 true,
1048 );
1049 } else {
1050 coercion.coerce_forced_unit(self, &cause, |_| (), true);
1051 }
1052 }
1053 self.tcx.types.never
1054 }
1055
1056 fn check_expr_become(
1057 &self,
1058 call: &'tcx hir::Expr<'tcx>,
1059 expr: &'tcx hir::Expr<'tcx>,
1060 ) -> Ty<'tcx> {
1061 match &self.ret_coercion {
1062 Some(ret_coercion) => {
1063 let ret_ty = ret_coercion.borrow().expected_ty();
1064 let call_expr_ty = self.check_expr_with_hint(call, ret_ty);
1065
1066 self.demand_suptype(expr.span, ret_ty, call_expr_ty);
1069 }
1070 None => {
1071 self.emit_return_outside_of_fn_body(expr, ReturnLikeStatementKind::Become);
1072
1073 self.check_expr(call);
1076 }
1077 }
1078
1079 self.tcx.types.never
1080 }
1081
1082 pub(super) fn check_return_or_body_tail(
1091 &self,
1092 return_expr: &'tcx hir::Expr<'tcx>,
1093 explicit_return: bool,
1094 ) {
1095 let ret_coercion = self.ret_coercion.as_ref().unwrap_or_else(|| {
1096 span_bug!(return_expr.span, "check_return_expr called outside fn body")
1097 });
1098
1099 let ret_ty = ret_coercion.borrow().expected_ty();
1100 let return_expr_ty = self.check_expr_with_hint(return_expr, ret_ty);
1101 let mut span = return_expr.span;
1102 let mut hir_id = return_expr.hir_id;
1103 if !explicit_return
1106 && let ExprKind::Block(body, _) = return_expr.kind
1107 && let Some(last_expr) = body.expr
1108 {
1109 span = last_expr.span;
1110 hir_id = last_expr.hir_id;
1111 }
1112 ret_coercion.borrow_mut().coerce(
1113 self,
1114 &self.cause(span, ObligationCauseCode::ReturnValue(return_expr.hir_id)),
1115 return_expr,
1116 return_expr_ty,
1117 );
1118
1119 if let Some(fn_sig) = self.body_fn_sig()
1120 && fn_sig.output().has_opaque_types()
1121 {
1122 self.select_obligations_where_possible(|errors| {
1125 self.point_at_return_for_opaque_ty_error(
1126 errors,
1127 hir_id,
1128 span,
1129 return_expr_ty,
1130 return_expr.span,
1131 );
1132 });
1133 }
1134 }
1135
1136 fn emit_return_outside_of_fn_body(&self, expr: &hir::Expr<'_>, kind: ReturnLikeStatementKind) {
1141 let mut err = ReturnStmtOutsideOfFnBody {
1142 span: expr.span,
1143 encl_body_span: None,
1144 encl_fn_span: None,
1145 statement_kind: kind,
1146 };
1147
1148 let encl_item_id = self.tcx.hir_get_parent_item(expr.hir_id);
1149
1150 if let hir::Node::Item(hir::Item {
1151 kind: hir::ItemKind::Fn { .. },
1152 span: encl_fn_span,
1153 ..
1154 })
1155 | hir::Node::TraitItem(hir::TraitItem {
1156 kind: hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(_)),
1157 span: encl_fn_span,
1158 ..
1159 })
1160 | hir::Node::ImplItem(hir::ImplItem {
1161 kind: hir::ImplItemKind::Fn(..),
1162 span: encl_fn_span,
1163 ..
1164 }) = self.tcx.hir_node_by_def_id(encl_item_id.def_id)
1165 {
1166 let encl_body_owner_id = self.tcx.hir_enclosing_body_owner(expr.hir_id);
1170
1171 assert_ne!(encl_item_id.def_id, encl_body_owner_id);
1174
1175 let encl_body = self.tcx.hir_body_owned_by(encl_body_owner_id);
1176
1177 err.encl_body_span = Some(encl_body.value.span);
1178 err.encl_fn_span = Some(*encl_fn_span);
1179 }
1180
1181 self.dcx().emit_err(err);
1182 }
1183
1184 fn point_at_return_for_opaque_ty_error(
1185 &self,
1186 errors: &mut Vec<traits::FulfillmentError<'tcx>>,
1187 hir_id: HirId,
1188 span: Span,
1189 return_expr_ty: Ty<'tcx>,
1190 return_span: Span,
1191 ) {
1192 if span == return_span {
1194 return;
1195 }
1196 for err in errors {
1197 let cause = &mut err.obligation.cause;
1198 if let ObligationCauseCode::OpaqueReturnType(None) = cause.code() {
1199 let new_cause = self.cause(
1200 cause.span,
1201 ObligationCauseCode::OpaqueReturnType(Some((return_expr_ty, hir_id))),
1202 );
1203 *cause = new_cause;
1204 }
1205 }
1206 }
1207
1208 pub(crate) fn check_lhs_assignable(
1209 &self,
1210 lhs: &'tcx hir::Expr<'tcx>,
1211 code: ErrCode,
1212 op_span: Span,
1213 adjust_err: impl FnOnce(&mut Diag<'_>),
1214 ) {
1215 if lhs.is_syntactic_place_expr() {
1216 return;
1217 }
1218
1219 let mut err = self.dcx().struct_span_err(op_span, "invalid left-hand side of assignment");
1220 err.code(code);
1221 err.span_label(lhs.span, "cannot assign to this expression");
1222
1223 self.comes_from_while_condition(lhs.hir_id, |expr| {
1224 err.span_suggestion_verbose(
1225 expr.span.shrink_to_lo(),
1226 "you might have meant to use pattern destructuring",
1227 "let ",
1228 Applicability::MachineApplicable,
1229 );
1230 });
1231 self.check_for_missing_semi(lhs, &mut err);
1232
1233 adjust_err(&mut err);
1234
1235 err.emit();
1236 }
1237
1238 pub(crate) fn check_for_missing_semi(
1240 &self,
1241 expr: &'tcx hir::Expr<'tcx>,
1242 err: &mut Diag<'_>,
1243 ) -> bool {
1244 if let hir::ExprKind::Binary(binop, lhs, rhs) = expr.kind
1245 && let hir::BinOpKind::Mul = binop.node
1246 && self.tcx.sess.source_map().is_multiline(lhs.span.between(rhs.span))
1247 && rhs.is_syntactic_place_expr()
1248 {
1249 err.span_suggestion_verbose(
1254 lhs.span.shrink_to_hi(),
1255 "you might have meant to write a semicolon here",
1256 ";",
1257 Applicability::MachineApplicable,
1258 );
1259 return true;
1260 }
1261 false
1262 }
1263
1264 pub(super) fn comes_from_while_condition(
1268 &self,
1269 original_expr_id: HirId,
1270 then: impl FnOnce(&hir::Expr<'_>),
1271 ) {
1272 let mut parent = self.tcx.parent_hir_id(original_expr_id);
1273 loop {
1274 let node = self.tcx.hir_node(parent);
1275 match node {
1276 hir::Node::Expr(hir::Expr {
1277 kind:
1278 hir::ExprKind::Loop(
1279 hir::Block {
1280 expr:
1281 Some(hir::Expr {
1282 kind:
1283 hir::ExprKind::Match(expr, ..) | hir::ExprKind::If(expr, ..),
1284 ..
1285 }),
1286 ..
1287 },
1288 _,
1289 hir::LoopSource::While,
1290 _,
1291 ),
1292 ..
1293 }) => {
1294 if self.tcx.hir_parent_id_iter(original_expr_id).any(|id| id == expr.hir_id) {
1298 then(expr);
1299 }
1300 break;
1301 }
1302 hir::Node::Item(_)
1303 | hir::Node::ImplItem(_)
1304 | hir::Node::TraitItem(_)
1305 | hir::Node::Crate(_) => break,
1306 _ => {
1307 parent = self.tcx.parent_hir_id(parent);
1308 }
1309 }
1310 }
1311 }
1312
1313 fn check_expr_if(
1316 &self,
1317 cond_expr: &'tcx hir::Expr<'tcx>,
1318 then_expr: &'tcx hir::Expr<'tcx>,
1319 opt_else_expr: Option<&'tcx hir::Expr<'tcx>>,
1320 sp: Span,
1321 orig_expected: Expectation<'tcx>,
1322 ) -> Ty<'tcx> {
1323 let cond_ty = self.check_expr_has_type_or_error(cond_expr, self.tcx.types.bool, |_| {});
1324
1325 self.warn_if_unreachable(
1326 cond_expr.hir_id,
1327 then_expr.span,
1328 "block in `if` or `while` expression",
1329 );
1330
1331 let cond_diverges = self.diverges.get();
1332 self.diverges.set(Diverges::Maybe);
1333
1334 let expected = orig_expected.try_structurally_resolve_and_adjust_for_branches(self, sp);
1335 let then_ty = self.check_expr_with_expectation(then_expr, expected);
1336 let then_diverges = self.diverges.get();
1337 self.diverges.set(Diverges::Maybe);
1338
1339 let coerce_to_ty = expected.coercion_target_type(self, sp);
1346 let mut coerce: DynamicCoerceMany<'_> = CoerceMany::new(coerce_to_ty);
1347
1348 coerce.coerce(self, &self.misc(sp), then_expr, then_ty);
1349
1350 if let Some(else_expr) = opt_else_expr {
1351 let else_ty = self.check_expr_with_expectation(else_expr, expected);
1352 let else_diverges = self.diverges.get();
1353
1354 let tail_defines_return_position_impl_trait =
1355 self.return_position_impl_trait_from_match_expectation(orig_expected);
1356 let if_cause = self.if_cause(
1357 sp,
1358 cond_expr.span,
1359 then_expr,
1360 else_expr,
1361 then_ty,
1362 else_ty,
1363 tail_defines_return_position_impl_trait,
1364 );
1365
1366 coerce.coerce(self, &if_cause, else_expr, else_ty);
1367
1368 self.diverges.set(cond_diverges | then_diverges & else_diverges);
1370 } else {
1371 self.if_fallback_coercion(sp, cond_expr, then_expr, &mut coerce);
1372
1373 self.diverges.set(cond_diverges);
1375 }
1376
1377 let result_ty = coerce.complete(self);
1378 if let Err(guar) = cond_ty.error_reported() {
1379 Ty::new_error(self.tcx, guar)
1380 } else {
1381 result_ty
1382 }
1383 }
1384
1385 fn check_expr_assign(
1388 &self,
1389 expr: &'tcx hir::Expr<'tcx>,
1390 expected: Expectation<'tcx>,
1391 lhs: &'tcx hir::Expr<'tcx>,
1392 rhs: &'tcx hir::Expr<'tcx>,
1393 span: Span,
1394 ) -> Ty<'tcx> {
1395 let expected_ty = expected.only_has_type(self);
1396 if expected_ty == Some(self.tcx.types.bool) {
1397 let guar = self.expr_assign_expected_bool_error(expr, lhs, rhs, span);
1398 return Ty::new_error(self.tcx, guar);
1399 }
1400
1401 let lhs_ty = self.check_expr_with_needs(lhs, Needs::MutPlace);
1402
1403 let suggest_deref_binop = |err: &mut Diag<'_>, rhs_ty: Ty<'tcx>| {
1404 if let Some(lhs_deref_ty) = self.deref_once_mutably_for_diagnostic(lhs_ty) {
1405 let lhs_deref_ty_is_sized = self
1408 .infcx
1409 .type_implements_trait(
1410 self.tcx.require_lang_item(LangItem::Sized, None),
1411 [lhs_deref_ty],
1412 self.param_env,
1413 )
1414 .may_apply();
1415 if lhs_deref_ty_is_sized && self.may_coerce(rhs_ty, lhs_deref_ty) {
1416 err.span_suggestion_verbose(
1417 lhs.span.shrink_to_lo(),
1418 "consider dereferencing here to assign to the mutably borrowed value",
1419 "*",
1420 Applicability::MachineApplicable,
1421 );
1422 }
1423 }
1424 };
1425
1426 let rhs_ty = self.check_expr_with_hint(rhs, lhs_ty);
1429 if let Err(mut diag) =
1430 self.demand_coerce_diag(rhs, rhs_ty, lhs_ty, Some(lhs), AllowTwoPhase::No)
1431 {
1432 suggest_deref_binop(&mut diag, rhs_ty);
1433 diag.emit();
1434 }
1435
1436 self.check_lhs_assignable(lhs, E0070, span, |err| {
1437 if let Some(rhs_ty) = self.typeck_results.borrow().expr_ty_opt(rhs) {
1438 suggest_deref_binop(err, rhs_ty);
1439 }
1440 });
1441
1442 self.require_type_is_sized(lhs_ty, lhs.span, ObligationCauseCode::AssignmentLhsSized);
1443
1444 if let Err(guar) = (lhs_ty, rhs_ty).error_reported() {
1445 Ty::new_error(self.tcx, guar)
1446 } else {
1447 self.tcx.types.unit
1448 }
1449 }
1450
1451 fn expr_assign_expected_bool_error(
1455 &self,
1456 expr: &'tcx hir::Expr<'tcx>,
1457 lhs: &'tcx hir::Expr<'tcx>,
1458 rhs: &'tcx hir::Expr<'tcx>,
1459 span: Span,
1460 ) -> ErrorGuaranteed {
1461 let actual_ty = self.tcx.types.unit;
1462 let expected_ty = self.tcx.types.bool;
1463 let mut err = self.demand_suptype_diag(expr.span, expected_ty, actual_ty).unwrap_err();
1464 let lhs_ty = self.check_expr(lhs);
1465 let rhs_ty = self.check_expr(rhs);
1466 let refs_can_coerce = |lhs: Ty<'tcx>, rhs: Ty<'tcx>| {
1467 let lhs = Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_erased, lhs.peel_refs());
1468 let rhs = Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_erased, rhs.peel_refs());
1469 self.may_coerce(rhs, lhs)
1470 };
1471 let (applicability, eq) = if self.may_coerce(rhs_ty, lhs_ty) {
1472 (Applicability::MachineApplicable, true)
1473 } else if refs_can_coerce(rhs_ty, lhs_ty) {
1474 (Applicability::MaybeIncorrect, true)
1477 } else if let ExprKind::Binary(
1478 Spanned { node: hir::BinOpKind::And | hir::BinOpKind::Or, .. },
1479 _,
1480 rhs_expr,
1481 ) = lhs.kind
1482 {
1483 let actual_lhs = self.check_expr(rhs_expr);
1486 let may_eq = self.may_coerce(rhs_ty, actual_lhs) || refs_can_coerce(rhs_ty, actual_lhs);
1487 (Applicability::MaybeIncorrect, may_eq)
1488 } else if let ExprKind::Binary(
1489 Spanned { node: hir::BinOpKind::And | hir::BinOpKind::Or, .. },
1490 lhs_expr,
1491 _,
1492 ) = rhs.kind
1493 {
1494 let actual_rhs = self.check_expr(lhs_expr);
1497 let may_eq = self.may_coerce(actual_rhs, lhs_ty) || refs_can_coerce(actual_rhs, lhs_ty);
1498 (Applicability::MaybeIncorrect, may_eq)
1499 } else {
1500 (Applicability::MaybeIncorrect, false)
1501 };
1502
1503 if !lhs.is_syntactic_place_expr()
1504 && lhs.is_approximately_pattern()
1505 && !matches!(lhs.kind, hir::ExprKind::Lit(_))
1506 {
1507 if let hir::Node::Expr(hir::Expr { kind: ExprKind::If { .. }, .. }) =
1509 self.tcx.parent_hir_node(expr.hir_id)
1510 {
1511 err.span_suggestion_verbose(
1512 expr.span.shrink_to_lo(),
1513 "you might have meant to use pattern matching",
1514 "let ",
1515 applicability,
1516 );
1517 };
1518 }
1519 if eq {
1520 err.span_suggestion_verbose(
1521 span.shrink_to_hi(),
1522 "you might have meant to compare for equality",
1523 '=',
1524 applicability,
1525 );
1526 }
1527
1528 err.emit_unless(lhs_ty.references_error() || rhs_ty.references_error())
1531 }
1532
1533 pub(super) fn check_expr_let(
1534 &self,
1535 let_expr: &'tcx hir::LetExpr<'tcx>,
1536 hir_id: HirId,
1537 ) -> Ty<'tcx> {
1538 GatherLocalsVisitor::gather_from_let_expr(self, let_expr, hir_id);
1539
1540 let init = let_expr.init;
1542 self.warn_if_unreachable(init.hir_id, init.span, "block in `let` expression");
1543
1544 self.check_decl((let_expr, hir_id).into());
1546
1547 if let ast::Recovered::Yes(error_guaranteed) = let_expr.recovered {
1549 self.set_tainted_by_errors(error_guaranteed);
1550 Ty::new_error(self.tcx, error_guaranteed)
1551 } else {
1552 self.tcx.types.bool
1553 }
1554 }
1555
1556 fn check_expr_loop(
1557 &self,
1558 body: &'tcx hir::Block<'tcx>,
1559 source: hir::LoopSource,
1560 expected: Expectation<'tcx>,
1561 expr: &'tcx hir::Expr<'tcx>,
1562 ) -> Ty<'tcx> {
1563 let coerce = match source {
1564 hir::LoopSource::Loop => {
1566 let coerce_to = expected.coercion_target_type(self, body.span);
1567 Some(CoerceMany::new(coerce_to))
1568 }
1569
1570 hir::LoopSource::While | hir::LoopSource::ForLoop => None,
1571 };
1572
1573 let ctxt = BreakableCtxt {
1574 coerce,
1575 may_break: false, };
1577
1578 let (ctxt, ()) = self.with_breakable_ctxt(expr.hir_id, ctxt, || {
1579 self.check_block_no_value(body);
1580 });
1581
1582 if ctxt.may_break {
1583 self.diverges.set(Diverges::Maybe);
1586 } else {
1587 self.diverges.set(self.diverges.get() | Diverges::always(expr.span));
1588 }
1589
1590 if ctxt.coerce.is_none() && !ctxt.may_break {
1596 self.dcx().span_bug(body.span, "no coercion, but loop may not break");
1597 }
1598 ctxt.coerce.map(|c| c.complete(self)).unwrap_or_else(|| self.tcx.types.unit)
1599 }
1600
1601 fn check_expr_method_call(
1603 &self,
1604 expr: &'tcx hir::Expr<'tcx>,
1605 segment: &'tcx hir::PathSegment<'tcx>,
1606 rcvr: &'tcx hir::Expr<'tcx>,
1607 args: &'tcx [hir::Expr<'tcx>],
1608 expected: Expectation<'tcx>,
1609 ) -> Ty<'tcx> {
1610 let rcvr_t = self.check_expr(rcvr);
1611 let rcvr_t = self.structurally_resolve_type(rcvr.span, rcvr_t);
1613
1614 match self.lookup_method(rcvr_t, segment, segment.ident.span, expr, rcvr, args) {
1615 Ok(method) => {
1616 self.write_method_call_and_enforce_effects(expr.hir_id, expr.span, method);
1617
1618 self.check_argument_types(
1619 segment.ident.span,
1620 expr,
1621 &method.sig.inputs()[1..],
1622 method.sig.output(),
1623 expected,
1624 args,
1625 method.sig.c_variadic,
1626 TupleArgumentsFlag::DontTupleArguments,
1627 Some(method.def_id),
1628 );
1629
1630 method.sig.output()
1631 }
1632 Err(error) => {
1633 let guar = self.report_method_error(expr.hir_id, rcvr_t, error, expected, false);
1634
1635 let err_inputs = self.err_args(args.len(), guar);
1636 let err_output = Ty::new_error(self.tcx, guar);
1637
1638 self.check_argument_types(
1639 segment.ident.span,
1640 expr,
1641 &err_inputs,
1642 err_output,
1643 NoExpectation,
1644 args,
1645 false,
1646 TupleArgumentsFlag::DontTupleArguments,
1647 None,
1648 );
1649
1650 err_output
1651 }
1652 }
1653 }
1654
1655 fn check_expr_use(
1657 &self,
1658 used_expr: &'tcx hir::Expr<'tcx>,
1659 expected: Expectation<'tcx>,
1660 ) -> Ty<'tcx> {
1661 self.check_expr_with_expectation(used_expr, expected)
1662 }
1663
1664 fn check_expr_cast(
1665 &self,
1666 e: &'tcx hir::Expr<'tcx>,
1667 t: &'tcx hir::Ty<'tcx>,
1668 expr: &'tcx hir::Expr<'tcx>,
1669 ) -> Ty<'tcx> {
1670 let t_cast = self.lower_ty_saving_user_provided_ty(t);
1673 let t_cast = self.resolve_vars_if_possible(t_cast);
1674 let t_expr = self.check_expr_with_expectation(e, ExpectCastableToType(t_cast));
1675 let t_expr = self.resolve_vars_if_possible(t_expr);
1676
1677 if let Err(guar) = (t_expr, t_cast).error_reported() {
1679 Ty::new_error(self.tcx, guar)
1680 } else {
1681 let mut deferred_cast_checks = self.deferred_cast_checks.borrow_mut();
1683 match cast::CastCheck::new(self, e, t_expr, t_cast, t.span, expr.span) {
1684 Ok(cast_check) => {
1685 debug!(
1686 "check_expr_cast: deferring cast from {:?} to {:?}: {:?}",
1687 t_cast, t_expr, cast_check,
1688 );
1689 deferred_cast_checks.push(cast_check);
1690 t_cast
1691 }
1692 Err(guar) => Ty::new_error(self.tcx, guar),
1693 }
1694 }
1695 }
1696
1697 fn check_expr_unsafe_binder_cast(
1698 &self,
1699 span: Span,
1700 kind: ast::UnsafeBinderCastKind,
1701 inner_expr: &'tcx hir::Expr<'tcx>,
1702 hir_ty: Option<&'tcx hir::Ty<'tcx>>,
1703 expected: Expectation<'tcx>,
1704 ) -> Ty<'tcx> {
1705 match kind {
1706 ast::UnsafeBinderCastKind::Wrap => {
1707 let ascribed_ty =
1708 hir_ty.map(|hir_ty| self.lower_ty_saving_user_provided_ty(hir_ty));
1709 let expected_ty = expected.only_has_type(self);
1710 let binder_ty = match (ascribed_ty, expected_ty) {
1711 (Some(ascribed_ty), Some(expected_ty)) => {
1712 self.demand_eqtype(inner_expr.span, expected_ty, ascribed_ty);
1713 expected_ty
1714 }
1715 (Some(ty), None) | (None, Some(ty)) => ty,
1716 (None, None) => self.next_ty_var(inner_expr.span),
1720 };
1721
1722 let binder_ty = self.structurally_resolve_type(inner_expr.span, binder_ty);
1723 let hint_ty = match *binder_ty.kind() {
1724 ty::UnsafeBinder(binder) => self.instantiate_binder_with_fresh_vars(
1725 inner_expr.span,
1726 infer::BoundRegionConversionTime::HigherRankedType,
1727 binder.into(),
1728 ),
1729 ty::Error(e) => Ty::new_error(self.tcx, e),
1730 _ => {
1731 let guar = self
1732 .dcx()
1733 .struct_span_err(
1734 hir_ty.map_or(span, |hir_ty| hir_ty.span),
1735 format!(
1736 "`wrap_binder!()` can only wrap into unsafe binder, not {}",
1737 binder_ty.sort_string(self.tcx)
1738 ),
1739 )
1740 .with_note("unsafe binders are the only valid output of wrap")
1741 .emit();
1742 Ty::new_error(self.tcx, guar)
1743 }
1744 };
1745
1746 self.check_expr_has_type_or_error(inner_expr, hint_ty, |_| {});
1747
1748 binder_ty
1749 }
1750 ast::UnsafeBinderCastKind::Unwrap => {
1751 let ascribed_ty =
1752 hir_ty.map(|hir_ty| self.lower_ty_saving_user_provided_ty(hir_ty));
1753 let hint_ty = ascribed_ty.unwrap_or_else(|| self.next_ty_var(inner_expr.span));
1754 let binder_ty = self.check_expr_has_type_or_error(inner_expr, hint_ty, |_| {});
1756
1757 let binder_ty = self.structurally_resolve_type(inner_expr.span, binder_ty);
1760 match *binder_ty.kind() {
1761 ty::UnsafeBinder(binder) => self.instantiate_binder_with_fresh_vars(
1762 inner_expr.span,
1763 infer::BoundRegionConversionTime::HigherRankedType,
1764 binder.into(),
1765 ),
1766 ty::Error(e) => Ty::new_error(self.tcx, e),
1767 _ => {
1768 let guar = self
1769 .dcx()
1770 .struct_span_err(
1771 hir_ty.map_or(inner_expr.span, |hir_ty| hir_ty.span),
1772 format!(
1773 "expected unsafe binder, found {} as input of \
1774 `unwrap_binder!()`",
1775 binder_ty.sort_string(self.tcx)
1776 ),
1777 )
1778 .with_note("only an unsafe binder type can be unwrapped")
1779 .emit();
1780 Ty::new_error(self.tcx, guar)
1781 }
1782 }
1783 }
1784 }
1785 }
1786
1787 fn check_expr_array(
1788 &self,
1789 args: &'tcx [hir::Expr<'tcx>],
1790 expected: Expectation<'tcx>,
1791 expr: &'tcx hir::Expr<'tcx>,
1792 ) -> Ty<'tcx> {
1793 let element_ty = if !args.is_empty() {
1794 let coerce_to = expected
1795 .to_option(self)
1796 .and_then(|uty| self.try_structurally_resolve_type(expr.span, uty).builtin_index())
1797 .unwrap_or_else(|| self.next_ty_var(expr.span));
1798 let mut coerce = CoerceMany::with_coercion_sites(coerce_to, args);
1799 assert_eq!(self.diverges.get(), Diverges::Maybe);
1800 for e in args {
1801 let e_ty = self.check_expr_with_hint(e, coerce_to);
1802 let cause = self.misc(e.span);
1803 coerce.coerce(self, &cause, e, e_ty);
1804 }
1805 coerce.complete(self)
1806 } else {
1807 self.next_ty_var(expr.span)
1808 };
1809 let array_len = args.len() as u64;
1810 self.suggest_array_len(expr, array_len);
1811 Ty::new_array(self.tcx, element_ty, array_len)
1812 }
1813
1814 fn suggest_array_len(&self, expr: &'tcx hir::Expr<'tcx>, array_len: u64) {
1815 let parent_node = self.tcx.hir_parent_iter(expr.hir_id).find(|(_, node)| {
1816 !matches!(node, hir::Node::Expr(hir::Expr { kind: hir::ExprKind::AddrOf(..), .. }))
1817 });
1818 let Some((_, hir::Node::LetStmt(hir::LetStmt { ty: Some(ty), .. }))) = parent_node else {
1819 return;
1820 };
1821 if let hir::TyKind::Array(_, ct) = ty.peel_refs().kind {
1822 let span = ct.span();
1823 self.dcx().try_steal_modify_and_emit_err(
1824 span,
1825 StashKey::UnderscoreForArrayLengths,
1826 |err| {
1827 err.span_suggestion(
1828 span,
1829 "consider specifying the array length",
1830 array_len,
1831 Applicability::MaybeIncorrect,
1832 );
1833 },
1834 );
1835 }
1836 }
1837
1838 pub(super) fn check_expr_const_block(
1839 &self,
1840 block: &'tcx hir::ConstBlock,
1841 expected: Expectation<'tcx>,
1842 ) -> Ty<'tcx> {
1843 let body = self.tcx.hir_body(block.body);
1844
1845 let def_id = block.def_id;
1847 let fcx = FnCtxt::new(self, self.param_env, def_id);
1848
1849 let ty = fcx.check_expr_with_expectation(body.value, expected);
1850 fcx.require_type_is_sized(ty, body.value.span, ObligationCauseCode::SizedConstOrStatic);
1851 fcx.write_ty(block.hir_id, ty);
1852 ty
1853 }
1854
1855 fn check_expr_repeat(
1856 &self,
1857 element: &'tcx hir::Expr<'tcx>,
1858 count: &'tcx hir::ConstArg<'tcx>,
1859 expected: Expectation<'tcx>,
1860 expr: &'tcx hir::Expr<'tcx>,
1861 ) -> Ty<'tcx> {
1862 let tcx = self.tcx;
1863 let count_span = count.span();
1864 let count = self.try_structurally_resolve_const(
1865 count_span,
1866 self.normalize(count_span, self.lower_const_arg(count, FeedConstTy::No)),
1867 );
1868
1869 if let Some(count) = count.try_to_target_usize(tcx) {
1870 self.suggest_array_len(expr, count);
1871 }
1872
1873 let uty = match expected {
1874 ExpectHasType(uty) => uty.builtin_index(),
1875 _ => None,
1876 };
1877
1878 let (element_ty, t) = match uty {
1879 Some(uty) => {
1880 self.check_expr_coercible_to_type(element, uty, None);
1881 (uty, uty)
1882 }
1883 None => {
1884 let ty = self.next_ty_var(element.span);
1885 let element_ty = self.check_expr_has_type_or_error(element, ty, |_| {});
1886 (element_ty, ty)
1887 }
1888 };
1889
1890 if let Err(guar) = element_ty.error_reported() {
1891 return Ty::new_error(tcx, guar);
1892 }
1893
1894 self.deferred_repeat_expr_checks.borrow_mut().push((element, element_ty, count));
1898
1899 let ty = Ty::new_array_with_const_len(tcx, t, count);
1900 self.register_wf_obligation(ty.into(), expr.span, ObligationCauseCode::WellFormed(None));
1901 ty
1902 }
1903
1904 fn check_expr_tuple(
1905 &self,
1906 elts: &'tcx [hir::Expr<'tcx>],
1907 expected: Expectation<'tcx>,
1908 expr: &'tcx hir::Expr<'tcx>,
1909 ) -> Ty<'tcx> {
1910 let flds = expected.only_has_type(self).and_then(|ty| {
1911 let ty = self.try_structurally_resolve_type(expr.span, ty);
1912 match ty.kind() {
1913 ty::Tuple(flds) => Some(&flds[..]),
1914 _ => None,
1915 }
1916 });
1917
1918 let elt_ts_iter = elts.iter().enumerate().map(|(i, e)| match flds {
1919 Some(fs) if i < fs.len() => {
1920 let ety = fs[i];
1921 self.check_expr_coercible_to_type(e, ety, None);
1922 ety
1923 }
1924 _ => self.check_expr_with_expectation(e, NoExpectation),
1925 });
1926 let tuple = Ty::new_tup_from_iter(self.tcx, elt_ts_iter);
1927 if let Err(guar) = tuple.error_reported() {
1928 Ty::new_error(self.tcx, guar)
1929 } else {
1930 self.require_type_is_sized(
1931 tuple,
1932 expr.span,
1933 ObligationCauseCode::TupleInitializerSized,
1934 );
1935 tuple
1936 }
1937 }
1938
1939 fn check_expr_struct(
1940 &self,
1941 expr: &hir::Expr<'tcx>,
1942 expected: Expectation<'tcx>,
1943 qpath: &'tcx QPath<'tcx>,
1944 fields: &'tcx [hir::ExprField<'tcx>],
1945 base_expr: &'tcx hir::StructTailExpr<'tcx>,
1946 ) -> Ty<'tcx> {
1947 let (variant, adt_ty) = match self.check_struct_path(qpath, expr.hir_id) {
1949 Ok(data) => data,
1950 Err(guar) => {
1951 self.check_struct_fields_on_error(fields, base_expr);
1952 return Ty::new_error(self.tcx, guar);
1953 }
1954 };
1955
1956 let adt = adt_ty.ty_adt_def().expect("`check_struct_path` returned non-ADT type");
1958 if variant.field_list_has_applicable_non_exhaustive() {
1959 self.dcx()
1960 .emit_err(StructExprNonExhaustive { span: expr.span, what: adt.variant_descr() });
1961 }
1962
1963 self.check_expr_struct_fields(
1964 adt_ty,
1965 expected,
1966 expr,
1967 qpath.span(),
1968 variant,
1969 fields,
1970 base_expr,
1971 );
1972
1973 self.require_type_is_sized(adt_ty, expr.span, ObligationCauseCode::StructInitializerSized);
1974 adt_ty
1975 }
1976
1977 fn check_expr_struct_fields(
1978 &self,
1979 adt_ty: Ty<'tcx>,
1980 expected: Expectation<'tcx>,
1981 expr: &hir::Expr<'_>,
1982 path_span: Span,
1983 variant: &'tcx ty::VariantDef,
1984 hir_fields: &'tcx [hir::ExprField<'tcx>],
1985 base_expr: &'tcx hir::StructTailExpr<'tcx>,
1986 ) {
1987 let tcx = self.tcx;
1988
1989 let adt_ty = self.try_structurally_resolve_type(path_span, adt_ty);
1990 let adt_ty_hint = expected.only_has_type(self).and_then(|expected| {
1991 self.fudge_inference_if_ok(|| {
1992 let ocx = ObligationCtxt::new(self);
1993 ocx.sup(&self.misc(path_span), self.param_env, expected, adt_ty)?;
1994 if !ocx.select_where_possible().is_empty() {
1995 return Err(TypeError::Mismatch);
1996 }
1997 Ok(self.resolve_vars_if_possible(adt_ty))
1998 })
1999 .ok()
2000 });
2001 if let Some(adt_ty_hint) = adt_ty_hint {
2002 self.demand_eqtype(path_span, adt_ty_hint, adt_ty);
2004 }
2005
2006 let ty::Adt(adt, args) = adt_ty.kind() else {
2007 span_bug!(path_span, "non-ADT passed to check_expr_struct_fields");
2008 };
2009 let adt_kind = adt.adt_kind();
2010
2011 let mut remaining_fields = variant
2012 .fields
2013 .iter_enumerated()
2014 .map(|(i, field)| (field.ident(tcx).normalize_to_macros_2_0(), (i, field)))
2015 .collect::<UnordMap<_, _>>();
2016
2017 let mut seen_fields = FxHashMap::default();
2018
2019 let mut error_happened = false;
2020
2021 if variant.fields.len() != remaining_fields.len() {
2022 let guar =
2025 self.dcx().span_delayed_bug(expr.span, "struct fields have non-unique names");
2026 self.set_tainted_by_errors(guar);
2027 error_happened = true;
2028 }
2029
2030 for (idx, field) in hir_fields.iter().enumerate() {
2032 let ident = tcx.adjust_ident(field.ident, variant.def_id);
2033 let field_type = if let Some((i, v_field)) = remaining_fields.remove(&ident) {
2034 seen_fields.insert(ident, field.span);
2035 self.write_field_index(field.hir_id, i);
2036
2037 if adt_kind != AdtKind::Enum {
2041 tcx.check_stability(v_field.did, Some(field.hir_id), field.span, None);
2042 }
2043
2044 self.field_ty(field.span, v_field, args)
2045 } else {
2046 error_happened = true;
2047 let guar = if let Some(prev_span) = seen_fields.get(&ident) {
2048 self.dcx().emit_err(FieldMultiplySpecifiedInInitializer {
2049 span: field.ident.span,
2050 prev_span: *prev_span,
2051 ident,
2052 })
2053 } else {
2054 self.report_unknown_field(
2055 adt_ty,
2056 variant,
2057 expr,
2058 field,
2059 hir_fields,
2060 adt.variant_descr(),
2061 )
2062 };
2063
2064 Ty::new_error(tcx, guar)
2065 };
2066
2067 self.register_wf_obligation(
2071 field_type.into(),
2072 field.expr.span,
2073 ObligationCauseCode::WellFormed(None),
2074 );
2075
2076 let ty = self.check_expr_with_hint(field.expr, field_type);
2079 let diag = self.demand_coerce_diag(field.expr, ty, field_type, None, AllowTwoPhase::No);
2080
2081 if let Err(diag) = diag {
2082 if idx == hir_fields.len() - 1 {
2083 if remaining_fields.is_empty() {
2084 self.suggest_fru_from_range_and_emit(field, variant, args, diag);
2085 } else {
2086 diag.stash(field.span, StashKey::MaybeFruTypo);
2087 }
2088 } else {
2089 diag.emit();
2090 }
2091 }
2092 }
2093
2094 if adt_kind == AdtKind::Union && hir_fields.len() != 1 {
2096 struct_span_code_err!(
2097 self.dcx(),
2098 path_span,
2099 E0784,
2100 "union expressions should have exactly one field",
2101 )
2102 .emit();
2103 }
2104
2105 if error_happened {
2109 if let hir::StructTailExpr::Base(base_expr) = base_expr {
2110 self.check_expr(base_expr);
2111 }
2112 return;
2113 }
2114
2115 if let hir::StructTailExpr::DefaultFields(span) = *base_expr {
2116 let mut missing_mandatory_fields = Vec::new();
2117 let mut missing_optional_fields = Vec::new();
2118 for f in &variant.fields {
2119 let ident = self.tcx.adjust_ident(f.ident(self.tcx), variant.def_id);
2120 if let Some(_) = remaining_fields.remove(&ident) {
2121 if f.value.is_none() {
2122 missing_mandatory_fields.push(ident);
2123 } else {
2124 missing_optional_fields.push(ident);
2125 }
2126 }
2127 }
2128 if !self.tcx.features().default_field_values() {
2129 let sugg = self.tcx.crate_level_attribute_injection_span(expr.hir_id);
2130 self.dcx().emit_err(BaseExpressionDoubleDot {
2131 span: span.shrink_to_hi(),
2132 default_field_values_suggestion: if self.tcx.sess.is_nightly_build()
2135 && missing_mandatory_fields.is_empty()
2136 && !missing_optional_fields.is_empty()
2137 && sugg.is_some()
2138 {
2139 sugg
2140 } else {
2141 None
2142 },
2143 default_field_values_help: if self.tcx.sess.is_nightly_build()
2144 && missing_mandatory_fields.is_empty()
2145 && !missing_optional_fields.is_empty()
2146 && sugg.is_none()
2147 {
2148 Some(BaseExpressionDoubleDotEnableDefaultFieldValues)
2149 } else {
2150 None
2151 },
2152 add_expr: if !missing_mandatory_fields.is_empty()
2153 || !missing_optional_fields.is_empty()
2154 {
2155 Some(BaseExpressionDoubleDotAddExpr { span: span.shrink_to_hi() })
2156 } else {
2157 None
2158 },
2159 remove_dots: if missing_mandatory_fields.is_empty()
2160 && missing_optional_fields.is_empty()
2161 {
2162 Some(BaseExpressionDoubleDotRemove { span })
2163 } else {
2164 None
2165 },
2166 });
2167 return;
2168 }
2169 if variant.fields.is_empty() {
2170 let mut err = self.dcx().struct_span_err(
2171 span,
2172 format!(
2173 "`{adt_ty}` has no fields, `..` needs at least one default field in the \
2174 struct definition",
2175 ),
2176 );
2177 err.span_label(path_span, "this type has no fields");
2178 err.emit();
2179 }
2180 if !missing_mandatory_fields.is_empty() {
2181 let s = pluralize!(missing_mandatory_fields.len());
2182 let fields = listify(&missing_mandatory_fields, |f| format!("`{f}`")).unwrap();
2183 self.dcx()
2184 .struct_span_err(
2185 span.shrink_to_lo(),
2186 format!("missing field{s} {fields} in initializer"),
2187 )
2188 .with_span_label(
2189 span.shrink_to_lo(),
2190 "fields that do not have a defaulted value must be provided explicitly",
2191 )
2192 .emit();
2193 return;
2194 }
2195 let fru_tys = match adt_ty.kind() {
2196 ty::Adt(adt, args) if adt.is_struct() => variant
2197 .fields
2198 .iter()
2199 .map(|f| self.normalize(span, f.ty(self.tcx, args)))
2200 .collect(),
2201 ty::Adt(adt, args) if adt.is_enum() => variant
2202 .fields
2203 .iter()
2204 .map(|f| self.normalize(span, f.ty(self.tcx, args)))
2205 .collect(),
2206 _ => {
2207 self.dcx().emit_err(FunctionalRecordUpdateOnNonStruct { span });
2208 return;
2209 }
2210 };
2211 self.typeck_results.borrow_mut().fru_field_types_mut().insert(expr.hir_id, fru_tys);
2212 } else if let hir::StructTailExpr::Base(base_expr) = base_expr {
2213 let fru_tys = if self.tcx.features().type_changing_struct_update() {
2216 if adt.is_struct() {
2217 let fresh_args = self.fresh_args_for_item(base_expr.span, adt.did());
2219 let fru_tys = variant
2224 .fields
2225 .iter()
2226 .map(|f| {
2227 let fru_ty = self
2228 .normalize(expr.span, self.field_ty(base_expr.span, f, fresh_args));
2229 let ident = self.tcx.adjust_ident(f.ident(self.tcx), variant.def_id);
2230 if let Some(_) = remaining_fields.remove(&ident) {
2231 let target_ty = self.field_ty(base_expr.span, f, args);
2232 let cause = self.misc(base_expr.span);
2233 match self.at(&cause, self.param_env).sup(
2234 DefineOpaqueTypes::Yes,
2238 target_ty,
2239 fru_ty,
2240 ) {
2241 Ok(InferOk { obligations, value: () }) => {
2242 self.register_predicates(obligations)
2243 }
2244 Err(_) => {
2245 span_bug!(
2246 cause.span,
2247 "subtyping remaining fields of type changing FRU failed: {target_ty} != {fru_ty}: {}::{}",
2248 variant.name,
2249 ident.name,
2250 );
2251 }
2252 }
2253 }
2254 self.resolve_vars_if_possible(fru_ty)
2255 })
2256 .collect();
2257 let fresh_base_ty = Ty::new_adt(self.tcx, *adt, fresh_args);
2276 self.check_expr_has_type_or_error(
2277 base_expr,
2278 self.resolve_vars_if_possible(fresh_base_ty),
2279 |_| {},
2280 );
2281 fru_tys
2282 } else {
2283 self.check_expr(base_expr);
2286 self.dcx().emit_err(FunctionalRecordUpdateOnNonStruct { span: base_expr.span });
2287 return;
2288 }
2289 } else {
2290 self.check_expr_has_type_or_error(base_expr, adt_ty, |_| {
2291 let base_ty = self.typeck_results.borrow().expr_ty(*base_expr);
2292 let same_adt = matches!((adt_ty.kind(), base_ty.kind()),
2293 (ty::Adt(adt, _), ty::Adt(base_adt, _)) if adt == base_adt);
2294 if self.tcx.sess.is_nightly_build() && same_adt {
2295 feature_err(
2296 &self.tcx.sess,
2297 sym::type_changing_struct_update,
2298 base_expr.span,
2299 "type changing struct updating is experimental",
2300 )
2301 .emit();
2302 }
2303 });
2304 match adt_ty.kind() {
2305 ty::Adt(adt, args) if adt.is_struct() => variant
2306 .fields
2307 .iter()
2308 .map(|f| self.normalize(expr.span, f.ty(self.tcx, args)))
2309 .collect(),
2310 _ => {
2311 self.dcx()
2312 .emit_err(FunctionalRecordUpdateOnNonStruct { span: base_expr.span });
2313 return;
2314 }
2315 }
2316 };
2317 self.typeck_results.borrow_mut().fru_field_types_mut().insert(expr.hir_id, fru_tys);
2318 } else if adt_kind != AdtKind::Union && !remaining_fields.is_empty() {
2319 debug!(?remaining_fields);
2320 let private_fields: Vec<&ty::FieldDef> = variant
2321 .fields
2322 .iter()
2323 .filter(|field| !field.vis.is_accessible_from(tcx.parent_module(expr.hir_id), tcx))
2324 .collect();
2325
2326 if !private_fields.is_empty() {
2327 self.report_private_fields(
2328 adt_ty,
2329 path_span,
2330 expr.span,
2331 private_fields,
2332 hir_fields,
2333 );
2334 } else {
2335 self.report_missing_fields(
2336 adt_ty,
2337 path_span,
2338 expr.span,
2339 remaining_fields,
2340 variant,
2341 hir_fields,
2342 args,
2343 );
2344 }
2345 }
2346 }
2347
2348 fn check_struct_fields_on_error(
2349 &self,
2350 fields: &'tcx [hir::ExprField<'tcx>],
2351 base_expr: &'tcx hir::StructTailExpr<'tcx>,
2352 ) {
2353 for field in fields {
2354 self.check_expr(field.expr);
2355 }
2356 if let hir::StructTailExpr::Base(base) = *base_expr {
2357 self.check_expr(base);
2358 }
2359 }
2360
2361 fn report_missing_fields(
2373 &self,
2374 adt_ty: Ty<'tcx>,
2375 span: Span,
2376 full_span: Span,
2377 remaining_fields: UnordMap<Ident, (FieldIdx, &ty::FieldDef)>,
2378 variant: &'tcx ty::VariantDef,
2379 hir_fields: &'tcx [hir::ExprField<'tcx>],
2380 args: GenericArgsRef<'tcx>,
2381 ) {
2382 let len = remaining_fields.len();
2383
2384 let displayable_field_names: Vec<&str> =
2385 remaining_fields.items().map(|(ident, _)| ident.as_str()).into_sorted_stable_ord();
2386
2387 let mut truncated_fields_error = String::new();
2388 let remaining_fields_names = match &displayable_field_names[..] {
2389 [field1] => format!("`{field1}`"),
2390 [field1, field2] => format!("`{field1}` and `{field2}`"),
2391 [field1, field2, field3] => format!("`{field1}`, `{field2}` and `{field3}`"),
2392 _ => {
2393 truncated_fields_error =
2394 format!(" and {} other field{}", len - 3, pluralize!(len - 3));
2395 displayable_field_names
2396 .iter()
2397 .take(3)
2398 .map(|n| format!("`{n}`"))
2399 .collect::<Vec<_>>()
2400 .join(", ")
2401 }
2402 };
2403
2404 let mut err = struct_span_code_err!(
2405 self.dcx(),
2406 span,
2407 E0063,
2408 "missing field{} {}{} in initializer of `{}`",
2409 pluralize!(len),
2410 remaining_fields_names,
2411 truncated_fields_error,
2412 adt_ty
2413 );
2414 err.span_label(span, format!("missing {remaining_fields_names}{truncated_fields_error}"));
2415
2416 if remaining_fields.items().all(|(_, (_, field))| field.value.is_some())
2417 && self.tcx.sess.is_nightly_build()
2418 {
2419 let msg = format!(
2420 "all remaining fields have default values, {you_can} use those values with `..`",
2421 you_can = if self.tcx.features().default_field_values() {
2422 "you can"
2423 } else {
2424 "if you added `#![feature(default_field_values)]` to your crate you could"
2425 },
2426 );
2427 if let Some(hir_field) = hir_fields.last() {
2428 err.span_suggestion_verbose(
2429 hir_field.span.shrink_to_hi(),
2430 msg,
2431 ", ..".to_string(),
2432 Applicability::MachineApplicable,
2433 );
2434 } else if hir_fields.is_empty() {
2435 err.span_suggestion_verbose(
2436 span.shrink_to_hi().with_hi(full_span.hi()),
2437 msg,
2438 " { .. }".to_string(),
2439 Applicability::MachineApplicable,
2440 );
2441 }
2442 }
2443
2444 if let Some(hir_field) = hir_fields.last() {
2445 self.suggest_fru_from_range_and_emit(hir_field, variant, args, err);
2446 } else {
2447 err.emit();
2448 }
2449 }
2450
2451 fn suggest_fru_from_range_and_emit(
2454 &self,
2455 last_expr_field: &hir::ExprField<'tcx>,
2456 variant: &ty::VariantDef,
2457 args: GenericArgsRef<'tcx>,
2458 mut err: Diag<'_>,
2459 ) {
2460 if let ExprKind::Struct(QPath::LangItem(LangItem::Range, ..), [range_start, range_end], _) =
2462 last_expr_field.expr.kind
2463 && let variant_field =
2464 variant.fields.iter().find(|field| field.ident(self.tcx) == last_expr_field.ident)
2465 && let range_def_id = self.tcx.lang_items().range_struct()
2466 && variant_field
2467 .and_then(|field| field.ty(self.tcx, args).ty_adt_def())
2468 .map(|adt| adt.did())
2469 != range_def_id
2470 {
2471 let expr = self
2475 .tcx
2476 .sess
2477 .source_map()
2478 .span_to_snippet(range_end.expr.span)
2479 .ok()
2480 .filter(|s| s.len() < 25 && !s.contains(|c: char| c.is_control()));
2481
2482 let fru_span = self
2483 .tcx
2484 .sess
2485 .source_map()
2486 .span_extend_while_whitespace(range_start.span)
2487 .shrink_to_hi()
2488 .to(range_end.span);
2489
2490 err.subdiagnostic(TypeMismatchFruTypo { expr_span: range_start.span, fru_span, expr });
2491
2492 self.dcx().try_steal_replace_and_emit_err(
2494 last_expr_field.span,
2495 StashKey::MaybeFruTypo,
2496 err,
2497 );
2498 } else {
2499 err.emit();
2500 }
2501 }
2502
2503 fn report_private_fields(
2515 &self,
2516 adt_ty: Ty<'tcx>,
2517 span: Span,
2518 expr_span: Span,
2519 private_fields: Vec<&ty::FieldDef>,
2520 used_fields: &'tcx [hir::ExprField<'tcx>],
2521 ) {
2522 let mut err =
2523 self.dcx().struct_span_err(
2524 span,
2525 format!(
2526 "cannot construct `{adt_ty}` with struct literal syntax due to private fields",
2527 ),
2528 );
2529 let (used_private_fields, remaining_private_fields): (
2530 Vec<(Symbol, Span, bool)>,
2531 Vec<(Symbol, Span, bool)>,
2532 ) = private_fields
2533 .iter()
2534 .map(|field| {
2535 match used_fields.iter().find(|used_field| field.name == used_field.ident.name) {
2536 Some(used_field) => (field.name, used_field.span, true),
2537 None => (field.name, self.tcx.def_span(field.did), false),
2538 }
2539 })
2540 .partition(|field| field.2);
2541 err.span_labels(used_private_fields.iter().map(|(_, span, _)| *span), "private field");
2542 if !remaining_private_fields.is_empty() {
2543 let names = if remaining_private_fields.len() > 6 {
2544 String::new()
2545 } else {
2546 format!(
2547 "{} ",
2548 listify(&remaining_private_fields, |(name, _, _)| format!("`{name}`"))
2549 .expect("expected at least one private field to report")
2550 )
2551 };
2552 err.note(format!(
2553 "{}private field{s} {names}that {were} not provided",
2554 if used_fields.is_empty() { "" } else { "...and other " },
2555 s = pluralize!(remaining_private_fields.len()),
2556 were = pluralize!("was", remaining_private_fields.len()),
2557 ));
2558 }
2559
2560 if let ty::Adt(def, _) = adt_ty.kind() {
2561 let def_id = def.did();
2562 let mut items = self
2563 .tcx
2564 .inherent_impls(def_id)
2565 .into_iter()
2566 .flat_map(|i| self.tcx.associated_items(i).in_definition_order())
2567 .filter(|item| item.is_fn() && !item.is_method())
2569 .filter_map(|item| {
2570 let fn_sig = self.tcx.fn_sig(item.def_id).skip_binder();
2572 let ret_ty = fn_sig.output();
2573 let ret_ty = self.tcx.normalize_erasing_late_bound_regions(
2574 self.typing_env(self.param_env),
2575 ret_ty,
2576 );
2577 if !self.can_eq(self.param_env, ret_ty, adt_ty) {
2578 return None;
2579 }
2580 let input_len = fn_sig.inputs().skip_binder().len();
2581 let name = item.name();
2582 let order = !name.as_str().starts_with("new");
2583 Some((order, name, input_len))
2584 })
2585 .collect::<Vec<_>>();
2586 items.sort_by_key(|(order, _, _)| *order);
2587 let suggestion = |name, args| {
2588 format!(
2589 "::{name}({})",
2590 std::iter::repeat("_").take(args).collect::<Vec<_>>().join(", ")
2591 )
2592 };
2593 match &items[..] {
2594 [] => {}
2595 [(_, name, args)] => {
2596 err.span_suggestion_verbose(
2597 span.shrink_to_hi().with_hi(expr_span.hi()),
2598 format!("you might have meant to use the `{name}` associated function"),
2599 suggestion(name, *args),
2600 Applicability::MaybeIncorrect,
2601 );
2602 }
2603 _ => {
2604 err.span_suggestions(
2605 span.shrink_to_hi().with_hi(expr_span.hi()),
2606 "you might have meant to use an associated function to build this type",
2607 items.iter().map(|(_, name, args)| suggestion(name, *args)),
2608 Applicability::MaybeIncorrect,
2609 );
2610 }
2611 }
2612 if let Some(default_trait) = self.tcx.get_diagnostic_item(sym::Default)
2613 && self
2614 .infcx
2615 .type_implements_trait(default_trait, [adt_ty], self.param_env)
2616 .may_apply()
2617 {
2618 err.multipart_suggestion(
2619 "consider using the `Default` trait",
2620 vec![
2621 (span.shrink_to_lo(), "<".to_string()),
2622 (
2623 span.shrink_to_hi().with_hi(expr_span.hi()),
2624 " as std::default::Default>::default()".to_string(),
2625 ),
2626 ],
2627 Applicability::MaybeIncorrect,
2628 );
2629 }
2630 }
2631
2632 err.emit();
2633 }
2634
2635 fn report_unknown_field(
2636 &self,
2637 ty: Ty<'tcx>,
2638 variant: &'tcx ty::VariantDef,
2639 expr: &hir::Expr<'_>,
2640 field: &hir::ExprField<'_>,
2641 skip_fields: &[hir::ExprField<'_>],
2642 kind_name: &str,
2643 ) -> ErrorGuaranteed {
2644 if let Err(guar) = variant.has_errors() {
2646 return guar;
2647 }
2648 let mut err = self.err_ctxt().type_error_struct_with_diag(
2649 field.ident.span,
2650 |actual| match ty.kind() {
2651 ty::Adt(adt, ..) if adt.is_enum() => struct_span_code_err!(
2652 self.dcx(),
2653 field.ident.span,
2654 E0559,
2655 "{} `{}::{}` has no field named `{}`",
2656 kind_name,
2657 actual,
2658 variant.name,
2659 field.ident
2660 ),
2661 _ => struct_span_code_err!(
2662 self.dcx(),
2663 field.ident.span,
2664 E0560,
2665 "{} `{}` has no field named `{}`",
2666 kind_name,
2667 actual,
2668 field.ident
2669 ),
2670 },
2671 ty,
2672 );
2673
2674 let variant_ident_span = self.tcx.def_ident_span(variant.def_id).unwrap();
2675 match variant.ctor {
2676 Some((CtorKind::Fn, def_id)) => match ty.kind() {
2677 ty::Adt(adt, ..) if adt.is_enum() => {
2678 err.span_label(
2679 variant_ident_span,
2680 format!(
2681 "`{adt}::{variant}` defined here",
2682 adt = ty,
2683 variant = variant.name,
2684 ),
2685 );
2686 err.span_label(field.ident.span, "field does not exist");
2687 let fn_sig = self.tcx.fn_sig(def_id).instantiate_identity();
2688 let inputs = fn_sig.inputs().skip_binder();
2689 let fields = format!(
2690 "({})",
2691 inputs.iter().map(|i| format!("/* {i} */")).collect::<Vec<_>>().join(", ")
2692 );
2693 let (replace_span, sugg) = match expr.kind {
2694 hir::ExprKind::Struct(qpath, ..) => {
2695 (qpath.span().shrink_to_hi().with_hi(expr.span.hi()), fields)
2696 }
2697 _ => {
2698 (expr.span, format!("{ty}::{variant}{fields}", variant = variant.name))
2699 }
2700 };
2701 err.span_suggestion_verbose(
2702 replace_span,
2703 format!(
2704 "`{adt}::{variant}` is a tuple {kind_name}, use the appropriate syntax",
2705 adt = ty,
2706 variant = variant.name,
2707 ),
2708 sugg,
2709 Applicability::HasPlaceholders,
2710 );
2711 }
2712 _ => {
2713 err.span_label(variant_ident_span, format!("`{ty}` defined here"));
2714 err.span_label(field.ident.span, "field does not exist");
2715 let fn_sig = self.tcx.fn_sig(def_id).instantiate_identity();
2716 let inputs = fn_sig.inputs().skip_binder();
2717 let fields = format!(
2718 "({})",
2719 inputs.iter().map(|i| format!("/* {i} */")).collect::<Vec<_>>().join(", ")
2720 );
2721 err.span_suggestion_verbose(
2722 expr.span,
2723 format!("`{ty}` is a tuple {kind_name}, use the appropriate syntax",),
2724 format!("{ty}{fields}"),
2725 Applicability::HasPlaceholders,
2726 );
2727 }
2728 },
2729 _ => {
2730 let available_field_names = self.available_field_names(variant, expr, skip_fields);
2732 if let Some(field_name) =
2733 find_best_match_for_name(&available_field_names, field.ident.name, None)
2734 {
2735 err.span_label(field.ident.span, "unknown field");
2736 err.span_suggestion_verbose(
2737 field.ident.span,
2738 "a field with a similar name exists",
2739 field_name,
2740 Applicability::MaybeIncorrect,
2741 );
2742 } else {
2743 match ty.kind() {
2744 ty::Adt(adt, ..) => {
2745 if adt.is_enum() {
2746 err.span_label(
2747 field.ident.span,
2748 format!("`{}::{}` does not have this field", ty, variant.name),
2749 );
2750 } else {
2751 err.span_label(
2752 field.ident.span,
2753 format!("`{ty}` does not have this field"),
2754 );
2755 }
2756 if available_field_names.is_empty() {
2757 err.note("all struct fields are already assigned");
2758 } else {
2759 err.note(format!(
2760 "available fields are: {}",
2761 self.name_series_display(available_field_names)
2762 ));
2763 }
2764 }
2765 _ => bug!("non-ADT passed to report_unknown_field"),
2766 }
2767 };
2768 }
2769 }
2770 err.emit()
2771 }
2772
2773 fn available_field_names(
2774 &self,
2775 variant: &'tcx ty::VariantDef,
2776 expr: &hir::Expr<'_>,
2777 skip_fields: &[hir::ExprField<'_>],
2778 ) -> Vec<Symbol> {
2779 variant
2780 .fields
2781 .iter()
2782 .filter(|field| {
2783 skip_fields.iter().all(|&skip| skip.ident.name != field.name)
2784 && self.is_field_suggestable(field, expr.hir_id, expr.span)
2785 })
2786 .map(|field| field.name)
2787 .collect()
2788 }
2789
2790 fn name_series_display(&self, names: Vec<Symbol>) -> String {
2791 let limit = if names.len() == 6 { 6 } else { 5 };
2793 let mut display =
2794 names.iter().take(limit).map(|n| format!("`{n}`")).collect::<Vec<_>>().join(", ");
2795 if names.len() > limit {
2796 display = format!("{} ... and {} others", display, names.len() - limit);
2797 }
2798 display
2799 }
2800
2801 fn find_adt_field(
2805 &self,
2806 base_def: ty::AdtDef<'tcx>,
2807 ident: Ident,
2808 ) -> Option<(FieldIdx, &'tcx ty::FieldDef)> {
2809 if base_def.is_enum() {
2811 return None;
2812 }
2813
2814 for (field_idx, field) in base_def.non_enum_variant().fields.iter_enumerated() {
2815 if field.ident(self.tcx).normalize_to_macros_2_0() == ident {
2816 return Some((field_idx, field));
2818 }
2819 }
2820
2821 None
2822 }
2823
2824 fn check_expr_field(
2834 &self,
2835 expr: &'tcx hir::Expr<'tcx>,
2836 base: &'tcx hir::Expr<'tcx>,
2837 field: Ident,
2838 expected: Expectation<'tcx>,
2840 ) -> Ty<'tcx> {
2841 debug!("check_field(expr: {:?}, base: {:?}, field: {:?})", expr, base, field);
2842 let base_ty = self.check_expr(base);
2843 let base_ty = self.structurally_resolve_type(base.span, base_ty);
2844
2845 let mut private_candidate = None;
2847
2848 let mut autoderef = self.autoderef(expr.span, base_ty);
2850 while let Some((deref_base_ty, _)) = autoderef.next() {
2851 debug!("deref_base_ty: {:?}", deref_base_ty);
2852 match deref_base_ty.kind() {
2853 ty::Adt(base_def, args) if !base_def.is_enum() => {
2854 debug!("struct named {:?}", deref_base_ty);
2855 if let Err(guar) = base_def.non_enum_variant().has_errors() {
2857 return Ty::new_error(self.tcx(), guar);
2858 }
2859
2860 let fn_body_hir_id = self.tcx.local_def_id_to_hir_id(self.body_id);
2861 let (ident, def_scope) =
2862 self.tcx.adjust_ident_and_get_scope(field, base_def.did(), fn_body_hir_id);
2863
2864 if let Some((idx, field)) = self.find_adt_field(*base_def, ident) {
2865 self.write_field_index(expr.hir_id, idx);
2866
2867 let adjustments = self.adjust_steps(&autoderef);
2868 if field.vis.is_accessible_from(def_scope, self.tcx) {
2869 self.apply_adjustments(base, adjustments);
2870 self.register_predicates(autoderef.into_obligations());
2871
2872 self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None);
2873 return self.field_ty(expr.span, field, args);
2874 }
2875
2876 private_candidate = Some((adjustments, base_def.did()));
2878 }
2879 }
2880 ty::Tuple(tys) => {
2881 if let Ok(index) = field.as_str().parse::<usize>() {
2882 if field.name == sym::integer(index) {
2883 if let Some(&field_ty) = tys.get(index) {
2884 let adjustments = self.adjust_steps(&autoderef);
2885 self.apply_adjustments(base, adjustments);
2886 self.register_predicates(autoderef.into_obligations());
2887
2888 self.write_field_index(expr.hir_id, FieldIdx::from_usize(index));
2889 return field_ty;
2890 }
2891 }
2892 }
2893 }
2894 _ => {}
2895 }
2896 }
2897 let final_ty = self.structurally_resolve_type(autoderef.span(), autoderef.final_ty(false));
2903 if let ty::Error(_) = final_ty.kind() {
2904 return final_ty;
2905 }
2906
2907 if let Some((adjustments, did)) = private_candidate {
2908 self.apply_adjustments(base, adjustments);
2911 let guar = self.ban_private_field_access(
2912 expr,
2913 base_ty,
2914 field,
2915 did,
2916 expected.only_has_type(self),
2917 );
2918 return Ty::new_error(self.tcx(), guar);
2919 }
2920
2921 let guar = if self.method_exists_for_diagnostic(
2922 field,
2923 base_ty,
2924 expr.hir_id,
2925 expected.only_has_type(self),
2926 ) {
2927 self.ban_take_value_of_method(expr, base_ty, field)
2929 } else if !base_ty.is_primitive_ty() {
2930 self.ban_nonexisting_field(field, base, expr, base_ty)
2931 } else {
2932 let field_name = field.to_string();
2933 let mut err = type_error_struct!(
2934 self.dcx(),
2935 field.span,
2936 base_ty,
2937 E0610,
2938 "`{base_ty}` is a primitive type and therefore doesn't have fields",
2939 );
2940 let is_valid_suffix = |field: &str| {
2941 if field == "f32" || field == "f64" {
2942 return true;
2943 }
2944 let mut chars = field.chars().peekable();
2945 match chars.peek() {
2946 Some('e') | Some('E') => {
2947 chars.next();
2948 if let Some(c) = chars.peek()
2949 && !c.is_numeric()
2950 && *c != '-'
2951 && *c != '+'
2952 {
2953 return false;
2954 }
2955 while let Some(c) = chars.peek() {
2956 if !c.is_numeric() {
2957 break;
2958 }
2959 chars.next();
2960 }
2961 }
2962 _ => (),
2963 }
2964 let suffix = chars.collect::<String>();
2965 suffix.is_empty() || suffix == "f32" || suffix == "f64"
2966 };
2967 let maybe_partial_suffix = |field: &str| -> Option<&str> {
2968 let first_chars = ['f', 'l'];
2969 if field.len() >= 1
2970 && field.to_lowercase().starts_with(first_chars)
2971 && field[1..].chars().all(|c| c.is_ascii_digit())
2972 {
2973 if field.to_lowercase().starts_with(['f']) { Some("f32") } else { Some("f64") }
2974 } else {
2975 None
2976 }
2977 };
2978 if let ty::Infer(ty::IntVar(_)) = base_ty.kind()
2979 && let ExprKind::Lit(Spanned {
2980 node: ast::LitKind::Int(_, ast::LitIntType::Unsuffixed),
2981 ..
2982 }) = base.kind
2983 && !base.span.from_expansion()
2984 {
2985 if is_valid_suffix(&field_name) {
2986 err.span_suggestion_verbose(
2987 field.span.shrink_to_lo(),
2988 "if intended to be a floating point literal, consider adding a `0` after the period",
2989 '0',
2990 Applicability::MaybeIncorrect,
2991 );
2992 } else if let Some(correct_suffix) = maybe_partial_suffix(&field_name) {
2993 err.span_suggestion_verbose(
2994 field.span,
2995 format!("if intended to be a floating point literal, consider adding a `0` after the period and a `{correct_suffix}` suffix"),
2996 format!("0{correct_suffix}"),
2997 Applicability::MaybeIncorrect,
2998 );
2999 }
3000 }
3001 err.emit()
3002 };
3003
3004 Ty::new_error(self.tcx(), guar)
3005 }
3006
3007 fn suggest_await_on_field_access(
3008 &self,
3009 err: &mut Diag<'_>,
3010 field_ident: Ident,
3011 base: &'tcx hir::Expr<'tcx>,
3012 ty: Ty<'tcx>,
3013 ) {
3014 let Some(output_ty) = self.err_ctxt().get_impl_future_output_ty(ty) else {
3015 err.span_label(field_ident.span, "unknown field");
3016 return;
3017 };
3018 let ty::Adt(def, _) = output_ty.kind() else {
3019 err.span_label(field_ident.span, "unknown field");
3020 return;
3021 };
3022 if def.is_enum() {
3024 err.span_label(field_ident.span, "unknown field");
3025 return;
3026 }
3027 if !def.non_enum_variant().fields.iter().any(|field| field.ident(self.tcx) == field_ident) {
3028 err.span_label(field_ident.span, "unknown field");
3029 return;
3030 }
3031 err.span_label(
3032 field_ident.span,
3033 "field not available in `impl Future`, but it is available in its `Output`",
3034 );
3035 match self.tcx.coroutine_kind(self.body_id) {
3036 Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) => {
3037 err.span_suggestion_verbose(
3038 base.span.shrink_to_hi(),
3039 "consider `await`ing on the `Future` to access the field",
3040 ".await",
3041 Applicability::MaybeIncorrect,
3042 );
3043 }
3044 _ => {
3045 let mut span: MultiSpan = base.span.into();
3046 span.push_span_label(self.tcx.def_span(self.body_id), "this is not `async`");
3047 err.span_note(
3048 span,
3049 "this implements `Future` and its output type has the field, \
3050 but the future cannot be awaited in a synchronous function",
3051 );
3052 }
3053 }
3054 }
3055
3056 fn ban_nonexisting_field(
3057 &self,
3058 ident: Ident,
3059 base: &'tcx hir::Expr<'tcx>,
3060 expr: &'tcx hir::Expr<'tcx>,
3061 base_ty: Ty<'tcx>,
3062 ) -> ErrorGuaranteed {
3063 debug!(
3064 "ban_nonexisting_field: field={:?}, base={:?}, expr={:?}, base_ty={:?}",
3065 ident, base, expr, base_ty
3066 );
3067 let mut err = self.no_such_field_err(ident, base_ty, expr);
3068
3069 match *base_ty.peel_refs().kind() {
3070 ty::Array(_, len) => {
3071 self.maybe_suggest_array_indexing(&mut err, base, ident, len);
3072 }
3073 ty::RawPtr(..) => {
3074 self.suggest_first_deref_field(&mut err, base, ident);
3075 }
3076 ty::Param(param_ty) => {
3077 err.span_label(ident.span, "unknown field");
3078 self.point_at_param_definition(&mut err, param_ty);
3079 }
3080 ty::Alias(ty::Opaque, _) => {
3081 self.suggest_await_on_field_access(&mut err, ident, base, base_ty.peel_refs());
3082 }
3083 _ => {
3084 err.span_label(ident.span, "unknown field");
3085 }
3086 }
3087
3088 self.suggest_fn_call(&mut err, base, base_ty, |output_ty| {
3089 if let ty::Adt(def, _) = output_ty.kind()
3090 && !def.is_enum()
3091 {
3092 def.non_enum_variant().fields.iter().any(|field| {
3093 field.ident(self.tcx) == ident
3094 && field.vis.is_accessible_from(expr.hir_id.owner.def_id, self.tcx)
3095 })
3096 } else if let ty::Tuple(tys) = output_ty.kind()
3097 && let Ok(idx) = ident.as_str().parse::<usize>()
3098 {
3099 idx < tys.len()
3100 } else {
3101 false
3102 }
3103 });
3104
3105 if ident.name == kw::Await {
3106 err.note("to `.await` a `Future`, switch to Rust 2018 or later");
3109 HelpUseLatestEdition::new().add_to_diag(&mut err);
3110 }
3111
3112 err.emit()
3113 }
3114
3115 fn ban_private_field_access(
3116 &self,
3117 expr: &hir::Expr<'tcx>,
3118 expr_t: Ty<'tcx>,
3119 field: Ident,
3120 base_did: DefId,
3121 return_ty: Option<Ty<'tcx>>,
3122 ) -> ErrorGuaranteed {
3123 let mut err = self.private_field_err(field, base_did);
3124
3125 if self.method_exists_for_diagnostic(field, expr_t, expr.hir_id, return_ty)
3127 && !self.expr_in_place(expr.hir_id)
3128 {
3129 self.suggest_method_call(
3130 &mut err,
3131 format!("a method `{field}` also exists, call it with parentheses"),
3132 field,
3133 expr_t,
3134 expr,
3135 None,
3136 );
3137 }
3138 err.emit()
3139 }
3140
3141 fn ban_take_value_of_method(
3142 &self,
3143 expr: &hir::Expr<'tcx>,
3144 expr_t: Ty<'tcx>,
3145 field: Ident,
3146 ) -> ErrorGuaranteed {
3147 let mut err = type_error_struct!(
3148 self.dcx(),
3149 field.span,
3150 expr_t,
3151 E0615,
3152 "attempted to take value of method `{field}` on type `{expr_t}`",
3153 );
3154 err.span_label(field.span, "method, not a field");
3155 let expr_is_call =
3156 if let hir::Node::Expr(hir::Expr { kind: ExprKind::Call(callee, _args), .. }) =
3157 self.tcx.parent_hir_node(expr.hir_id)
3158 {
3159 expr.hir_id == callee.hir_id
3160 } else {
3161 false
3162 };
3163 let expr_snippet =
3164 self.tcx.sess.source_map().span_to_snippet(expr.span).unwrap_or_default();
3165 let is_wrapped = expr_snippet.starts_with('(') && expr_snippet.ends_with(')');
3166 let after_open = expr.span.lo() + rustc_span::BytePos(1);
3167 let before_close = expr.span.hi() - rustc_span::BytePos(1);
3168
3169 if expr_is_call && is_wrapped {
3170 err.multipart_suggestion(
3171 "remove wrapping parentheses to call the method",
3172 vec![
3173 (expr.span.with_hi(after_open), String::new()),
3174 (expr.span.with_lo(before_close), String::new()),
3175 ],
3176 Applicability::MachineApplicable,
3177 );
3178 } else if !self.expr_in_place(expr.hir_id) {
3179 let span = if is_wrapped {
3181 expr.span.with_lo(after_open).with_hi(before_close)
3182 } else {
3183 expr.span
3184 };
3185 self.suggest_method_call(
3186 &mut err,
3187 "use parentheses to call the method",
3188 field,
3189 expr_t,
3190 expr,
3191 Some(span),
3192 );
3193 } else if let ty::RawPtr(ptr_ty, _) = expr_t.kind()
3194 && let ty::Adt(adt_def, _) = ptr_ty.kind()
3195 && let ExprKind::Field(base_expr, _) = expr.kind
3196 && let [variant] = &adt_def.variants().raw
3197 && variant.fields.iter().any(|f| f.ident(self.tcx) == field)
3198 {
3199 err.multipart_suggestion(
3200 "to access the field, dereference first",
3201 vec![
3202 (base_expr.span.shrink_to_lo(), "(*".to_string()),
3203 (base_expr.span.shrink_to_hi(), ")".to_string()),
3204 ],
3205 Applicability::MaybeIncorrect,
3206 );
3207 } else {
3208 err.help("methods are immutable and cannot be assigned to");
3209 }
3210
3211 self.dcx().try_steal_replace_and_emit_err(field.span, StashKey::GenericInFieldExpr, err)
3213 }
3214
3215 fn point_at_param_definition(&self, err: &mut Diag<'_>, param: ty::ParamTy) {
3216 let generics = self.tcx.generics_of(self.body_id);
3217 let generic_param = generics.type_param(param, self.tcx);
3218 if let ty::GenericParamDefKind::Type { synthetic: true, .. } = generic_param.kind {
3219 return;
3220 }
3221 let param_def_id = generic_param.def_id;
3222 let param_hir_id = match param_def_id.as_local() {
3223 Some(x) => self.tcx.local_def_id_to_hir_id(x),
3224 None => return,
3225 };
3226 let param_span = self.tcx.hir_span(param_hir_id);
3227 let param_name = self.tcx.hir_ty_param_name(param_def_id.expect_local());
3228
3229 err.span_label(param_span, format!("type parameter '{param_name}' declared here"));
3230 }
3231
3232 fn maybe_suggest_array_indexing(
3233 &self,
3234 err: &mut Diag<'_>,
3235 base: &hir::Expr<'_>,
3236 field: Ident,
3237 len: ty::Const<'tcx>,
3238 ) {
3239 err.span_label(field.span, "unknown field");
3240 if let (Some(len), Ok(user_index)) = (
3241 self.try_structurally_resolve_const(base.span, len).try_to_target_usize(self.tcx),
3242 field.as_str().parse::<u64>(),
3243 ) {
3244 let help = "instead of using tuple indexing, use array indexing";
3245 let applicability = if len < user_index {
3246 Applicability::MachineApplicable
3247 } else {
3248 Applicability::MaybeIncorrect
3249 };
3250 err.multipart_suggestion(
3251 help,
3252 vec![
3253 (base.span.between(field.span), "[".to_string()),
3254 (field.span.shrink_to_hi(), "]".to_string()),
3255 ],
3256 applicability,
3257 );
3258 }
3259 }
3260
3261 fn suggest_first_deref_field(&self, err: &mut Diag<'_>, base: &hir::Expr<'_>, field: Ident) {
3262 err.span_label(field.span, "unknown field");
3263 let val = if let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span)
3264 && base.len() < 20
3265 {
3266 format!("`{base}`")
3267 } else {
3268 "the value".to_string()
3269 };
3270 err.multipart_suggestion(
3271 format!("{val} is a raw pointer; try dereferencing it"),
3272 vec![
3273 (base.span.shrink_to_lo(), "(*".into()),
3274 (base.span.between(field.span), format!(").")),
3275 ],
3276 Applicability::MaybeIncorrect,
3277 );
3278 }
3279
3280 fn no_such_field_err(
3281 &self,
3282 field: Ident,
3283 base_ty: Ty<'tcx>,
3284 expr: &hir::Expr<'tcx>,
3285 ) -> Diag<'_> {
3286 let span = field.span;
3287 debug!("no_such_field_err(span: {:?}, field: {:?}, expr_t: {:?})", span, field, base_ty);
3288
3289 let mut err = self.dcx().create_err(NoFieldOnType { span, ty: base_ty, field });
3290 if base_ty.references_error() {
3291 err.downgrade_to_delayed_bug();
3292 }
3293
3294 if let Some(within_macro_span) = span.within_macro(expr.span, self.tcx.sess.source_map()) {
3295 err.span_label(within_macro_span, "due to this macro variable");
3296 }
3297
3298 let mod_id = self.tcx.parent_module(expr.hir_id).to_def_id();
3300 let (ty, unwrap) = if let ty::Adt(def, args) = base_ty.kind()
3301 && (self.tcx.is_diagnostic_item(sym::Result, def.did())
3302 || self.tcx.is_diagnostic_item(sym::Option, def.did()))
3303 && let Some(arg) = args.get(0)
3304 && let Some(ty) = arg.as_type()
3305 {
3306 (ty, "unwrap().")
3307 } else {
3308 (base_ty, "")
3309 };
3310 for (found_fields, args) in
3311 self.get_field_candidates_considering_privacy_for_diag(span, ty, mod_id, expr.hir_id)
3312 {
3313 let field_names = found_fields.iter().map(|field| field.name).collect::<Vec<_>>();
3314 let mut candidate_fields: Vec<_> = found_fields
3315 .into_iter()
3316 .filter_map(|candidate_field| {
3317 self.check_for_nested_field_satisfying_condition_for_diag(
3318 span,
3319 &|candidate_field, _| candidate_field.ident(self.tcx()) == field,
3320 candidate_field,
3321 args,
3322 vec![],
3323 mod_id,
3324 expr.hir_id,
3325 )
3326 })
3327 .map(|mut field_path| {
3328 field_path.pop();
3329 field_path.iter().map(|id| format!("{}.", id)).collect::<String>()
3330 })
3331 .collect::<Vec<_>>();
3332 candidate_fields.sort();
3333
3334 let len = candidate_fields.len();
3335 if len > 0 && expr.span.eq_ctxt(field.span) {
3338 err.span_suggestions(
3339 field.span.shrink_to_lo(),
3340 format!(
3341 "{} of the expressions' fields {} a field of the same name",
3342 if len > 1 { "some" } else { "one" },
3343 if len > 1 { "have" } else { "has" },
3344 ),
3345 candidate_fields.iter().map(|path| format!("{unwrap}{path}")),
3346 Applicability::MaybeIncorrect,
3347 );
3348 } else if let Some(field_name) =
3349 find_best_match_for_name(&field_names, field.name, None)
3350 {
3351 err.span_suggestion_verbose(
3352 field.span,
3353 "a field with a similar name exists",
3354 format!("{unwrap}{}", field_name),
3355 Applicability::MaybeIncorrect,
3356 );
3357 } else if !field_names.is_empty() {
3358 let is = if field_names.len() == 1 { " is" } else { "s are" };
3359 err.note(
3360 format!("available field{is}: {}", self.name_series_display(field_names),),
3361 );
3362 }
3363 }
3364 err
3365 }
3366
3367 fn private_field_err(&self, field: Ident, base_did: DefId) -> Diag<'_> {
3368 let struct_path = self.tcx().def_path_str(base_did);
3369 let kind_name = self.tcx().def_descr(base_did);
3370 struct_span_code_err!(
3371 self.dcx(),
3372 field.span,
3373 E0616,
3374 "field `{field}` of {kind_name} `{struct_path}` is private",
3375 )
3376 .with_span_label(field.span, "private field")
3377 }
3378
3379 pub(crate) fn get_field_candidates_considering_privacy_for_diag(
3380 &self,
3381 span: Span,
3382 base_ty: Ty<'tcx>,
3383 mod_id: DefId,
3384 hir_id: HirId,
3385 ) -> Vec<(Vec<&'tcx ty::FieldDef>, GenericArgsRef<'tcx>)> {
3386 debug!("get_field_candidates(span: {:?}, base_t: {:?}", span, base_ty);
3387
3388 let mut autoderef = self.autoderef(span, base_ty).silence_errors();
3389 let deref_chain: Vec<_> = autoderef.by_ref().collect();
3390
3391 if autoderef.reached_recursion_limit() {
3395 return vec![];
3396 }
3397
3398 deref_chain
3399 .into_iter()
3400 .filter_map(move |(base_t, _)| {
3401 match base_t.kind() {
3402 ty::Adt(base_def, args) if !base_def.is_enum() => {
3403 let tcx = self.tcx;
3404 let fields = &base_def.non_enum_variant().fields;
3405 if fields.iter().all(|field| !field.vis.is_accessible_from(mod_id, tcx)) {
3409 return None;
3410 }
3411 return Some((
3412 fields
3413 .iter()
3414 .filter(move |field| {
3415 field.vis.is_accessible_from(mod_id, tcx)
3416 && self.is_field_suggestable(field, hir_id, span)
3417 })
3418 .take(100)
3420 .collect::<Vec<_>>(),
3421 *args,
3422 ));
3423 }
3424 _ => None,
3425 }
3426 })
3427 .collect()
3428 }
3429
3430 pub(crate) fn check_for_nested_field_satisfying_condition_for_diag(
3433 &self,
3434 span: Span,
3435 matches: &impl Fn(&ty::FieldDef, Ty<'tcx>) -> bool,
3436 candidate_field: &ty::FieldDef,
3437 subst: GenericArgsRef<'tcx>,
3438 mut field_path: Vec<Ident>,
3439 mod_id: DefId,
3440 hir_id: HirId,
3441 ) -> Option<Vec<Ident>> {
3442 debug!(
3443 "check_for_nested_field_satisfying(span: {:?}, candidate_field: {:?}, field_path: {:?}",
3444 span, candidate_field, field_path
3445 );
3446
3447 if field_path.len() > 3 {
3448 None
3451 } else {
3452 field_path.push(candidate_field.ident(self.tcx).normalize_to_macros_2_0());
3453 let field_ty = candidate_field.ty(self.tcx, subst);
3454 if matches(candidate_field, field_ty) {
3455 return Some(field_path);
3456 } else {
3457 for (nested_fields, subst) in self
3458 .get_field_candidates_considering_privacy_for_diag(
3459 span, field_ty, mod_id, hir_id,
3460 )
3461 {
3462 for field in nested_fields {
3464 if let Some(field_path) = self
3465 .check_for_nested_field_satisfying_condition_for_diag(
3466 span,
3467 matches,
3468 field,
3469 subst,
3470 field_path.clone(),
3471 mod_id,
3472 hir_id,
3473 )
3474 {
3475 return Some(field_path);
3476 }
3477 }
3478 }
3479 }
3480 None
3481 }
3482 }
3483
3484 fn check_expr_index(
3485 &self,
3486 base: &'tcx hir::Expr<'tcx>,
3487 idx: &'tcx hir::Expr<'tcx>,
3488 expr: &'tcx hir::Expr<'tcx>,
3489 brackets_span: Span,
3490 ) -> Ty<'tcx> {
3491 let base_t = self.check_expr(base);
3492 let idx_t = self.check_expr(idx);
3493
3494 if base_t.references_error() {
3495 base_t
3496 } else if idx_t.references_error() {
3497 idx_t
3498 } else {
3499 let base_t = self.structurally_resolve_type(base.span, base_t);
3500 match self.lookup_indexing(expr, base, base_t, idx, idx_t) {
3501 Some((index_ty, element_ty)) => {
3502 self.demand_coerce(idx, idx_t, index_ty, None, AllowTwoPhase::No);
3504 self.select_obligations_where_possible(|errors| {
3505 self.point_at_index(errors, idx.span);
3506 });
3507 element_ty
3508 }
3509 None => {
3510 for (base_t, _) in self.autoderef(base.span, base_t).silence_errors() {
3513 if let Some((_, index_ty, element_ty)) =
3514 self.find_and_report_unsatisfied_index_impl(base, base_t)
3515 {
3516 self.demand_coerce(idx, idx_t, index_ty, None, AllowTwoPhase::No);
3517 return element_ty;
3518 }
3519 }
3520
3521 let mut err = type_error_struct!(
3522 self.dcx(),
3523 brackets_span,
3524 base_t,
3525 E0608,
3526 "cannot index into a value of type `{base_t}`",
3527 );
3528 if let ty::Tuple(types) = base_t.kind() {
3530 let mut needs_note = true;
3531 if let ExprKind::Lit(lit) = idx.kind
3534 && let ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) = lit.node
3535 && i.get()
3536 < types
3537 .len()
3538 .try_into()
3539 .expect("expected tuple index to be < usize length")
3540 {
3541 err.span_suggestion(
3542 brackets_span,
3543 "to access tuple elements, use",
3544 format!(".{i}"),
3545 Applicability::MachineApplicable,
3546 );
3547 needs_note = false;
3548 } else if let ExprKind::Path(..) = idx.peel_borrows().kind {
3549 err.span_label(
3550 idx.span,
3551 "cannot access tuple elements at a variable index",
3552 );
3553 }
3554 if needs_note {
3555 err.help(
3556 "to access tuple elements, use tuple indexing \
3557 syntax (e.g., `tuple.0`)",
3558 );
3559 }
3560 }
3561
3562 if base_t.is_raw_ptr() && idx_t.is_integral() {
3563 err.multipart_suggestion(
3564 "consider using `wrapping_add` or `add` for indexing into raw pointer",
3565 vec![
3566 (base.span.between(idx.span), ".wrapping_add(".to_owned()),
3567 (
3568 idx.span.shrink_to_hi().until(expr.span.shrink_to_hi()),
3569 ")".to_owned(),
3570 ),
3571 ],
3572 Applicability::MaybeIncorrect,
3573 );
3574 }
3575
3576 let reported = err.emit();
3577 Ty::new_error(self.tcx, reported)
3578 }
3579 }
3580 }
3581 }
3582
3583 fn find_and_report_unsatisfied_index_impl(
3591 &self,
3592 base_expr: &hir::Expr<'_>,
3593 base_ty: Ty<'tcx>,
3594 ) -> Option<(ErrorGuaranteed, Ty<'tcx>, Ty<'tcx>)> {
3595 let index_trait_def_id = self.tcx.lang_items().index_trait()?;
3596 let index_trait_output_def_id = self.tcx.get_diagnostic_item(sym::IndexOutput)?;
3597
3598 let mut relevant_impls = vec![];
3599 self.tcx.for_each_relevant_impl(index_trait_def_id, base_ty, |impl_def_id| {
3600 relevant_impls.push(impl_def_id);
3601 });
3602 let [impl_def_id] = relevant_impls[..] else {
3603 return None;
3605 };
3606
3607 self.commit_if_ok(|snapshot| {
3608 let outer_universe = self.universe();
3609
3610 let ocx = ObligationCtxt::new_with_diagnostics(self);
3611 let impl_args = self.fresh_args_for_item(base_expr.span, impl_def_id);
3612 let impl_trait_ref =
3613 self.tcx.impl_trait_ref(impl_def_id).unwrap().instantiate(self.tcx, impl_args);
3614 let cause = self.misc(base_expr.span);
3615
3616 let impl_trait_ref = ocx.normalize(&cause, self.param_env, impl_trait_ref);
3619 ocx.eq(&cause, self.param_env, base_ty, impl_trait_ref.self_ty())?;
3620
3621 ocx.register_obligations(traits::predicates_for_generics(
3625 |idx, span| {
3626 cause.clone().derived_cause(
3627 ty::Binder::dummy(ty::TraitPredicate {
3628 trait_ref: impl_trait_ref,
3629 polarity: ty::PredicatePolarity::Positive,
3630 }),
3631 |derived| {
3632 ObligationCauseCode::ImplDerived(Box::new(traits::ImplDerivedCause {
3633 derived,
3634 impl_or_alias_def_id: impl_def_id,
3635 impl_def_predicate_index: Some(idx),
3636 span,
3637 }))
3638 },
3639 )
3640 },
3641 self.param_env,
3642 self.tcx.predicates_of(impl_def_id).instantiate(self.tcx, impl_args),
3643 ));
3644
3645 let element_ty = ocx.normalize(
3648 &cause,
3649 self.param_env,
3650 Ty::new_projection_from_args(
3651 self.tcx,
3652 index_trait_output_def_id,
3653 impl_trait_ref.args,
3654 ),
3655 );
3656
3657 let true_errors = ocx.select_where_possible();
3658
3659 self.leak_check(outer_universe, Some(snapshot))?;
3663
3664 let ambiguity_errors = ocx.select_all_or_error();
3666 if true_errors.is_empty() && !ambiguity_errors.is_empty() {
3667 return Err(NoSolution);
3668 }
3669
3670 Ok::<_, NoSolution>((
3673 self.err_ctxt().report_fulfillment_errors(true_errors),
3674 impl_trait_ref.args.type_at(1),
3675 element_ty,
3676 ))
3677 })
3678 .ok()
3679 }
3680
3681 fn point_at_index(&self, errors: &mut Vec<traits::FulfillmentError<'tcx>>, span: Span) {
3682 let mut seen_preds = FxHashSet::default();
3683 errors.sort_by_key(|error| error.root_obligation.recursion_depth);
3687 for error in errors {
3688 match (
3689 error.root_obligation.predicate.kind().skip_binder(),
3690 error.obligation.predicate.kind().skip_binder(),
3691 ) {
3692 (ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)), _)
3693 if self.tcx.is_lang_item(predicate.trait_ref.def_id, LangItem::Index) =>
3694 {
3695 seen_preds.insert(error.obligation.predicate.kind().skip_binder());
3696 }
3697 (_, ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)))
3698 if self.tcx.is_diagnostic_item(sym::SliceIndex, predicate.trait_ref.def_id) =>
3699 {
3700 seen_preds.insert(error.obligation.predicate.kind().skip_binder());
3701 }
3702 (root, pred) if seen_preds.contains(&pred) || seen_preds.contains(&root) => {}
3703 _ => continue,
3704 }
3705 error.obligation.cause.span = span;
3706 }
3707 }
3708
3709 fn check_expr_yield(
3710 &self,
3711 value: &'tcx hir::Expr<'tcx>,
3712 expr: &'tcx hir::Expr<'tcx>,
3713 ) -> Ty<'tcx> {
3714 match self.coroutine_types {
3715 Some(CoroutineTypes { resume_ty, yield_ty }) => {
3716 self.check_expr_coercible_to_type(value, yield_ty, None);
3717
3718 resume_ty
3719 }
3720 _ => {
3721 self.dcx().emit_err(YieldExprOutsideOfCoroutine { span: expr.span });
3722 self.check_expr(value);
3724 self.tcx.types.unit
3725 }
3726 }
3727 }
3728
3729 fn check_expr_asm_operand(&self, expr: &'tcx hir::Expr<'tcx>, is_input: bool) {
3730 let needs = if is_input { Needs::None } else { Needs::MutPlace };
3731 let ty = self.check_expr_with_needs(expr, needs);
3732 self.require_type_is_sized(ty, expr.span, ObligationCauseCode::InlineAsmSized);
3733
3734 if !is_input && !expr.is_syntactic_place_expr() {
3735 self.dcx()
3736 .struct_span_err(expr.span, "invalid asm output")
3737 .with_span_label(expr.span, "cannot assign to this expression")
3738 .emit();
3739 }
3740
3741 if is_input {
3749 let ty = self.structurally_resolve_type(expr.span, ty);
3750 match *ty.kind() {
3751 ty::FnDef(..) => {
3752 let fnptr_ty = Ty::new_fn_ptr(self.tcx, ty.fn_sig(self.tcx));
3753 self.demand_coerce(expr, ty, fnptr_ty, None, AllowTwoPhase::No);
3754 }
3755 ty::Ref(_, base_ty, mutbl) => {
3756 let ptr_ty = Ty::new_ptr(self.tcx, base_ty, mutbl);
3757 self.demand_coerce(expr, ty, ptr_ty, None, AllowTwoPhase::No);
3758 }
3759 _ => {}
3760 }
3761 }
3762 }
3763
3764 fn check_expr_asm(&self, asm: &'tcx hir::InlineAsm<'tcx>) -> Ty<'tcx> {
3765 let mut diverge = asm.asm_macro.diverges(asm.options);
3766
3767 for (op, _op_sp) in asm.operands {
3768 match *op {
3769 hir::InlineAsmOperand::In { expr, .. } => {
3770 self.check_expr_asm_operand(expr, true);
3771 }
3772 hir::InlineAsmOperand::Out { expr: Some(expr), .. }
3773 | hir::InlineAsmOperand::InOut { expr, .. } => {
3774 self.check_expr_asm_operand(expr, false);
3775 }
3776 hir::InlineAsmOperand::Out { expr: None, .. } => {}
3777 hir::InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
3778 self.check_expr_asm_operand(in_expr, true);
3779 if let Some(out_expr) = out_expr {
3780 self.check_expr_asm_operand(out_expr, false);
3781 }
3782 }
3783 hir::InlineAsmOperand::Const { ref anon_const } => {
3784 self.check_expr_const_block(anon_const, Expectation::NoExpectation);
3785 }
3786 hir::InlineAsmOperand::SymFn { expr } => {
3787 self.check_expr(expr);
3788 }
3789 hir::InlineAsmOperand::SymStatic { .. } => {}
3790 hir::InlineAsmOperand::Label { block } => {
3791 let previous_diverges = self.diverges.get();
3792
3793 let ty = self.check_expr_block(block, ExpectHasType(self.tcx.types.unit));
3795 if !ty.is_never() {
3796 self.demand_suptype(block.span, self.tcx.types.unit, ty);
3797 diverge = false;
3798 }
3799
3800 self.diverges.set(previous_diverges);
3802 }
3803 }
3804 }
3805
3806 if diverge { self.tcx.types.never } else { self.tcx.types.unit }
3807 }
3808
3809 fn check_expr_offset_of(
3810 &self,
3811 container: &'tcx hir::Ty<'tcx>,
3812 fields: &[Ident],
3813 expr: &'tcx hir::Expr<'tcx>,
3814 ) -> Ty<'tcx> {
3815 let container = self.lower_ty(container).normalized;
3816
3817 let mut field_indices = Vec::with_capacity(fields.len());
3818 let mut current_container = container;
3819 let mut fields = fields.into_iter();
3820
3821 while let Some(&field) = fields.next() {
3822 let container = self.structurally_resolve_type(expr.span, current_container);
3823
3824 match container.kind() {
3825 ty::Adt(container_def, args) if container_def.is_enum() => {
3826 let block = self.tcx.local_def_id_to_hir_id(self.body_id);
3827 let (ident, _def_scope) =
3828 self.tcx.adjust_ident_and_get_scope(field, container_def.did(), block);
3829
3830 if !self.tcx.features().offset_of_enum() {
3831 rustc_session::parse::feature_err(
3832 &self.tcx.sess,
3833 sym::offset_of_enum,
3834 ident.span,
3835 "using enums in offset_of is experimental",
3836 )
3837 .emit();
3838 }
3839
3840 let Some((index, variant)) = container_def
3841 .variants()
3842 .iter_enumerated()
3843 .find(|(_, v)| v.ident(self.tcx).normalize_to_macros_2_0() == ident)
3844 else {
3845 self.dcx()
3846 .create_err(NoVariantNamed { span: ident.span, ident, ty: container })
3847 .with_span_label(field.span, "variant not found")
3848 .emit_unless(container.references_error());
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 self.dcx()
3872 .create_err(NoFieldOnVariant {
3873 span: ident.span,
3874 container,
3875 ident,
3876 field: subfield,
3877 enum_span: field.span,
3878 field_span: subident.span,
3879 })
3880 .emit_unless(container.references_error());
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).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}