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