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