rustc_borrowck/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
10use 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    /// Check explicit closure signature annotation,
26    /// e.g., `|x: FxIndexMap<_, &'static u32>| ...`.
27    #[instrument(skip(self), level = "debug")]
28    pub(super) fn check_signature_annotation(&mut self) {
29        let mir_def_id = self.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        // Instantiate the canonicalized variables from user-provided signature
38        // (e.g., the `_` in the code above) with fresh variables.
39        // Then replace the bound items in the fn sig with fresh variables,
40        // so that they represent the view from "inside" the closure.
41        let user_provided_sig = self.instantiate_canonical(self.body.span, &user_provided_poly_sig);
42        let mut user_provided_sig = self.infcx.instantiate_binder_with_fresh_vars(
43            self.body.span,
44            BoundRegionConversionTime::FnCall,
45            user_provided_sig,
46        );
47
48        // FIXME(async_closures): It's kind of wacky that we must apply this
49        // transformation here, since we do the same thing in HIR typeck.
50        // Maybe we could just fix up the canonicalized signature during HIR typeck?
51        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            // Make sure to use the args from `DefiningTy` so the right NLL region vids are
61            // prepopulated into the type.
62            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
70                    .next_region_var(RegionVariableOrigin::MiscVariable(self.body.span), || {
71                        RegionCtxt::Unknown
72                    }),
73            );
74
75            let next_ty_var = || self.infcx.next_ty_var(self.body.span);
76            let output_ty = Ty::new_coroutine(
77                self.tcx(),
78                self.tcx().coroutine_for_closure(mir_def_id),
79                ty::CoroutineArgs::new(
80                    self.tcx(),
81                    ty::CoroutineArgsParts {
82                        parent_args: args.parent_args(),
83                        kind_ty: Ty::from_coroutine_closure_kind(self.tcx(), args.kind()),
84                        return_ty: user_provided_sig.output(),
85                        tupled_upvars_ty,
86                        // For async closures, none of these can be annotated, so just fill
87                        // them with fresh ty vars.
88                        resume_ty: next_ty_var(),
89                        yield_ty: next_ty_var(),
90                        witness: next_ty_var(),
91                    },
92                )
93                .args,
94            );
95
96            user_provided_sig = self.tcx().mk_fn_sig(
97                user_provided_sig.inputs().iter().copied(),
98                output_ty,
99                user_provided_sig.c_variadic,
100                user_provided_sig.safety,
101                user_provided_sig.abi,
102            );
103        }
104
105        let is_coroutine_with_implicit_resume_ty = self.tcx().is_coroutine(mir_def_id.to_def_id())
106            && user_provided_sig.inputs().is_empty();
107
108        for (&user_ty, arg_decl) in user_provided_sig.inputs().iter().zip_eq(
109            // In MIR, closure args begin with an implicit `self`.
110            // Also, coroutines have a resume type which may be implicitly `()`.
111            self.body
112                .args_iter()
113                .skip(1 + if is_coroutine_with_implicit_resume_ty { 1 } else { 0 })
114                .map(|local| &self.body.local_decls[local]),
115        ) {
116            self.ascribe_user_type_skip_wf(
117                arg_decl.ty,
118                ty::UserType::new(ty::UserTypeKind::Ty(user_ty)),
119                arg_decl.source_info.span,
120            );
121        }
122
123        // If the user explicitly annotated the output type, enforce it.
124        let output_decl = &self.body.local_decls[RETURN_PLACE];
125        self.ascribe_user_type_skip_wf(
126            output_decl.ty,
127            ty::UserType::new(ty::UserTypeKind::Ty(user_provided_sig.output())),
128            output_decl.source_info.span,
129        );
130    }
131
132    #[instrument(skip(self), level = "debug")]
133    pub(super) fn equate_inputs_and_outputs(&mut self, normalized_inputs_and_output: &[Ty<'tcx>]) {
134        let (&normalized_output_ty, normalized_input_tys) =
135            normalized_inputs_and_output.split_last().unwrap();
136
137        debug!(?normalized_output_ty);
138        debug!(?normalized_input_tys);
139
140        // Equate expected input tys with those in the MIR.
141        for (argument_index, &normalized_input_ty) in normalized_input_tys.iter().enumerate() {
142            if argument_index + 1 >= self.body.local_decls.len() {
143                self.tcx()
144                    .dcx()
145                    .span_bug(self.body.span, "found more normalized_input_ty than local_decls");
146            }
147
148            // In MIR, argument N is stored in local N+1.
149            let local = Local::from_usize(argument_index + 1);
150
151            let mir_input_ty = self.body.local_decls[local].ty;
152
153            let mir_input_span = self.body.local_decls[local].source_info.span;
154            self.equate_normalized_input_or_output(
155                normalized_input_ty,
156                mir_input_ty,
157                mir_input_span,
158            );
159        }
160
161        if let Some(mir_yield_ty) = self.body.yield_ty() {
162            let yield_span = self.body.local_decls[RETURN_PLACE].source_info.span;
163            self.equate_normalized_input_or_output(
164                self.universal_regions.yield_ty.unwrap(),
165                mir_yield_ty,
166                yield_span,
167            );
168        }
169
170        if let Some(mir_resume_ty) = self.body.resume_ty() {
171            let yield_span = self.body.local_decls[RETURN_PLACE].source_info.span;
172            self.equate_normalized_input_or_output(
173                self.universal_regions.resume_ty.unwrap(),
174                mir_resume_ty,
175                yield_span,
176            );
177        }
178
179        // Return types are a bit more complex. They may contain opaque `impl Trait` types.
180        let mir_output_ty = self.body.local_decls[RETURN_PLACE].ty;
181        let output_span = self.body.local_decls[RETURN_PLACE].source_info.span;
182        self.equate_normalized_input_or_output(normalized_output_ty, mir_output_ty, output_span);
183    }
184
185    #[instrument(skip(self), level = "debug")]
186    fn equate_normalized_input_or_output(&mut self, a: Ty<'tcx>, b: Ty<'tcx>, span: Span) {
187        if let Err(_) =
188            self.eq_types(a, b, Locations::All(span), ConstraintCategory::BoringNoLocation)
189        {
190            // FIXME(jackh726): This is a hack. It's somewhat like
191            // `rustc_traits::normalize_after_erasing_regions`. Ideally, we'd
192            // like to normalize *before* inserting into `local_decls`, but
193            // doing so ends up causing some other trouble.
194            let b = self.normalize(b, Locations::All(span));
195
196            // Note: if we have to introduce new placeholders during normalization above, then we
197            // won't have added those universes to the universe info, which we would want in
198            // `relate_tys`.
199            if let Err(terr) =
200                self.eq_types(a, b, Locations::All(span), ConstraintCategory::BoringNoLocation)
201            {
202                span_mirbug!(
203                    self,
204                    Location::START,
205                    "equate_normalized_input_or_output: `{:?}=={:?}` failed with `{:?}`",
206                    a,
207                    b,
208                    terr
209                );
210            }
211        }
212    }
213}