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