Skip to content

Commit 688ab5a

Browse files
spastorinonikomatsakis
authored andcommitted
Check functions predicates
1 parent 5010496 commit 688ab5a

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

src/librustc_mir/transform/type_check.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ pub fn type_check<'a, 'gcx, 'tcx>(
5959
}
6060

6161
fn mirbug(tcx: TyCtxt, span: Span, msg: &str) {
62-
tcx.sess.diagnostic().span_bug(span, msg);
62+
// We sometimes see MIR failures (notably predicate failures) due to
63+
// the fact that we check rvalue sized predicates here. So use `delay_span_bug`
64+
// to avoid reporting bugs in those cases.
65+
tcx.sess.diagnostic().delay_span_bug(span, msg);
6366
}
6467

6568
macro_rules! span_mirbug {
@@ -171,7 +174,44 @@ impl<'a, 'b, 'gcx, 'tcx> TypeVerifier<'a, 'b, 'gcx, 'tcx> {
171174
);
172175

173176
let expected_ty = match constant.literal {
174-
Literal::Value { value } => value.ty,
177+
Literal::Value { value } => {
178+
if let ConstVal::Function(def_id, ..) = value.val {
179+
let tcx = self.tcx();
180+
let type_checker = &mut self.cx;
181+
182+
// FIXME -- For now, use the substitutions from
183+
// `value.ty` rather than `value.val`. The
184+
// renumberer will rewrite them to independent
185+
// sets of regions; in principle, we ought to
186+
// derive the type of the `value.val` from "first
187+
// principles" and equate with value.ty, but as we
188+
// are transitioning to the miri-based system, we
189+
// don't have a handy function for that, so for
190+
// now we just ignore `value.val` regions.
191+
let substs = match value.ty.sty {
192+
ty::TyFnDef(ty_def_id, substs) => {
193+
assert_eq!(def_id, ty_def_id);
194+
substs
195+
}
196+
_ => {
197+
span_bug!(
198+
self.last_span,
199+
"unexpected type for constant function: {:?}",
200+
value.ty
201+
)
202+
}
203+
};
204+
205+
let instantiated_predicates =
206+
tcx.predicates_of(def_id).instantiate(tcx, substs);
207+
let predicates =
208+
type_checker.normalize(&instantiated_predicates.predicates, location);
209+
type_checker.prove_predicates(&predicates, location);
210+
}
211+
212+
value.ty
213+
}
214+
175215
Literal::Promoted { .. } => {
176216
// FIXME -- promoted MIR return types reference
177217
// various "free regions" (e.g., scopes and things)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
// compile-flags: -Z borrowck=mir -Z nll
12+
13+
#![allow(dead_code)]
14+
15+
fn foo<'a, 'b>(x: &'a u32, y: &'b u32) -> (&'a u32, &'b u32)
16+
where
17+
'a: 'b,
18+
{
19+
(x, y)
20+
}
21+
22+
fn bar<'a, 'b>(x: &'a u32, y: &'b u32) -> (&'a u32, &'b u32) {
23+
foo(x, y)
24+
//~^ ERROR free region `'_#1r` does not outlive free region `'_#2r`
25+
//~| WARNING not reporting region error due to -Znll
26+
}
27+
28+
fn main() {}

0 commit comments

Comments
 (0)