1 use crate::callee::{self, DeferredCallResolution};
2 use crate::method::{self, MethodCallee, SelfSource};
3 use crate::rvalue_scopes;
4 use crate::{BreakableCtxt, Diverges, Expectation, FnCtxt, LocalTy, RawTy};
5 use rustc_data_structures::captures::Captures;
6 use rustc_data_structures::fx::FxHashSet;
7 use rustc_errors::{Applicability, Diagnostic, ErrorGuaranteed, MultiSpan};
9 use rustc_hir::def::{CtorOf, DefKind, Res};
10 use rustc_hir::def_id::DefId;
11 use rustc_hir::lang_items::LangItem;
12 use rustc_hir::{ExprKind, GenericArg, Node, QPath};
13 use rustc_hir_analysis::astconv::generics::{
14 check_generic_arg_count_for_call, create_substs_for_generic_args,
16 use rustc_hir_analysis::astconv::{
17 AstConv, CreateSubstsForGenericArgsCtxt, ExplicitLateBound, GenericArgCountMismatch,
18 GenericArgCountResult, IsMethodCall, PathSeg,
20 use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryResponse};
21 use rustc_infer::infer::error_reporting::TypeAnnotationNeeded::E0282;
22 use rustc_infer::infer::InferResult;
23 use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
24 use rustc_middle::ty::error::TypeError;
25 use rustc_middle::ty::fold::TypeFoldable;
26 use rustc_middle::ty::visit::TypeVisitable;
27 use rustc_middle::ty::{
28 self, AdtKind, CanonicalUserType, DefIdTree, GenericParamDefKind, Ty, UserType,
30 use rustc_middle::ty::{GenericArgKind, SubstsRef, UserSelfTy, UserSubsts};
31 use rustc_session::lint;
32 use rustc_span::def_id::LocalDefId;
33 use rustc_span::hygiene::DesugaringKind;
34 use rustc_span::symbol::{kw, sym, Ident};
35 use rustc_span::{Span, DUMMY_SP};
36 use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
37 use rustc_trait_selection::traits::{self, NormalizeExt, ObligationCauseCode, ObligationCtxt};
39 use std::collections::hash_map::Entry;
42 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
43 /// Produces warning on the given node, if the current point in the
44 /// function is unreachable, and there hasn't been another warning.
45 pub(in super::super) fn warn_if_unreachable(&self, id: hir::HirId, span: Span, kind: &str) {
46 // FIXME: Combine these two 'if' expressions into one once
47 // let chains are implemented
48 if let Diverges::Always { span: orig_span, custom_note } = self.diverges.get() {
49 // If span arose from a desugaring of `if` or `while`, then it is the condition itself,
50 // which diverges, that we are about to lint on. This gives suboptimal diagnostics.
51 // Instead, stop here so that the `if`- or `while`-expression's block is linted instead.
52 if !span.is_desugaring(DesugaringKind::CondTemporary)
53 && !span.is_desugaring(DesugaringKind::Async)
54 && !orig_span.is_desugaring(DesugaringKind::Await)
56 self.diverges.set(Diverges::WarnedAlways);
58 debug!("warn_if_unreachable: id={:?} span={:?} kind={}", id, span, kind);
60 let msg = format!("unreachable {}", kind);
61 self.tcx().struct_span_lint_hir(
62 lint::builtin::UNREACHABLE_CODE,
67 lint.span_label(span, &msg).span_label(
70 .unwrap_or("any code following this expression is unreachable"),
78 /// Resolves type and const variables in `ty` if possible. Unlike the infcx
79 /// version (resolve_vars_if_possible), this version will
80 /// also select obligations if it seems useful, in an effort
81 /// to get more type information.
82 pub(in super::super) fn resolve_vars_with_obligations(&self, ty: Ty<'tcx>) -> Ty<'tcx> {
83 self.resolve_vars_with_obligations_and_mutate_fulfillment(ty, |_| {})
86 #[instrument(skip(self, mutate_fulfillment_errors), level = "debug", ret)]
87 pub(in super::super) fn resolve_vars_with_obligations_and_mutate_fulfillment(
90 mutate_fulfillment_errors: impl Fn(&mut Vec<traits::FulfillmentError<'tcx>>),
92 // No Infer()? Nothing needs doing.
93 if !ty.has_non_region_infer() {
94 debug!("no inference var, nothing needs doing");
98 // If `ty` is a type variable, see whether we already know what it is.
99 ty = self.resolve_vars_if_possible(ty);
100 if !ty.has_non_region_infer() {
105 // If not, try resolving pending obligations as much as
106 // possible. This can help substantially when there are
107 // indirect dependencies that don't seem worth tracking
109 self.select_obligations_where_possible(mutate_fulfillment_errors);
110 self.resolve_vars_if_possible(ty)
113 pub(in super::super) fn record_deferred_call_resolution(
115 closure_def_id: LocalDefId,
116 r: DeferredCallResolution<'tcx>,
118 let mut deferred_call_resolutions = self.deferred_call_resolutions.borrow_mut();
119 deferred_call_resolutions.entry(closure_def_id).or_default().push(r);
122 pub(in super::super) fn remove_deferred_call_resolutions(
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()
130 pub fn tag(&self) -> String {
131 format!("{:p}", self)
134 pub fn local_ty(&self, span: Span, nid: hir::HirId) -> LocalTy<'tcx> {
135 self.locals.borrow().get(&nid).cloned().unwrap_or_else(|| {
136 span_bug!(span, "no type for local variable {}", self.tcx.hir().node_to_string(nid))
141 pub fn write_ty(&self, id: hir::HirId, ty: Ty<'tcx>) {
142 debug!("write_ty({:?}, {:?}) in fcx {}", id, self.resolve_vars_if_possible(ty), self.tag());
143 self.typeck_results.borrow_mut().node_types_mut().insert(id, ty);
145 if let Err(e) = ty.error_reported() {
146 self.set_tainted_by_errors(e);
150 pub fn write_field_index(&self, hir_id: hir::HirId, index: usize) {
151 self.typeck_results.borrow_mut().field_indices_mut().insert(hir_id, index);
154 #[instrument(level = "debug", skip(self))]
155 pub(in super::super) fn write_resolution(
158 r: Result<(DefKind, DefId), ErrorGuaranteed>,
160 self.typeck_results.borrow_mut().type_dependent_defs_mut().insert(hir_id, r);
163 #[instrument(level = "debug", skip(self))]
164 pub fn write_method_call(&self, hir_id: hir::HirId, method: MethodCallee<'tcx>) {
165 self.write_resolution(hir_id, Ok((DefKind::AssocFn, method.def_id)));
166 self.write_substs(hir_id, method.substs);
169 pub fn write_substs(&self, node_id: hir::HirId, substs: SubstsRef<'tcx>) {
170 if !substs.is_empty() {
171 debug!("write_substs({:?}, {:?}) in fcx {}", node_id, substs, self.tag());
173 self.typeck_results.borrow_mut().node_substs_mut().insert(node_id, substs);
177 /// Given the substs that we just converted from the HIR, try to
178 /// canonicalize them and store them as user-given substitutions
179 /// (i.e., substitutions that must be respected by the NLL check).
181 /// This should be invoked **before any unifications have
182 /// occurred**, so that annotations like `Vec<_>` are preserved
184 #[instrument(skip(self), level = "debug")]
185 pub fn write_user_type_annotation_from_substs(
189 substs: SubstsRef<'tcx>,
190 user_self_ty: Option<UserSelfTy<'tcx>>,
192 debug!("fcx {}", self.tag());
194 if Self::can_contain_user_lifetime_bounds((substs, user_self_ty)) {
195 let canonicalized = self.canonicalize_user_type_annotation(UserType::TypeOf(
197 UserSubsts { substs, user_self_ty },
199 debug!(?canonicalized);
200 self.write_user_type_annotation(hir_id, canonicalized);
204 #[instrument(skip(self), level = "debug")]
205 pub fn write_user_type_annotation(
208 canonical_user_type_annotation: CanonicalUserType<'tcx>,
210 debug!("fcx {}", self.tag());
212 if !canonical_user_type_annotation.is_identity() {
215 .user_provided_types_mut()
216 .insert(hir_id, canonical_user_type_annotation);
218 debug!("skipping identity substs");
222 #[instrument(skip(self, expr), level = "debug")]
223 pub fn apply_adjustments(&self, expr: &hir::Expr<'_>, adj: Vec<Adjustment<'tcx>>) {
224 debug!("expr = {:#?}", expr);
231 if let Adjust::NeverToAny = a.kind {
232 if a.target.is_ty_var() {
233 self.diverging_type_vars.borrow_mut().insert(a.target);
234 debug!("apply_adjustments: adding `{:?}` as diverging type var", a.target);
239 let autoborrow_mut = adj.iter().any(|adj| {
243 kind: Adjust::Borrow(AutoBorrow::Ref(_, AutoBorrowMutability::Mut { .. })),
249 match self.typeck_results.borrow_mut().adjustments_mut().entry(expr.hir_id) {
250 Entry::Vacant(entry) => {
253 Entry::Occupied(mut entry) => {
254 debug!(" - composing on top of {:?}", entry.get());
255 match (&entry.get()[..], &adj[..]) {
256 // Applying any adjustment on top of a NeverToAny
257 // is a valid NeverToAny adjustment, because it can't
259 (&[Adjustment { kind: Adjust::NeverToAny, .. }], _) => return,
262 Adjustment { kind: Adjust::Deref(_), .. },
263 Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(..)), .. },
266 Adjustment { kind: Adjust::Deref(_), .. },
267 .., // Any following adjustments are allowed.
270 // A reborrow has no effect before a dereference.
272 // FIXME: currently we never try to compose autoderefs
273 // and ReifyFnPointer/UnsafeFnPointer, but we could.
275 self.tcx.sess.delay_span_bug(
278 "while adjusting {:?}, can't compose {:?} and {:?}",
286 *entry.get_mut() = adj;
290 // If there is an mutable auto-borrow, it is equivalent to `&mut <expr>`.
291 // In this case implicit use of `Deref` and `Index` within `<expr>` should
292 // instead be `DerefMut` and `IndexMut`, so fix those up.
294 self.convert_place_derefs_to_mutable(expr);
298 /// Instantiates and normalizes the bounds for a given item
299 pub(in super::super) fn instantiate_bounds(
303 substs: SubstsRef<'tcx>,
304 ) -> (ty::InstantiatedPredicates<'tcx>, Vec<Span>) {
305 let bounds = self.tcx.predicates_of(def_id);
306 let spans: Vec<Span> = bounds.predicates.iter().map(|(_, span)| *span).collect();
307 let result = bounds.instantiate(self.tcx, substs);
308 let result = self.normalize(span, result);
310 "instantiate_bounds(bounds={:?}, substs={:?}) = {:?}, {:?}",
311 bounds, substs, result, spans,
316 pub(in super::super) fn normalize<T>(&self, span: Span, value: T) -> T
318 T: TypeFoldable<'tcx>,
320 self.register_infer_ok_obligations(
321 self.at(&self.misc(span), self.param_env).normalize(value),
325 pub fn require_type_meets(
329 code: traits::ObligationCauseCode<'tcx>,
332 self.register_bound(ty, def_id, traits::ObligationCause::new(span, self.body_id, code));
335 pub fn require_type_is_sized(
339 code: traits::ObligationCauseCode<'tcx>,
341 if !ty.references_error() {
342 let lang_item = self.tcx.require_lang_item(LangItem::Sized, None);
343 self.require_type_meets(ty, span, code, lang_item);
347 pub fn require_type_is_sized_deferred(
351 code: traits::ObligationCauseCode<'tcx>,
353 if !ty.references_error() {
354 self.deferred_sized_obligations.borrow_mut().push((ty, span, code));
358 pub fn register_bound(
362 cause: traits::ObligationCause<'tcx>,
364 if !ty.references_error() {
365 self.fulfillment_cx.borrow_mut().register_bound(
375 pub fn handle_raw_ty(&self, span: Span, ty: Ty<'tcx>) -> RawTy<'tcx> {
376 RawTy { raw: ty, normalized: self.normalize(span, ty) }
379 pub fn to_ty(&self, ast_t: &hir::Ty<'_>) -> RawTy<'tcx> {
380 let t = self.astconv().ast_ty_to_ty(ast_t);
381 self.register_wf_obligation(t.into(), ast_t.span, traits::WellFormed(None));
382 self.handle_raw_ty(ast_t.span, t)
385 pub fn to_ty_saving_user_provided_ty(&self, ast_ty: &hir::Ty<'_>) -> Ty<'tcx> {
386 let ty = self.to_ty(ast_ty);
387 debug!("to_ty_saving_user_provided_ty: ty={:?}", ty);
389 if Self::can_contain_user_lifetime_bounds(ty.raw) {
390 let c_ty = self.canonicalize_response(UserType::Ty(ty.raw));
391 debug!("to_ty_saving_user_provided_ty: c_ty={:?}", c_ty);
392 self.typeck_results.borrow_mut().user_provided_types_mut().insert(ast_ty.hir_id, c_ty);
398 pub(super) fn user_substs_for_adt(ty: RawTy<'tcx>) -> UserSubsts<'tcx> {
399 match (ty.raw.kind(), ty.normalized.kind()) {
400 (ty::Adt(_, substs), _) => UserSubsts { substs, user_self_ty: None },
401 (_, ty::Adt(adt, substs)) => UserSubsts {
403 user_self_ty: Some(UserSelfTy { impl_def_id: adt.did(), self_ty: ty.raw }),
405 _ => bug!("non-adt type {:?}", ty),
409 pub fn array_length_to_const(&self, length: &hir::ArrayLen) -> ty::Const<'tcx> {
411 &hir::ArrayLen::Infer(_, span) => self.ct_infer(self.tcx.types.usize, None, span),
412 hir::ArrayLen::Body(anon_const) => {
413 let span = self.tcx.def_span(anon_const.def_id);
414 let c = ty::Const::from_anon_const(self.tcx, anon_const.def_id);
415 self.register_wf_obligation(c.into(), span, ObligationCauseCode::WellFormed(None));
416 self.normalize(span, c)
421 pub fn const_arg_to_const(
423 ast_c: &hir::AnonConst,
425 ) -> ty::Const<'tcx> {
427 ty::WithOptConstParam { did: ast_c.def_id, const_param_did: Some(param_def_id) };
428 let c = ty::Const::from_opt_const_arg_anon_const(self.tcx, const_def);
429 self.register_wf_obligation(
431 self.tcx.hir().span(ast_c.hir_id),
432 ObligationCauseCode::WellFormed(None),
437 // If the type given by the user has free regions, save it for later, since
438 // NLL would like to enforce those. Also pass in types that involve
439 // projections, since those can resolve to `'static` bounds (modulo #54940,
440 // which hopefully will be fixed by the time you see this comment, dear
441 // reader, although I have my doubts). Also pass in types with inference
442 // types, because they may be repeated. Other sorts of things are already
443 // sufficiently enforced with erased regions. =)
444 fn can_contain_user_lifetime_bounds<T>(t: T) -> bool
446 T: TypeVisitable<'tcx>,
448 t.has_free_regions() || t.has_projections() || t.has_infer_types()
451 pub fn node_ty(&self, id: hir::HirId) -> Ty<'tcx> {
452 match self.typeck_results.borrow().node_types().get(id) {
454 None if let Some(e) = self.tainted_by_errors() => self.tcx.ty_error_with_guaranteed(e),
457 "no type for node {} in fcx {}",
458 self.tcx.hir().node_to_string(id),
465 pub fn node_ty_opt(&self, id: hir::HirId) -> Option<Ty<'tcx>> {
466 match self.typeck_results.borrow().node_types().get(id) {
468 None if let Some(e) = self.tainted_by_errors() => Some(self.tcx.ty_error_with_guaranteed(e)),
473 /// Registers an obligation for checking later, during regionck, that `arg` is well-formed.
474 pub fn register_wf_obligation(
476 arg: ty::GenericArg<'tcx>,
478 code: traits::ObligationCauseCode<'tcx>,
480 // WF obligations never themselves fail, so no real need to give a detailed cause:
481 let cause = traits::ObligationCause::new(span, self.body_id, code);
482 self.register_predicate(traits::Obligation::new(
486 ty::Binder::dummy(ty::PredicateKind::WellFormed(arg)),
490 /// Registers obligations that all `substs` are well-formed.
491 pub fn add_wf_bounds(&self, substs: SubstsRef<'tcx>, expr: &hir::Expr<'_>) {
492 for arg in substs.iter().filter(|arg| {
493 matches!(arg.unpack(), GenericArgKind::Type(..) | GenericArgKind::Const(..))
495 self.register_wf_obligation(arg, expr.span, traits::WellFormed(None));
499 // FIXME(arielb1): use this instead of field.ty everywhere
500 // Only for fields! Returns <none> for methods>
501 // Indifferent to privacy flags
505 field: &'tcx ty::FieldDef,
506 substs: SubstsRef<'tcx>,
508 self.normalize(span, field.ty(self.tcx, substs))
511 pub(in super::super) fn resolve_rvalue_scopes(&self, def_id: DefId) {
512 let scope_tree = self.tcx.region_scope_tree(def_id);
513 let rvalue_scopes = { rvalue_scopes::resolve_rvalue_scopes(self, &scope_tree, def_id) };
514 let mut typeck_results = self.inh.typeck_results.borrow_mut();
515 typeck_results.rvalue_scopes = rvalue_scopes;
518 pub(in super::super) fn resolve_generator_interiors(&self, def_id: DefId) {
519 if self.tcx.sess.opts.unstable_opts.drop_tracking_mir {
520 self.save_generator_interior_predicates(def_id);
524 self.select_obligations_where_possible(|_| {});
526 let mut generators = self.deferred_generator_interiors.borrow_mut();
527 for (_, body_id, interior, kind) in generators.drain(..) {
528 crate::generator_interior::resolve_interior(self, def_id, body_id, interior, kind);
529 self.select_obligations_where_possible(|_| {});
533 /// Unify the inference variables corresponding to generator witnesses, and save all the
534 /// predicates that were stalled on those inference variables.
536 /// This process allows to conservatively save all predicates that do depend on the generator
537 /// interior types, for later processing by `check_generator_obligations`.
539 /// We must not attempt to select obligations after this method has run, or risk query cycle
541 #[instrument(level = "debug", skip(self))]
542 fn save_generator_interior_predicates(&self, def_id: DefId) {
543 // Try selecting all obligations that are not blocked on inference variables.
544 // Once we start unifying generator witnesses, trying to select obligations on them will
545 // trigger query cycle ICEs, as doing so requires MIR.
546 self.select_obligations_where_possible(|_| {});
548 let generators = std::mem::take(&mut *self.deferred_generator_interiors.borrow_mut());
551 for &(expr_def_id, body_id, interior, _) in generators.iter() {
552 debug!(?expr_def_id);
554 // Create the `GeneratorWitness` type that we will unify with `interior`.
555 let substs = ty::InternalSubsts::identity_for_item(
557 self.tcx.typeck_root_def_id(expr_def_id.to_def_id()),
559 let witness = self.tcx.mk_generator_witness_mir(expr_def_id.to_def_id(), substs);
561 // Unify `interior` with `witness` and collect all the resulting obligations.
562 let span = self.tcx.hir().body(body_id).value.span;
564 .at(&self.misc(span), self.param_env)
565 .eq(interior, witness)
566 .expect("Failed to unify generator interior type");
567 let mut obligations = ok.obligations;
569 // Also collect the obligations that were unstalled by this unification.
571 .extend(self.fulfillment_cx.borrow_mut().drain_unstalled_obligations(&self.infcx));
573 let obligations = obligations.into_iter().map(|o| (o.predicate, o.cause)).collect();
574 debug!(?obligations);
577 .generator_interior_predicates
578 .insert(expr_def_id, obligations);
582 #[instrument(skip(self), level = "debug")]
583 pub(in super::super) fn report_ambiguity_errors(&self) {
584 let mut errors = self.fulfillment_cx.borrow_mut().collect_remaining_errors();
586 if !errors.is_empty() {
587 self.adjust_fulfillment_errors_for_expr_obligation(&mut errors);
588 self.err_ctxt().report_fulfillment_errors(&errors, self.inh.body_id);
592 /// Select as many obligations as we can at present.
593 pub(in super::super) fn select_obligations_where_possible(
595 mutate_fulfillment_errors: impl Fn(&mut Vec<traits::FulfillmentError<'tcx>>),
597 let mut result = self.fulfillment_cx.borrow_mut().select_where_possible(self);
598 if !result.is_empty() {
599 mutate_fulfillment_errors(&mut result);
600 self.adjust_fulfillment_errors_for_expr_obligation(&mut result);
601 self.err_ctxt().report_fulfillment_errors(&result, self.inh.body_id);
605 /// For the overloaded place expressions (`*x`, `x[3]`), the trait
606 /// returns a type of `&T`, but the actual type we assign to the
607 /// *expression* is `T`. So this function just peels off the return
608 /// type by one layer to yield `T`.
609 pub(in super::super) fn make_overloaded_place_return_type(
611 method: MethodCallee<'tcx>,
612 ) -> ty::TypeAndMut<'tcx> {
613 // extract method return type, which will be &T;
614 let ret_ty = method.sig.output();
616 // method returns &T, but the type as visible to user is T, so deref
617 ret_ty.builtin_deref(true).unwrap()
620 #[instrument(skip(self), level = "debug")]
621 fn self_type_matches_expected_vid(&self, self_ty: Ty<'tcx>, expected_vid: ty::TyVid) -> bool {
622 let self_ty = self.shallow_resolve(self_ty);
625 match *self_ty.kind() {
626 ty::Infer(ty::TyVar(found_vid)) => {
627 // FIXME: consider using `sub_root_var` here so we
628 // can see through subtyping.
629 let found_vid = self.root_var(found_vid);
630 debug!("self_type_matches_expected_vid - found_vid={:?}", found_vid);
631 expected_vid == found_vid
637 #[instrument(skip(self), level = "debug")]
638 pub(in super::super) fn obligations_for_self_ty<'b>(
641 ) -> impl DoubleEndedIterator<Item = traits::PredicateObligation<'tcx>> + Captures<'tcx> + 'b
643 // FIXME: consider using `sub_root_var` here so we
644 // can see through subtyping.
645 let ty_var_root = self.root_var(self_ty);
646 trace!("pending_obligations = {:#?}", self.fulfillment_cx.borrow().pending_obligations());
648 self.fulfillment_cx.borrow().pending_obligations().into_iter().filter_map(
649 move |obligation| match &obligation.predicate.kind().skip_binder() {
650 ty::PredicateKind::Clause(ty::Clause::Projection(data))
651 if self.self_type_matches_expected_vid(
652 data.projection_ty.self_ty(),
658 ty::PredicateKind::Clause(ty::Clause::Trait(data))
659 if self.self_type_matches_expected_vid(data.self_ty(), ty_var_root) =>
664 ty::PredicateKind::Clause(ty::Clause::Trait(..))
665 | ty::PredicateKind::Clause(ty::Clause::Projection(..))
666 | ty::PredicateKind::Subtype(..)
667 | ty::PredicateKind::Coerce(..)
668 | ty::PredicateKind::Clause(ty::Clause::RegionOutlives(..))
669 | ty::PredicateKind::Clause(ty::Clause::TypeOutlives(..))
670 | ty::PredicateKind::WellFormed(..)
671 | ty::PredicateKind::ObjectSafe(..)
672 | ty::PredicateKind::ConstEvaluatable(..)
673 | ty::PredicateKind::ConstEquate(..)
674 // N.B., this predicate is created by breaking down a
675 // `ClosureType: FnFoo()` predicate, where
676 // `ClosureType` represents some `Closure`. It can't
677 // possibly be referring to the current closure,
678 // because we haven't produced the `Closure` for
679 // this closure yet; this is exactly why the other
680 // code is looking for a self type of an unresolved
681 // inference variable.
682 | ty::PredicateKind::ClosureKind(..)
683 | ty::PredicateKind::Ambiguous
684 | ty::PredicateKind::TypeWellFormedFromEnv(..) => None,
689 pub(in super::super) fn type_var_is_sized(&self, self_ty: ty::TyVid) -> bool {
690 let sized_did = self.tcx.lang_items().sized_trait();
691 self.obligations_for_self_ty(self_ty).any(|obligation| {
692 match obligation.predicate.kind().skip_binder() {
693 ty::PredicateKind::Clause(ty::Clause::Trait(data)) => {
694 Some(data.def_id()) == sized_did
701 pub(in super::super) fn err_args(&self, len: usize) -> Vec<Ty<'tcx>> {
702 vec![self.tcx.ty_error(); len]
705 /// Unifies the output type with the expected type early, for more coercions
706 /// and forward type information on the input expressions.
707 #[instrument(skip(self, call_span), level = "debug")]
708 pub(in super::super) fn expected_inputs_for_expected_output(
711 expected_ret: Expectation<'tcx>,
712 formal_ret: Ty<'tcx>,
713 formal_args: &[Ty<'tcx>],
714 ) -> Option<Vec<Ty<'tcx>>> {
715 let formal_ret = self.resolve_vars_with_obligations(formal_ret);
716 let ret_ty = expected_ret.only_has_type(self)?;
718 // HACK(oli-obk): This is a hack to keep RPIT and TAIT in sync wrt their behaviour.
719 // Without it, the inference
720 // variable will get instantiated with the opaque type. The inference variable often
721 // has various helpful obligations registered for it that help closures figure out their
722 // signature. If we infer the inference var to the opaque type, the closure won't be able
723 // to find those obligations anymore, and it can't necessarily find them from the opaque
724 // type itself. We could be more powerful with inference if we *combined* the obligations
725 // so that we got both the obligations from the opaque type and the ones from the inference
726 // variable. That will accept more code than we do right now, so we need to carefully consider
728 // Note: this check is pessimistic, as the inference type could be matched with something other
729 // than the opaque type, but then we need a new `TypeRelation` just for this specific case and
730 // can't re-use `sup` below.
731 // See tests/ui/impl-trait/hidden-type-is-opaque.rs and
732 // tests/ui/impl-trait/hidden-type-is-opaque-2.rs for examples that hit this path.
733 if formal_ret.has_infer_types() {
734 for ty in ret_ty.walk() {
735 if let ty::subst::GenericArgKind::Type(ty) = ty.unpack()
736 && let ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) = *ty.kind()
737 && let Some(def_id) = def_id.as_local()
738 && self.opaque_type_origin(def_id, DUMMY_SP).is_some() {
744 let expect_args = self
745 .fudge_inference_if_ok(|| {
746 let ocx = ObligationCtxt::new_in_snapshot(self);
748 // Attempt to apply a subtyping relationship between the formal
749 // return type (likely containing type variables if the function
750 // is polymorphic) and the expected return type.
751 // No argument expectations are produced if unification fails.
752 let origin = self.misc(call_span);
753 ocx.sup(&origin, self.param_env, ret_ty, formal_ret)?;
754 if !ocx.select_where_possible().is_empty() {
755 return Err(TypeError::Mismatch);
758 // Record all the argument types, with the substitutions
759 // produced from the above subtyping unification.
760 Ok(Some(formal_args.iter().map(|&ty| self.resolve_vars_if_possible(ty)).collect()))
762 .unwrap_or_default();
763 debug!(?formal_args, ?formal_ret, ?expect_args, ?expected_ret);
767 pub(in super::super) fn resolve_lang_item_path(
769 lang_item: hir::LangItem,
772 expr_hir_id: Option<hir::HirId>,
773 ) -> (Res, Ty<'tcx>) {
774 let def_id = self.tcx.require_lang_item(lang_item, Some(span));
775 let def_kind = self.tcx.def_kind(def_id);
777 let item_ty = if let DefKind::Variant = def_kind {
778 self.tcx.bound_type_of(self.tcx.parent(def_id))
780 self.tcx.bound_type_of(def_id)
782 let substs = self.fresh_substs_for_item(span, def_id);
783 let ty = item_ty.subst(self.tcx, substs);
785 self.write_resolution(hir_id, Ok((def_kind, def_id)));
787 let code = match lang_item {
788 hir::LangItem::IntoFutureIntoFuture => {
789 Some(ObligationCauseCode::AwaitableExpr(expr_hir_id))
791 hir::LangItem::IteratorNext | hir::LangItem::IntoIterIntoIter => {
792 Some(ObligationCauseCode::ForLoopIterator)
794 hir::LangItem::TryTraitFromOutput
795 | hir::LangItem::TryTraitFromResidual
796 | hir::LangItem::TryTraitBranch => Some(ObligationCauseCode::QuestionMark),
799 if let Some(code) = code {
800 self.add_required_obligations_with_code(span, def_id, substs, move |_, _| code.clone());
802 self.add_required_obligations_for_hir(span, def_id, substs, hir_id);
805 (Res::Def(def_kind, def_id), ty)
808 /// Resolves an associated value path into a base type and associated constant, or method
809 /// resolution. The newly resolved definition is written into `type_dependent_defs`.
810 pub fn resolve_ty_and_res_fully_qualified_call(
812 qpath: &'tcx QPath<'tcx>,
815 ) -> (Res, Option<RawTy<'tcx>>, &'tcx [hir::PathSegment<'tcx>]) {
817 "resolve_ty_and_res_fully_qualified_call: qpath={:?} hir_id={:?} span={:?}",
820 let (ty, qself, item_segment) = match *qpath {
821 QPath::Resolved(ref opt_qself, ref path) => {
824 opt_qself.as_ref().map(|qself| self.to_ty(qself)),
828 QPath::TypeRelative(ref qself, ref segment) => {
829 // Don't use `self.to_ty`, since this will register a WF obligation.
830 // If we're trying to call a non-existent method on a trait
831 // (e.g. `MyTrait::missing_method`), then resolution will
832 // give us a `QPath::TypeRelative` with a trait object as
833 // `qself`. In that case, we want to avoid registering a WF obligation
834 // for `dyn MyTrait`, since we don't actually need the trait
835 // to be object-safe.
836 // We manually call `register_wf_obligation` in the success path
838 let ty = self.astconv().ast_ty_to_ty_in_path(qself);
839 (self.handle_raw_ty(span, ty), qself, segment)
841 QPath::LangItem(..) => {
842 bug!("`resolve_ty_and_res_fully_qualified_call` called on `LangItem`")
845 if let Some(&cached_result) = self.typeck_results.borrow().type_dependent_defs().get(hir_id)
847 self.register_wf_obligation(ty.raw.into(), qself.span, traits::WellFormed(None));
848 // Return directly on cache hit. This is useful to avoid doubly reporting
849 // errors with default match binding modes. See #44614.
850 let def = cached_result.map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id));
851 return (def, Some(ty), slice::from_ref(&**item_segment));
853 let item_name = item_segment.ident;
855 .resolve_fully_qualified_call(span, item_name, ty.normalized, qself.span, hir_id)
857 let result = match error {
858 method::MethodError::PrivateMatch(kind, def_id, _) => Ok((kind, def_id)),
859 _ => Err(ErrorGuaranteed::unchecked_claim_error_was_emitted()),
862 // If we have a path like `MyTrait::missing_method`, then don't register
863 // a WF obligation for `dyn MyTrait` when method lookup fails. Otherwise,
864 // register a WF obligation so that we can detect any additional
865 // errors in the self type.
866 if !(matches!(error, method::MethodError::NoMatch(_)) && ty.normalized.is_trait()) {
867 self.register_wf_obligation(
870 traits::WellFormed(None),
873 if item_name.name != kw::Empty {
874 if let Some(mut e) = self.report_method_error(
878 SelfSource::QPath(qself),
881 Expectation::NoExpectation,
890 self.register_wf_obligation(ty.raw.into(), qself.span, traits::WellFormed(None));
893 // Write back the new resolution.
894 self.write_resolution(hir_id, result);
896 result.map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id)),
898 slice::from_ref(&**item_segment),
902 /// Given a function `Node`, return its `FnDecl` if it exists, or `None` otherwise.
903 pub(in super::super) fn get_node_fn_decl(
906 ) -> Option<(&'tcx hir::FnDecl<'tcx>, Ident, bool)> {
908 Node::Item(&hir::Item { ident, kind: hir::ItemKind::Fn(ref sig, ..), .. }) => {
909 // This is less than ideal, it will not suggest a return type span on any
910 // method called `main`, regardless of whether it is actually the entry point,
911 // but it will still present it as the reason for the expected type.
912 Some((&sig.decl, ident, ident.name != sym::main))
914 Node::TraitItem(&hir::TraitItem {
916 kind: hir::TraitItemKind::Fn(ref sig, ..),
918 }) => Some((&sig.decl, ident, true)),
919 Node::ImplItem(&hir::ImplItem {
921 kind: hir::ImplItemKind::Fn(ref sig, ..),
923 }) => Some((&sig.decl, ident, false)),
928 /// Given a `HirId`, return the `FnDecl` of the method it is enclosed by and whether a
929 /// suggestion can be made, `None` otherwise.
930 pub fn get_fn_decl(&self, blk_id: hir::HirId) -> Option<(&'tcx hir::FnDecl<'tcx>, bool)> {
931 // Get enclosing Fn, if it is a function or a trait method, unless there's a `loop` or
932 // `while` before reaching it, as block tail returns are not available in them.
933 self.tcx.hir().get_return_block(blk_id).and_then(|blk_id| {
934 let parent = self.tcx.hir().get(blk_id);
935 self.get_node_fn_decl(parent).map(|(fn_decl, _, is_main)| (fn_decl, is_main))
939 pub(in super::super) fn note_internal_mutation_in_method(
941 err: &mut Diagnostic,
942 expr: &hir::Expr<'_>,
946 if found != self.tcx.types.unit {
949 if let ExprKind::MethodCall(path_segment, rcvr, ..) = expr.kind {
953 .expr_ty_adjusted_opt(rcvr)
954 .map_or(true, |ty| expected.peel_refs() != ty.peel_refs())
958 let mut sp = MultiSpan::from_span(path_segment.ident.span);
960 path_segment.ident.span,
962 "this call modifies {} in-place",
964 ExprKind::Path(QPath::Resolved(
966 hir::Path { segments: [segment], .. },
967 )) => format!("`{}`", segment.ident),
968 _ => "its receiver".to_string(),
974 "you probably want to use this value after calling the method...",
978 &format!("method `{}` modifies its receiver in-place", path_segment.ident),
980 err.note(&format!("...instead of the `()` output of method `{}`", path_segment.ident));
984 // Instantiates the given path, which must refer to an item with the given
985 // number of type parameters and type.
986 #[instrument(skip(self, span), level = "debug")]
987 pub fn instantiate_value_path(
989 segments: &[hir::PathSegment<'_>],
990 self_ty: Option<RawTy<'tcx>>,
994 ) -> (Ty<'tcx>, Res) {
997 let path_segs = match res {
998 Res::Local(_) | Res::SelfCtor(_) => vec![],
999 Res::Def(kind, def_id) => self.astconv().def_ids_for_value_path_segments(
1001 self_ty.map(|ty| ty.raw),
1006 _ => bug!("instantiate_value_path on {:?}", res),
1009 let mut user_self_ty = None;
1010 let mut is_alias_variant_ctor = false;
1012 Res::Def(DefKind::Ctor(CtorOf::Variant, _), _)
1013 if let Some(self_ty) = self_ty =>
1015 let adt_def = self_ty.normalized.ty_adt_def().unwrap();
1016 user_self_ty = Some(UserSelfTy { impl_def_id: adt_def.did(), self_ty: self_ty.raw });
1017 is_alias_variant_ctor = true;
1019 Res::Def(DefKind::AssocFn | DefKind::AssocConst, def_id) => {
1020 let assoc_item = tcx.associated_item(def_id);
1021 let container = assoc_item.container;
1022 let container_id = assoc_item.container_id(tcx);
1023 debug!(?def_id, ?container, ?container_id);
1025 ty::TraitContainer => {
1026 callee::check_legal_trait_for_method_call(tcx, span, None, span, container_id)
1028 ty::ImplContainer => {
1029 if segments.len() == 1 {
1030 // `<T>::assoc` will end up here, and so
1031 // can `T::assoc`. It this came from an
1032 // inherent impl, we need to record the
1033 // `T` for posterity (see `UserSelfTy` for
1035 let self_ty = self_ty.expect("UFCS sugared assoc missing Self").raw;
1036 user_self_ty = Some(UserSelfTy { impl_def_id: container_id, self_ty });
1044 // Now that we have categorized what space the parameters for each
1045 // segment belong to, let's sort out the parameters that the user
1046 // provided (if any) into their appropriate spaces. We'll also report
1047 // errors if type parameters are provided in an inappropriate place.
1049 let generic_segs: FxHashSet<_> = path_segs.iter().map(|PathSeg(_, index)| index).collect();
1050 let generics_has_err = self.astconv().prohibit_generics(
1051 segments.iter().enumerate().filter_map(|(index, seg)| {
1052 if !generic_segs.contains(&index) || is_alias_variant_ctor {
1061 if let Res::Local(hid) = res {
1062 let ty = self.local_ty(span, hid).decl_ty;
1063 let ty = self.normalize(span, ty);
1064 self.write_ty(hir_id, ty);
1068 if generics_has_err {
1069 // Don't try to infer type parameters when prohibited generic arguments were given.
1070 user_self_ty = None;
1073 // Now we have to compare the types that the user *actually*
1074 // provided against the types that were *expected*. If the user
1075 // did not provide any types, then we want to substitute inference
1076 // variables. If the user provided some types, we may still need
1077 // to add defaults. If the user provided *too many* types, that's
1080 let mut infer_args_for_err = FxHashSet::default();
1082 let mut explicit_late_bound = ExplicitLateBound::No;
1083 for &PathSeg(def_id, index) in &path_segs {
1084 let seg = &segments[index];
1085 let generics = tcx.generics_of(def_id);
1087 // Argument-position `impl Trait` is treated as a normal generic
1088 // parameter internally, but we don't allow users to specify the
1089 // parameter's value explicitly, so we have to do some error-
1091 let arg_count = check_generic_arg_count_for_call(
1100 if let ExplicitLateBound::Yes = arg_count.explicit_late_bound {
1101 explicit_late_bound = ExplicitLateBound::Yes;
1104 if let Err(GenericArgCountMismatch { reported: Some(e), .. }) = arg_count.correct {
1105 infer_args_for_err.insert(index);
1106 self.set_tainted_by_errors(e); // See issue #53251.
1110 let has_self = path_segs
1112 .map(|PathSeg(def_id, _)| tcx.generics_of(*def_id).has_self)
1115 let (res, self_ctor_substs) = if let Res::SelfCtor(impl_def_id) = res {
1116 let ty = self.handle_raw_ty(span, tcx.at(span).type_of(impl_def_id));
1117 match ty.normalized.ty_adt_def() {
1118 Some(adt_def) if adt_def.has_ctor() => {
1119 let (ctor_kind, ctor_def_id) = adt_def.non_enum_variant().ctor.unwrap();
1120 let new_res = Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
1121 let user_substs = Self::user_substs_for_adt(ty);
1122 user_self_ty = user_substs.user_self_ty;
1123 (new_res, Some(user_substs.substs))
1126 let mut err = tcx.sess.struct_span_err(
1128 "the `Self` constructor can only be used with tuple or unit structs",
1130 if let Some(adt_def) = ty.normalized.ty_adt_def() {
1131 match adt_def.adt_kind() {
1133 err.help("did you mean to use one of the enum's variants?");
1135 AdtKind::Struct | AdtKind::Union => {
1136 err.span_suggestion(
1138 "use curly brackets",
1139 "Self { /* fields */ }",
1140 Applicability::HasPlaceholders,
1145 let reported = err.emit();
1146 return (tcx.ty_error_with_guaranteed(reported), res);
1152 let def_id = res.def_id();
1154 let arg_count = GenericArgCountResult {
1155 explicit_late_bound,
1156 correct: if infer_args_for_err.is_empty() {
1159 Err(GenericArgCountMismatch::default())
1163 struct CreateCtorSubstsContext<'a, 'tcx> {
1164 fcx: &'a FnCtxt<'a, 'tcx>,
1166 path_segs: &'a [PathSeg],
1167 infer_args_for_err: &'a FxHashSet<usize>,
1168 segments: &'a [hir::PathSegment<'a>],
1170 impl<'tcx, 'a> CreateSubstsForGenericArgsCtxt<'a, 'tcx> for CreateCtorSubstsContext<'a, 'tcx> {
1174 ) -> (Option<&'a hir::GenericArgs<'a>>, bool) {
1175 if let Some(&PathSeg(_, index)) =
1176 self.path_segs.iter().find(|&PathSeg(did, _)| *did == def_id)
1178 // If we've encountered an `impl Trait`-related error, we're just
1179 // going to infer the arguments for better error messages.
1180 if !self.infer_args_for_err.contains(&index) {
1181 // Check whether the user has provided generic arguments.
1182 if let Some(ref data) = self.segments[index].args {
1183 return (Some(data), self.segments[index].infer_args);
1186 return (None, self.segments[index].infer_args);
1194 param: &ty::GenericParamDef,
1195 arg: &GenericArg<'_>,
1196 ) -> ty::GenericArg<'tcx> {
1197 match (¶m.kind, arg) {
1198 (GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
1199 self.fcx.astconv().ast_region_to_region(lt, Some(param)).into()
1201 (GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
1202 self.fcx.to_ty(ty).raw.into()
1204 (GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => {
1205 self.fcx.const_arg_to_const(&ct.value, param.def_id).into()
1207 (GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
1208 self.fcx.ty_infer(Some(param), inf.span).into()
1210 (GenericParamDefKind::Const { .. }, GenericArg::Infer(inf)) => {
1211 let tcx = self.fcx.tcx();
1212 self.fcx.ct_infer(tcx.type_of(param.def_id), Some(param), inf.span).into()
1214 _ => unreachable!(),
1220 substs: Option<&[ty::GenericArg<'tcx>]>,
1221 param: &ty::GenericParamDef,
1223 ) -> ty::GenericArg<'tcx> {
1224 let tcx = self.fcx.tcx();
1226 GenericParamDefKind::Lifetime => {
1227 self.fcx.re_infer(Some(param), self.span).unwrap().into()
1229 GenericParamDefKind::Type { has_default, .. } => {
1230 if !infer_args && has_default {
1231 // If we have a default, then we it doesn't matter that we're not
1232 // inferring the type arguments: we provide the default where any
1234 tcx.bound_type_of(param.def_id).subst(tcx, substs.unwrap()).into()
1236 // If no type arguments were provided, we have to infer them.
1237 // This case also occurs as a result of some malformed input, e.g.
1238 // a lifetime argument being given instead of a type parameter.
1239 // Using inference instead of `Error` gives better error messages.
1240 self.fcx.var_for_def(self.span, param)
1243 GenericParamDefKind::Const { has_default } => {
1244 if !infer_args && has_default {
1245 tcx.const_param_default(param.def_id).subst(tcx, substs.unwrap()).into()
1247 self.fcx.var_for_def(self.span, param)
1254 let substs_raw = self_ctor_substs.unwrap_or_else(|| {
1255 create_substs_for_generic_args(
1260 self_ty.map(|s| s.raw),
1262 &mut CreateCtorSubstsContext {
1265 path_segs: &path_segs,
1266 infer_args_for_err: &infer_args_for_err,
1272 // First, store the "user substs" for later.
1273 self.write_user_type_annotation_from_substs(hir_id, def_id, substs_raw, user_self_ty);
1275 // Normalize only after registering type annotations.
1276 let substs = self.normalize(span, substs_raw);
1278 self.add_required_obligations_for_hir(span, def_id, &substs, hir_id);
1280 // Substitute the values for the type parameters into the type of
1281 // the referenced item.
1282 let ty = tcx.bound_type_of(def_id);
1283 assert!(!substs.has_escaping_bound_vars());
1284 assert!(!ty.0.has_escaping_bound_vars());
1285 let ty_substituted = self.normalize(span, ty.subst(tcx, substs));
1287 if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty {
1288 // In the case of `Foo<T>::method` and `<Foo<T>>::method`, if `method`
1289 // is inherent, there is no `Self` parameter; instead, the impl needs
1290 // type parameters, which we can infer by unifying the provided `Self`
1291 // with the substituted impl type.
1292 // This also occurs for an enum variant on a type alias.
1293 let impl_ty = self.normalize(span, tcx.bound_type_of(impl_def_id).subst(tcx, substs));
1294 let self_ty = self.normalize(span, self_ty);
1295 match self.at(&self.misc(span), self.param_env).eq(impl_ty, self_ty) {
1296 Ok(ok) => self.register_infer_ok_obligations(ok),
1298 self.tcx.sess.delay_span_bug(
1301 "instantiate_value_path: (UFCS) {:?} was a subtype of {:?} but now is not?",
1310 debug!("instantiate_value_path: type of {:?} is {:?}", hir_id, ty_substituted);
1311 self.write_substs(hir_id, substs);
1313 (ty_substituted, res)
1316 /// Add all the obligations that are required, substituting and normalized appropriately.
1317 pub(crate) fn add_required_obligations_for_hir(
1321 substs: SubstsRef<'tcx>,
1324 self.add_required_obligations_with_code(span, def_id, substs, |idx, span| {
1325 if span.is_dummy() {
1326 ObligationCauseCode::ExprItemObligation(def_id, hir_id, idx)
1328 ObligationCauseCode::ExprBindingObligation(def_id, span, hir_id, idx)
1333 #[instrument(level = "debug", skip(self, code, span, substs))]
1334 fn add_required_obligations_with_code(
1338 substs: SubstsRef<'tcx>,
1339 code: impl Fn(usize, Span) -> ObligationCauseCode<'tcx>,
1341 let param_env = self.param_env;
1343 let remap = match self.tcx.def_kind(def_id) {
1344 // Associated consts have `Self: ~const Trait` bounds that should be satisfiable when
1345 // `Self: Trait` is satisfied because it does not matter whether the impl is `const`.
1346 // Therefore we have to remap the param env here to be non-const.
1347 hir::def::DefKind::AssocConst => true,
1348 hir::def::DefKind::AssocFn
1349 if self.tcx.def_kind(self.tcx.parent(def_id)) == hir::def::DefKind::Trait =>
1351 // N.B.: All callsites to this function involve checking a path expression.
1353 // When instantiating a trait method as a function item, it does not actually matter whether
1354 // the trait is `const` or not, or whether `where T: ~const Tr` needs to be satisfied as
1355 // `const`. If we were to introduce instantiating trait methods as `const fn`s, we would
1356 // check that after this, either via a bound `where F: ~const FnOnce` or when coercing to a
1357 // `const fn` pointer.
1359 // FIXME(fee1-dead) FIXME(const_trait_impl): update this doc when trait methods can satisfy
1360 // `~const FnOnce` or can be coerced to `const fn` pointer.
1365 let (bounds, _) = self.instantiate_bounds(span, def_id, &substs);
1367 for mut obligation in traits::predicates_for_generics(
1368 |idx, predicate_span| {
1369 traits::ObligationCause::new(span, self.body_id, code(idx, predicate_span))
1375 obligation = obligation.without_const(self.tcx);
1377 self.register_predicate(obligation);
1381 /// Resolves `typ` by a single level if `typ` is a type variable.
1382 /// If no resolution is possible, then an error is reported.
1383 /// Numeric inference variables may be left unresolved.
1384 pub fn structurally_resolved_type(&self, sp: Span, ty: Ty<'tcx>) -> Ty<'tcx> {
1385 let ty = self.resolve_vars_with_obligations(ty);
1386 if !ty.is_ty_var() {
1389 let e = self.tainted_by_errors().unwrap_or_else(|| {
1391 .emit_inference_failure_err((**self).body_id, sp, ty.into(), E0282, true)
1394 let err = self.tcx.ty_error_with_guaranteed(e);
1395 self.demand_suptype(sp, err, ty);
1400 pub(in super::super) fn with_breakable_ctxt<F: FnOnce() -> R, R>(
1403 ctxt: BreakableCtxt<'tcx>,
1405 ) -> (BreakableCtxt<'tcx>, R) {
1408 let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
1409 index = enclosing_breakables.stack.len();
1410 enclosing_breakables.by_id.insert(id, index);
1411 enclosing_breakables.stack.push(ctxt);
1415 let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
1416 debug_assert!(enclosing_breakables.stack.len() == index + 1);
1417 enclosing_breakables.by_id.remove(&id).expect("missing breakable context");
1418 enclosing_breakables.stack.pop().expect("missing breakable context")
1423 /// Instantiate a QueryResponse in a probe context, without a
1424 /// good ObligationCause.
1425 pub(in super::super) fn probe_instantiate_query_response(
1428 original_values: &OriginalQueryValues<'tcx>,
1429 query_result: &Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>,
1430 ) -> InferResult<'tcx, Ty<'tcx>> {
1431 self.instantiate_query_response_and_region_obligations(
1432 &traits::ObligationCause::misc(span, self.body_id),
1439 /// Returns `true` if an expression is contained inside the LHS of an assignment expression.
1440 pub(in super::super) fn expr_in_place(&self, mut expr_id: hir::HirId) -> bool {
1441 let mut contained_in_place = false;
1443 while let hir::Node::Expr(parent_expr) = self.tcx.hir().get_parent(expr_id) {
1444 match &parent_expr.kind {
1445 hir::ExprKind::Assign(lhs, ..) | hir::ExprKind::AssignOp(_, lhs, ..) => {
1446 if lhs.hir_id == expr_id {
1447 contained_in_place = true;
1453 expr_id = parent_expr.hir_id;