1use std::collections::hash_map::Entry;
2use std::slice;
3
4use rustc_abi::FieldIdx;
5use rustc_data_structures::fx::FxHashSet;
6use rustc_errors::{Applicability, Diag, ErrorGuaranteed, MultiSpan};
7use rustc_hir::def::{CtorOf, DefKind, Res};
8use rustc_hir::def_id::DefId;
9use rustc_hir::intravisit::VisitorExt;
10use rustc_hir::lang_items::LangItem;
11use rustc_hir::{self as hir, AmbigArg, ExprKind, GenericArg, HirId, Node, QPath, intravisit};
12use rustc_hir_analysis::hir_ty_lowering::errors::GenericsArgsErrExtend;
13use rustc_hir_analysis::hir_ty_lowering::generics::{
14 check_generic_arg_count_for_call, lower_generic_args,
15};
16use rustc_hir_analysis::hir_ty_lowering::{
17 ExplicitLateBound, FeedConstTy, GenericArgCountMismatch, GenericArgCountResult,
18 GenericArgsLowerer, GenericPathSegment, HirTyLowerer, IsMethodCall, RegionInferReason,
19};
20use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryResponse};
21use rustc_infer::infer::{DefineOpaqueTypes, InferResult};
22use rustc_lint::builtin::SELF_CONSTRUCTOR_FROM_OUTER_ITEM;
23use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
24use rustc_middle::ty::{
25 self, AdtKind, CanonicalUserType, GenericArgsRef, GenericParamDefKind, IsIdentity,
26 SizedTraitKind, Ty, TyCtxt, TypeFoldable, TypeVisitable, TypeVisitableExt, UserArgs,
27 UserSelfTy,
28};
29use rustc_middle::{bug, span_bug};
30use rustc_session::lint;
31use rustc_span::Span;
32use rustc_span::def_id::LocalDefId;
33use rustc_span::hygiene::DesugaringKind;
34use rustc_trait_selection::error_reporting::infer::need_type_info::TypeAnnotationNeeded;
35use rustc_trait_selection::traits::{
36 self, NormalizeExt, ObligationCauseCode, StructurallyNormalizeExt,
37};
38use tracing::{debug, instrument};
39
40use crate::callee::{self, DeferredCallResolution};
41use crate::errors::{self, CtorIsPrivate};
42use crate::method::{self, MethodCallee};
43use crate::{BreakableCtxt, Diverges, Expectation, FnCtxt, LoweredTy, rvalue_scopes};
44
45impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
46 pub(crate) fn warn_if_unreachable(&self, id: HirId, span: Span, kind: &str) {
49 let Diverges::Always { span: orig_span, custom_note } = self.diverges.get() else {
50 return;
51 };
52
53 match span.desugaring_kind() {
54 Some(DesugaringKind::Async) => return,
57
58 Some(DesugaringKind::Await) => return,
62
63 _ => {}
64 }
65
66 self.diverges.set(Diverges::WarnedAlways);
68
69 debug!("warn_if_unreachable: id={:?} span={:?} kind={}", id, span, kind);
70
71 let msg = format!("unreachable {kind}");
72 self.tcx().node_span_lint(lint::builtin::UNREACHABLE_CODE, id, span, |lint| {
73 lint.primary_message(msg.clone());
74 lint.span_label(span, msg).span_label(
75 orig_span,
76 custom_note.unwrap_or("any code following this expression is unreachable"),
77 );
78 })
79 }
80
81 #[instrument(skip(self), level = "debug", ret)]
88 pub(crate) fn resolve_vars_with_obligations<T: TypeFoldable<TyCtxt<'tcx>>>(
89 &self,
90 mut t: T,
91 ) -> T {
92 if !t.has_non_region_infer() {
94 debug!("no inference var, nothing needs doing");
95 return t;
96 }
97
98 t = self.resolve_vars_if_possible(t);
100 if !t.has_non_region_infer() {
101 debug!(?t);
102 return t;
103 }
104
105 self.select_obligations_where_possible(|_| {});
110 self.resolve_vars_if_possible(t)
111 }
112
113 pub(crate) fn record_deferred_call_resolution(
114 &self,
115 closure_def_id: LocalDefId,
116 r: DeferredCallResolution<'tcx>,
117 ) {
118 let mut deferred_call_resolutions = self.deferred_call_resolutions.borrow_mut();
119 deferred_call_resolutions.entry(closure_def_id).or_default().push(r);
120 }
121
122 pub(crate) fn remove_deferred_call_resolutions(
123 &self,
124 closure_def_id: LocalDefId,
125 ) -> Vec<DeferredCallResolution<'tcx>> {
126 let mut deferred_call_resolutions = self.deferred_call_resolutions.borrow_mut();
127 deferred_call_resolutions.remove(&closure_def_id).unwrap_or_default()
128 }
129
130 fn tag(&self) -> String {
131 format!("{self:p}")
132 }
133
134 pub(crate) fn local_ty(&self, span: Span, nid: HirId) -> Ty<'tcx> {
135 self.locals.borrow().get(&nid).cloned().unwrap_or_else(|| {
136 span_bug!(span, "no type for local variable {}", self.tcx.hir_id_to_string(nid))
137 })
138 }
139
140 #[inline]
141 pub(crate) fn write_ty(&self, id: HirId, ty: Ty<'tcx>) {
142 debug!("write_ty({:?}, {:?}) in fcx {}", id, self.resolve_vars_if_possible(ty), self.tag());
143 let mut typeck = self.typeck_results.borrow_mut();
144 let mut node_ty = typeck.node_types_mut();
145
146 if let Some(prev) = node_ty.insert(id, ty) {
147 if prev.references_error() {
148 node_ty.insert(id, prev);
149 } else if !ty.references_error() {
150 self.dcx().span_delayed_bug(
155 self.tcx.hir_span(id),
156 format!("`{prev}` overridden by `{ty}` for {id:?} in {:?}", self.body_id),
157 );
158 }
159 }
160
161 if let Err(e) = ty.error_reported() {
162 self.set_tainted_by_errors(e);
163 }
164 }
165
166 pub(crate) fn write_field_index(&self, hir_id: HirId, index: FieldIdx) {
167 self.typeck_results.borrow_mut().field_indices_mut().insert(hir_id, index);
168 }
169
170 #[instrument(level = "debug", skip(self))]
171 pub(crate) fn write_resolution(
172 &self,
173 hir_id: HirId,
174 r: Result<(DefKind, DefId), ErrorGuaranteed>,
175 ) {
176 self.typeck_results.borrow_mut().type_dependent_defs_mut().insert(hir_id, r);
177 }
178
179 #[instrument(level = "debug", skip(self))]
180 pub(crate) fn write_method_call_and_enforce_effects(
181 &self,
182 hir_id: HirId,
183 span: Span,
184 method: MethodCallee<'tcx>,
185 ) {
186 self.enforce_context_effects(Some(hir_id), span, method.def_id, method.args);
187 self.write_resolution(hir_id, Ok((DefKind::AssocFn, method.def_id)));
188 self.write_args(hir_id, method.args);
189 }
190
191 fn write_args(&self, node_id: HirId, args: GenericArgsRef<'tcx>) {
192 if !args.is_empty() {
193 debug!("write_args({:?}, {:?}) in fcx {}", node_id, args, self.tag());
194
195 self.typeck_results.borrow_mut().node_args_mut().insert(node_id, args);
196 }
197 }
198
199 #[instrument(skip(self), level = "debug")]
207 pub(crate) fn write_user_type_annotation_from_args(
208 &self,
209 hir_id: HirId,
210 def_id: DefId,
211 args: GenericArgsRef<'tcx>,
212 user_self_ty: Option<UserSelfTy<'tcx>>,
213 ) {
214 debug!("fcx {}", self.tag());
215
216 if matches!(self.tcx.def_kind(def_id), DefKind::ConstParam) {
220 return;
221 }
222
223 if Self::can_contain_user_lifetime_bounds((args, user_self_ty)) {
224 let canonicalized = self.canonicalize_user_type_annotation(ty::UserType::new(
225 ty::UserTypeKind::TypeOf(def_id, UserArgs { args, user_self_ty }),
226 ));
227 debug!(?canonicalized);
228 self.write_user_type_annotation(hir_id, canonicalized);
229 }
230 }
231
232 #[instrument(skip(self), level = "debug")]
233 pub(crate) fn write_user_type_annotation(
234 &self,
235 hir_id: HirId,
236 canonical_user_type_annotation: CanonicalUserType<'tcx>,
237 ) {
238 debug!("fcx {}", self.tag());
239
240 if !canonical_user_type_annotation.is_identity() {
242 self.typeck_results
243 .borrow_mut()
244 .user_provided_types_mut()
245 .insert(hir_id, canonical_user_type_annotation);
246 } else {
247 debug!("skipping identity args");
248 }
249 }
250
251 #[instrument(skip(self, expr), level = "debug")]
252 pub(crate) fn apply_adjustments(&self, expr: &hir::Expr<'_>, adj: Vec<Adjustment<'tcx>>) {
253 debug!("expr = {:#?}", expr);
254
255 if adj.is_empty() {
256 return;
257 }
258
259 let mut expr_ty = self.typeck_results.borrow().expr_ty_adjusted(expr);
260
261 for a in &adj {
262 match a.kind {
263 Adjust::NeverToAny => {
264 if a.target.is_ty_var() {
265 self.diverging_type_vars.borrow_mut().insert(a.target);
266 debug!("apply_adjustments: adding `{:?}` as diverging type var", a.target);
267 }
268 }
269 Adjust::Deref(Some(overloaded_deref)) => {
270 self.enforce_context_effects(
271 None,
272 expr.span,
273 overloaded_deref.method_call(self.tcx),
274 self.tcx.mk_args(&[expr_ty.into()]),
275 );
276 }
277 Adjust::Deref(None) => {
278 }
280 Adjust::Pointer(_pointer_coercion) => {
281 }
283 Adjust::ReborrowPin(_mutability) => {
284 }
287 Adjust::Borrow(_) => {
288 }
290 }
291
292 expr_ty = a.target;
293 }
294
295 let autoborrow_mut = adj.iter().any(|adj| {
296 matches!(
297 adj,
298 &Adjustment {
299 kind: Adjust::Borrow(AutoBorrow::Ref(AutoBorrowMutability::Mut { .. })),
300 ..
301 }
302 )
303 });
304
305 match self.typeck_results.borrow_mut().adjustments_mut().entry(expr.hir_id) {
306 Entry::Vacant(entry) => {
307 entry.insert(adj);
308 }
309 Entry::Occupied(mut entry) => {
310 debug!(" - composing on top of {:?}", entry.get());
311 match (&mut entry.get_mut()[..], &adj[..]) {
312 (
313 [Adjustment { kind: Adjust::NeverToAny, target }],
314 &[.., Adjustment { target: new_target, .. }],
315 ) => {
316 *target = new_target;
324 }
325
326 (
327 &mut [
328 Adjustment { kind: Adjust::Deref(_), .. },
329 Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(..)), .. },
330 ],
331 &[
332 Adjustment { kind: Adjust::Deref(_), .. },
333 .., ],
335 ) => {
336 *entry.get_mut() = adj;
338 }
339
340 _ => {
341 self.dcx().span_delayed_bug(
344 expr.span,
345 format!(
346 "while adjusting {:?}, can't compose {:?} and {:?}",
347 expr,
348 entry.get(),
349 adj
350 ),
351 );
352
353 *entry.get_mut() = adj;
354 }
355 }
356 }
357 }
358
359 if autoborrow_mut {
363 self.convert_place_derefs_to_mutable(expr);
364 }
365 }
366
367 pub(crate) fn instantiate_bounds(
369 &self,
370 span: Span,
371 def_id: DefId,
372 args: GenericArgsRef<'tcx>,
373 ) -> ty::InstantiatedPredicates<'tcx> {
374 let bounds = self.tcx.predicates_of(def_id);
375 let result = bounds.instantiate(self.tcx, args);
376 let result = self.normalize(span, result);
377 debug!("instantiate_bounds(bounds={:?}, args={:?}) = {:?}", bounds, args, result);
378 result
379 }
380
381 pub(crate) fn normalize<T>(&self, span: Span, value: T) -> T
382 where
383 T: TypeFoldable<TyCtxt<'tcx>>,
384 {
385 self.register_infer_ok_obligations(
386 self.at(&self.misc(span), self.param_env).normalize(value),
387 )
388 }
389
390 pub(crate) fn require_type_meets(
391 &self,
392 ty: Ty<'tcx>,
393 span: Span,
394 code: traits::ObligationCauseCode<'tcx>,
395 def_id: DefId,
396 ) {
397 self.register_bound(ty, def_id, self.cause(span, code));
398 }
399
400 pub(crate) fn require_type_is_sized(
401 &self,
402 ty: Ty<'tcx>,
403 span: Span,
404 code: traits::ObligationCauseCode<'tcx>,
405 ) {
406 if !ty.references_error() {
407 let lang_item = self.tcx.require_lang_item(LangItem::Sized, span);
408 self.require_type_meets(ty, span, code, lang_item);
409 }
410 }
411
412 pub(crate) fn require_type_is_sized_deferred(
413 &self,
414 ty: Ty<'tcx>,
415 span: Span,
416 code: traits::ObligationCauseCode<'tcx>,
417 ) {
418 if !ty.references_error() {
419 self.deferred_sized_obligations.borrow_mut().push((ty, span, code));
420 }
421 }
422
423 pub(crate) fn require_type_has_static_alignment(&self, ty: Ty<'tcx>, span: Span) {
424 if !ty.references_error() {
425 let tail = self.tcx.struct_tail_raw(
426 ty,
427 |ty| {
428 if self.next_trait_solver() {
429 self.try_structurally_resolve_type(span, ty)
430 } else {
431 self.normalize(span, ty)
432 }
433 },
434 || {},
435 );
436 if tail.has_trivial_sizedness(self.tcx, SizedTraitKind::Sized)
438 || matches!(tail.kind(), ty::Slice(..))
439 {
440 } else {
442 let lang_item = self.tcx.require_lang_item(LangItem::Sized, span);
444 self.require_type_meets(ty, span, ObligationCauseCode::Misc, lang_item);
445 }
446 }
447 }
448
449 pub(crate) fn register_bound(
450 &self,
451 ty: Ty<'tcx>,
452 def_id: DefId,
453 cause: traits::ObligationCause<'tcx>,
454 ) {
455 if !ty.references_error() {
456 self.fulfillment_cx.borrow_mut().register_bound(
457 self,
458 self.param_env,
459 ty,
460 def_id,
461 cause,
462 );
463 }
464 }
465
466 pub(crate) fn lower_ty(&self, hir_ty: &hir::Ty<'tcx>) -> LoweredTy<'tcx> {
467 let ty = self.lowerer().lower_ty(hir_ty);
468 self.register_wf_obligation(ty.into(), hir_ty.span, ObligationCauseCode::WellFormed(None));
469 LoweredTy::from_raw(self, hir_ty.span, ty)
470 }
471
472 pub(crate) fn collect_impl_trait_clauses_from_hir_ty(
476 &self,
477 hir_ty: &'tcx hir::Ty<'tcx>,
478 ) -> ty::Clauses<'tcx> {
479 struct CollectClauses<'a, 'tcx> {
480 clauses: Vec<ty::Clause<'tcx>>,
481 fcx: &'a FnCtxt<'a, 'tcx>,
482 }
483
484 impl<'tcx> intravisit::Visitor<'tcx> for CollectClauses<'_, 'tcx> {
485 fn visit_ty(&mut self, ty: &'tcx hir::Ty<'tcx, AmbigArg>) {
486 if let Some(clauses) = self.fcx.trait_ascriptions.borrow().get(&ty.hir_id.local_id)
487 {
488 self.clauses.extend(clauses.iter().cloned());
489 }
490 intravisit::walk_ty(self, ty)
491 }
492 }
493
494 let mut clauses = CollectClauses { clauses: vec![], fcx: self };
495 clauses.visit_ty_unambig(hir_ty);
496 self.tcx.mk_clauses(&clauses.clauses)
497 }
498
499 #[instrument(level = "debug", skip_all)]
500 pub(crate) fn lower_ty_saving_user_provided_ty(&self, hir_ty: &'tcx hir::Ty<'tcx>) -> Ty<'tcx> {
501 let ty = self.lower_ty(hir_ty);
502 debug!(?ty);
503
504 if Self::can_contain_user_lifetime_bounds(ty.raw) {
505 let c_ty = self.canonicalize_response(ty::UserType::new(ty::UserTypeKind::Ty(ty.raw)));
506 debug!(?c_ty);
507 self.typeck_results.borrow_mut().user_provided_types_mut().insert(hir_ty.hir_id, c_ty);
508 }
509
510 ty.normalized
511 }
512
513 pub(super) fn user_args_for_adt(ty: LoweredTy<'tcx>) -> UserArgs<'tcx> {
514 match (ty.raw.kind(), ty.normalized.kind()) {
515 (ty::Adt(_, args), _) => UserArgs { args, user_self_ty: None },
516 (_, ty::Adt(adt, args)) => UserArgs {
517 args,
518 user_self_ty: Some(UserSelfTy { impl_def_id: adt.did(), self_ty: ty.raw }),
519 },
520 _ => bug!("non-adt type {:?}", ty),
521 }
522 }
523
524 pub(crate) fn lower_const_arg(
525 &self,
526 const_arg: &'tcx hir::ConstArg<'tcx>,
527 feed: FeedConstTy<'_, 'tcx>,
528 ) -> ty::Const<'tcx> {
529 let ct = self.lowerer().lower_const_arg(const_arg, feed);
530 self.register_wf_obligation(
531 ct.into(),
532 self.tcx.hir_span(const_arg.hir_id),
533 ObligationCauseCode::WellFormed(None),
534 );
535 ct
536 }
537
538 fn can_contain_user_lifetime_bounds<T>(t: T) -> bool
546 where
547 T: TypeVisitable<TyCtxt<'tcx>>,
548 {
549 t.has_free_regions() || t.has_aliases() || t.has_infer_types()
550 }
551
552 pub(crate) fn node_ty(&self, id: HirId) -> Ty<'tcx> {
553 match self.typeck_results.borrow().node_types().get(id) {
554 Some(&t) => t,
555 None if let Some(e) = self.tainted_by_errors() => Ty::new_error(self.tcx, e),
556 None => {
557 bug!("no type for node {} in fcx {}", self.tcx.hir_id_to_string(id), self.tag());
558 }
559 }
560 }
561
562 pub(crate) fn node_ty_opt(&self, id: HirId) -> Option<Ty<'tcx>> {
563 match self.typeck_results.borrow().node_types().get(id) {
564 Some(&t) => Some(t),
565 None if let Some(e) = self.tainted_by_errors() => Some(Ty::new_error(self.tcx, e)),
566 None => None,
567 }
568 }
569
570 pub(crate) fn register_wf_obligation(
572 &self,
573 term: ty::Term<'tcx>,
574 span: Span,
575 code: traits::ObligationCauseCode<'tcx>,
576 ) {
577 let cause = self.cause(span, code);
579 self.register_predicate(traits::Obligation::new(
580 self.tcx,
581 cause,
582 self.param_env,
583 ty::ClauseKind::WellFormed(term),
584 ));
585 }
586
587 pub(crate) fn add_wf_bounds(&self, args: GenericArgsRef<'tcx>, span: Span) {
589 for term in args.iter().filter_map(ty::GenericArg::as_term) {
590 self.register_wf_obligation(term, span, ObligationCauseCode::WellFormed(None));
591 }
592 }
593
594 pub(crate) fn field_ty(
598 &self,
599 span: Span,
600 field: &'tcx ty::FieldDef,
601 args: GenericArgsRef<'tcx>,
602 ) -> Ty<'tcx> {
603 self.normalize(span, field.ty(self.tcx, args))
604 }
605
606 pub(crate) fn resolve_rvalue_scopes(&self, def_id: DefId) {
607 let scope_tree = self.tcx.region_scope_tree(def_id);
608 let rvalue_scopes = { rvalue_scopes::resolve_rvalue_scopes(self, scope_tree, def_id) };
609 let mut typeck_results = self.typeck_results.borrow_mut();
610 typeck_results.rvalue_scopes = rvalue_scopes;
611 }
612
613 #[instrument(level = "debug", skip(self))]
622 pub(crate) fn resolve_coroutine_interiors(&self) {
623 self.select_obligations_where_possible(|_| {});
627
628 let ty::TypingMode::Analysis { defining_opaque_types_and_generators } = self.typing_mode()
629 else {
630 bug!();
631 };
632
633 if defining_opaque_types_and_generators
634 .iter()
635 .any(|def_id| self.tcx.is_coroutine(def_id.to_def_id()))
636 {
637 self.typeck_results.borrow_mut().coroutine_stalled_predicates.extend(
638 self.fulfillment_cx
639 .borrow_mut()
640 .drain_stalled_obligations_for_coroutines(&self.infcx)
641 .into_iter()
642 .map(|o| (o.predicate, o.cause)),
643 );
644 }
645 }
646
647 #[instrument(skip(self), level = "debug")]
648 pub(crate) fn report_ambiguity_errors(&self) {
649 let mut errors = self.fulfillment_cx.borrow_mut().collect_remaining_errors(self);
650
651 if !errors.is_empty() {
652 self.adjust_fulfillment_errors_for_expr_obligation(&mut errors);
653 self.err_ctxt().report_fulfillment_errors(errors);
654 }
655 }
656
657 pub(crate) fn select_obligations_where_possible(
659 &self,
660 mutate_fulfillment_errors: impl Fn(&mut Vec<traits::FulfillmentError<'tcx>>),
661 ) {
662 let mut result = self.fulfillment_cx.borrow_mut().select_where_possible(self);
663 if !result.is_empty() {
664 mutate_fulfillment_errors(&mut result);
665 self.adjust_fulfillment_errors_for_expr_obligation(&mut result);
666 self.err_ctxt().report_fulfillment_errors(result);
667 }
668 }
669
670 pub(crate) fn make_overloaded_place_return_type(&self, method: MethodCallee<'tcx>) -> Ty<'tcx> {
675 let ret_ty = method.sig.output();
677
678 ret_ty.builtin_deref(true).unwrap()
680 }
681
682 pub(crate) fn type_var_is_sized(&self, self_ty: ty::TyVid) -> bool {
683 let sized_did = self.tcx.lang_items().sized_trait();
684 self.obligations_for_self_ty(self_ty).into_iter().any(|obligation| {
685 match obligation.predicate.kind().skip_binder() {
686 ty::PredicateKind::Clause(ty::ClauseKind::Trait(data)) => {
687 Some(data.def_id()) == sized_did
688 }
689 _ => false,
690 }
691 })
692 }
693
694 pub(crate) fn err_args(&self, len: usize, guar: ErrorGuaranteed) -> Vec<Ty<'tcx>> {
695 let ty_error = Ty::new_error(self.tcx, guar);
696 vec![ty_error; len]
697 }
698
699 pub(crate) fn resolve_lang_item_path(
700 &self,
701 lang_item: hir::LangItem,
702 span: Span,
703 hir_id: HirId,
704 ) -> (Res, Ty<'tcx>) {
705 let def_id = self.tcx.require_lang_item(lang_item, span);
706 let def_kind = self.tcx.def_kind(def_id);
707
708 let item_ty = if let DefKind::Variant = def_kind {
709 self.tcx.type_of(self.tcx.parent(def_id))
710 } else {
711 self.tcx.type_of(def_id)
712 };
713 let args = self.fresh_args_for_item(span, def_id);
714 let ty = item_ty.instantiate(self.tcx, args);
715
716 self.write_args(hir_id, args);
717 self.write_resolution(hir_id, Ok((def_kind, def_id)));
718
719 let code = match lang_item {
720 hir::LangItem::IntoFutureIntoFuture => {
721 if let hir::Node::Expr(into_future_call) = self.tcx.parent_hir_node(hir_id)
722 && let hir::ExprKind::Call(_, [arg0]) = &into_future_call.kind
723 {
724 Some(ObligationCauseCode::AwaitableExpr(arg0.hir_id))
725 } else {
726 None
727 }
728 }
729 hir::LangItem::IteratorNext | hir::LangItem::IntoIterIntoIter => {
730 Some(ObligationCauseCode::ForLoopIterator)
731 }
732 hir::LangItem::TryTraitFromOutput
733 | hir::LangItem::TryTraitFromResidual
734 | hir::LangItem::TryTraitBranch => Some(ObligationCauseCode::QuestionMark),
735 _ => None,
736 };
737 if let Some(code) = code {
738 self.add_required_obligations_with_code(span, def_id, args, move |_, _| code.clone());
739 } else {
740 self.add_required_obligations_for_hir(span, def_id, args, hir_id);
741 }
742
743 (Res::Def(def_kind, def_id), ty)
744 }
745
746 #[instrument(level = "trace", skip(self), ret)]
749 pub(crate) fn resolve_ty_and_res_fully_qualified_call(
750 &self,
751 qpath: &'tcx QPath<'tcx>,
752 hir_id: HirId,
753 span: Span,
754 ) -> (Res, Option<LoweredTy<'tcx>>, &'tcx [hir::PathSegment<'tcx>]) {
755 let (ty, qself, item_segment) = match *qpath {
756 QPath::Resolved(ref opt_qself, path) => {
757 return (
758 path.res,
759 opt_qself.as_ref().map(|qself| self.lower_ty(qself)),
760 path.segments,
761 );
762 }
763 QPath::TypeRelative(ref qself, ref segment) => {
764 let ty = self.lowerer().lower_ty(qself);
774 (LoweredTy::from_raw(self, span, ty), qself, segment)
775 }
776 QPath::LangItem(..) => {
777 bug!("`resolve_ty_and_res_fully_qualified_call` called on `LangItem`")
778 }
779 };
780
781 self.register_wf_obligation(
782 ty.raw.into(),
783 qself.span,
784 ObligationCauseCode::WellFormed(None),
785 );
786 self.select_obligations_where_possible(|_| {});
787
788 if let Some(&cached_result) = self.typeck_results.borrow().type_dependent_defs().get(hir_id)
789 {
790 let def = cached_result.map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id));
793 return (def, Some(ty), slice::from_ref(&**item_segment));
794 }
795 let item_name = item_segment.ident;
796 let result = self
797 .resolve_fully_qualified_call(span, item_name, ty.normalized, qself.span, hir_id)
798 .or_else(|error| {
799 let guar = self
800 .dcx()
801 .span_delayed_bug(span, "method resolution should've emitted an error");
802 let result = match error {
803 method::MethodError::PrivateMatch(kind, def_id, _) => Ok((kind, def_id)),
804 _ => Err(guar),
805 };
806
807 let trait_missing_method =
808 matches!(error, method::MethodError::NoMatch(_)) && ty.normalized.is_trait();
809 self.report_method_error(
810 hir_id,
811 ty.normalized,
812 error,
813 Expectation::NoExpectation,
814 trait_missing_method && span.edition().at_least_rust_2021(), );
816
817 result
818 });
819
820 self.write_resolution(hir_id, result);
822 (
823 result.map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id)),
824 Some(ty),
825 slice::from_ref(&**item_segment),
826 )
827 }
828
829 pub(crate) fn get_fn_decl(
831 &self,
832 blk_id: HirId,
833 ) -> Option<(LocalDefId, &'tcx hir::FnDecl<'tcx>)> {
834 self.tcx.hir_get_fn_id_for_return_block(blk_id).and_then(|item_id| {
837 match self.tcx.hir_node(item_id) {
838 Node::Item(&hir::Item {
839 kind: hir::ItemKind::Fn { sig, .. }, owner_id, ..
840 }) => Some((owner_id.def_id, sig.decl)),
841 Node::TraitItem(&hir::TraitItem {
842 kind: hir::TraitItemKind::Fn(ref sig, ..),
843 owner_id,
844 ..
845 }) => Some((owner_id.def_id, sig.decl)),
846 Node::ImplItem(&hir::ImplItem {
847 kind: hir::ImplItemKind::Fn(ref sig, ..),
848 owner_id,
849 ..
850 }) => Some((owner_id.def_id, sig.decl)),
851 Node::Expr(&hir::Expr {
852 hir_id,
853 kind: hir::ExprKind::Closure(&hir::Closure { def_id, kind, fn_decl, .. }),
854 ..
855 }) => {
856 match kind {
857 hir::ClosureKind::CoroutineClosure(_) => {
858 return None;
860 }
861 hir::ClosureKind::Closure => Some((def_id, fn_decl)),
862 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
863 _,
864 hir::CoroutineSource::Fn,
865 )) => {
866 let (sig, owner_id) = match self.tcx.parent_hir_node(hir_id) {
867 Node::Item(&hir::Item {
868 kind: hir::ItemKind::Fn { ref sig, .. },
869 owner_id,
870 ..
871 }) => (sig, owner_id),
872 Node::TraitItem(&hir::TraitItem {
873 kind: hir::TraitItemKind::Fn(ref sig, ..),
874 owner_id,
875 ..
876 }) => (sig, owner_id),
877 Node::ImplItem(&hir::ImplItem {
878 kind: hir::ImplItemKind::Fn(ref sig, ..),
879 owner_id,
880 ..
881 }) => (sig, owner_id),
882 _ => return None,
883 };
884 Some((owner_id.def_id, sig.decl))
885 }
886 _ => None,
887 }
888 }
889 _ => None,
890 }
891 })
892 }
893
894 pub(crate) fn note_internal_mutation_in_method(
895 &self,
896 err: &mut Diag<'_>,
897 expr: &hir::Expr<'_>,
898 expected: Option<Ty<'tcx>>,
899 found: Ty<'tcx>,
900 ) {
901 if found != self.tcx.types.unit {
902 return;
903 }
904
905 let ExprKind::MethodCall(path_segment, rcvr, ..) = expr.kind else {
906 return;
907 };
908
909 let rcvr_has_the_expected_type = self
910 .typeck_results
911 .borrow()
912 .expr_ty_adjusted_opt(rcvr)
913 .zip(expected)
914 .is_some_and(|(ty, expected_ty)| expected_ty.peel_refs() == ty.peel_refs());
915
916 let prev_call_mutates_and_returns_unit = || {
917 self.typeck_results
918 .borrow()
919 .type_dependent_def_id(expr.hir_id)
920 .map(|def_id| self.tcx.fn_sig(def_id).skip_binder().skip_binder())
921 .and_then(|sig| sig.inputs_and_output.split_last())
922 .is_some_and(|(output, inputs)| {
923 output.is_unit()
924 && inputs
925 .get(0)
926 .and_then(|self_ty| self_ty.ref_mutability())
927 .is_some_and(rustc_ast::Mutability::is_mut)
928 })
929 };
930
931 if !(rcvr_has_the_expected_type || prev_call_mutates_and_returns_unit()) {
932 return;
933 }
934
935 let mut sp = MultiSpan::from_span(path_segment.ident.span);
936 sp.push_span_label(
937 path_segment.ident.span,
938 format!(
939 "this call modifies {} in-place",
940 match rcvr.kind {
941 ExprKind::Path(QPath::Resolved(
942 None,
943 hir::Path { segments: [segment], .. },
944 )) => format!("`{}`", segment.ident),
945 _ => "its receiver".to_string(),
946 }
947 ),
948 );
949
950 let modifies_rcvr_note =
951 format!("method `{}` modifies its receiver in-place", path_segment.ident);
952 if rcvr_has_the_expected_type {
953 sp.push_span_label(
954 rcvr.span,
955 "you probably want to use this value after calling the method...",
956 );
957 err.span_note(sp, modifies_rcvr_note);
958 err.note(format!("...instead of the `()` output of method `{}`", path_segment.ident));
959 } else if let ExprKind::MethodCall(..) = rcvr.kind {
960 err.span_note(
961 sp,
962 modifies_rcvr_note + ", it is not meant to be used in method chains.",
963 );
964 } else {
965 err.span_note(sp, modifies_rcvr_note);
966 }
967 }
968
969 #[instrument(skip(self, span), level = "debug")]
972 pub(crate) fn instantiate_value_path(
973 &self,
974 segments: &'tcx [hir::PathSegment<'tcx>],
975 self_ty: Option<LoweredTy<'tcx>>,
976 res: Res,
977 span: Span,
978 path_span: Span,
979 hir_id: HirId,
980 ) -> (Ty<'tcx>, Res) {
981 let tcx = self.tcx;
982
983 let generic_segments = match res {
984 Res::Local(_) | Res::SelfCtor(_) => vec![],
985 Res::Def(kind, def_id) => self.lowerer().probe_generic_path_segments(
986 segments,
987 self_ty.map(|ty| ty.raw),
988 kind,
989 def_id,
990 span,
991 ),
992 Res::Err => {
993 return (
994 Ty::new_error(
995 tcx,
996 tcx.dcx().span_delayed_bug(span, "could not resolve path {:?}"),
997 ),
998 res,
999 );
1000 }
1001 _ => bug!("instantiate_value_path on {:?}", res),
1002 };
1003
1004 let mut user_self_ty = None;
1005 let mut is_alias_variant_ctor = false;
1006 let mut err_extend = GenericsArgsErrExtend::None;
1007 match res {
1008 Res::Def(DefKind::Ctor(CtorOf::Variant, _), _) if let Some(self_ty) = self_ty => {
1009 let adt_def = self_ty.normalized.ty_adt_def().unwrap();
1010 user_self_ty =
1011 Some(UserSelfTy { impl_def_id: adt_def.did(), self_ty: self_ty.raw });
1012 is_alias_variant_ctor = true;
1013 err_extend = GenericsArgsErrExtend::DefVariant(segments);
1014 }
1015 Res::Def(DefKind::Ctor(CtorOf::Variant, _), _) => {
1016 err_extend = GenericsArgsErrExtend::DefVariant(segments);
1017 }
1018 Res::Def(DefKind::AssocFn | DefKind::AssocConst, def_id) => {
1019 let assoc_item = tcx.associated_item(def_id);
1020 let container = assoc_item.container;
1021 let container_id = assoc_item.container_id(tcx);
1022 debug!(?def_id, ?container, ?container_id);
1023 match container {
1024 ty::AssocContainer::Trait => {
1025 if let Err(e) = callee::check_legal_trait_for_method_call(
1026 tcx,
1027 path_span,
1028 None,
1029 span,
1030 container_id,
1031 self.body_id.to_def_id(),
1032 ) {
1033 self.set_tainted_by_errors(e);
1034 }
1035 }
1036 ty::AssocContainer::InherentImpl | ty::AssocContainer::TraitImpl(_) => {
1037 if segments.len() == 1 {
1038 let self_ty = self_ty.expect("UFCS sugared assoc missing Self").raw;
1044 user_self_ty = Some(UserSelfTy { impl_def_id: container_id, self_ty });
1045 }
1046 }
1047 }
1048 }
1049 _ => {}
1050 }
1051
1052 let indices: FxHashSet<_> =
1058 generic_segments.iter().map(|GenericPathSegment(_, index)| index).collect();
1059 let generics_err = self.lowerer().prohibit_generic_args(
1060 segments.iter().enumerate().filter_map(|(index, seg)| {
1061 if !indices.contains(&index) || is_alias_variant_ctor { Some(seg) } else { None }
1062 }),
1063 err_extend,
1064 );
1065
1066 if let Res::Local(hid) = res {
1067 let ty = self.local_ty(span, hid);
1068 let ty = self.normalize(span, ty);
1069 return (ty, res);
1070 }
1071
1072 if let Err(_) = generics_err {
1073 user_self_ty = None;
1075 }
1076
1077 let mut infer_args_for_err = None;
1085
1086 let mut explicit_late_bound = ExplicitLateBound::No;
1087 for &GenericPathSegment(def_id, index) in &generic_segments {
1088 let seg = &segments[index];
1089 let generics = tcx.generics_of(def_id);
1090
1091 let arg_count =
1096 check_generic_arg_count_for_call(self, def_id, generics, seg, IsMethodCall::No);
1097
1098 if let ExplicitLateBound::Yes = arg_count.explicit_late_bound {
1099 explicit_late_bound = ExplicitLateBound::Yes;
1100 }
1101
1102 if let Err(GenericArgCountMismatch { reported, .. }) = arg_count.correct {
1103 infer_args_for_err
1104 .get_or_insert_with(|| (reported, FxHashSet::default()))
1105 .1
1106 .insert(index);
1107 self.set_tainted_by_errors(reported); }
1109 }
1110
1111 let has_self = generic_segments
1112 .last()
1113 .is_some_and(|GenericPathSegment(def_id, _)| tcx.generics_of(*def_id).has_self);
1114
1115 let (res, implicit_args) = if let Res::Def(DefKind::ConstParam, def) = res {
1116 (res, Some(ty::GenericArgs::identity_for_item(tcx, tcx.parent(def))))
1126 } else if let Res::SelfCtor(impl_def_id) = res {
1127 let ty = LoweredTy::from_raw(
1128 self,
1129 span,
1130 tcx.at(span).type_of(impl_def_id).instantiate_identity(),
1131 );
1132
1133 if std::iter::successors(Some(self.body_id.to_def_id()), |def_id| {
1141 self.tcx.generics_of(def_id).parent
1142 })
1143 .all(|def_id| def_id != impl_def_id)
1144 {
1145 let sugg = ty.normalized.ty_adt_def().map(|def| errors::ReplaceWithName {
1146 span: path_span,
1147 name: self.tcx.item_name(def.did()).to_ident_string(),
1148 });
1149 if ty.raw.has_param() {
1150 let guar = self.dcx().emit_err(errors::SelfCtorFromOuterItem {
1151 span: path_span,
1152 impl_span: tcx.def_span(impl_def_id),
1153 sugg,
1154 });
1155 return (Ty::new_error(self.tcx, guar), res);
1156 } else {
1157 self.tcx.emit_node_span_lint(
1158 SELF_CONSTRUCTOR_FROM_OUTER_ITEM,
1159 hir_id,
1160 path_span,
1161 errors::SelfCtorFromOuterItemLint {
1162 impl_span: tcx.def_span(impl_def_id),
1163 sugg,
1164 },
1165 );
1166 }
1167 }
1168
1169 match ty.normalized.ty_adt_def() {
1170 Some(adt_def) if adt_def.has_ctor() => {
1171 let (ctor_kind, ctor_def_id) = adt_def.non_enum_variant().ctor.unwrap();
1172 let vis = tcx.visibility(ctor_def_id);
1174 if !vis.is_accessible_from(tcx.parent_module(hir_id).to_def_id(), tcx) {
1175 self.dcx()
1176 .emit_err(CtorIsPrivate { span, def: tcx.def_path_str(adt_def.did()) });
1177 }
1178 let new_res = Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
1179 let user_args = Self::user_args_for_adt(ty);
1180 user_self_ty = user_args.user_self_ty;
1181 (new_res, Some(user_args.args))
1182 }
1183 _ => {
1184 let mut err = self.dcx().struct_span_err(
1185 span,
1186 "the `Self` constructor can only be used with tuple or unit structs",
1187 );
1188 if let Some(adt_def) = ty.normalized.ty_adt_def() {
1189 match adt_def.adt_kind() {
1190 AdtKind::Enum => {
1191 err.help("did you mean to use one of the enum's variants?");
1192 }
1193 AdtKind::Struct | AdtKind::Union => {
1194 err.span_suggestion(
1195 span,
1196 "use curly brackets",
1197 "Self { /* fields */ }",
1198 Applicability::HasPlaceholders,
1199 );
1200 }
1201 }
1202 }
1203 let reported = err.emit();
1204 return (Ty::new_error(tcx, reported), res);
1205 }
1206 }
1207 } else {
1208 (res, None)
1209 };
1210 let def_id = res.def_id();
1211
1212 let (correct, infer_args_for_err) = match infer_args_for_err {
1213 Some((reported, args)) => {
1214 (Err(GenericArgCountMismatch { reported, invalid_args: vec![] }), args)
1215 }
1216 None => (Ok(()), Default::default()),
1217 };
1218
1219 let arg_count = GenericArgCountResult { explicit_late_bound, correct };
1220
1221 struct CtorGenericArgsCtxt<'a, 'tcx> {
1222 fcx: &'a FnCtxt<'a, 'tcx>,
1223 span: Span,
1224 generic_segments: &'a [GenericPathSegment],
1225 infer_args_for_err: &'a FxHashSet<usize>,
1226 segments: &'tcx [hir::PathSegment<'tcx>],
1227 }
1228 impl<'a, 'tcx> GenericArgsLowerer<'a, 'tcx> for CtorGenericArgsCtxt<'a, 'tcx> {
1229 fn args_for_def_id(
1230 &mut self,
1231 def_id: DefId,
1232 ) -> (Option<&'a hir::GenericArgs<'tcx>>, bool) {
1233 if let Some(&GenericPathSegment(_, index)) =
1234 self.generic_segments.iter().find(|&GenericPathSegment(did, _)| *did == def_id)
1235 {
1236 if !self.infer_args_for_err.contains(&index) {
1239 if let Some(data) = self.segments[index].args {
1241 return (Some(data), self.segments[index].infer_args);
1242 }
1243 }
1244 return (None, self.segments[index].infer_args);
1245 }
1246
1247 (None, true)
1248 }
1249
1250 fn provided_kind(
1251 &mut self,
1252 preceding_args: &[ty::GenericArg<'tcx>],
1253 param: &ty::GenericParamDef,
1254 arg: &GenericArg<'tcx>,
1255 ) -> ty::GenericArg<'tcx> {
1256 match (¶m.kind, arg) {
1257 (GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => self
1258 .fcx
1259 .lowerer()
1260 .lower_lifetime(lt, RegionInferReason::Param(param))
1261 .into(),
1262 (GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
1263 self.fcx.lower_ty(ty.as_unambig_ty()).raw.into()
1265 }
1266 (GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
1267 self.fcx.lower_ty(&inf.to_ty()).raw.into()
1268 }
1269 (GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => self
1270 .fcx
1271 .lower_const_arg(
1273 ct.as_unambig_ct(),
1274 FeedConstTy::Param(param.def_id, preceding_args),
1275 )
1276 .into(),
1277 (&GenericParamDefKind::Const { .. }, GenericArg::Infer(inf)) => {
1278 self.fcx.ct_infer(Some(param), inf.span).into()
1279 }
1280 _ => unreachable!(),
1281 }
1282 }
1283
1284 fn inferred_kind(
1285 &mut self,
1286 preceding_args: &[ty::GenericArg<'tcx>],
1287 param: &ty::GenericParamDef,
1288 infer_args: bool,
1289 ) -> ty::GenericArg<'tcx> {
1290 let tcx = self.fcx.tcx();
1291 if !infer_args && let Some(default) = param.default_value(tcx) {
1292 return default.instantiate(tcx, preceding_args);
1295 }
1296 self.fcx.var_for_def(self.span, param)
1301 }
1302 }
1303
1304 let args_raw = implicit_args.unwrap_or_else(|| {
1305 lower_generic_args(
1306 self,
1307 def_id,
1308 &[],
1309 has_self,
1310 self_ty.map(|s| s.raw),
1311 &arg_count,
1312 &mut CtorGenericArgsCtxt {
1313 fcx: self,
1314 span,
1315 generic_segments: &generic_segments,
1316 infer_args_for_err: &infer_args_for_err,
1317 segments,
1318 },
1319 )
1320 });
1321
1322 self.write_user_type_annotation_from_args(hir_id, def_id, args_raw, user_self_ty);
1324
1325 let args = self.normalize(span, args_raw);
1327
1328 self.add_required_obligations_for_hir(span, def_id, args, hir_id);
1329
1330 let ty = tcx.type_of(def_id);
1333 assert!(!args.has_escaping_bound_vars());
1334 assert!(!ty.skip_binder().has_escaping_bound_vars());
1335 let ty_instantiated = self.normalize(span, ty.instantiate(tcx, args));
1336
1337 if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty {
1338 let impl_ty = self.normalize(span, tcx.type_of(impl_def_id).instantiate(tcx, args));
1344 let self_ty = self.normalize(span, self_ty);
1345 match self.at(&self.misc(span), self.param_env).eq(
1346 DefineOpaqueTypes::Yes,
1347 impl_ty,
1348 self_ty,
1349 ) {
1350 Ok(ok) => self.register_infer_ok_obligations(ok),
1351 Err(_) => {
1352 self.dcx().span_bug(
1353 span,
1354 format!(
1355 "instantiate_value_path: (UFCS) {self_ty:?} was a subtype of {impl_ty:?} but now is not?",
1356 ),
1357 );
1358 }
1359 }
1360 }
1361
1362 debug!("instantiate_value_path: type of {:?} is {:?}", hir_id, ty_instantiated);
1363 self.write_args(hir_id, args);
1364
1365 (ty_instantiated, res)
1366 }
1367
1368 pub(crate) fn add_required_obligations_for_hir(
1370 &self,
1371 span: Span,
1372 def_id: DefId,
1373 args: GenericArgsRef<'tcx>,
1374 hir_id: HirId,
1375 ) {
1376 self.add_required_obligations_with_code(span, def_id, args, |idx, span| {
1377 ObligationCauseCode::WhereClauseInExpr(def_id, span, hir_id, idx)
1378 })
1379 }
1380
1381 #[instrument(level = "debug", skip(self, code, span, args))]
1382 fn add_required_obligations_with_code(
1383 &self,
1384 span: Span,
1385 def_id: DefId,
1386 args: GenericArgsRef<'tcx>,
1387 code: impl Fn(usize, Span) -> ObligationCauseCode<'tcx>,
1388 ) {
1389 let param_env = self.param_env;
1390
1391 let bounds = self.instantiate_bounds(span, def_id, args);
1392
1393 for obligation in traits::predicates_for_generics(
1394 |idx, predicate_span| self.cause(span, code(idx, predicate_span)),
1395 param_env,
1396 bounds,
1397 ) {
1398 self.register_predicate(obligation);
1399 }
1400 }
1401
1402 #[instrument(level = "debug", skip(self, sp), ret)]
1408 pub(crate) fn try_structurally_resolve_type(&self, sp: Span, ty: Ty<'tcx>) -> Ty<'tcx> {
1409 if self.next_trait_solver()
1410 && let ty::Alias(..) = ty.kind()
1411 {
1412 let result = self
1416 .at(&self.misc(sp), self.param_env)
1417 .structurally_normalize_ty(ty, &mut **self.fulfillment_cx.borrow_mut());
1418 match result {
1419 Ok(normalized_ty) => normalized_ty,
1420 Err(errors) => {
1421 let guar = self.err_ctxt().report_fulfillment_errors(errors);
1422 return Ty::new_error(self.tcx, guar);
1423 }
1424 }
1425 } else {
1426 self.resolve_vars_with_obligations(ty)
1427 }
1428 }
1429
1430 #[instrument(level = "debug", skip(self, sp), ret)]
1431 pub(crate) fn try_structurally_resolve_const(
1432 &self,
1433 sp: Span,
1434 ct: ty::Const<'tcx>,
1435 ) -> ty::Const<'tcx> {
1436 let ct = self.resolve_vars_with_obligations(ct);
1437
1438 if self.next_trait_solver()
1439 && let ty::ConstKind::Unevaluated(..) = ct.kind()
1440 {
1441 let result = self
1445 .at(&self.misc(sp), self.param_env)
1446 .structurally_normalize_const(ct, &mut **self.fulfillment_cx.borrow_mut());
1447 match result {
1448 Ok(normalized_ct) => normalized_ct,
1449 Err(errors) => {
1450 let guar = self.err_ctxt().report_fulfillment_errors(errors);
1451 return ty::Const::new_error(self.tcx, guar);
1452 }
1453 }
1454 } else if self.tcx.features().generic_const_exprs() {
1455 rustc_trait_selection::traits::evaluate_const(&self.infcx, ct, self.param_env)
1456 } else {
1457 ct
1458 }
1459 }
1460
1461 pub(crate) fn structurally_resolve_type(&self, sp: Span, ty: Ty<'tcx>) -> Ty<'tcx> {
1470 let ty = self.try_structurally_resolve_type(sp, ty);
1471
1472 if !ty.is_ty_var() {
1473 ty
1474 } else {
1475 let e = self.tainted_by_errors().unwrap_or_else(|| {
1476 self.err_ctxt()
1477 .emit_inference_failure_err(
1478 self.body_id,
1479 sp,
1480 ty.into(),
1481 TypeAnnotationNeeded::E0282,
1482 true,
1483 )
1484 .emit()
1485 });
1486 let err = Ty::new_error(self.tcx, e);
1487 self.demand_suptype(sp, err, ty);
1488 err
1489 }
1490 }
1491
1492 pub(crate) fn structurally_resolve_const(
1493 &self,
1494 sp: Span,
1495 ct: ty::Const<'tcx>,
1496 ) -> ty::Const<'tcx> {
1497 let ct = self.try_structurally_resolve_const(sp, ct);
1498
1499 if !ct.is_ct_infer() {
1500 ct
1501 } else {
1502 let e = self.tainted_by_errors().unwrap_or_else(|| {
1503 self.err_ctxt()
1504 .emit_inference_failure_err(
1505 self.body_id,
1506 sp,
1507 ct.into(),
1508 TypeAnnotationNeeded::E0282,
1509 true,
1510 )
1511 .emit()
1512 });
1513 ty::Const::new_error(self.tcx, e)
1515 }
1516 }
1517
1518 pub(crate) fn with_breakable_ctxt<F: FnOnce() -> R, R>(
1519 &self,
1520 id: HirId,
1521 ctxt: BreakableCtxt<'tcx>,
1522 f: F,
1523 ) -> (BreakableCtxt<'tcx>, R) {
1524 let index;
1525 {
1526 let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
1527 index = enclosing_breakables.stack.len();
1528 enclosing_breakables.by_id.insert(id, index);
1529 enclosing_breakables.stack.push(ctxt);
1530 }
1531 let result = f();
1532 let ctxt = {
1533 let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
1534 debug_assert!(enclosing_breakables.stack.len() == index + 1);
1535 enclosing_breakables.by_id.swap_remove(&id).expect("missing breakable context");
1537 enclosing_breakables.stack.pop().expect("missing breakable context")
1538 };
1539 (ctxt, result)
1540 }
1541
1542 pub(crate) fn probe_instantiate_query_response(
1545 &self,
1546 span: Span,
1547 original_values: &OriginalQueryValues<'tcx>,
1548 query_result: &Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>,
1549 ) -> InferResult<'tcx, Ty<'tcx>> {
1550 self.instantiate_query_response_and_region_obligations(
1551 &self.misc(span),
1552 self.param_env,
1553 original_values,
1554 query_result,
1555 )
1556 }
1557
1558 pub(crate) fn expr_in_place(&self, mut expr_id: HirId) -> bool {
1560 let mut contained_in_place = false;
1561
1562 while let hir::Node::Expr(parent_expr) = self.tcx.parent_hir_node(expr_id) {
1563 match &parent_expr.kind {
1564 hir::ExprKind::Assign(lhs, ..) | hir::ExprKind::AssignOp(_, lhs, ..) => {
1565 if lhs.hir_id == expr_id {
1566 contained_in_place = true;
1567 break;
1568 }
1569 }
1570 _ => (),
1571 }
1572 expr_id = parent_expr.hir_id;
1573 }
1574
1575 contained_in_place
1576 }
1577}