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