]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_borrowck/src/type_check/input_output.rs
Rollup merge of #106291 - obeis:issue-106182, r=oli-obk
[rust.git] / compiler / rustc_borrowck / src / type_check / input_output.rs
1 //! This module contains code to equate the input/output types appearing
2 //! in the MIR with the expected input/output types from the function
3 //! signature. This requires a bit of processing, as the expected types
4 //! are supplied to us before normalization and may contain opaque
5 //! `impl Trait` instances. In contrast, the input/output types found in
6 //! the MIR (specifically, in the special local variables for the
7 //! `RETURN_PLACE` the MIR arguments) are always fully normalized (and
8 //! contain revealed `impl Trait` values).
9
10 use rustc_index::vec::Idx;
11 use rustc_infer::infer::LateBoundRegionConversionTime;
12 use rustc_middle::mir::*;
13 use rustc_middle::ty::Ty;
14 use rustc_span::Span;
15
16 use crate::universal_regions::UniversalRegions;
17
18 use super::{Locations, TypeChecker};
19
20 impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
21     #[instrument(skip(self, body, universal_regions), level = "debug")]
22     pub(super) fn equate_inputs_and_outputs(
23         &mut self,
24         body: &Body<'tcx>,
25         universal_regions: &UniversalRegions<'tcx>,
26         normalized_inputs_and_output: &[Ty<'tcx>],
27     ) {
28         let (&normalized_output_ty, normalized_input_tys) =
29             normalized_inputs_and_output.split_last().unwrap();
30
31         debug!(?normalized_output_ty);
32         debug!(?normalized_input_tys);
33
34         let mir_def_id = body.source.def_id().expect_local();
35
36         // If the user explicitly annotated the input types, extract
37         // those.
38         //
39         // e.g., `|x: FxHashMap<_, &'static u32>| ...`
40         let user_provided_sig = if !self.tcx().is_closure(mir_def_id.to_def_id()) {
41             None
42         } else {
43             let typeck_results = self.tcx().typeck(mir_def_id);
44
45             typeck_results.user_provided_sigs.get(&mir_def_id).map(|user_provided_poly_sig| {
46                 // Instantiate the canonicalized variables from
47                 // user-provided signature (e.g., the `_` in the code
48                 // above) with fresh variables.
49                 let poly_sig = self.instantiate_canonical_with_fresh_inference_vars(
50                     body.span,
51                     &user_provided_poly_sig,
52                 );
53
54                 // Replace the bound items in the fn sig with fresh
55                 // variables, so that they represent the view from
56                 // "inside" the closure.
57                 self.infcx.replace_bound_vars_with_fresh_vars(
58                     body.span,
59                     LateBoundRegionConversionTime::FnCall,
60                     poly_sig,
61                 )
62             })
63         };
64
65         debug!(?normalized_input_tys, ?body.local_decls);
66
67         // Equate expected input tys with those in the MIR.
68         for (argument_index, &normalized_input_ty) in normalized_input_tys.iter().enumerate() {
69             if argument_index + 1 >= body.local_decls.len() {
70                 self.tcx()
71                     .sess
72                     .delay_span_bug(body.span, "found more normalized_input_ty than local_decls");
73                 break;
74             }
75
76             // In MIR, argument N is stored in local N+1.
77             let local = Local::new(argument_index + 1);
78
79             let mir_input_ty = body.local_decls[local].ty;
80
81             let mir_input_span = body.local_decls[local].source_info.span;
82             self.equate_normalized_input_or_output(
83                 normalized_input_ty,
84                 mir_input_ty,
85                 mir_input_span,
86             );
87         }
88
89         if let Some(user_provided_sig) = user_provided_sig {
90             for (argument_index, &user_provided_input_ty) in
91                 user_provided_sig.inputs().iter().enumerate()
92             {
93                 // In MIR, closures begin an implicit `self`, so
94                 // argument N is stored in local N+2.
95                 let local = Local::new(argument_index + 2);
96                 let mir_input_ty = body.local_decls[local].ty;
97                 let mir_input_span = body.local_decls[local].source_info.span;
98
99                 // If the user explicitly annotated the input types, enforce those.
100                 let user_provided_input_ty =
101                     self.normalize(user_provided_input_ty, Locations::All(mir_input_span));
102
103                 self.equate_normalized_input_or_output(
104                     user_provided_input_ty,
105                     mir_input_ty,
106                     mir_input_span,
107                 );
108             }
109         }
110
111         debug!(
112             "equate_inputs_and_outputs: body.yield_ty {:?}, universal_regions.yield_ty {:?}",
113             body.yield_ty(),
114             universal_regions.yield_ty
115         );
116
117         // We will not have a universal_regions.yield_ty if we yield (by accident)
118         // outside of a generator and return an `impl Trait`, so emit a delay_span_bug
119         // because we don't want to panic in an assert here if we've already got errors.
120         if body.yield_ty().is_some() != universal_regions.yield_ty.is_some() {
121             self.tcx().sess.delay_span_bug(
122                 body.span,
123                 &format!(
124                     "Expected body to have yield_ty ({:?}) iff we have a UR yield_ty ({:?})",
125                     body.yield_ty(),
126                     universal_regions.yield_ty,
127                 ),
128             );
129         }
130
131         if let (Some(mir_yield_ty), Some(ur_yield_ty)) =
132             (body.yield_ty(), universal_regions.yield_ty)
133         {
134             let yield_span = body.local_decls[RETURN_PLACE].source_info.span;
135             self.equate_normalized_input_or_output(ur_yield_ty, mir_yield_ty, yield_span);
136         }
137
138         // Return types are a bit more complex. They may contain opaque `impl Trait` types.
139         let mir_output_ty = body.local_decls[RETURN_PLACE].ty;
140         let output_span = body.local_decls[RETURN_PLACE].source_info.span;
141         if let Err(terr) = self.eq_types(
142             normalized_output_ty,
143             mir_output_ty,
144             Locations::All(output_span),
145             ConstraintCategory::BoringNoLocation,
146         ) {
147             span_mirbug!(
148                 self,
149                 Location::START,
150                 "equate_inputs_and_outputs: `{:?}=={:?}` failed with `{:?}`",
151                 normalized_output_ty,
152                 mir_output_ty,
153                 terr
154             );
155         };
156
157         // If the user explicitly annotated the output types, enforce those.
158         // Note that this only happens for closures.
159         if let Some(user_provided_sig) = user_provided_sig {
160             let user_provided_output_ty = user_provided_sig.output();
161             let user_provided_output_ty =
162                 self.normalize(user_provided_output_ty, Locations::All(output_span));
163             if let Err(err) = self.eq_types(
164                 user_provided_output_ty,
165                 mir_output_ty,
166                 Locations::All(output_span),
167                 ConstraintCategory::BoringNoLocation,
168             ) {
169                 span_mirbug!(
170                     self,
171                     Location::START,
172                     "equate_inputs_and_outputs: `{:?}=={:?}` failed with `{:?}`",
173                     mir_output_ty,
174                     user_provided_output_ty,
175                     err
176                 );
177             }
178         }
179     }
180
181     #[instrument(skip(self), level = "debug")]
182     fn equate_normalized_input_or_output(&mut self, a: Ty<'tcx>, b: Ty<'tcx>, span: Span) {
183         if let Err(_) =
184             self.eq_types(a, b, Locations::All(span), ConstraintCategory::BoringNoLocation)
185         {
186             // FIXME(jackh726): This is a hack. It's somewhat like
187             // `rustc_traits::normalize_after_erasing_regions`. Ideally, we'd
188             // like to normalize *before* inserting into `local_decls`, but
189             // doing so ends up causing some other trouble.
190             let b = self.normalize(b, Locations::All(span));
191
192             // Note: if we have to introduce new placeholders during normalization above, then we won't have
193             // added those universes to the universe info, which we would want in `relate_tys`.
194             if let Err(terr) =
195                 self.eq_types(a, b, Locations::All(span), ConstraintCategory::BoringNoLocation)
196             {
197                 span_mirbug!(
198                     self,
199                     Location::START,
200                     "equate_normalized_input_or_output: `{:?}=={:?}` failed with `{:?}`",
201                     a,
202                     b,
203                     terr
204                 );
205             }
206         }
207     }
208 }