Skip to content

Commit da63aaa

Browse files
committed
extract input_output code into its own module
No functional change.
1 parent a66c651 commit da63aaa

File tree

2 files changed

+101
-51
lines changed

2 files changed

+101
-51
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! This module contains code to equate the input/output types appearing
12+
//! in the MIR with the expected input/output types from the function
13+
//! signature. This requires a bit of processing, as the expected types
14+
//! are supplied to us before normalization and may contain existential
15+
//! `impl Trait` instances. In contrast, the input/output types found in
16+
//! the MIR (specifically, in the special local variables for the
17+
//! `RETURN_PLACE` the MIR arguments) are always fully normalize (and
18+
//! contain revealed `impl Trait` values).
19+
20+
use borrow_check::nll::universal_regions::UniversalRegions;
21+
use rustc::ty::Ty;
22+
use rustc::mir::*;
23+
24+
use rustc_data_structures::indexed_vec::Idx;
25+
26+
use super::{AtLocation, TypeChecker};
27+
28+
impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
29+
pub(super) fn equate_inputs_and_outputs(
30+
&mut self,
31+
mir: &Mir<'tcx>,
32+
universal_regions: &UniversalRegions<'tcx>,
33+
) {
34+
let &UniversalRegions {
35+
unnormalized_output_ty,
36+
unnormalized_input_tys,
37+
..
38+
} = universal_regions;
39+
40+
let start_position = Location {
41+
block: START_BLOCK,
42+
statement_index: 0,
43+
};
44+
45+
// Equate expected input tys with those in the MIR.
46+
let argument_locals = (1..).map(Local::new);
47+
for (&unnormalized_input_ty, local) in unnormalized_input_tys.iter().zip(argument_locals) {
48+
let input_ty = self.normalize(&unnormalized_input_ty, start_position);
49+
let mir_input_ty = mir.local_decls[local].ty;
50+
self.equate_normalized_input_or_output(start_position, input_ty, mir_input_ty);
51+
}
52+
53+
// Return types are a bit more complex. They may contain existential `impl Trait`
54+
// types.
55+
56+
let output_ty = self.normalize(&unnormalized_output_ty, start_position);
57+
let mir_output_ty = mir.local_decls[RETURN_PLACE].ty;
58+
self.equate_normalized_input_or_output(start_position, output_ty, mir_output_ty);
59+
}
60+
61+
fn equate_normalized_input_or_output(&mut self, location: Location, a: Ty<'tcx>, b: Ty<'tcx>) {
62+
debug!("equate_normalized_input_or_output(a={:?}, b={:?})", a, b);
63+
64+
if let Err(terr) = self.eq_types(a, b, location.at_self()) {
65+
span_mirbug!(
66+
self,
67+
location,
68+
"equate_normalized_input_or_output: `{:?}=={:?}` failed with `{:?}`",
69+
a,
70+
b,
71+
terr
72+
);
73+
}
74+
}
75+
}

src/librustc_mir/borrow_check/nll/type_check/mod.rs

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,32 @@ use util::liveness::LivenessResults;
3636
use rustc_data_structures::fx::FxHashSet;
3737
use rustc_data_structures::indexed_vec::Idx;
3838

39+
macro_rules! span_mirbug {
40+
($context:expr, $elem:expr, $($message:tt)*) => ({
41+
$crate::borrow_check::nll::type_check::mirbug(
42+
$context.tcx(),
43+
$context.last_span,
44+
&format!(
45+
"broken MIR in {:?} ({:?}): {}",
46+
$context.body_id,
47+
$elem,
48+
format_args!($($message)*),
49+
),
50+
)
51+
})
52+
}
53+
54+
macro_rules! span_mirbug_and_err {
55+
($context:expr, $elem:expr, $($message:tt)*) => ({
56+
{
57+
span_mirbug!($context, $elem, $($message)*);
58+
$context.error()
59+
}
60+
})
61+
}
62+
3963
mod liveness;
64+
mod input_output;
4065

4166
/// Type checks the given `mir` in the context of the inference
4267
/// context `infcx`. Returns any region constraints that have yet to
@@ -88,18 +113,7 @@ pub(crate) fn type_check<'gcx, 'tcx>(
88113
&mut |cx| {
89114
liveness::generate(cx, mir, liveness, flow_inits, move_data);
90115

91-
// Equate the input and output tys given by the user with
92-
// the ones found in the MIR.
93-
let &UniversalRegions {
94-
unnormalized_output_ty,
95-
unnormalized_input_tys,
96-
..
97-
} = universal_regions;
98-
cx.equate_input_or_output(unnormalized_output_ty, mir.local_decls[RETURN_PLACE].ty);
99-
let arg_locals = (1..).map(Local::new);
100-
for (&input_ty, local) in unnormalized_input_tys.iter().zip(arg_locals) {
101-
cx.equate_input_or_output(input_ty, mir.local_decls[local].ty);
102-
}
116+
cx.equate_inputs_and_outputs(mir, universal_regions);
103117
},
104118
)
105119
}
@@ -136,33 +150,13 @@ fn type_check_internal<'gcx, 'tcx>(
136150
checker.constraints
137151
}
138152

139-
140153
fn mirbug(tcx: TyCtxt, span: Span, msg: &str) {
141154
// We sometimes see MIR failures (notably predicate failures) due to
142155
// the fact that we check rvalue sized predicates here. So use `delay_span_bug`
143156
// to avoid reporting bugs in those cases.
144157
tcx.sess.diagnostic().delay_span_bug(span, msg);
145158
}
146159

147-
macro_rules! span_mirbug {
148-
($context:expr, $elem:expr, $($message:tt)*) => ({
149-
mirbug($context.tcx(), $context.last_span,
150-
&format!("broken MIR in {:?} ({:?}): {}",
151-
$context.body_id,
152-
$elem,
153-
format_args!($($message)*)))
154-
})
155-
}
156-
157-
macro_rules! span_mirbug_and_err {
158-
($context:expr, $elem:expr, $($message:tt)*) => ({
159-
{
160-
span_mirbug!($context, $elem, $($message)*);
161-
$context.error()
162-
}
163-
})
164-
}
165-
166160
enum FieldAccessError {
167161
OutOfRange { field_count: usize },
168162
}
@@ -714,25 +708,6 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
714708
})
715709
}
716710

717-
fn equate_input_or_output(&mut self, unnormalized_a: Ty<'tcx>, b: Ty<'tcx>) {
718-
let start_position = Location {
719-
block: START_BLOCK,
720-
statement_index: 0,
721-
};
722-
let a = self.normalize(&unnormalized_a, start_position);
723-
if let Err(terr) = self.eq_types(a, b, start_position.at_self()) {
724-
span_mirbug!(
725-
self,
726-
start_position,
727-
"bad input or output {:?} normalized to {:?} should equal {:?} but got error {:?}",
728-
unnormalized_a,
729-
a,
730-
b,
731-
terr
732-
);
733-
}
734-
}
735-
736711
fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx> {
737712
self.infcx.tcx
738713
}

0 commit comments

Comments
 (0)