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