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