Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NLL: improve inference with flow results, represent regions with bitsets, and more #46319

Merged
merged 30 commits into from
Dec 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
932452e
separate out the liveness constraints from the final value
nikomatsakis Nov 10, 2017
a96b0cf
replace constant regions with a post-inference check
nikomatsakis Nov 10, 2017
08c8d7e
borrow_check.rs: rustfmt
nikomatsakis Nov 17, 2017
a9cb25b
inform constraint generation using maybe-init
Nashenas88 Nov 17, 2017
22b3175
region_infer: BitMatrix representation of region values
zackmdavis Nov 21, 2017
aa51603
extract the `implied_bounds` code into a helper function
nikomatsakis Nov 10, 2017
abd7d88
move `free_regions_map` into `infer::outlives`
nikomatsakis Nov 10, 2017
5562663
make `resolve_regions_and_report_errors` take an `OutlivesEnv`
nikomatsakis Dec 1, 2017
1f33145
make `no_late_bound_regions` a method on `Binder<T>`
nikomatsakis Nov 12, 2017
c45307f
extract the code to create `OutlivesBounds` into its own module
nikomatsakis Nov 12, 2017
31d61f1
rename `implied_bounds` module to `bounds`
nikomatsakis Nov 12, 2017
45f6027
break type-checking of aggregate-kind out into helper function
nikomatsakis Nov 17, 2017
fb4b06a
rename `greater_than` to `reachable_from`
nikomatsakis Nov 20, 2017
663e7a4
promote region_infer into its own module
nikomatsakis Nov 20, 2017
4de7336
extend TransitiveRelation with `parents` function
nikomatsakis Nov 20, 2017
49d2274
constraint_generation: create liveness constraints more thoroughly
nikomatsakis Nov 20, 2017
e9067bd
MIR dump: restructure pretty to be more extensible by other code
nikomatsakis Nov 20, 2017
7f247ad
move `liberate_late_bound_regions` to a method on the tcx
nikomatsakis Nov 21, 2017
fa813f7
introduce `closure_env_ty` helper to compute ty of closure env arg
nikomatsakis Nov 21, 2017
243bf3f
rename "free region" to "universally quantified region"
nikomatsakis Nov 21, 2017
2e63bb3
fix debruijn account in `for_each_free_region`
nikomatsakis Nov 22, 2017
18cc0f9
document `closure_base_def_id`
nikomatsakis Nov 22, 2017
506e80c
remove unused span from `eq_types` (and rustfmt slightly)
nikomatsakis Nov 22, 2017
c1708ab
type_check.rs: rustfmt
nikomatsakis Dec 2, 2017
14480b4
free_region_map: Fix typo in comment: r_a <= r_b
nikomatsakis Dec 4, 2017
a393596
region_infer: Fix typo in comment
nikomatsakis Dec 4, 2017
7409ffd
outlives/env: Fix comment that lost surrounding context.
nikomatsakis Dec 4, 2017
2716160
transtive_relation: fix typo in comment for `parents`
nikomatsakis Dec 4, 2017
ab2bc9f
outlives/env: fix comment, say must and not should
nikomatsakis Dec 4, 2017
a6adc74
adopt `longer` and `shorter` rather than `fr` and `outlived_fr`
nikomatsakis Dec 4, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/librustc/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub use ty::IntVarValue;
pub use self::freshen::TypeFreshener;

use hir::def_id::DefId;
use middle::free_region::{FreeRegionMap, RegionRelations};
use middle::free_region::RegionRelations;
use middle::region;
use middle::lang_items;
use mir::tcx::PlaceTy;
Expand All @@ -44,6 +44,7 @@ use self::higher_ranked::HrMatchResult;
use self::region_constraints::{RegionConstraintCollector, RegionSnapshot};
use self::region_constraints::{GenericKind, VerifyBound, RegionConstraintData, VarOrigins};
use self::lexical_region_resolve::LexicalRegionResolutions;
use self::outlives::env::OutlivesEnvironment;
use self::type_variable::TypeVariableOrigin;
use self::unify_key::ToType;

Expand All @@ -58,15 +59,13 @@ pub mod lattice;
mod lub;
pub mod region_constraints;
mod lexical_region_resolve;
mod outlives;
pub mod outlives;
pub mod resolve;
mod freshen;
mod sub;
pub mod type_variable;
pub mod unify_key;

pub use self::outlives::env::OutlivesEnvironment;

#[must_use]
pub struct InferOk<'tcx, T> {
pub value: T,
Expand Down Expand Up @@ -1157,15 +1156,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
pub fn resolve_regions_and_report_errors(&self,
region_context: DefId,
region_map: &region::ScopeTree,
free_regions: &FreeRegionMap<'tcx>) {
outlives_env: &OutlivesEnvironment<'tcx>) {
assert!(self.is_tainted_by_errors() || self.region_obligations.borrow().is_empty(),
"region_obligations not empty: {:#?}",
self.region_obligations.borrow());

let region_rels = &RegionRelations::new(self.tcx,
region_context,
region_map,
free_regions);
outlives_env.free_region_map());
let (var_origins, data) = self.region_constraints.borrow_mut()
.take()
.expect("regions already resolved")
Expand Down
218 changes: 218 additions & 0 deletions src/librustc/infer/outlives/bounds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use infer::InferCtxt;
use syntax::ast;
use syntax::codemap::Span;
use traits::FulfillmentContext;
use ty::{self, Ty, TypeFoldable};
use ty::outlives::Component;
use ty::wf;

/// Outlives bounds are relationships between generic parameters,
/// whether they both be regions (`'a: 'b`) or whether types are
/// involved (`T: 'a`). These relationships can be extracted from the
/// full set of predicates we understand or also from types (in which
/// case they are called implied bounds). They are fed to the
/// `OutlivesEnv` which in turn is supplied to the region checker and
/// other parts of the inference system.
#[derive(Debug)]
pub enum OutlivesBound<'tcx> {
RegionSubRegion(ty::Region<'tcx>, ty::Region<'tcx>),
RegionSubParam(ty::Region<'tcx>, ty::ParamTy),
RegionSubProjection(ty::Region<'tcx>, ty::ProjectionTy<'tcx>),
}

impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
/// Implied bounds are region relationships that we deduce
/// automatically. The idea is that (e.g.) a caller must check that a
/// function's argument types are well-formed immediately before
/// calling that fn, and hence the *callee* can assume that its
/// argument types are well-formed. This may imply certain relationships
/// between generic parameters. For example:
///
/// fn foo<'a,T>(x: &'a T)
///
/// can only be called with a `'a` and `T` such that `&'a T` is WF.
/// For `&'a T` to be WF, `T: 'a` must hold. So we can assume `T: 'a`.
///
/// # Parameters
///
/// - `param_env`, the where-clauses in scope
/// - `body_id`, the body-id to use when normalizing assoc types.
/// Note that this may cause outlives obligations to be injected
/// into the inference context with this body-id.
/// - `ty`, the type that we are supposed to assume is WF.
/// - `span`, a span to use when normalizing, hopefully not important,
/// might be useful if a `bug!` occurs.
pub fn implied_outlives_bounds(
&self,
param_env: ty::ParamEnv<'tcx>,
body_id: ast::NodeId,
ty: Ty<'tcx>,
span: Span,
) -> Vec<OutlivesBound<'tcx>> {
let tcx = self.tcx;

// Sometimes when we ask what it takes for T: WF, we get back that
// U: WF is required; in that case, we push U onto this stack and
// process it next. Currently (at least) these resulting
// predicates are always guaranteed to be a subset of the original
// type, so we need not fear non-termination.
let mut wf_types = vec![ty];

let mut implied_bounds = vec![];

let mut fulfill_cx = FulfillmentContext::new();

while let Some(ty) = wf_types.pop() {
// Compute the obligations for `ty` to be well-formed. If `ty` is
// an unresolved inference variable, just substituted an empty set
// -- because the return type here is going to be things we *add*
// to the environment, it's always ok for this set to be smaller
// than the ultimate set. (Note: normally there won't be
// unresolved inference variables here anyway, but there might be
// during typeck under some circumstances.)
let obligations = wf::obligations(self, param_env, body_id, ty, span).unwrap_or(vec![]);

// NB: All of these predicates *ought* to be easily proven
// true. In fact, their correctness is (mostly) implied by
// other parts of the program. However, in #42552, we had
// an annoying scenario where:
//
// - Some `T::Foo` gets normalized, resulting in a
// variable `_1` and a `T: Trait<Foo=_1>` constraint
// (not sure why it couldn't immediately get
// solved). This result of `_1` got cached.
// - These obligations were dropped on the floor here,
// rather than being registered.
// - Then later we would get a request to normalize
// `T::Foo` which would result in `_1` being used from
// the cache, but hence without the `T: Trait<Foo=_1>`
// constraint. As a result, `_1` never gets resolved,
// and we get an ICE (in dropck).
//
// Therefore, we register any predicates involving
// inference variables. We restrict ourselves to those
// involving inference variables both for efficiency and
// to avoids duplicate errors that otherwise show up.
fulfill_cx.register_predicate_obligations(
self,
obligations
.iter()
.filter(|o| o.predicate.has_infer_types())
.cloned(),
);

// From the full set of obligations, just filter down to the
// region relationships.
implied_bounds.extend(obligations.into_iter().flat_map(|obligation| {
assert!(!obligation.has_escaping_regions());
match obligation.predicate {
ty::Predicate::Trait(..) |
ty::Predicate::Equate(..) |
ty::Predicate::Subtype(..) |
ty::Predicate::Projection(..) |
ty::Predicate::ClosureKind(..) |
ty::Predicate::ObjectSafe(..) |
ty::Predicate::ConstEvaluatable(..) => vec![],

ty::Predicate::WellFormed(subty) => {
wf_types.push(subty);
vec![]
}

ty::Predicate::RegionOutlives(ref data) => match data.no_late_bound_regions() {
None => vec![],
Some(ty::OutlivesPredicate(r_a, r_b)) => {
vec![OutlivesBound::RegionSubRegion(r_b, r_a)]
}
},

ty::Predicate::TypeOutlives(ref data) => match data.no_late_bound_regions() {
None => vec![],
Some(ty::OutlivesPredicate(ty_a, r_b)) => {
let ty_a = self.resolve_type_vars_if_possible(&ty_a);
let components = tcx.outlives_components(ty_a);
Self::implied_bounds_from_components(r_b, components)
}
},
}
}));
}

// Ensure that those obligations that we had to solve
// get solved *here*.
match fulfill_cx.select_all_or_error(self) {
Ok(()) => (),
Err(errors) => self.report_fulfillment_errors(&errors, None),
}

implied_bounds
}

/// When we have an implied bound that `T: 'a`, we can further break
/// this down to determine what relationships would have to hold for
/// `T: 'a` to hold. We get to assume that the caller has validated
/// those relationships.
fn implied_bounds_from_components(
sub_region: ty::Region<'tcx>,
sup_components: Vec<Component<'tcx>>,
) -> Vec<OutlivesBound<'tcx>> {
sup_components
.into_iter()
.flat_map(|component| {
match component {
Component::Region(r) =>
vec![OutlivesBound::RegionSubRegion(sub_region, r)],
Component::Param(p) =>
vec![OutlivesBound::RegionSubParam(sub_region, p)],
Component::Projection(p) =>
vec![OutlivesBound::RegionSubProjection(sub_region, p)],
Component::EscapingProjection(_) =>
// If the projection has escaping regions, don't
// try to infer any implied bounds even for its
// free components. This is conservative, because
// the caller will still have to prove that those
// free components outlive `sub_region`. But the
// idea is that the WAY that the caller proves
// that may change in the future and we want to
// give ourselves room to get smarter here.
vec![],
Component::UnresolvedInferenceVariable(..) =>
vec![],
}
})
.collect()
}
}

pub fn explicit_outlives_bounds<'tcx>(
param_env: ty::ParamEnv<'tcx>,
) -> impl Iterator<Item = OutlivesBound<'tcx>> + 'tcx {
debug!("explicit_outlives_bounds()");
param_env
.caller_bounds
.into_iter()
.filter_map(move |predicate| match predicate {
ty::Predicate::Projection(..) |
ty::Predicate::Trait(..) |
ty::Predicate::Equate(..) |
ty::Predicate::Subtype(..) |
ty::Predicate::WellFormed(..) |
ty::Predicate::ObjectSafe(..) |
ty::Predicate::ClosureKind(..) |
ty::Predicate::TypeOutlives(..) |
ty::Predicate::ConstEvaluatable(..) => None,
ty::Predicate::RegionOutlives(ref data) => data.no_late_bound_regions().map(
|ty::OutlivesPredicate(r_a, r_b)| OutlivesBound::RegionSubRegion(r_b, r_a),
),
})
}
Loading