]>
Commit | Line | Data |
---|---|---|
2b03887a FG |
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}; | |
29967ef6 XL |
5 | use rustc_data_structures::captures::Captures; |
6 | use rustc_data_structures::fx::FxHashSet; | |
04454e1e | 7 | use rustc_errors::{Applicability, Diagnostic, ErrorGuaranteed, MultiSpan}; |
29967ef6 XL |
8 | use rustc_hir as hir; |
9 | use rustc_hir::def::{CtorOf, DefKind, Res}; | |
10 | use rustc_hir::def_id::DefId; | |
11 | use rustc_hir::lang_items::LangItem; | |
a2a8927a | 12 | use rustc_hir::{ExprKind, GenericArg, Node, QPath}; |
2b03887a FG |
13 | use rustc_hir_analysis::astconv::{ |
14 | AstConv, CreateSubstsForGenericArgsCtxt, ExplicitLateBound, GenericArgCountMismatch, | |
15 | GenericArgCountResult, IsMethodCall, PathSeg, | |
16 | }; | |
29967ef6 XL |
17 | use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryResponse}; |
18 | use rustc_infer::infer::error_reporting::TypeAnnotationNeeded::E0282; | |
487cf647 | 19 | use rustc_infer::infer::InferResult; |
29967ef6 | 20 | use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability}; |
487cf647 | 21 | use rustc_middle::ty::error::TypeError; |
29967ef6 | 22 | use rustc_middle::ty::fold::TypeFoldable; |
064997fb | 23 | use rustc_middle::ty::visit::TypeVisitable; |
29967ef6 | 24 | use rustc_middle::ty::{ |
487cf647 | 25 | self, AdtKind, CanonicalUserType, DefIdTree, EarlyBinder, GenericParamDefKind, Ty, UserType, |
29967ef6 | 26 | }; |
2b03887a | 27 | use rustc_middle::ty::{GenericArgKind, InternalSubsts, SubstsRef, UserSelfTy, UserSubsts}; |
29967ef6 | 28 | use rustc_session::lint; |
064997fb | 29 | use rustc_span::def_id::LocalDefId; |
94222f64 | 30 | use rustc_span::hygiene::DesugaringKind; |
29967ef6 | 31 | use rustc_span::symbol::{kw, sym, Ident}; |
064997fb | 32 | use rustc_span::{Span, DUMMY_SP}; |
2b03887a | 33 | use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _; |
487cf647 | 34 | use rustc_trait_selection::traits::{self, NormalizeExt, ObligationCauseCode, ObligationCtxt}; |
29967ef6 XL |
35 | |
36 | use std::collections::hash_map::Entry; | |
37 | use std::slice; | |
38 | ||
39 | impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
40 | /// Produces warning on the given node, if the current point in the | |
41 | /// function is unreachable, and there hasn't been another warning. | |
42 | pub(in super::super) fn warn_if_unreachable(&self, id: hir::HirId, span: Span, kind: &str) { | |
43 | // FIXME: Combine these two 'if' expressions into one once | |
44 | // let chains are implemented | |
45 | if let Diverges::Always { span: orig_span, custom_note } = self.diverges.get() { | |
46 | // If span arose from a desugaring of `if` or `while`, then it is the condition itself, | |
47 | // which diverges, that we are about to lint on. This gives suboptimal diagnostics. | |
48 | // Instead, stop here so that the `if`- or `while`-expression's block is linted instead. | |
49 | if !span.is_desugaring(DesugaringKind::CondTemporary) | |
50 | && !span.is_desugaring(DesugaringKind::Async) | |
51 | && !orig_span.is_desugaring(DesugaringKind::Await) | |
52 | { | |
53 | self.diverges.set(Diverges::WarnedAlways); | |
54 | ||
55 | debug!("warn_if_unreachable: id={:?} span={:?} kind={}", id, span, kind); | |
56 | ||
2b03887a FG |
57 | let msg = format!("unreachable {}", kind); |
58 | self.tcx().struct_span_lint_hir( | |
59 | lint::builtin::UNREACHABLE_CODE, | |
60 | id, | |
61 | span, | |
62 | &msg, | |
63 | |lint| { | |
64 | lint.span_label(span, &msg).span_label( | |
29967ef6 XL |
65 | orig_span, |
66 | custom_note | |
67 | .unwrap_or("any code following this expression is unreachable"), | |
68 | ) | |
2b03887a FG |
69 | }, |
70 | ) | |
29967ef6 XL |
71 | } |
72 | } | |
73 | } | |
74 | ||
75 | /// Resolves type and const variables in `ty` if possible. Unlike the infcx | |
76 | /// version (resolve_vars_if_possible), this version will | |
77 | /// also select obligations if it seems useful, in an effort | |
78 | /// to get more type information. | |
c295e0f8 XL |
79 | pub(in super::super) fn resolve_vars_with_obligations(&self, ty: Ty<'tcx>) -> Ty<'tcx> { |
80 | self.resolve_vars_with_obligations_and_mutate_fulfillment(ty, |_| {}) | |
81 | } | |
29967ef6 | 82 | |
f2b60f7d | 83 | #[instrument(skip(self, mutate_fulfillment_errors), level = "debug", ret)] |
c295e0f8 XL |
84 | pub(in super::super) fn resolve_vars_with_obligations_and_mutate_fulfillment( |
85 | &self, | |
86 | mut ty: Ty<'tcx>, | |
87 | mutate_fulfillment_errors: impl Fn(&mut Vec<traits::FulfillmentError<'tcx>>), | |
88 | ) -> Ty<'tcx> { | |
29967ef6 | 89 | // No Infer()? Nothing needs doing. |
2b03887a | 90 | if !ty.has_non_region_infer() { |
c295e0f8 | 91 | debug!("no inference var, nothing needs doing"); |
29967ef6 XL |
92 | return ty; |
93 | } | |
94 | ||
95 | // If `ty` is a type variable, see whether we already know what it is. | |
fc512014 | 96 | ty = self.resolve_vars_if_possible(ty); |
2b03887a | 97 | if !ty.has_non_region_infer() { |
c295e0f8 | 98 | debug!(?ty); |
29967ef6 XL |
99 | return ty; |
100 | } | |
101 | ||
102 | // If not, try resolving pending obligations as much as | |
103 | // possible. This can help substantially when there are | |
104 | // indirect dependencies that don't seem worth tracking | |
105 | // precisely. | |
487cf647 | 106 | self.select_obligations_where_possible(mutate_fulfillment_errors); |
f2b60f7d | 107 | self.resolve_vars_if_possible(ty) |
29967ef6 XL |
108 | } |
109 | ||
110 | pub(in super::super) fn record_deferred_call_resolution( | |
111 | &self, | |
064997fb | 112 | closure_def_id: LocalDefId, |
29967ef6 XL |
113 | r: DeferredCallResolution<'tcx>, |
114 | ) { | |
115 | let mut deferred_call_resolutions = self.deferred_call_resolutions.borrow_mut(); | |
116 | deferred_call_resolutions.entry(closure_def_id).or_default().push(r); | |
117 | } | |
118 | ||
119 | pub(in super::super) fn remove_deferred_call_resolutions( | |
120 | &self, | |
064997fb | 121 | closure_def_id: LocalDefId, |
29967ef6 XL |
122 | ) -> Vec<DeferredCallResolution<'tcx>> { |
123 | let mut deferred_call_resolutions = self.deferred_call_resolutions.borrow_mut(); | |
124 | deferred_call_resolutions.remove(&closure_def_id).unwrap_or_default() | |
125 | } | |
126 | ||
127 | pub fn tag(&self) -> String { | |
128 | format!("{:p}", self) | |
129 | } | |
130 | ||
131 | pub fn local_ty(&self, span: Span, nid: hir::HirId) -> LocalTy<'tcx> { | |
132 | self.locals.borrow().get(&nid).cloned().unwrap_or_else(|| { | |
133 | span_bug!(span, "no type for local variable {}", self.tcx.hir().node_to_string(nid)) | |
134 | }) | |
135 | } | |
136 | ||
137 | #[inline] | |
138 | pub fn write_ty(&self, id: hir::HirId, ty: Ty<'tcx>) { | |
fc512014 | 139 | debug!("write_ty({:?}, {:?}) in fcx {}", id, self.resolve_vars_if_possible(ty), self.tag()); |
29967ef6 XL |
140 | self.typeck_results.borrow_mut().node_types_mut().insert(id, ty); |
141 | ||
487cf647 FG |
142 | if let Err(e) = ty.error_reported() { |
143 | self.set_tainted_by_errors(e); | |
29967ef6 XL |
144 | } |
145 | } | |
146 | ||
147 | pub fn write_field_index(&self, hir_id: hir::HirId, index: usize) { | |
148 | self.typeck_results.borrow_mut().field_indices_mut().insert(hir_id, index); | |
149 | } | |
150 | ||
064997fb | 151 | #[instrument(level = "debug", skip(self))] |
29967ef6 XL |
152 | pub(in super::super) fn write_resolution( |
153 | &self, | |
154 | hir_id: hir::HirId, | |
5e7ed085 | 155 | r: Result<(DefKind, DefId), ErrorGuaranteed>, |
29967ef6 XL |
156 | ) { |
157 | self.typeck_results.borrow_mut().type_dependent_defs_mut().insert(hir_id, r); | |
158 | } | |
159 | ||
064997fb | 160 | #[instrument(level = "debug", skip(self))] |
29967ef6 | 161 | pub fn write_method_call(&self, hir_id: hir::HirId, method: MethodCallee<'tcx>) { |
29967ef6 XL |
162 | self.write_resolution(hir_id, Ok((DefKind::AssocFn, method.def_id))); |
163 | self.write_substs(hir_id, method.substs); | |
164 | ||
165 | // When the method is confirmed, the `method.substs` includes | |
166 | // parameters from not just the method, but also the impl of | |
167 | // the method -- in particular, the `Self` type will be fully | |
168 | // resolved. However, those are not something that the "user | |
169 | // specified" -- i.e., those types come from the inferred type | |
170 | // of the receiver, not something the user wrote. So when we | |
171 | // create the user-substs, we want to replace those earlier | |
172 | // types with just the types that the user actually wrote -- | |
173 | // that is, those that appear on the *method itself*. | |
174 | // | |
175 | // As an example, if the user wrote something like | |
176 | // `foo.bar::<u32>(...)` -- the `Self` type here will be the | |
177 | // type of `foo` (possibly adjusted), but we don't want to | |
178 | // include that. We want just the `[_, u32]` part. | |
5099ac24 | 179 | if !method.substs.is_empty() { |
29967ef6 XL |
180 | let method_generics = self.tcx.generics_of(method.def_id); |
181 | if !method_generics.params.is_empty() { | |
064997fb | 182 | let user_type_annotation = self.probe(|_| { |
29967ef6 XL |
183 | let user_substs = UserSubsts { |
184 | substs: InternalSubsts::for_item(self.tcx, method.def_id, |param, _| { | |
185 | let i = param.index as usize; | |
186 | if i < method_generics.parent_count { | |
064997fb | 187 | self.var_for_def(DUMMY_SP, param) |
29967ef6 XL |
188 | } else { |
189 | method.substs[i] | |
190 | } | |
191 | }), | |
192 | user_self_ty: None, // not relevant here | |
193 | }; | |
194 | ||
064997fb | 195 | self.canonicalize_user_type_annotation(UserType::TypeOf( |
29967ef6 XL |
196 | method.def_id, |
197 | user_substs, | |
198 | )) | |
199 | }); | |
200 | ||
201 | debug!("write_method_call: user_type_annotation={:?}", user_type_annotation); | |
202 | self.write_user_type_annotation(hir_id, user_type_annotation); | |
203 | } | |
204 | } | |
205 | } | |
206 | ||
207 | pub fn write_substs(&self, node_id: hir::HirId, substs: SubstsRef<'tcx>) { | |
5099ac24 | 208 | if !substs.is_empty() { |
29967ef6 XL |
209 | debug!("write_substs({:?}, {:?}) in fcx {}", node_id, substs, self.tag()); |
210 | ||
211 | self.typeck_results.borrow_mut().node_substs_mut().insert(node_id, substs); | |
212 | } | |
213 | } | |
214 | ||
215 | /// Given the substs that we just converted from the HIR, try to | |
216 | /// canonicalize them and store them as user-given substitutions | |
217 | /// (i.e., substitutions that must be respected by the NLL check). | |
218 | /// | |
219 | /// This should be invoked **before any unifications have | |
220 | /// occurred**, so that annotations like `Vec<_>` are preserved | |
221 | /// properly. | |
c295e0f8 | 222 | #[instrument(skip(self), level = "debug")] |
29967ef6 XL |
223 | pub fn write_user_type_annotation_from_substs( |
224 | &self, | |
225 | hir_id: hir::HirId, | |
226 | def_id: DefId, | |
227 | substs: SubstsRef<'tcx>, | |
228 | user_self_ty: Option<UserSelfTy<'tcx>>, | |
229 | ) { | |
c295e0f8 | 230 | debug!("fcx {}", self.tag()); |
29967ef6 | 231 | |
5099ac24 | 232 | if Self::can_contain_user_lifetime_bounds((substs, user_self_ty)) { |
064997fb | 233 | let canonicalized = self.canonicalize_user_type_annotation(UserType::TypeOf( |
29967ef6 XL |
234 | def_id, |
235 | UserSubsts { substs, user_self_ty }, | |
236 | )); | |
c295e0f8 | 237 | debug!(?canonicalized); |
29967ef6 XL |
238 | self.write_user_type_annotation(hir_id, canonicalized); |
239 | } | |
240 | } | |
241 | ||
c295e0f8 | 242 | #[instrument(skip(self), level = "debug")] |
29967ef6 XL |
243 | pub fn write_user_type_annotation( |
244 | &self, | |
245 | hir_id: hir::HirId, | |
246 | canonical_user_type_annotation: CanonicalUserType<'tcx>, | |
247 | ) { | |
c295e0f8 | 248 | debug!("fcx {}", self.tag()); |
29967ef6 XL |
249 | |
250 | if !canonical_user_type_annotation.is_identity() { | |
251 | self.typeck_results | |
252 | .borrow_mut() | |
253 | .user_provided_types_mut() | |
254 | .insert(hir_id, canonical_user_type_annotation); | |
255 | } else { | |
c295e0f8 | 256 | debug!("skipping identity substs"); |
29967ef6 XL |
257 | } |
258 | } | |
259 | ||
c295e0f8 | 260 | #[instrument(skip(self, expr), level = "debug")] |
29967ef6 | 261 | pub fn apply_adjustments(&self, expr: &hir::Expr<'_>, adj: Vec<Adjustment<'tcx>>) { |
c295e0f8 | 262 | debug!("expr = {:#?}", expr); |
29967ef6 XL |
263 | |
264 | if adj.is_empty() { | |
265 | return; | |
266 | } | |
267 | ||
c295e0f8 XL |
268 | for a in &adj { |
269 | if let Adjust::NeverToAny = a.kind { | |
270 | if a.target.is_ty_var() { | |
271 | self.diverging_type_vars.borrow_mut().insert(a.target); | |
272 | debug!("apply_adjustments: adding `{:?}` as diverging type var", a.target); | |
273 | } | |
274 | } | |
275 | } | |
276 | ||
29967ef6 | 277 | let autoborrow_mut = adj.iter().any(|adj| { |
5869c6ff XL |
278 | matches!( |
279 | adj, | |
280 | &Adjustment { | |
281 | kind: Adjust::Borrow(AutoBorrow::Ref(_, AutoBorrowMutability::Mut { .. })), | |
282 | .. | |
283 | } | |
284 | ) | |
29967ef6 XL |
285 | }); |
286 | ||
287 | match self.typeck_results.borrow_mut().adjustments_mut().entry(expr.hir_id) { | |
288 | Entry::Vacant(entry) => { | |
289 | entry.insert(adj); | |
290 | } | |
291 | Entry::Occupied(mut entry) => { | |
292 | debug!(" - composing on top of {:?}", entry.get()); | |
293 | match (&entry.get()[..], &adj[..]) { | |
294 | // Applying any adjustment on top of a NeverToAny | |
295 | // is a valid NeverToAny adjustment, because it can't | |
296 | // be reached. | |
297 | (&[Adjustment { kind: Adjust::NeverToAny, .. }], _) => return, | |
a2a8927a XL |
298 | ( |
299 | &[ | |
300 | Adjustment { kind: Adjust::Deref(_), .. }, | |
301 | Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(..)), .. }, | |
302 | ], | |
303 | &[ | |
304 | Adjustment { kind: Adjust::Deref(_), .. }, | |
305 | .., // Any following adjustments are allowed. | |
306 | ], | |
307 | ) => { | |
29967ef6 XL |
308 | // A reborrow has no effect before a dereference. |
309 | } | |
310 | // FIXME: currently we never try to compose autoderefs | |
311 | // and ReifyFnPointer/UnsafeFnPointer, but we could. | |
5e7ed085 FG |
312 | _ => { |
313 | self.tcx.sess.delay_span_bug( | |
314 | expr.span, | |
315 | &format!( | |
316 | "while adjusting {:?}, can't compose {:?} and {:?}", | |
317 | expr, | |
318 | entry.get(), | |
319 | adj | |
320 | ), | |
321 | ); | |
322 | } | |
323 | } | |
29967ef6 XL |
324 | *entry.get_mut() = adj; |
325 | } | |
326 | } | |
327 | ||
328 | // If there is an mutable auto-borrow, it is equivalent to `&mut <expr>`. | |
329 | // In this case implicit use of `Deref` and `Index` within `<expr>` should | |
330 | // instead be `DerefMut` and `IndexMut`, so fix those up. | |
331 | if autoborrow_mut { | |
332 | self.convert_place_derefs_to_mutable(expr); | |
333 | } | |
334 | } | |
335 | ||
336 | /// Basically whenever we are converting from a type scheme into | |
337 | /// the fn body space, we always want to normalize associated | |
338 | /// types as well. This function combines the two. | |
fc512014 | 339 | fn instantiate_type_scheme<T>(&self, span: Span, substs: SubstsRef<'tcx>, value: T) -> T |
29967ef6 XL |
340 | where |
341 | T: TypeFoldable<'tcx>, | |
342 | { | |
fc512014 | 343 | debug!("instantiate_type_scheme(value={:?}, substs={:?})", value, substs); |
04454e1e | 344 | let value = EarlyBinder(value).subst(self.tcx, substs); |
487cf647 | 345 | let result = self.normalize(span, value); |
fc512014 | 346 | debug!("instantiate_type_scheme = {:?}", result); |
29967ef6 XL |
347 | result |
348 | } | |
349 | ||
350 | /// As `instantiate_type_scheme`, but for the bounds found in a | |
351 | /// generic type scheme. | |
352 | pub(in super::super) fn instantiate_bounds( | |
353 | &self, | |
354 | span: Span, | |
355 | def_id: DefId, | |
356 | substs: SubstsRef<'tcx>, | |
357 | ) -> (ty::InstantiatedPredicates<'tcx>, Vec<Span>) { | |
358 | let bounds = self.tcx.predicates_of(def_id); | |
359 | let spans: Vec<Span> = bounds.predicates.iter().map(|(_, span)| *span).collect(); | |
360 | let result = bounds.instantiate(self.tcx, substs); | |
487cf647 | 361 | let result = self.normalize(span, result); |
29967ef6 XL |
362 | debug!( |
363 | "instantiate_bounds(bounds={:?}, substs={:?}) = {:?}, {:?}", | |
364 | bounds, substs, result, spans, | |
365 | ); | |
366 | (result, spans) | |
367 | } | |
368 | ||
487cf647 | 369 | pub(in super::super) fn normalize<T>(&self, span: Span, value: T) -> T |
29967ef6 XL |
370 | where |
371 | T: TypeFoldable<'tcx>, | |
372 | { | |
487cf647 FG |
373 | self.register_infer_ok_obligations( |
374 | self.at(&self.misc(span), self.param_env).normalize(value), | |
5e7ed085 FG |
375 | ) |
376 | } | |
377 | ||
29967ef6 XL |
378 | pub fn require_type_meets( |
379 | &self, | |
380 | ty: Ty<'tcx>, | |
381 | span: Span, | |
382 | code: traits::ObligationCauseCode<'tcx>, | |
383 | def_id: DefId, | |
384 | ) { | |
385 | self.register_bound(ty, def_id, traits::ObligationCause::new(span, self.body_id, code)); | |
386 | } | |
387 | ||
388 | pub fn require_type_is_sized( | |
389 | &self, | |
390 | ty: Ty<'tcx>, | |
391 | span: Span, | |
392 | code: traits::ObligationCauseCode<'tcx>, | |
393 | ) { | |
394 | if !ty.references_error() { | |
395 | let lang_item = self.tcx.require_lang_item(LangItem::Sized, None); | |
396 | self.require_type_meets(ty, span, code, lang_item); | |
397 | } | |
398 | } | |
399 | ||
400 | pub fn require_type_is_sized_deferred( | |
401 | &self, | |
402 | ty: Ty<'tcx>, | |
403 | span: Span, | |
404 | code: traits::ObligationCauseCode<'tcx>, | |
405 | ) { | |
406 | if !ty.references_error() { | |
407 | self.deferred_sized_obligations.borrow_mut().push((ty, span, code)); | |
408 | } | |
409 | } | |
410 | ||
411 | pub fn register_bound( | |
412 | &self, | |
413 | ty: Ty<'tcx>, | |
414 | def_id: DefId, | |
415 | cause: traits::ObligationCause<'tcx>, | |
416 | ) { | |
417 | if !ty.references_error() { | |
418 | self.fulfillment_cx.borrow_mut().register_bound( | |
419 | self, | |
420 | self.param_env, | |
421 | ty, | |
422 | def_id, | |
423 | cause, | |
424 | ); | |
425 | } | |
426 | } | |
427 | ||
428 | pub fn to_ty(&self, ast_t: &hir::Ty<'_>) -> Ty<'tcx> { | |
6a06907d | 429 | let t = <dyn AstConv<'_>>::ast_ty_to_ty(self, ast_t); |
064997fb | 430 | self.register_wf_obligation(t.into(), ast_t.span, traits::WellFormed(None)); |
29967ef6 XL |
431 | t |
432 | } | |
433 | ||
434 | pub fn to_ty_saving_user_provided_ty(&self, ast_ty: &hir::Ty<'_>) -> Ty<'tcx> { | |
435 | let ty = self.to_ty(ast_ty); | |
436 | debug!("to_ty_saving_user_provided_ty: ty={:?}", ty); | |
437 | ||
5099ac24 | 438 | if Self::can_contain_user_lifetime_bounds(ty) { |
064997fb | 439 | let c_ty = self.canonicalize_response(UserType::Ty(ty)); |
29967ef6 XL |
440 | debug!("to_ty_saving_user_provided_ty: c_ty={:?}", c_ty); |
441 | self.typeck_results.borrow_mut().user_provided_types_mut().insert(ast_ty.hir_id, c_ty); | |
442 | } | |
443 | ||
444 | ty | |
445 | } | |
446 | ||
5099ac24 | 447 | pub fn array_length_to_const(&self, length: &hir::ArrayLen) -> ty::Const<'tcx> { |
a2a8927a XL |
448 | match length { |
449 | &hir::ArrayLen::Infer(_, span) => self.ct_infer(self.tcx.types.usize, None, span), | |
2b03887a | 450 | hir::ArrayLen::Body(anon_const) => { |
487cf647 FG |
451 | let span = self.tcx.def_span(anon_const.def_id); |
452 | let c = ty::Const::from_anon_const(self.tcx, anon_const.def_id); | |
2b03887a | 453 | self.register_wf_obligation(c.into(), span, ObligationCauseCode::WellFormed(None)); |
487cf647 | 454 | self.normalize(span, c) |
2b03887a | 455 | } |
a2a8927a XL |
456 | } |
457 | } | |
458 | ||
29967ef6 XL |
459 | pub fn const_arg_to_const( |
460 | &self, | |
461 | ast_c: &hir::AnonConst, | |
462 | param_def_id: DefId, | |
5099ac24 | 463 | ) -> ty::Const<'tcx> { |
487cf647 FG |
464 | let const_def = |
465 | ty::WithOptConstParam { did: ast_c.def_id, const_param_did: Some(param_def_id) }; | |
29967ef6 XL |
466 | let c = ty::Const::from_opt_const_arg_anon_const(self.tcx, const_def); |
467 | self.register_wf_obligation( | |
468 | c.into(), | |
469 | self.tcx.hir().span(ast_c.hir_id), | |
064997fb | 470 | ObligationCauseCode::WellFormed(None), |
29967ef6 XL |
471 | ); |
472 | c | |
473 | } | |
474 | ||
475 | // If the type given by the user has free regions, save it for later, since | |
476 | // NLL would like to enforce those. Also pass in types that involve | |
477 | // projections, since those can resolve to `'static` bounds (modulo #54940, | |
478 | // which hopefully will be fixed by the time you see this comment, dear | |
479 | // reader, although I have my doubts). Also pass in types with inference | |
480 | // types, because they may be repeated. Other sorts of things are already | |
481 | // sufficiently enforced with erased regions. =) | |
5099ac24 | 482 | fn can_contain_user_lifetime_bounds<T>(t: T) -> bool |
29967ef6 | 483 | where |
064997fb | 484 | T: TypeVisitable<'tcx>, |
29967ef6 | 485 | { |
5099ac24 | 486 | t.has_free_regions() || t.has_projections() || t.has_infer_types() |
29967ef6 XL |
487 | } |
488 | ||
489 | pub fn node_ty(&self, id: hir::HirId) -> Ty<'tcx> { | |
490 | match self.typeck_results.borrow().node_types().get(id) { | |
491 | Some(&t) => t, | |
487cf647 | 492 | None if let Some(e) = self.tainted_by_errors() => self.tcx.ty_error_with_guaranteed(e), |
29967ef6 XL |
493 | None => { |
494 | bug!( | |
495 | "no type for node {}: {} in fcx {}", | |
496 | id, | |
497 | self.tcx.hir().node_to_string(id), | |
498 | self.tag() | |
499 | ); | |
500 | } | |
501 | } | |
502 | } | |
503 | ||
94222f64 XL |
504 | pub fn node_ty_opt(&self, id: hir::HirId) -> Option<Ty<'tcx>> { |
505 | match self.typeck_results.borrow().node_types().get(id) { | |
506 | Some(&t) => Some(t), | |
487cf647 | 507 | None if let Some(e) = self.tainted_by_errors() => Some(self.tcx.ty_error_with_guaranteed(e)), |
94222f64 XL |
508 | None => None, |
509 | } | |
510 | } | |
511 | ||
29967ef6 XL |
512 | /// Registers an obligation for checking later, during regionck, that `arg` is well-formed. |
513 | pub fn register_wf_obligation( | |
514 | &self, | |
2b03887a | 515 | arg: ty::GenericArg<'tcx>, |
29967ef6 XL |
516 | span: Span, |
517 | code: traits::ObligationCauseCode<'tcx>, | |
518 | ) { | |
519 | // WF obligations never themselves fail, so no real need to give a detailed cause: | |
520 | let cause = traits::ObligationCause::new(span, self.body_id, code); | |
521 | self.register_predicate(traits::Obligation::new( | |
487cf647 | 522 | self.tcx, |
29967ef6 XL |
523 | cause, |
524 | self.param_env, | |
487cf647 | 525 | ty::Binder::dummy(ty::PredicateKind::WellFormed(arg)), |
29967ef6 XL |
526 | )); |
527 | } | |
528 | ||
529 | /// Registers obligations that all `substs` are well-formed. | |
530 | pub fn add_wf_bounds(&self, substs: SubstsRef<'tcx>, expr: &hir::Expr<'_>) { | |
531 | for arg in substs.iter().filter(|arg| { | |
532 | matches!(arg.unpack(), GenericArgKind::Type(..) | GenericArgKind::Const(..)) | |
533 | }) { | |
064997fb | 534 | self.register_wf_obligation(arg, expr.span, traits::WellFormed(None)); |
29967ef6 XL |
535 | } |
536 | } | |
537 | ||
29967ef6 XL |
538 | // FIXME(arielb1): use this instead of field.ty everywhere |
539 | // Only for fields! Returns <none> for methods> | |
540 | // Indifferent to privacy flags | |
541 | pub fn field_ty( | |
542 | &self, | |
543 | span: Span, | |
544 | field: &'tcx ty::FieldDef, | |
545 | substs: SubstsRef<'tcx>, | |
546 | ) -> Ty<'tcx> { | |
487cf647 | 547 | self.normalize(span, field.ty(self.tcx, substs)) |
29967ef6 XL |
548 | } |
549 | ||
923072b8 FG |
550 | pub(in super::super) fn resolve_rvalue_scopes(&self, def_id: DefId) { |
551 | let scope_tree = self.tcx.region_scope_tree(def_id); | |
552 | let rvalue_scopes = { rvalue_scopes::resolve_rvalue_scopes(self, &scope_tree, def_id) }; | |
553 | let mut typeck_results = self.inh.typeck_results.borrow_mut(); | |
554 | typeck_results.rvalue_scopes = rvalue_scopes; | |
555 | } | |
556 | ||
29967ef6 XL |
557 | pub(in super::super) fn resolve_generator_interiors(&self, def_id: DefId) { |
558 | let mut generators = self.deferred_generator_interiors.borrow_mut(); | |
559 | for (body_id, interior, kind) in generators.drain(..) { | |
487cf647 | 560 | self.select_obligations_where_possible(|_| {}); |
2b03887a | 561 | crate::generator_interior::resolve_interior(self, def_id, body_id, interior, kind); |
29967ef6 XL |
562 | } |
563 | } | |
564 | ||
c295e0f8 | 565 | #[instrument(skip(self), level = "debug")] |
29967ef6 | 566 | pub(in super::super) fn select_all_obligations_or_error(&self) { |
f2b60f7d | 567 | let mut errors = self.fulfillment_cx.borrow_mut().select_all_or_error(&self); |
3c0e092e XL |
568 | |
569 | if !errors.is_empty() { | |
f2b60f7d | 570 | self.adjust_fulfillment_errors_for_expr_obligation(&mut errors); |
487cf647 | 571 | self.err_ctxt().report_fulfillment_errors(&errors, self.inh.body_id); |
29967ef6 XL |
572 | } |
573 | } | |
574 | ||
575 | /// Select as many obligations as we can at present. | |
576 | pub(in super::super) fn select_obligations_where_possible( | |
577 | &self, | |
17df50a5 | 578 | mutate_fulfillment_errors: impl Fn(&mut Vec<traits::FulfillmentError<'tcx>>), |
29967ef6 | 579 | ) { |
a2a8927a | 580 | let mut result = self.fulfillment_cx.borrow_mut().select_where_possible(self); |
3c0e092e XL |
581 | if !result.is_empty() { |
582 | mutate_fulfillment_errors(&mut result); | |
f2b60f7d | 583 | self.adjust_fulfillment_errors_for_expr_obligation(&mut result); |
487cf647 | 584 | self.err_ctxt().report_fulfillment_errors(&result, self.inh.body_id); |
29967ef6 XL |
585 | } |
586 | } | |
587 | ||
588 | /// For the overloaded place expressions (`*x`, `x[3]`), the trait | |
589 | /// returns a type of `&T`, but the actual type we assign to the | |
590 | /// *expression* is `T`. So this function just peels off the return | |
591 | /// type by one layer to yield `T`. | |
592 | pub(in super::super) fn make_overloaded_place_return_type( | |
593 | &self, | |
594 | method: MethodCallee<'tcx>, | |
595 | ) -> ty::TypeAndMut<'tcx> { | |
596 | // extract method return type, which will be &T; | |
597 | let ret_ty = method.sig.output(); | |
598 | ||
599 | // method returns &T, but the type as visible to user is T, so deref | |
600 | ret_ty.builtin_deref(true).unwrap() | |
601 | } | |
602 | ||
c295e0f8 | 603 | #[instrument(skip(self), level = "debug")] |
487cf647 FG |
604 | fn self_type_matches_expected_vid(&self, self_ty: Ty<'tcx>, expected_vid: ty::TyVid) -> bool { |
605 | let self_ty = self.shallow_resolve(self_ty); | |
c295e0f8 XL |
606 | debug!(?self_ty); |
607 | ||
29967ef6 XL |
608 | match *self_ty.kind() { |
609 | ty::Infer(ty::TyVar(found_vid)) => { | |
610 | // FIXME: consider using `sub_root_var` here so we | |
611 | // can see through subtyping. | |
612 | let found_vid = self.root_var(found_vid); | |
613 | debug!("self_type_matches_expected_vid - found_vid={:?}", found_vid); | |
614 | expected_vid == found_vid | |
615 | } | |
616 | _ => false, | |
617 | } | |
618 | } | |
619 | ||
c295e0f8 | 620 | #[instrument(skip(self), level = "debug")] |
29967ef6 XL |
621 | pub(in super::super) fn obligations_for_self_ty<'b>( |
622 | &'b self, | |
623 | self_ty: ty::TyVid, | |
487cf647 FG |
624 | ) -> impl DoubleEndedIterator<Item = traits::PredicateObligation<'tcx>> + Captures<'tcx> + 'b |
625 | { | |
29967ef6 XL |
626 | // FIXME: consider using `sub_root_var` here so we |
627 | // can see through subtyping. | |
628 | let ty_var_root = self.root_var(self_ty); | |
c295e0f8 | 629 | trace!("pending_obligations = {:#?}", self.fulfillment_cx.borrow().pending_obligations()); |
29967ef6 | 630 | |
487cf647 FG |
631 | self.fulfillment_cx.borrow().pending_obligations().into_iter().filter_map( |
632 | move |obligation| match &obligation.predicate.kind().skip_binder() { | |
633 | ty::PredicateKind::Clause(ty::Clause::Projection(data)) | |
634 | if self.self_type_matches_expected_vid( | |
635 | data.projection_ty.self_ty(), | |
636 | ty_var_root, | |
637 | ) => | |
638 | { | |
639 | Some(obligation) | |
29967ef6 | 640 | } |
487cf647 FG |
641 | ty::PredicateKind::Clause(ty::Clause::Trait(data)) |
642 | if self.self_type_matches_expected_vid(data.self_ty(), ty_var_root) => | |
643 | { | |
644 | Some(obligation) | |
645 | } | |
646 | ||
647 | ty::PredicateKind::Clause(ty::Clause::Trait(..)) | |
648 | | ty::PredicateKind::Clause(ty::Clause::Projection(..)) | |
649 | | ty::PredicateKind::Subtype(..) | |
650 | | ty::PredicateKind::Coerce(..) | |
651 | | ty::PredicateKind::Clause(ty::Clause::RegionOutlives(..)) | |
652 | | ty::PredicateKind::Clause(ty::Clause::TypeOutlives(..)) | |
653 | | ty::PredicateKind::WellFormed(..) | |
654 | | ty::PredicateKind::ObjectSafe(..) | |
655 | | ty::PredicateKind::ConstEvaluatable(..) | |
656 | | ty::PredicateKind::ConstEquate(..) | |
657 | // N.B., this predicate is created by breaking down a | |
658 | // `ClosureType: FnFoo()` predicate, where | |
659 | // `ClosureType` represents some `Closure`. It can't | |
660 | // possibly be referring to the current closure, | |
661 | // because we haven't produced the `Closure` for | |
662 | // this closure yet; this is exactly why the other | |
663 | // code is looking for a self type of an unresolved | |
664 | // inference variable. | |
665 | | ty::PredicateKind::ClosureKind(..) | |
666 | | ty::PredicateKind::Ambiguous | |
667 | | ty::PredicateKind::TypeWellFormedFromEnv(..) => None, | |
668 | }, | |
669 | ) | |
29967ef6 XL |
670 | } |
671 | ||
672 | pub(in super::super) fn type_var_is_sized(&self, self_ty: ty::TyVid) -> bool { | |
487cf647 FG |
673 | let sized_did = self.tcx.lang_items().sized_trait(); |
674 | self.obligations_for_self_ty(self_ty).any(|obligation| { | |
675 | match obligation.predicate.kind().skip_binder() { | |
676 | ty::PredicateKind::Clause(ty::Clause::Trait(data)) => { | |
677 | Some(data.def_id()) == sized_did | |
678 | } | |
679 | _ => false, | |
680 | } | |
681 | }) | |
29967ef6 XL |
682 | } |
683 | ||
684 | pub(in super::super) fn err_args(&self, len: usize) -> Vec<Ty<'tcx>> { | |
685 | vec![self.tcx.ty_error(); len] | |
686 | } | |
687 | ||
688 | /// Unifies the output type with the expected type early, for more coercions | |
689 | /// and forward type information on the input expressions. | |
c295e0f8 | 690 | #[instrument(skip(self, call_span), level = "debug")] |
29967ef6 XL |
691 | pub(in super::super) fn expected_inputs_for_expected_output( |
692 | &self, | |
693 | call_span: Span, | |
694 | expected_ret: Expectation<'tcx>, | |
695 | formal_ret: Ty<'tcx>, | |
696 | formal_args: &[Ty<'tcx>], | |
923072b8 | 697 | ) -> Option<Vec<Ty<'tcx>>> { |
29967ef6 | 698 | let formal_ret = self.resolve_vars_with_obligations(formal_ret); |
923072b8 | 699 | let ret_ty = expected_ret.only_has_type(self)?; |
5e7ed085 FG |
700 | |
701 | // HACK(oli-obk): This is a hack to keep RPIT and TAIT in sync wrt their behaviour. | |
702 | // Without it, the inference | |
703 | // variable will get instantiated with the opaque type. The inference variable often | |
704 | // has various helpful obligations registered for it that help closures figure out their | |
705 | // signature. If we infer the inference var to the opaque type, the closure won't be able | |
706 | // to find those obligations anymore, and it can't necessarily find them from the opaque | |
707 | // type itself. We could be more powerful with inference if we *combined* the obligations | |
708 | // so that we got both the obligations from the opaque type and the ones from the inference | |
709 | // variable. That will accept more code than we do right now, so we need to carefully consider | |
710 | // the implications. | |
711 | // Note: this check is pessimistic, as the inference type could be matched with something other | |
712 | // than the opaque type, but then we need a new `TypeRelation` just for this specific case and | |
713 | // can't re-use `sup` below. | |
714 | // See src/test/ui/impl-trait/hidden-type-is-opaque.rs and | |
715 | // src/test/ui/impl-trait/hidden-type-is-opaque-2.rs for examples that hit this path. | |
716 | if formal_ret.has_infer_types() { | |
717 | for ty in ret_ty.walk() { | |
064997fb FG |
718 | if let ty::subst::GenericArgKind::Type(ty) = ty.unpack() |
719 | && let ty::Opaque(def_id, _) = *ty.kind() | |
720 | && let Some(def_id) = def_id.as_local() | |
721 | && self.opaque_type_origin(def_id, DUMMY_SP).is_some() { | |
722 | return None; | |
5e7ed085 FG |
723 | } |
724 | } | |
725 | } | |
726 | ||
29967ef6 XL |
727 | let expect_args = self |
728 | .fudge_inference_if_ok(|| { | |
487cf647 FG |
729 | let ocx = ObligationCtxt::new_in_snapshot(self); |
730 | ||
29967ef6 XL |
731 | // Attempt to apply a subtyping relationship between the formal |
732 | // return type (likely containing type variables if the function | |
733 | // is polymorphic) and the expected return type. | |
734 | // No argument expectations are produced if unification fails. | |
735 | let origin = self.misc(call_span); | |
487cf647 FG |
736 | ocx.sup(&origin, self.param_env, ret_ty, formal_ret)?; |
737 | if !ocx.select_where_possible().is_empty() { | |
738 | return Err(TypeError::Mismatch); | |
29967ef6 XL |
739 | } |
740 | ||
741 | // Record all the argument types, with the substitutions | |
742 | // produced from the above subtyping unification. | |
923072b8 | 743 | Ok(Some(formal_args.iter().map(|&ty| self.resolve_vars_if_possible(ty)).collect())) |
29967ef6 XL |
744 | }) |
745 | .unwrap_or_default(); | |
c295e0f8 | 746 | debug!(?formal_args, ?formal_ret, ?expect_args, ?expected_ret); |
29967ef6 XL |
747 | expect_args |
748 | } | |
749 | ||
750 | pub(in super::super) fn resolve_lang_item_path( | |
751 | &self, | |
752 | lang_item: hir::LangItem, | |
753 | span: Span, | |
754 | hir_id: hir::HirId, | |
a2a8927a | 755 | expr_hir_id: Option<hir::HirId>, |
29967ef6 XL |
756 | ) -> (Res, Ty<'tcx>) { |
757 | let def_id = self.tcx.require_lang_item(lang_item, Some(span)); | |
758 | let def_kind = self.tcx.def_kind(def_id); | |
759 | ||
760 | let item_ty = if let DefKind::Variant = def_kind { | |
04454e1e | 761 | self.tcx.bound_type_of(self.tcx.parent(def_id)) |
29967ef6 | 762 | } else { |
04454e1e | 763 | self.tcx.bound_type_of(def_id) |
29967ef6 | 764 | }; |
064997fb | 765 | let substs = self.fresh_substs_for_item(span, def_id); |
29967ef6 XL |
766 | let ty = item_ty.subst(self.tcx, substs); |
767 | ||
768 | self.write_resolution(hir_id, Ok((def_kind, def_id))); | |
f2b60f7d FG |
769 | |
770 | let code = match lang_item { | |
771 | hir::LangItem::IntoFutureIntoFuture => { | |
772 | Some(ObligationCauseCode::AwaitableExpr(expr_hir_id)) | |
773 | } | |
774 | hir::LangItem::IteratorNext | hir::LangItem::IntoIterIntoIter => { | |
775 | Some(ObligationCauseCode::ForLoopIterator) | |
776 | } | |
777 | hir::LangItem::TryTraitFromOutput | |
778 | | hir::LangItem::TryTraitFromResidual | |
779 | | hir::LangItem::TryTraitBranch => Some(ObligationCauseCode::QuestionMark), | |
780 | _ => None, | |
781 | }; | |
782 | if let Some(code) = code { | |
783 | self.add_required_obligations_with_code(span, def_id, substs, move |_, _| code.clone()); | |
784 | } else { | |
785 | self.add_required_obligations_for_hir(span, def_id, substs, hir_id); | |
786 | } | |
787 | ||
29967ef6 XL |
788 | (Res::Def(def_kind, def_id), ty) |
789 | } | |
790 | ||
791 | /// Resolves an associated value path into a base type and associated constant, or method | |
792 | /// resolution. The newly resolved definition is written into `type_dependent_defs`. | |
136023e0 | 793 | pub fn resolve_ty_and_res_fully_qualified_call( |
29967ef6 | 794 | &self, |
cdc7bbd5 | 795 | qpath: &'tcx QPath<'tcx>, |
29967ef6 XL |
796 | hir_id: hir::HirId, |
797 | span: Span, | |
cdc7bbd5 | 798 | ) -> (Res, Option<Ty<'tcx>>, &'tcx [hir::PathSegment<'tcx>]) { |
136023e0 XL |
799 | debug!( |
800 | "resolve_ty_and_res_fully_qualified_call: qpath={:?} hir_id={:?} span={:?}", | |
801 | qpath, hir_id, span | |
802 | ); | |
29967ef6 XL |
803 | let (ty, qself, item_segment) = match *qpath { |
804 | QPath::Resolved(ref opt_qself, ref path) => { | |
805 | return ( | |
806 | path.res, | |
807 | opt_qself.as_ref().map(|qself| self.to_ty(qself)), | |
6a06907d | 808 | path.segments, |
29967ef6 XL |
809 | ); |
810 | } | |
94222f64 XL |
811 | QPath::TypeRelative(ref qself, ref segment) => { |
812 | // Don't use `self.to_ty`, since this will register a WF obligation. | |
813 | // If we're trying to call a non-existent method on a trait | |
814 | // (e.g. `MyTrait::missing_method`), then resolution will | |
815 | // give us a `QPath::TypeRelative` with a trait object as | |
816 | // `qself`. In that case, we want to avoid registering a WF obligation | |
817 | // for `dyn MyTrait`, since we don't actually need the trait | |
818 | // to be object-safe. | |
819 | // We manually call `register_wf_obligation` in the success path | |
820 | // below. | |
a2a8927a | 821 | (<dyn AstConv<'_>>::ast_ty_to_ty_in_path(self, qself), qself, segment) |
94222f64 | 822 | } |
136023e0 XL |
823 | QPath::LangItem(..) => { |
824 | bug!("`resolve_ty_and_res_fully_qualified_call` called on `LangItem`") | |
825 | } | |
29967ef6 XL |
826 | }; |
827 | if let Some(&cached_result) = self.typeck_results.borrow().type_dependent_defs().get(hir_id) | |
828 | { | |
94222f64 | 829 | self.register_wf_obligation(ty.into(), qself.span, traits::WellFormed(None)); |
29967ef6 XL |
830 | // Return directly on cache hit. This is useful to avoid doubly reporting |
831 | // errors with default match binding modes. See #44614. | |
5869c6ff | 832 | let def = cached_result.map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id)); |
29967ef6 XL |
833 | return (def, Some(ty), slice::from_ref(&**item_segment)); |
834 | } | |
835 | let item_name = item_segment.ident; | |
136023e0 XL |
836 | let result = self |
837 | .resolve_fully_qualified_call(span, item_name, ty, qself.span, hir_id) | |
838 | .or_else(|error| { | |
839 | let result = match error { | |
840 | method::MethodError::PrivateMatch(kind, def_id, _) => Ok((kind, def_id)), | |
5e7ed085 | 841 | _ => Err(ErrorGuaranteed::unchecked_claim_error_was_emitted()), |
136023e0 | 842 | }; |
94222f64 XL |
843 | |
844 | // If we have a path like `MyTrait::missing_method`, then don't register | |
845 | // a WF obligation for `dyn MyTrait` when method lookup fails. Otherwise, | |
846 | // register a WF obligation so that we can detect any additional | |
847 | // errors in the self type. | |
848 | if !(matches!(error, method::MethodError::NoMatch(_)) && ty.is_trait()) { | |
849 | self.register_wf_obligation(ty.into(), qself.span, traits::WellFormed(None)); | |
850 | } | |
136023e0 XL |
851 | if item_name.name != kw::Empty { |
852 | if let Some(mut e) = self.report_method_error( | |
853 | span, | |
854 | ty, | |
855 | item_name, | |
856 | SelfSource::QPath(qself), | |
857 | error, | |
858 | None, | |
859 | ) { | |
860 | e.emit(); | |
861 | } | |
29967ef6 | 862 | } |
136023e0 XL |
863 | result |
864 | }); | |
29967ef6 | 865 | |
6a06907d | 866 | if result.is_ok() { |
94222f64 | 867 | self.register_wf_obligation(ty.into(), qself.span, traits::WellFormed(None)); |
6a06907d XL |
868 | } |
869 | ||
29967ef6 XL |
870 | // Write back the new resolution. |
871 | self.write_resolution(hir_id, result); | |
872 | ( | |
5869c6ff | 873 | result.map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id)), |
29967ef6 XL |
874 | Some(ty), |
875 | slice::from_ref(&**item_segment), | |
876 | ) | |
877 | } | |
878 | ||
879 | /// Given a function `Node`, return its `FnDecl` if it exists, or `None` otherwise. | |
880 | pub(in super::super) fn get_node_fn_decl( | |
881 | &self, | |
882 | node: Node<'tcx>, | |
883 | ) -> Option<(&'tcx hir::FnDecl<'tcx>, Ident, bool)> { | |
884 | match node { | |
885 | Node::Item(&hir::Item { ident, kind: hir::ItemKind::Fn(ref sig, ..), .. }) => { | |
886 | // This is less than ideal, it will not suggest a return type span on any | |
887 | // method called `main`, regardless of whether it is actually the entry point, | |
888 | // but it will still present it as the reason for the expected type. | |
889 | Some((&sig.decl, ident, ident.name != sym::main)) | |
890 | } | |
891 | Node::TraitItem(&hir::TraitItem { | |
892 | ident, | |
893 | kind: hir::TraitItemKind::Fn(ref sig, ..), | |
894 | .. | |
895 | }) => Some((&sig.decl, ident, true)), | |
896 | Node::ImplItem(&hir::ImplItem { | |
897 | ident, | |
898 | kind: hir::ImplItemKind::Fn(ref sig, ..), | |
899 | .. | |
900 | }) => Some((&sig.decl, ident, false)), | |
901 | _ => None, | |
902 | } | |
903 | } | |
904 | ||
905 | /// Given a `HirId`, return the `FnDecl` of the method it is enclosed by and whether a | |
906 | /// suggestion can be made, `None` otherwise. | |
907 | pub fn get_fn_decl(&self, blk_id: hir::HirId) -> Option<(&'tcx hir::FnDecl<'tcx>, bool)> { | |
908 | // Get enclosing Fn, if it is a function or a trait method, unless there's a `loop` or | |
909 | // `while` before reaching it, as block tail returns are not available in them. | |
910 | self.tcx.hir().get_return_block(blk_id).and_then(|blk_id| { | |
911 | let parent = self.tcx.hir().get(blk_id); | |
912 | self.get_node_fn_decl(parent).map(|(fn_decl, _, is_main)| (fn_decl, is_main)) | |
913 | }) | |
914 | } | |
915 | ||
916 | pub(in super::super) fn note_internal_mutation_in_method( | |
917 | &self, | |
5e7ed085 | 918 | err: &mut Diagnostic, |
29967ef6 XL |
919 | expr: &hir::Expr<'_>, |
920 | expected: Ty<'tcx>, | |
921 | found: Ty<'tcx>, | |
922 | ) { | |
923 | if found != self.tcx.types.unit { | |
924 | return; | |
925 | } | |
f2b60f7d | 926 | if let ExprKind::MethodCall(path_segment, rcvr, ..) = expr.kind { |
29967ef6 XL |
927 | if self |
928 | .typeck_results | |
929 | .borrow() | |
930 | .expr_ty_adjusted_opt(rcvr) | |
931 | .map_or(true, |ty| expected.peel_refs() != ty.peel_refs()) | |
932 | { | |
933 | return; | |
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 | sp.push_span_label( | |
950 | rcvr.span, | |
064997fb | 951 | "you probably want to use this value after calling the method...", |
29967ef6 XL |
952 | ); |
953 | err.span_note( | |
954 | sp, | |
955 | &format!("method `{}` modifies its receiver in-place", path_segment.ident), | |
956 | ); | |
957 | err.note(&format!("...instead of the `()` output of method `{}`", path_segment.ident)); | |
958 | } | |
959 | } | |
960 | ||
961 | pub(in super::super) fn note_need_for_fn_pointer( | |
962 | &self, | |
5e7ed085 | 963 | err: &mut Diagnostic, |
29967ef6 XL |
964 | expected: Ty<'tcx>, |
965 | found: Ty<'tcx>, | |
966 | ) { | |
967 | let (sig, did, substs) = match (&expected.kind(), &found.kind()) { | |
968 | (ty::FnDef(did1, substs1), ty::FnDef(did2, substs2)) => { | |
04454e1e FG |
969 | let sig1 = self.tcx.bound_fn_sig(*did1).subst(self.tcx, substs1); |
970 | let sig2 = self.tcx.bound_fn_sig(*did2).subst(self.tcx, substs2); | |
29967ef6 XL |
971 | if sig1 != sig2 { |
972 | return; | |
973 | } | |
974 | err.note( | |
975 | "different `fn` items always have unique types, even if their signatures are \ | |
976 | the same", | |
977 | ); | |
978 | (sig1, *did1, substs1) | |
979 | } | |
980 | (ty::FnDef(did, substs), ty::FnPtr(sig2)) => { | |
04454e1e | 981 | let sig1 = self.tcx.bound_fn_sig(*did).subst(self.tcx, substs); |
29967ef6 XL |
982 | if sig1 != *sig2 { |
983 | return; | |
984 | } | |
985 | (sig1, *did, substs) | |
986 | } | |
987 | _ => return, | |
988 | }; | |
989 | err.help(&format!("change the expected type to be function pointer `{}`", sig)); | |
990 | err.help(&format!( | |
991 | "if the expected type is due to type inference, cast the expected `fn` to a function \ | |
992 | pointer: `{} as {}`", | |
993 | self.tcx.def_path_str_with_substs(did, substs), | |
994 | sig | |
995 | )); | |
996 | } | |
997 | ||
29967ef6 XL |
998 | // Instantiates the given path, which must refer to an item with the given |
999 | // number of type parameters and type. | |
c295e0f8 | 1000 | #[instrument(skip(self, span), level = "debug")] |
29967ef6 XL |
1001 | pub fn instantiate_value_path( |
1002 | &self, | |
1003 | segments: &[hir::PathSegment<'_>], | |
1004 | self_ty: Option<Ty<'tcx>>, | |
1005 | res: Res, | |
1006 | span: Span, | |
1007 | hir_id: hir::HirId, | |
1008 | ) -> (Ty<'tcx>, Res) { | |
29967ef6 XL |
1009 | let tcx = self.tcx; |
1010 | ||
1011 | let path_segs = match res { | |
1012 | Res::Local(_) | Res::SelfCtor(_) => vec![], | |
6a06907d XL |
1013 | Res::Def(kind, def_id) => <dyn AstConv<'_>>::def_ids_for_value_path_segments( |
1014 | self, segments, self_ty, kind, def_id, | |
1015 | ), | |
29967ef6 XL |
1016 | _ => bug!("instantiate_value_path on {:?}", res), |
1017 | }; | |
1018 | ||
1019 | let mut user_self_ty = None; | |
1020 | let mut is_alias_variant_ctor = false; | |
1021 | match res { | |
94222f64 XL |
1022 | Res::Def(DefKind::Ctor(CtorOf::Variant, _), _) |
1023 | if let Some(self_ty) = self_ty => | |
1024 | { | |
1025 | let adt_def = self_ty.ty_adt_def().unwrap(); | |
5e7ed085 | 1026 | user_self_ty = Some(UserSelfTy { impl_def_id: adt_def.did(), self_ty }); |
94222f64 | 1027 | is_alias_variant_ctor = true; |
29967ef6 XL |
1028 | } |
1029 | Res::Def(DefKind::AssocFn | DefKind::AssocConst, def_id) => { | |
064997fb FG |
1030 | let assoc_item = tcx.associated_item(def_id); |
1031 | let container = assoc_item.container; | |
1032 | let container_id = assoc_item.container_id(tcx); | |
1033 | debug!(?def_id, ?container, ?container_id); | |
29967ef6 | 1034 | match container { |
064997fb FG |
1035 | ty::TraitContainer => { |
1036 | callee::check_legal_trait_for_method_call(tcx, span, None, span, container_id) | |
29967ef6 | 1037 | } |
064997fb | 1038 | ty::ImplContainer => { |
29967ef6 XL |
1039 | if segments.len() == 1 { |
1040 | // `<T>::assoc` will end up here, and so | |
1041 | // can `T::assoc`. It this came from an | |
1042 | // inherent impl, we need to record the | |
1043 | // `T` for posterity (see `UserSelfTy` for | |
1044 | // details). | |
1045 | let self_ty = self_ty.expect("UFCS sugared assoc missing Self"); | |
064997fb | 1046 | user_self_ty = Some(UserSelfTy { impl_def_id: container_id, self_ty }); |
29967ef6 XL |
1047 | } |
1048 | } | |
1049 | } | |
1050 | } | |
1051 | _ => {} | |
1052 | } | |
1053 | ||
1054 | // Now that we have categorized what space the parameters for each | |
1055 | // segment belong to, let's sort out the parameters that the user | |
1056 | // provided (if any) into their appropriate spaces. We'll also report | |
1057 | // errors if type parameters are provided in an inappropriate place. | |
1058 | ||
1059 | let generic_segs: FxHashSet<_> = path_segs.iter().map(|PathSeg(_, index)| index).collect(); | |
6a06907d | 1060 | let generics_has_err = <dyn AstConv<'_>>::prohibit_generics( |
29967ef6 XL |
1061 | self, |
1062 | segments.iter().enumerate().filter_map(|(index, seg)| { | |
1063 | if !generic_segs.contains(&index) || is_alias_variant_ctor { | |
1064 | Some(seg) | |
1065 | } else { | |
1066 | None | |
1067 | } | |
1068 | }), | |
923072b8 | 1069 | |_| {}, |
29967ef6 XL |
1070 | ); |
1071 | ||
1072 | if let Res::Local(hid) = res { | |
1073 | let ty = self.local_ty(span, hid).decl_ty; | |
487cf647 | 1074 | let ty = self.normalize(span, ty); |
29967ef6 XL |
1075 | self.write_ty(hir_id, ty); |
1076 | return (ty, res); | |
1077 | } | |
1078 | ||
1079 | if generics_has_err { | |
1080 | // Don't try to infer type parameters when prohibited generic arguments were given. | |
1081 | user_self_ty = None; | |
1082 | } | |
1083 | ||
1084 | // Now we have to compare the types that the user *actually* | |
1085 | // provided against the types that were *expected*. If the user | |
1086 | // did not provide any types, then we want to substitute inference | |
1087 | // variables. If the user provided some types, we may still need | |
1088 | // to add defaults. If the user provided *too many* types, that's | |
1089 | // a problem. | |
1090 | ||
1091 | let mut infer_args_for_err = FxHashSet::default(); | |
5869c6ff | 1092 | |
17df50a5 | 1093 | let mut explicit_late_bound = ExplicitLateBound::No; |
29967ef6 XL |
1094 | for &PathSeg(def_id, index) in &path_segs { |
1095 | let seg = &segments[index]; | |
1096 | let generics = tcx.generics_of(def_id); | |
5869c6ff | 1097 | |
29967ef6 XL |
1098 | // Argument-position `impl Trait` is treated as a normal generic |
1099 | // parameter internally, but we don't allow users to specify the | |
1100 | // parameter's value explicitly, so we have to do some error- | |
1101 | // checking here. | |
17df50a5 | 1102 | let arg_count = <dyn AstConv<'_>>::check_generic_arg_count_for_call( |
5869c6ff XL |
1103 | tcx, |
1104 | span, | |
1105 | def_id, | |
1106 | &generics, | |
1107 | seg, | |
1108 | IsMethodCall::No, | |
17df50a5 XL |
1109 | ); |
1110 | ||
1111 | if let ExplicitLateBound::Yes = arg_count.explicit_late_bound { | |
1112 | explicit_late_bound = ExplicitLateBound::Yes; | |
1113 | } | |
1114 | ||
487cf647 | 1115 | if let Err(GenericArgCountMismatch { reported: Some(e), .. }) = arg_count.correct { |
29967ef6 | 1116 | infer_args_for_err.insert(index); |
487cf647 | 1117 | self.set_tainted_by_errors(e); // See issue #53251. |
29967ef6 XL |
1118 | } |
1119 | } | |
1120 | ||
1121 | let has_self = path_segs | |
1122 | .last() | |
1123 | .map(|PathSeg(def_id, _)| tcx.generics_of(*def_id).has_self) | |
1124 | .unwrap_or(false); | |
1125 | ||
1126 | let (res, self_ctor_substs) = if let Res::SelfCtor(impl_def_id) = res { | |
1127 | let ty = self.normalize_ty(span, tcx.at(span).type_of(impl_def_id)); | |
1128 | match *ty.kind() { | |
1129 | ty::Adt(adt_def, substs) if adt_def.has_ctor() => { | |
1130 | let variant = adt_def.non_enum_variant(); | |
487cf647 FG |
1131 | let (ctor_kind, ctor_def_id) = variant.ctor.unwrap(); |
1132 | (Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id), Some(substs)) | |
29967ef6 XL |
1133 | } |
1134 | _ => { | |
1135 | let mut err = tcx.sess.struct_span_err( | |
1136 | span, | |
1137 | "the `Self` constructor can only be used with tuple or unit structs", | |
1138 | ); | |
1139 | if let Some(adt_def) = ty.ty_adt_def() { | |
1140 | match adt_def.adt_kind() { | |
1141 | AdtKind::Enum => { | |
1142 | err.help("did you mean to use one of the enum's variants?"); | |
1143 | } | |
1144 | AdtKind::Struct | AdtKind::Union => { | |
1145 | err.span_suggestion( | |
1146 | span, | |
1147 | "use curly brackets", | |
923072b8 | 1148 | "Self { /* fields */ }", |
29967ef6 XL |
1149 | Applicability::HasPlaceholders, |
1150 | ); | |
1151 | } | |
1152 | } | |
1153 | } | |
487cf647 FG |
1154 | let reported = err.emit(); |
1155 | return (tcx.ty_error_with_guaranteed(reported), res); | |
29967ef6 XL |
1156 | } |
1157 | } | |
1158 | } else { | |
1159 | (res, None) | |
1160 | }; | |
1161 | let def_id = res.def_id(); | |
1162 | ||
1163 | // The things we are substituting into the type should not contain | |
1164 | // escaping late-bound regions, and nor should the base type scheme. | |
1165 | let ty = tcx.type_of(def_id); | |
1166 | ||
1167 | let arg_count = GenericArgCountResult { | |
17df50a5 | 1168 | explicit_late_bound, |
29967ef6 XL |
1169 | correct: if infer_args_for_err.is_empty() { |
1170 | Ok(()) | |
1171 | } else { | |
1172 | Err(GenericArgCountMismatch::default()) | |
1173 | }, | |
1174 | }; | |
1175 | ||
fc512014 XL |
1176 | struct CreateCtorSubstsContext<'a, 'tcx> { |
1177 | fcx: &'a FnCtxt<'a, 'tcx>, | |
1178 | span: Span, | |
1179 | path_segs: &'a [PathSeg], | |
1180 | infer_args_for_err: &'a FxHashSet<usize>, | |
1181 | segments: &'a [hir::PathSegment<'a>], | |
1182 | } | |
1183 | impl<'tcx, 'a> CreateSubstsForGenericArgsCtxt<'a, 'tcx> for CreateCtorSubstsContext<'a, 'tcx> { | |
1184 | fn args_for_def_id( | |
1185 | &mut self, | |
1186 | def_id: DefId, | |
1187 | ) -> (Option<&'a hir::GenericArgs<'a>>, bool) { | |
1188 | if let Some(&PathSeg(_, index)) = | |
1189 | self.path_segs.iter().find(|&PathSeg(did, _)| *did == def_id) | |
1190 | { | |
1191 | // If we've encountered an `impl Trait`-related error, we're just | |
1192 | // going to infer the arguments for better error messages. | |
1193 | if !self.infer_args_for_err.contains(&index) { | |
1194 | // Check whether the user has provided generic arguments. | |
1195 | if let Some(ref data) = self.segments[index].args { | |
1196 | return (Some(data), self.segments[index].infer_args); | |
29967ef6 | 1197 | } |
29967ef6 | 1198 | } |
fc512014 XL |
1199 | return (None, self.segments[index].infer_args); |
1200 | } | |
29967ef6 | 1201 | |
fc512014 XL |
1202 | (None, true) |
1203 | } | |
1204 | ||
1205 | fn provided_kind( | |
1206 | &mut self, | |
1207 | param: &ty::GenericParamDef, | |
1208 | arg: &GenericArg<'_>, | |
2b03887a | 1209 | ) -> ty::GenericArg<'tcx> { |
fc512014 | 1210 | match (¶m.kind, arg) { |
29967ef6 | 1211 | (GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => { |
6a06907d | 1212 | <dyn AstConv<'_>>::ast_region_to_region(self.fcx, lt, Some(param)).into() |
29967ef6 XL |
1213 | } |
1214 | (GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => { | |
fc512014 | 1215 | self.fcx.to_ty(ty).into() |
29967ef6 | 1216 | } |
cdc7bbd5 | 1217 | (GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => { |
fc512014 | 1218 | self.fcx.const_arg_to_const(&ct.value, param.def_id).into() |
29967ef6 | 1219 | } |
94222f64 XL |
1220 | (GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => { |
1221 | self.fcx.ty_infer(Some(param), inf.span).into() | |
1222 | } | |
1223 | (GenericParamDefKind::Const { .. }, GenericArg::Infer(inf)) => { | |
1224 | let tcx = self.fcx.tcx(); | |
1225 | self.fcx.ct_infer(tcx.type_of(param.def_id), Some(param), inf.span).into() | |
1226 | } | |
29967ef6 | 1227 | _ => unreachable!(), |
fc512014 XL |
1228 | } |
1229 | } | |
1230 | ||
1231 | fn inferred_kind( | |
1232 | &mut self, | |
2b03887a | 1233 | substs: Option<&[ty::GenericArg<'tcx>]>, |
fc512014 XL |
1234 | param: &ty::GenericParamDef, |
1235 | infer_args: bool, | |
2b03887a | 1236 | ) -> ty::GenericArg<'tcx> { |
fc512014 XL |
1237 | let tcx = self.fcx.tcx(); |
1238 | match param.kind { | |
1239 | GenericParamDefKind::Lifetime => { | |
1240 | self.fcx.re_infer(Some(param), self.span).unwrap().into() | |
1241 | } | |
1242 | GenericParamDefKind::Type { has_default, .. } => { | |
1243 | if !infer_args && has_default { | |
1244 | // If we have a default, then we it doesn't matter that we're not | |
1245 | // inferring the type arguments: we provide the default where any | |
1246 | // is missing. | |
04454e1e | 1247 | let default = tcx.bound_type_of(param.def_id); |
fc512014 | 1248 | self.fcx |
04454e1e | 1249 | .normalize_ty(self.span, default.subst(tcx, substs.unwrap())) |
29967ef6 | 1250 | .into() |
fc512014 XL |
1251 | } else { |
1252 | // If no type arguments were provided, we have to infer them. | |
1253 | // This case also occurs as a result of some malformed input, e.g. | |
1254 | // a lifetime argument being given instead of a type parameter. | |
1255 | // Using inference instead of `Error` gives better error messages. | |
1256 | self.fcx.var_for_def(self.span, param) | |
29967ef6 XL |
1257 | } |
1258 | } | |
5e7ed085 | 1259 | GenericParamDefKind::Const { has_default } => { |
cdc7bbd5 | 1260 | if !infer_args && has_default { |
064997fb | 1261 | tcx.bound_const_param_default(param.def_id) |
04454e1e | 1262 | .subst(tcx, substs.unwrap()) |
cdc7bbd5 XL |
1263 | .into() |
1264 | } else { | |
1265 | self.fcx.var_for_def(self.span, param) | |
1266 | } | |
fc512014 XL |
1267 | } |
1268 | } | |
1269 | } | |
1270 | } | |
1271 | ||
1272 | let substs = self_ctor_substs.unwrap_or_else(|| { | |
6a06907d | 1273 | <dyn AstConv<'_>>::create_substs_for_generic_args( |
fc512014 XL |
1274 | tcx, |
1275 | def_id, | |
a2a8927a | 1276 | &[], |
fc512014 XL |
1277 | has_self, |
1278 | self_ty, | |
cdc7bbd5 | 1279 | &arg_count, |
fc512014 XL |
1280 | &mut CreateCtorSubstsContext { |
1281 | fcx: self, | |
1282 | span, | |
1283 | path_segs: &path_segs, | |
1284 | infer_args_for_err: &infer_args_for_err, | |
1285 | segments, | |
29967ef6 XL |
1286 | }, |
1287 | ) | |
1288 | }); | |
1289 | assert!(!substs.has_escaping_bound_vars()); | |
1290 | assert!(!ty.has_escaping_bound_vars()); | |
1291 | ||
1292 | // First, store the "user substs" for later. | |
1293 | self.write_user_type_annotation_from_substs(hir_id, def_id, substs, user_self_ty); | |
1294 | ||
f2b60f7d | 1295 | self.add_required_obligations_for_hir(span, def_id, &substs, hir_id); |
29967ef6 XL |
1296 | |
1297 | // Substitute the values for the type parameters into the type of | |
1298 | // the referenced item. | |
fc512014 | 1299 | let ty_substituted = self.instantiate_type_scheme(span, &substs, ty); |
29967ef6 XL |
1300 | |
1301 | if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty { | |
1302 | // In the case of `Foo<T>::method` and `<Foo<T>>::method`, if `method` | |
1303 | // is inherent, there is no `Self` parameter; instead, the impl needs | |
1304 | // type parameters, which we can infer by unifying the provided `Self` | |
1305 | // with the substituted impl type. | |
1306 | // This also occurs for an enum variant on a type alias. | |
1307 | let ty = tcx.type_of(impl_def_id); | |
1308 | ||
fc512014 | 1309 | let impl_ty = self.instantiate_type_scheme(span, &substs, ty); |
17df50a5 | 1310 | match self.at(&self.misc(span), self.param_env).eq(impl_ty, self_ty) { |
29967ef6 XL |
1311 | Ok(ok) => self.register_infer_ok_obligations(ok), |
1312 | Err(_) => { | |
1313 | self.tcx.sess.delay_span_bug( | |
1314 | span, | |
1315 | &format!( | |
1316 | "instantiate_value_path: (UFCS) {:?} was a subtype of {:?} but now is not?", | |
1317 | self_ty, | |
1318 | impl_ty, | |
1319 | ), | |
1320 | ); | |
1321 | } | |
1322 | } | |
1323 | } | |
1324 | ||
29967ef6 XL |
1325 | debug!("instantiate_value_path: type of {:?} is {:?}", hir_id, ty_substituted); |
1326 | self.write_substs(hir_id, substs); | |
1327 | ||
1328 | (ty_substituted, res) | |
1329 | } | |
1330 | ||
1331 | /// Add all the obligations that are required, substituting and normalized appropriately. | |
f2b60f7d | 1332 | pub(crate) fn add_required_obligations_for_hir( |
923072b8 FG |
1333 | &self, |
1334 | span: Span, | |
1335 | def_id: DefId, | |
f2b60f7d FG |
1336 | substs: SubstsRef<'tcx>, |
1337 | hir_id: hir::HirId, | |
923072b8 | 1338 | ) { |
f2b60f7d FG |
1339 | self.add_required_obligations_with_code(span, def_id, substs, |idx, span| { |
1340 | if span.is_dummy() { | |
1341 | ObligationCauseCode::ExprItemObligation(def_id, hir_id, idx) | |
1342 | } else { | |
1343 | ObligationCauseCode::ExprBindingObligation(def_id, span, hir_id, idx) | |
1344 | } | |
1345 | }) | |
a2a8927a XL |
1346 | } |
1347 | ||
2b03887a | 1348 | #[instrument(level = "debug", skip(self, code, span, substs))] |
a2a8927a XL |
1349 | fn add_required_obligations_with_code( |
1350 | &self, | |
1351 | span: Span, | |
1352 | def_id: DefId, | |
f2b60f7d FG |
1353 | substs: SubstsRef<'tcx>, |
1354 | code: impl Fn(usize, Span) -> ObligationCauseCode<'tcx>, | |
a2a8927a | 1355 | ) { |
2b03887a FG |
1356 | let param_env = self.param_env; |
1357 | ||
1358 | let remap = match self.tcx.def_kind(def_id) { | |
1359 | // Associated consts have `Self: ~const Trait` bounds that should be satisfiable when | |
1360 | // `Self: Trait` is satisfied because it does not matter whether the impl is `const`. | |
1361 | // Therefore we have to remap the param env here to be non-const. | |
1362 | hir::def::DefKind::AssocConst => true, | |
1363 | hir::def::DefKind::AssocFn | |
1364 | if self.tcx.def_kind(self.tcx.parent(def_id)) == hir::def::DefKind::Trait => | |
1365 | { | |
1366 | // N.B.: All callsites to this function involve checking a path expression. | |
1367 | // | |
1368 | // When instantiating a trait method as a function item, it does not actually matter whether | |
1369 | // the trait is `const` or not, or whether `where T: ~const Tr` needs to be satisfied as | |
1370 | // `const`. If we were to introduce instantiating trait methods as `const fn`s, we would | |
1371 | // check that after this, either via a bound `where F: ~const FnOnce` or when coercing to a | |
1372 | // `const fn` pointer. | |
1373 | // | |
1374 | // FIXME(fee1-dead) FIXME(const_trait_impl): update this doc when trait methods can satisfy | |
1375 | // `~const FnOnce` or can be coerced to `const fn` pointer. | |
1376 | true | |
1377 | } | |
1378 | _ => false, | |
1379 | }; | |
3c0e092e | 1380 | let (bounds, _) = self.instantiate_bounds(span, def_id, &substs); |
29967ef6 | 1381 | |
2b03887a | 1382 | for mut obligation in traits::predicates_for_generics( |
f2b60f7d FG |
1383 | |idx, predicate_span| { |
1384 | traits::ObligationCause::new(span, self.body_id, code(idx, predicate_span)) | |
1385 | }, | |
2b03887a | 1386 | param_env, |
29967ef6 | 1387 | bounds, |
3c0e092e | 1388 | ) { |
2b03887a FG |
1389 | if remap { |
1390 | obligation = obligation.without_const(self.tcx); | |
1391 | } | |
29967ef6 XL |
1392 | self.register_predicate(obligation); |
1393 | } | |
1394 | } | |
1395 | ||
1396 | /// Resolves `typ` by a single level if `typ` is a type variable. | |
1397 | /// If no resolution is possible, then an error is reported. | |
1398 | /// Numeric inference variables may be left unresolved. | |
1399 | pub fn structurally_resolved_type(&self, sp: Span, ty: Ty<'tcx>) -> Ty<'tcx> { | |
1400 | let ty = self.resolve_vars_with_obligations(ty); | |
1401 | if !ty.is_ty_var() { | |
1402 | ty | |
1403 | } else { | |
487cf647 | 1404 | let e = self.tainted_by_errors().unwrap_or_else(|| { |
2b03887a FG |
1405 | self.err_ctxt() |
1406 | .emit_inference_failure_err((**self).body_id, sp, ty.into(), E0282, true) | |
487cf647 FG |
1407 | .emit() |
1408 | }); | |
1409 | let err = self.tcx.ty_error_with_guaranteed(e); | |
29967ef6 XL |
1410 | self.demand_suptype(sp, err, ty); |
1411 | err | |
1412 | } | |
1413 | } | |
1414 | ||
1415 | pub(in super::super) fn with_breakable_ctxt<F: FnOnce() -> R, R>( | |
1416 | &self, | |
1417 | id: hir::HirId, | |
1418 | ctxt: BreakableCtxt<'tcx>, | |
1419 | f: F, | |
1420 | ) -> (BreakableCtxt<'tcx>, R) { | |
1421 | let index; | |
1422 | { | |
1423 | let mut enclosing_breakables = self.enclosing_breakables.borrow_mut(); | |
1424 | index = enclosing_breakables.stack.len(); | |
1425 | enclosing_breakables.by_id.insert(id, index); | |
1426 | enclosing_breakables.stack.push(ctxt); | |
1427 | } | |
1428 | let result = f(); | |
1429 | let ctxt = { | |
1430 | let mut enclosing_breakables = self.enclosing_breakables.borrow_mut(); | |
1431 | debug_assert!(enclosing_breakables.stack.len() == index + 1); | |
1432 | enclosing_breakables.by_id.remove(&id).expect("missing breakable context"); | |
1433 | enclosing_breakables.stack.pop().expect("missing breakable context") | |
1434 | }; | |
1435 | (ctxt, result) | |
1436 | } | |
1437 | ||
1438 | /// Instantiate a QueryResponse in a probe context, without a | |
1439 | /// good ObligationCause. | |
1440 | pub(in super::super) fn probe_instantiate_query_response( | |
1441 | &self, | |
1442 | span: Span, | |
1443 | original_values: &OriginalQueryValues<'tcx>, | |
1444 | query_result: &Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>, | |
1445 | ) -> InferResult<'tcx, Ty<'tcx>> { | |
1446 | self.instantiate_query_response_and_region_obligations( | |
1447 | &traits::ObligationCause::misc(span, self.body_id), | |
1448 | self.param_env, | |
1449 | original_values, | |
1450 | query_result, | |
1451 | ) | |
1452 | } | |
1453 | ||
1454 | /// Returns `true` if an expression is contained inside the LHS of an assignment expression. | |
1455 | pub(in super::super) fn expr_in_place(&self, mut expr_id: hir::HirId) -> bool { | |
1456 | let mut contained_in_place = false; | |
1457 | ||
1458 | while let hir::Node::Expr(parent_expr) = | |
1459 | self.tcx.hir().get(self.tcx.hir().get_parent_node(expr_id)) | |
1460 | { | |
1461 | match &parent_expr.kind { | |
1462 | hir::ExprKind::Assign(lhs, ..) | hir::ExprKind::AssignOp(_, lhs, ..) => { | |
1463 | if lhs.hir_id == expr_id { | |
1464 | contained_in_place = true; | |
1465 | break; | |
1466 | } | |
1467 | } | |
1468 | _ => (), | |
1469 | } | |
1470 | expr_id = parent_expr.hir_id; | |
1471 | } | |
1472 | ||
1473 | contained_in_place | |
1474 | } | |
1475 | } |