Skip to content

Commit

Permalink
Prefer lifetime suggestion over generic error
Browse files Browse the repository at this point in the history
  • Loading branch information
ktt3ja committed Mar 24, 2014
1 parent 6f430c4 commit 19d913b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/librustc/middle/typeck/infer/error_reporting.rs
Expand Up @@ -195,13 +195,15 @@ impl<'a> ErrorReporting for InferCtxt<'a> {
// failed (so the return value of this method should not be used)
fn process_errors(&self, errors: &Vec<RegionResolutionError>)
-> Vec<RegionResolutionError> {
debug!("process_errors()");
let mut var_origins = Vec::new();
let mut trace_origins = Vec::new();
let mut same_regions = Vec::new();
let mut processed_errors = Vec::new();
for error in errors.iter() {
match *error {
ConcreteFailure(origin, sub, sup) => {
debug!("processing ConcreteFailure")
let trace = match origin {
infer::Subtype(trace) => Some(trace),
_ => None,
Expand All @@ -218,6 +220,7 @@ impl<'a> ErrorReporting for InferCtxt<'a> {
}
}
SubSupConflict(var_origin, _, sub_r, _, sup_r) => {
debug!("processing SubSupConflict")
match free_regions_from_same_fn(self.tcx, sub_r, sup_r) {
Some(ref same_frs) => {
var_origins.push(var_origin);
Expand All @@ -237,10 +240,13 @@ impl<'a> ErrorReporting for InferCtxt<'a> {
// declaration, we want to make sure that they are, in fact,
// from the same scope
if sr.scope_id != common_scope_id {
debug!("returning empty result from process_errors because
{} != {}", sr.scope_id, common_scope_id);
return vec!();
}
}
let pe = ProcessedErrors(var_origins, trace_origins, same_regions);
debug!("errors processed: {:?}", pe);
processed_errors.push(pe);
}
return processed_errors;
Expand All @@ -256,6 +262,7 @@ impl<'a> ErrorReporting for InferCtxt<'a> {
sub: Region,
sup: Region)
-> Option<FreeRegionsFromSameFn> {
debug!("free_regions_from_same_fn(sub={:?}, sup={:?})", sub, sup);
let (scope_id, fr1, fr2) = match (sub, sup) {
(ReFree(fr1), ReFree(fr2)) => {
if fr1.scope_id != fr2.scope_id {
Expand Down Expand Up @@ -284,7 +291,10 @@ impl<'a> ErrorReporting for InferCtxt<'a> {
},
_ => None
},
None => None
None => {
debug!("no parent node of scope_id {}", scope_id)
None
}
}
}

Expand Down
20 changes: 18 additions & 2 deletions src/librustc/middle/typeck/infer/region_inference/mod.rs
Expand Up @@ -1117,17 +1117,33 @@ impl<'a> RegionVarBindings<'a> {
{
// Errors in expanding nodes result from a lower-bound that is
// not contained by an upper-bound.
let (lower_bounds, lower_dup) =
let (mut lower_bounds, lower_dup) =
self.collect_concrete_regions(graph, var_data, node_idx,
graph::Incoming, dup_vec);
let (upper_bounds, upper_dup) =
let (mut upper_bounds, upper_dup) =
self.collect_concrete_regions(graph, var_data, node_idx,
graph::Outgoing, dup_vec);

if lower_dup || upper_dup {
return;
}

// We place free regions first because we are special casing
// SubSupConflict(ReFree, ReFree) when reporting error, and so
// the user will more likely get a specific suggestion.
fn free_regions_first(a: &RegionAndOrigin,
b: &RegionAndOrigin)
-> Ordering {
match (a.region, b.region) {
(ReFree(..), ReFree(..)) => Equal,
(ReFree(..), _) => Less,
(_, ReFree(..)) => Greater,
(_, _) => Equal,
}
}
lower_bounds.sort_by(|a, b| { free_regions_first(a, b) });
upper_bounds.sort_by(|a, b| { free_regions_first(a, b) });

for lower_bound in lower_bounds.iter() {
for upper_bound in upper_bounds.iter() {
if !self.is_subregion_of(lower_bound.region,
Expand Down

5 comments on commit 19d913b

@bors
Copy link
Contributor

@bors bors commented on 19d913b Mar 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from cmr
at ktt3ja@19d913b

@bors
Copy link
Contributor

@bors bors commented on 19d913b Mar 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging ktt3ja/rust/deterministic-lifetime-suggestion = 19d913b into auto

@bors
Copy link
Contributor

@bors bors commented on 19d913b Mar 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ktt3ja/rust/deterministic-lifetime-suggestion = 19d913b merged ok, testing candidate = 2c71cdf

@bors
Copy link
Contributor

@bors bors commented on 19d913b Mar 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 19d913b Mar 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 2c71cdf

Please sign in to comment.