rustc_borrowck/type_check/
input_output.rs1use std::assert_matches::assert_matches;
11
12use itertools::Itertools;
13use rustc_hir as hir;
14use rustc_infer::infer::{BoundRegionConversionTime, RegionVariableOrigin};
15use rustc_middle::mir::*;
16use rustc_middle::ty::{self, Ty};
17use rustc_span::Span;
18use tracing::{debug, instrument};
19
20use super::{Locations, TypeChecker};
21use crate::renumber::RegionCtxt;
22use crate::universal_regions::DefiningTy;
23
24impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
25 #[instrument(skip(self, body), level = "debug")]
28 pub(super) fn check_signature_annotation(&mut self, body: &Body<'tcx>) {
29 let mir_def_id = body.source.def_id().expect_local();
30
31 if !self.tcx().is_closure_like(mir_def_id.to_def_id()) {
32 return;
33 }
34
35 let user_provided_poly_sig = self.tcx().closure_user_provided_sig(mir_def_id);
36
37 let user_provided_sig = self.instantiate_canonical(body.span, &user_provided_poly_sig);
42 let mut user_provided_sig = self.infcx.instantiate_binder_with_fresh_vars(
43 body.span,
44 BoundRegionConversionTime::FnCall,
45 user_provided_sig,
46 );
47
48 if let DefiningTy::CoroutineClosure(_, args) = self.universal_regions.defining_ty {
52 assert_matches!(
53 self.tcx().coroutine_kind(self.tcx().coroutine_for_closure(mir_def_id)),
54 Some(hir::CoroutineKind::Desugared(
55 hir::CoroutineDesugaring::Async,
56 hir::CoroutineSource::Closure
57 )),
58 "this needs to be modified if we're lowering non-async closures"
59 );
60 let args = args.as_coroutine_closure();
63 let tupled_upvars_ty = ty::CoroutineClosureSignature::tupled_upvars_by_closure_kind(
64 self.tcx(),
65 args.kind(),
66 Ty::new_tup(self.tcx(), user_provided_sig.inputs()),
67 args.tupled_upvars_ty(),
68 args.coroutine_captures_by_ref_ty(),
69 self.infcx.next_region_var(RegionVariableOrigin::MiscVariable(body.span), || {
70 RegionCtxt::Unknown
71 }),
72 );
73
74 let next_ty_var = || self.infcx.next_ty_var(body.span);
75 let output_ty = Ty::new_coroutine(
76 self.tcx(),
77 self.tcx().coroutine_for_closure(mir_def_id),
78 ty::CoroutineArgs::new(
79 self.tcx(),
80 ty::CoroutineArgsParts {
81 parent_args: args.parent_args(),
82 kind_ty: Ty::from_coroutine_closure_kind(self.tcx(), args.kind()),
83 return_ty: user_provided_sig.output(),
84 tupled_upvars_ty,
85 resume_ty: next_ty_var(),
88 yield_ty: next_ty_var(),
89 witness: next_ty_var(),
90 },
91 )
92 .args,
93 );
94
95 user_provided_sig = self.tcx().mk_fn_sig(
96 user_provided_sig.inputs().iter().copied(),
97 output_ty,
98 user_provided_sig.c_variadic,
99 user_provided_sig.safety,
100 user_provided_sig.abi,
101 );
102 }
103
104 let is_coroutine_with_implicit_resume_ty = self.tcx().is_coroutine(mir_def_id.to_def_id())
105 && user_provided_sig.inputs().is_empty();
106
107 for (&user_ty, arg_decl) in user_provided_sig.inputs().iter().zip_eq(
108 body.args_iter()
111 .skip(1 + if is_coroutine_with_implicit_resume_ty { 1 } else { 0 })
112 .map(|local| &body.local_decls[local]),
113 ) {
114 self.ascribe_user_type_skip_wf(
115 arg_decl.ty,
116 ty::UserType::new(ty::UserTypeKind::Ty(user_ty)),
117 arg_decl.source_info.span,
118 );
119 }
120
121 let output_decl = &body.local_decls[RETURN_PLACE];
123 self.ascribe_user_type_skip_wf(
124 output_decl.ty,
125 ty::UserType::new(ty::UserTypeKind::Ty(user_provided_sig.output())),
126 output_decl.source_info.span,
127 );
128 }
129
130 #[instrument(skip(self, body), level = "debug")]
131 pub(super) fn equate_inputs_and_outputs(
132 &mut self,
133 body: &Body<'tcx>,
134 normalized_inputs_and_output: &[Ty<'tcx>],
135 ) {
136 let (&normalized_output_ty, normalized_input_tys) =
137 normalized_inputs_and_output.split_last().unwrap();
138
139 debug!(?normalized_output_ty);
140 debug!(?normalized_input_tys);
141
142 for (argument_index, &normalized_input_ty) in normalized_input_tys.iter().enumerate() {
144 if argument_index + 1 >= body.local_decls.len() {
145 self.tcx()
146 .dcx()
147 .span_bug(body.span, "found more normalized_input_ty than local_decls");
148 }
149
150 let local = Local::from_usize(argument_index + 1);
152
153 let mir_input_ty = body.local_decls[local].ty;
154
155 let mir_input_span = body.local_decls[local].source_info.span;
156 self.equate_normalized_input_or_output(
157 normalized_input_ty,
158 mir_input_ty,
159 mir_input_span,
160 );
161 }
162
163 if let Some(mir_yield_ty) = body.yield_ty() {
164 let yield_span = body.local_decls[RETURN_PLACE].source_info.span;
165 self.equate_normalized_input_or_output(
166 self.universal_regions.yield_ty.unwrap(),
167 mir_yield_ty,
168 yield_span,
169 );
170 }
171
172 if let Some(mir_resume_ty) = body.resume_ty() {
173 let yield_span = body.local_decls[RETURN_PLACE].source_info.span;
174 self.equate_normalized_input_or_output(
175 self.universal_regions.resume_ty.unwrap(),
176 mir_resume_ty,
177 yield_span,
178 );
179 }
180
181 let mir_output_ty = body.local_decls[RETURN_PLACE].ty;
183 let output_span = body.local_decls[RETURN_PLACE].source_info.span;
184 self.equate_normalized_input_or_output(normalized_output_ty, mir_output_ty, output_span);
185 }
186
187 #[instrument(skip(self), level = "debug")]
188 fn equate_normalized_input_or_output(&mut self, a: Ty<'tcx>, b: Ty<'tcx>, span: Span) {
189 if let Err(_) =
190 self.eq_types(a, b, Locations::All(span), ConstraintCategory::BoringNoLocation)
191 {
192 let b = self.normalize(b, Locations::All(span));
197
198 if let Err(terr) =
202 self.eq_types(a, b, Locations::All(span), ConstraintCategory::BoringNoLocation)
203 {
204 span_mirbug!(
205 self,
206 Location::START,
207 "equate_normalized_input_or_output: `{:?}=={:?}` failed with `{:?}`",
208 a,
209 b,
210 terr
211 );
212 }
213 }
214 }
215}