Skip to content

Commit dddf24d

Browse files
committed
Auto merge of #43006 - GuillaumeGomez:e0611-cleanup, r=nikomatsakis
Clean up some code From #42669. r? @nikomatsakis
2 parents a1f180b + 24a5cea commit dddf24d

File tree

5 files changed

+41
-55
lines changed

5 files changed

+41
-55
lines changed

src/librustc/diagnostics.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -1947,35 +1947,34 @@ Either way, try to update/remove it in order to fix the error.
19471947
"##,
19481948

19491949
E0621: r##"
1950-
This error code indicates a mismatch between the function signature (i.e.,
1951-
the parameter types and the return type) and the function body. Most of
1952-
the time, this indicates that the function signature needs to be changed to
1953-
match the body, but it may be that the body needs to be changed to match
1954-
the signature.
1950+
This error code indicates a mismatch between the lifetimes appearing in the
1951+
function signature (i.e., the parameter types and the return type) and the
1952+
data-flow found in the function body.
19551953
1956-
Specifically, one or more of the parameters contain borrowed data that
1957-
needs to have a named lifetime in order for the body to type-check. Most of
1958-
the time, this is because the borrowed data is being returned from the
1959-
function, as in this example:
1954+
Erroneous code example:
19601955
19611956
```compile_fail,E0621
1962-
fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32 { // explicit lifetime required
1963-
// in the type of `y`
1957+
fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32 { // error: explicit lifetime
1958+
// required in the type of
1959+
// `y`
19641960
if x > y { x } else { y }
19651961
}
19661962
```
19671963
1968-
Here, the function is returning data borrowed from either x or y, but the
1969-
'a annotation indicates that it is returning data only from x. We can make
1970-
the signature match the body by changing the type of y to &'a i32, like so:
1964+
In the code above, the function is returning data borrowed from either `x` or
1965+
`y`, but the `'a` annotation indicates that it is returning data only from `x`.
1966+
To fix the error, the signature and the body must be made to match. Typically,
1967+
this is done by updating the function signature. So, in this case, we change
1968+
the type of `y` to `&'a i32`, like so:
19711969
19721970
```
19731971
fn foo<'a>(x: &'a i32, y: &'a i32) -> &'a i32 {
19741972
if x > y { x } else { y }
19751973
}
19761974
```
19771975
1978-
Alternatively, you could change the body not to return data from y:
1976+
Now the signature indicates that the function data borrowed from either `x` or
1977+
`y`. Alternatively, you could change the body to not return data from `y`:
19791978
19801979
```
19811980
fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32 {

src/librustc/infer/error_reporting/mod.rs

+25-31
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ use ty::error::TypeError;
7272
use syntax::ast::DUMMY_NODE_ID;
7373
use syntax_pos::{Pos, Span};
7474
use errors::{DiagnosticBuilder, DiagnosticStyledString};
75+
7576
mod note;
7677

7778
mod need_type_info;
7879
mod named_anon_conflict;
7980

80-
8181
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
8282
pub fn note_and_explain_region(self,
8383
err: &mut DiagnosticBuilder,
@@ -265,40 +265,34 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
265265
// together into a `ProcessedErrors` group:
266266
let errors = self.process_errors(errors);
267267

268-
debug!("report_region_errors: {} errors after preprocessing",
269-
errors.len());
268+
debug!("report_region_errors: {} errors after preprocessing", errors.len());
270269

271270
for error in errors {
272-
273271
debug!("report_region_errors: error = {:?}", error);
274272

275-
if !self.try_report_named_anon_conflict(&error){
276-
277-
match error.clone() {
278-
// These errors could indicate all manner of different
279-
// problems with many different solutions. Rather
280-
// than generate a "one size fits all" error, what we
281-
// attempt to do is go through a number of specific
282-
// scenarios and try to find the best way to present
283-
// the error. If all of these fails, we fall back to a rather
284-
// general bit of code that displays the error information
285-
ConcreteFailure(origin, sub, sup) => {
286-
287-
self.report_concrete_failure(origin, sub, sup).emit();
288-
}
289-
290-
GenericBoundFailure(kind, param_ty, sub) => {
291-
self.report_generic_bound_failure(kind, param_ty, sub);
292-
}
293-
294-
SubSupConflict(var_origin,
295-
sub_origin, sub_r,
296-
sup_origin, sup_r) => {
297-
self.report_sub_sup_conflict(var_origin,
298-
sub_origin, sub_r,
299-
sup_origin, sup_r);
300-
}
301-
}
273+
if !self.try_report_named_anon_conflict(&error) {
274+
match error.clone() {
275+
// These errors could indicate all manner of different
276+
// problems with many different solutions. Rather
277+
// than generate a "one size fits all" error, what we
278+
// attempt to do is go through a number of specific
279+
// scenarios and try to find the best way to present
280+
// the error. If all of these fails, we fall back to a rather
281+
// general bit of code that displays the error information
282+
ConcreteFailure(origin, sub, sup) => {
283+
self.report_concrete_failure(origin, sub, sup).emit();
284+
}
285+
GenericBoundFailure(kind, param_ty, sub) => {
286+
self.report_generic_bound_failure(kind, param_ty, sub);
287+
}
288+
SubSupConflict(var_origin,
289+
sub_origin, sub_r,
290+
sup_origin, sup_r) => {
291+
self.report_sub_sup_conflict(var_origin,
292+
sub_origin, sub_r,
293+
sup_origin, sup_r);
294+
}
295+
}
302296
}
303297
}
304298
}

src/librustc/infer/error_reporting/named_anon_conflict.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
3737

3838
match *anon_region {
3939
ty::ReFree(ref free_region) => {
40-
4140
let id = free_region.scope;
4241
let node_id = self.tcx.hir.as_local_node_id(id).unwrap();
4342
let body_id = self.tcx.hir.maybe_body_owned_by(node_id).unwrap();
@@ -69,15 +68,13 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
6968
}
7069
}
7170
_ => None,
72-
7371
}
7472
}
7573

7674
// This method generates the error message for the case when
7775
// the function arguments consist of a named region and an anonymous
7876
// region and corresponds to `ConcreteFailure(..)`
7977
pub fn try_report_named_anon_conflict(&self, error: &RegionResolutionError<'tcx>) -> bool {
80-
8178
let (span, sub, sup) = match *error {
8279
ConcreteFailure(ref origin, sub, sup) => (origin.span(), sub, sup),
8380
_ => return false, // inapplicable
@@ -113,7 +110,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
113110
.collect_referenced_late_bound_regions(&sig.output());
114111
if late_bound_regions.iter().any(|r| *r == br) {
115112
return false;
116-
} else {
117113
}
118114
}
119115
_ => {}
@@ -134,10 +130,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
134130
let (error_var, span_label_var) = if let Some(simple_name) = arg.pat.simple_name() {
135131
(format!("the type of `{}`", simple_name), format!("the type of `{}`", simple_name))
136132
} else {
137-
(format!("parameter type"), format!("type"))
133+
("parameter type".to_owned(), "type".to_owned())
138134
};
139135

140-
141136
struct_span_err!(self.tcx.sess,
142137
span,
143138
E0621,
@@ -149,13 +144,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
149144
.emit();
150145

151146
return true;
152-
153147
}
154148

155149
// This method returns whether the given Region is Anonymous
156150
// and returns the DefId corresponding to the region.
157151
pub fn is_suitable_anonymous_region(&self, region: Region<'tcx>) -> Option<DefId> {
158-
159152
match *region {
160153
ty::ReFree(ref free_region) => {
161154
match free_region.bound_region {

src/librustc/infer/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use errors::DiagnosticBuilder;
3838
use syntax_pos::{self, Span, DUMMY_SP};
3939
use util::nodemap::FxHashMap;
4040
use arena::DroplessArena;
41+
4142
use self::combine::CombineFields;
4243
use self::higher_ranked::HrMatchResult;
4344
use self::region_inference::{RegionVarBindings, RegionSnapshot};

src/librustc/ty/sty.rs

-1
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,6 @@ impl RegionKind {
993993

994994
// This method returns whether the given Region is Named
995995
pub fn is_named_region(&self) -> bool {
996-
997996
match *self {
998997
ty::ReFree(ref free_region) => {
999998
match free_region.bound_region {

0 commit comments

Comments
 (0)