Skip to content

Commit 987c6a7

Browse files
committed
make resolve_regions_and_report_errors take an OutlivesEnv
This revealed some shortcomings, one of which is fixed. Fixes rust-lang#45937.
1 parent 8bfccb6 commit 987c6a7

File tree

9 files changed

+89
-45
lines changed

9 files changed

+89
-45
lines changed

src/librustc/infer/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use self::higher_ranked::HrMatchResult;
4343
use self::region_constraints::{RegionConstraintCollector, RegionSnapshot};
4444
use self::region_constraints::{GenericKind, VerifyBound, RegionConstraintData, VarOrigins};
4545
use self::lexical_region_resolve::LexicalRegionResolutions;
46-
use self::outlives::free_region_map::FreeRegionMap;
46+
use self::outlives::env::OutlivesEnvironment;
4747
use self::type_variable::TypeVariableOrigin;
4848
use self::unify_key::ToType;
4949

@@ -1155,15 +1155,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
11551155
pub fn resolve_regions_and_report_errors(&self,
11561156
region_context: DefId,
11571157
region_map: &region::ScopeTree,
1158-
free_regions: &FreeRegionMap<'tcx>) {
1158+
outlives_env: &OutlivesEnvironment<'tcx>) {
11591159
assert!(self.is_tainted_by_errors() || self.region_obligations.borrow().is_empty(),
11601160
"region_obligations not empty: {:#?}",
11611161
self.region_obligations.borrow());
11621162

11631163
let region_rels = &RegionRelations::new(self.tcx,
11641164
region_context,
11651165
region_map,
1166-
free_regions);
1166+
outlives_env.free_region_map());
11671167
let (var_origins, data) = self.region_constraints.borrow_mut()
11681168
.take()
11691169
.expect("regions already resolved")

src/librustc/infer/outlives/env.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@ pub struct OutlivesEnvironment<'tcx> {
4444

4545
impl<'a, 'gcx: 'tcx, 'tcx: 'a> OutlivesEnvironment<'tcx> {
4646
pub fn new(param_env: ty::ParamEnv<'tcx>) -> Self {
47-
let mut free_region_map = FreeRegionMap::new();
48-
free_region_map.relate_free_regions_from_predicates(&param_env.caller_bounds);
49-
50-
OutlivesEnvironment {
47+
let mut env = OutlivesEnvironment {
5148
param_env,
52-
free_region_map,
49+
free_region_map: FreeRegionMap::new(),
5350
region_bound_pairs: vec![],
54-
}
51+
};
52+
53+
env.init_free_regions_from_predicates();
54+
55+
env
5556
}
5657

5758
/// Borrows current value of the `free_region_map`.
@@ -183,4 +184,27 @@ impl<'a, 'gcx: 'tcx, 'tcx: 'a> OutlivesEnvironment<'tcx> {
183184
}
184185
}
185186
}
187+
188+
fn init_free_regions_from_predicates(&mut self) {
189+
debug!("init_free_regions_from_predicates()");
190+
for predicate in self.param_env.caller_bounds {
191+
debug!("init_free_regions_from_predicates: predicate={:?}", predicate);
192+
match *predicate {
193+
ty::Predicate::Projection(..) |
194+
ty::Predicate::Trait(..) |
195+
ty::Predicate::Equate(..) |
196+
ty::Predicate::Subtype(..) |
197+
ty::Predicate::WellFormed(..) |
198+
ty::Predicate::ObjectSafe(..) |
199+
ty::Predicate::ClosureKind(..) |
200+
ty::Predicate::TypeOutlives(..) |
201+
ty::Predicate::ConstEvaluatable(..) => {
202+
// No region bounds here
203+
}
204+
ty::Predicate::RegionOutlives(ty::Binder(ty::OutlivesPredicate(r_a, r_b))) => {
205+
self.free_region_map.relate_regions(r_b, r_a);
206+
}
207+
}
208+
}
209+
}
186210
}

src/librustc/infer/outlives/free_region_map.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,6 @@ impl<'tcx> FreeRegionMap<'tcx> {
2929
self.relation.is_empty()
3030
}
3131

32-
pub fn relate_free_regions_from_predicates(&mut self,
33-
predicates: &[ty::Predicate<'tcx>]) {
34-
debug!("relate_free_regions_from_predicates(predicates={:?})", predicates);
35-
for predicate in predicates {
36-
match *predicate {
37-
ty::Predicate::Projection(..) |
38-
ty::Predicate::Trait(..) |
39-
ty::Predicate::Equate(..) |
40-
ty::Predicate::Subtype(..) |
41-
ty::Predicate::WellFormed(..) |
42-
ty::Predicate::ObjectSafe(..) |
43-
ty::Predicate::ClosureKind(..) |
44-
ty::Predicate::TypeOutlives(..) |
45-
ty::Predicate::ConstEvaluatable(..) => {
46-
// No region bounds here
47-
}
48-
ty::Predicate::RegionOutlives(ty::Binder(ty::OutlivesPredicate(r_a, r_b))) => {
49-
self.relate_regions(r_b, r_a);
50-
}
51-
}
52-
}
53-
}
54-
5532
// Record that `'sup:'sub`. Or, put another way, `'sub <= 'sup`.
5633
// (with the exception that `'static: 'x` is not notable)
5734
pub fn relate_regions(&mut self, sub: Region<'tcx>, sup: Region<'tcx>) {

src/librustc/traits/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub use self::ObligationCauseCode::*;
1717

1818
use hir;
1919
use hir::def_id::DefId;
20-
use infer::outlives::free_region_map::FreeRegionMap;
20+
use infer::outlives::env::OutlivesEnvironment;
2121
use middle::const_val::ConstEvalErr;
2222
use middle::region;
2323
use ty::subst::Substs;
@@ -535,7 +535,6 @@ pub fn normalize_param_env_or_error<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
535535
predicates);
536536

537537
let region_scope_tree = region::ScopeTree::default();
538-
let free_regions = FreeRegionMap::new();
539538

540539
// FIXME. We should really... do something with these region
541540
// obligations. But this call just continues the older
@@ -547,7 +546,12 @@ pub fn normalize_param_env_or_error<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
547546
// out). -nmatsakis
548547
let _ = infcx.ignore_region_obligations();
549548

550-
infcx.resolve_regions_and_report_errors(region_context, &region_scope_tree, &free_regions);
549+
// We can use the `elaborated_env` here; the region code only
550+
// cares about declarations like `'a: 'b`.
551+
let outlives_env = OutlivesEnvironment::new(elaborated_env);
552+
553+
infcx.resolve_regions_and_report_errors(region_context, &region_scope_tree, &outlives_env);
554+
551555
let predicates = match infcx.fully_resolve(&predicates) {
552556
Ok(predicates) => predicates,
553557
Err(fixup_err) => {

src/librustc_driver/test.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ use driver;
1717
use rustc_lint;
1818
use rustc_resolve::MakeGlobMap;
1919
use rustc_trans;
20-
use rustc::middle::free_region::FreeRegionMap;
2120
use rustc::middle::region;
2221
use rustc::middle::resolve_lifetime;
2322
use rustc::ty::subst::{Kind, Subst};
2423
use rustc::traits::{ObligationCause, Reveal};
2524
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
2625
use rustc::ty::maps::OnDiskCache;
2726
use rustc::infer::{self, InferOk, InferResult};
27+
use rustc::infer::outlives::env::OutlivesEnvironment;
2828
use rustc::infer::type_variable::TypeVariableOrigin;
2929
use rustc_metadata::cstore::CStore;
3030
use rustc::hir::map as hir_map;
@@ -162,14 +162,15 @@ fn test_env<F>(source_string: &str,
162162
|tcx| {
163163
tcx.infer_ctxt().enter(|infcx| {
164164
let mut region_scope_tree = region::ScopeTree::default();
165+
let param_env = ty::ParamEnv::empty(Reveal::UserFacing);
165166
body(Env {
166167
infcx: &infcx,
167168
region_scope_tree: &mut region_scope_tree,
168-
param_env: ty::ParamEnv::empty(Reveal::UserFacing),
169+
param_env: param_env,
169170
});
170-
let free_regions = FreeRegionMap::new();
171+
let outlives_env = OutlivesEnvironment::new(param_env);
171172
let def_id = tcx.hir.local_def_id(ast::CRATE_NODE_ID);
172-
infcx.resolve_regions_and_report_errors(def_id, &region_scope_tree, &free_regions);
173+
infcx.resolve_regions_and_report_errors(def_id, &region_scope_tree, &outlives_env);
173174
assert_eq!(tcx.sess.err_count(), expected_err_count);
174175
});
175176
});

src/librustc_typeck/check/dropck.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ use check::regionck::RegionCtxt;
1212

1313
use hir::def_id::DefId;
1414
use rustc::infer::{self, InferOk};
15-
use rustc::infer::outlives::free_region_map::FreeRegionMap;
15+
use rustc::infer::outlives::env::OutlivesEnvironment;
1616
use rustc::middle::region;
1717
use rustc::ty::subst::{Subst, Substs};
1818
use rustc::ty::{self, Ty, TyCtxt};
19-
use rustc::traits::{self, ObligationCause};
19+
use rustc::traits::{self, Reveal, ObligationCause};
2020
use util::common::ErrorReported;
2121
use util::nodemap::FxHashSet;
2222

@@ -115,8 +115,9 @@ fn ensure_drop_params_and_item_params_correspond<'a, 'tcx>(
115115
}
116116

117117
let region_scope_tree = region::ScopeTree::default();
118-
let free_regions = FreeRegionMap::new();
119-
infcx.resolve_regions_and_report_errors(drop_impl_did, &region_scope_tree, &free_regions);
118+
// TODO. Using an empty param-environment here surely causes unnecessary region errors.
119+
let outlives_env = OutlivesEnvironment::new(ty::ParamEnv::empty(Reveal::UserFacing));
120+
infcx.resolve_regions_and_report_errors(drop_impl_did, &region_scope_tree, &outlives_env);
120121
Ok(())
121122
})
122123
}

src/librustc_typeck/check/regionck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ impl<'a, 'gcx, 'tcx> RegionCtxt<'a, 'gcx, 'tcx> {
362362
fn resolve_regions_and_report_errors(&self) {
363363
self.fcx.resolve_regions_and_report_errors(self.subject_def_id,
364364
&self.region_scope_tree,
365-
self.outlives_environment.free_region_map());
365+
&self.outlives_environment);
366366
}
367367

368368
fn constrain_bindings_in_pat(&mut self, pat: &hir::Pat) {

src/librustc_typeck/coherence/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ pub fn coerce_unsized_info<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
395395
infcx.resolve_regions_and_report_errors(
396396
impl_did,
397397
&region_scope_tree,
398-
outlives_env.free_region_map()
398+
&outlives_env,
399399
);
400400

401401
CoerceUnsizedInfo {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2016 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+
// Test that we are able to normalize in the list of where-clauses,
12+
// even if `'a: 'b` is required.
13+
14+
trait Project<'a, 'b> {
15+
type Item;
16+
}
17+
18+
impl<'a, 'b> Project<'a, 'b> for ()
19+
where 'a: 'b
20+
{
21+
type Item = ();
22+
}
23+
24+
// No error here, we have 'a: 'b. We used to report an error here
25+
// though, see https://github.com/rust-lang/rust/issues/45937.
26+
fn foo<'a: 'b, 'b>()
27+
where <() as Project<'a, 'b>>::Item : Eq
28+
{
29+
}
30+
31+
// Here we get an error: we need `'a: 'b`.
32+
fn bar<'a, 'b>() //~ ERROR cannot infer
33+
where <() as Project<'a, 'b>>::Item : Eq
34+
{
35+
}
36+
37+
fn main() { }

0 commit comments

Comments
 (0)