Skip to content

Commit f407b2b

Browse files
Rollup merge of rust-lang#44124 - gaurikholkar:return_self, r=arielb1
adding E0623 for return types - both parameters are anonymous This is a fix for rust-lang#44018 ``` error[E0621]: explicit lifetime required in the type of `self` --> $DIR/ex3-both-anon-regions-return-type-is-anon.rs:17:5 | 16 | fn foo<'a>(&self, x: &i32) -> &i32 { | ---- ---- | | | this parameter and the return type are declared with different lifetimes... 17 | x | ^ ...but data from `x` is returned here error: aborting due to previous error ``` It also works for the below case where we have self as anonymous ``` error[E0623]: lifetime mismatch --> src/test/ui/lifetime-errors/ex3-both-anon-regions-self-is-anon.rs:17:19 | 16 | fn foo<'a>(&self, x: &Foo) -> &Foo { | ---- ---- | | | this parameter and the return type are declared with different lifetimes... 17 | if true { x } else { self } | ^ ...but data from `x` is returned here error: aborting due to previous error ``` r? @nikomatsakis Currently, I have enabled E0621 where return type and self are anonymous, hence WIP.
2 parents 6f87d20 + 73543d5 commit f407b2b

12 files changed

+141
-249
lines changed

src/librustc/diagnostics.rs

+1-68
Original file line numberDiff line numberDiff line change
@@ -1351,74 +1351,6 @@ struct Foo<T: 'static> {
13511351
```
13521352
"##,
13531353

1354-
E0312: r##"
1355-
A lifetime of reference outlives lifetime of borrowed content.
1356-
1357-
Erroneous code example:
1358-
1359-
```compile_fail,E0312
1360-
fn make_child<'tree, 'human>(
1361-
x: &'human i32,
1362-
y: &'tree i32
1363-
) -> &'human i32 {
1364-
if x > y
1365-
{ x }
1366-
else
1367-
{ y }
1368-
// error: lifetime of reference outlives lifetime of borrowed content
1369-
}
1370-
```
1371-
1372-
The function declares that it returns a reference with the `'human`
1373-
lifetime, but it may return data with the `'tree` lifetime. As neither
1374-
lifetime is declared longer than the other, this results in an
1375-
error. Sometimes, this error is because the function *body* is
1376-
incorrect -- that is, maybe you did not *mean* to return data from
1377-
`y`. In that case, you should fix the function body.
1378-
1379-
Often, however, the body is correct. In that case, the function
1380-
signature needs to be altered to match the body, so that the caller
1381-
understands that data from either `x` or `y` may be returned. The
1382-
simplest way to do this is to give both function parameters the *same*
1383-
named lifetime:
1384-
1385-
```
1386-
fn make_child<'human>(
1387-
x: &'human i32,
1388-
y: &'human i32
1389-
) -> &'human i32 {
1390-
if x > y
1391-
{ x }
1392-
else
1393-
{ y } // ok!
1394-
}
1395-
```
1396-
1397-
However, in some cases, you may prefer to explicitly declare that one lifetime
1398-
outlives another using a `where` clause:
1399-
1400-
```
1401-
fn make_child<'tree, 'human>(
1402-
x: &'human i32,
1403-
y: &'tree i32
1404-
) -> &'human i32
1405-
where
1406-
'tree: 'human
1407-
{
1408-
if x > y
1409-
{ x }
1410-
else
1411-
{ y } // ok!
1412-
}
1413-
```
1414-
1415-
Here, the where clause `'tree: 'human` can be read as "the lifetime
1416-
'tree outlives the lifetime 'human" -- meaning, references with the
1417-
`'tree` lifetime live *at least as long as* references with the
1418-
`'human` lifetime. Therefore, it is safe to return data with lifetime
1419-
`'tree` when data with the lifetime `'human` is needed.
1420-
"##,
1421-
14221354
E0317: r##"
14231355
This error occurs when an `if` expression without an `else` block is used in a
14241356
context where a type other than `()` is expected, for example a `let`
@@ -2028,6 +1960,7 @@ register_diagnostics! {
20281960
// E0304, // expected signed integer constant
20291961
// E0305, // expected constant
20301962
E0311, // thing may not live long enough
1963+
E0312, // lifetime of reference outlives lifetime of borrowed content
20311964
E0313, // lifetime of borrowed pointer outlives lifetime of captured variable
20321965
E0314, // closure outlives stack frame
20331966
E0315, // cannot invoke closure outside of its lifetime

src/librustc/infer/error_reporting/different_lifetimes.rs

+71-51
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use infer::region_inference::RegionResolutionError;
1818
use hir::map as hir_map;
1919
use middle::resolve_lifetime as rl;
2020
use hir::intravisit::{self, Visitor, NestedVisitorMap};
21+
use infer::error_reporting::util::AnonymousArgInfo;
2122

2223
impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
2324
// This method prints the error message for lifetime errors when both the concerned regions
@@ -57,6 +58,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
5758
let ty_sup = or_false!(self.find_anon_type(sup, &bregion_sup));
5859

5960
let ty_sub = or_false!(self.find_anon_type(sub, &bregion_sub));
61+
6062
debug!("try_report_anon_anon_conflict: found_arg1={:?} sup={:?} br1={:?}",
6163
ty_sub,
6264
sup,
@@ -66,56 +68,70 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
6668
sub,
6769
bregion_sub);
6870

69-
let (main_label, label1, label2) = if let (Some(sup_arg), Some(sub_arg)) =
70-
(self.find_arg_with_region(sup, sup), self.find_arg_with_region(sub, sub)) {
71+
let (ty_sup, ty_fndecl_sup) = ty_sup;
72+
let (ty_sub, ty_fndecl_sub) = ty_sub;
7173

72-
let (anon_arg_sup, is_first_sup, anon_arg_sub, is_first_sub) =
73-
(sup_arg.arg, sup_arg.is_first, sub_arg.arg, sub_arg.is_first);
74-
if self.is_self_anon(is_first_sup, scope_def_id_sup) ||
75-
self.is_self_anon(is_first_sub, scope_def_id_sub) {
76-
return false;
77-
}
74+
let AnonymousArgInfo { arg: anon_arg_sup, .. } =
75+
or_false!(self.find_arg_with_region(sup, sup));
76+
let AnonymousArgInfo { arg: anon_arg_sub, .. } =
77+
or_false!(self.find_arg_with_region(sub, sub));
7878

79-
if self.is_return_type_anon(scope_def_id_sup, bregion_sup) ||
80-
self.is_return_type_anon(scope_def_id_sub, bregion_sub) {
81-
return false;
82-
}
79+
let sup_is_ret_type =
80+
self.is_return_type_anon(scope_def_id_sup, bregion_sup, ty_fndecl_sup);
81+
let sub_is_ret_type =
82+
self.is_return_type_anon(scope_def_id_sub, bregion_sub, ty_fndecl_sub);
8383

84-
if anon_arg_sup == anon_arg_sub {
85-
(format!("this type was declared with multiple lifetimes..."),
86-
format!(" with one lifetime"),
87-
format!(" into the other"))
88-
} else {
89-
let span_label_var1 = if let Some(simple_name) = anon_arg_sup.pat.simple_name() {
90-
format!(" from `{}`", simple_name)
91-
} else {
92-
format!("")
93-
};
84+
let span_label_var1 = if let Some(simple_name) = anon_arg_sup.pat.simple_name() {
85+
format!(" from `{}`", simple_name)
86+
} else {
87+
format!("")
88+
};
89+
90+
let span_label_var2 = if let Some(simple_name) = anon_arg_sub.pat.simple_name() {
91+
format!(" into `{}`", simple_name)
92+
} else {
93+
format!("")
94+
};
95+
96+
97+
let (span_1, span_2, main_label, span_label) = match (sup_is_ret_type, sub_is_ret_type) {
98+
(None, None) => {
99+
let (main_label_1, span_label_1) = if ty_sup == ty_sub {
94100

95-
let span_label_var2 = if let Some(simple_name) = anon_arg_sub.pat.simple_name() {
96-
format!(" into `{}`", simple_name)
101+
(format!("this type is declared with multiple lifetimes..."),
102+
format!("...but data{} flows{} here",
103+
format!(" with one lifetime"),
104+
format!(" into the other")))
97105
} else {
98-
format!("")
106+
(format!("these two types are declared with different lifetimes..."),
107+
format!("...but data{} flows{} here",
108+
span_label_var1,
109+
span_label_var2))
99110
};
111+
(ty_sup.span, ty_sub.span, main_label_1, span_label_1)
112+
}
100113

101-
let span_label =
102-
format!("these two types are declared with different lifetimes...",);
103-
104-
(span_label, span_label_var1, span_label_var2)
114+
(Some(ret_span), _) => {
115+
(ty_sub.span,
116+
ret_span,
117+
format!("this parameter and the return type are declared \
118+
with different lifetimes...",),
119+
format!("...but data{} is returned here", span_label_var1))
120+
}
121+
(_, Some(ret_span)) => {
122+
(ty_sup.span,
123+
ret_span,
124+
format!("this parameter and the return type are declared \
125+
with different lifetimes...",),
126+
format!("...but data{} is returned here", span_label_var1))
105127
}
106-
} else {
107-
debug!("no arg with anon region found");
108-
debug!("try_report_anon_anon_conflict: is_suitable(sub) = {:?}",
109-
self.is_suitable_region(sub));
110-
debug!("try_report_anon_anon_conflict: is_suitable(sup) = {:?}",
111-
self.is_suitable_region(sup));
112-
return false;
113128
};
114129

130+
115131
struct_span_err!(self.tcx.sess, span, E0623, "lifetime mismatch")
116-
.span_label(ty_sup.span, main_label)
117-
.span_label(ty_sub.span, format!(""))
118-
.span_label(span, format!("...but data{} flows{} here", label1, label2))
132+
.span_label(span_1, main_label)
133+
.span_label(span_2, format!(""))
134+
.span_label(span, span_label)
119135
.emit();
120136
return true;
121137
}
@@ -135,28 +151,32 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
135151
/// ```
136152
/// The function returns the nested type corresponding to the anonymous region
137153
/// for e.g. `&u8` and Vec<`&u8`.
138-
pub fn find_anon_type(&self, region: Region<'tcx>, br: &ty::BoundRegion) -> Option<&hir::Ty> {
154+
pub fn find_anon_type(&self,
155+
region: Region<'tcx>,
156+
br: &ty::BoundRegion)
157+
-> Option<(&hir::Ty, &hir::FnDecl)> {
139158
if let Some(anon_reg) = self.is_suitable_region(region) {
140159
let def_id = anon_reg.def_id;
141160
if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
142-
let inputs: &[_] = match self.tcx.hir.get(node_id) {
161+
let fndecl = match self.tcx.hir.get(node_id) {
143162
hir_map::NodeItem(&hir::Item { node: hir::ItemFn(ref fndecl, ..), .. }) => {
144-
&fndecl.inputs
163+
&fndecl
145164
}
146165
hir_map::NodeTraitItem(&hir::TraitItem {
147-
node: hir::TraitItemKind::Method(ref fndecl, ..), ..
148-
}) => &fndecl.decl.inputs,
166+
node: hir::TraitItemKind::Method(ref m, ..), ..
167+
}) |
149168
hir_map::NodeImplItem(&hir::ImplItem {
150-
node: hir::ImplItemKind::Method(ref fndecl, ..), ..
151-
}) => &fndecl.decl.inputs,
152-
153-
_ => &[],
169+
node: hir::ImplItemKind::Method(ref m, ..), ..
170+
}) => &m.decl,
171+
_ => return None,
154172
};
155173

156-
return inputs
174+
return fndecl
175+
.inputs
157176
.iter()
158-
.filter_map(|arg| self.find_component_for_bound_region(&**arg, br))
159-
.next();
177+
.filter_map(|arg| self.find_component_for_bound_region(arg, br))
178+
.next()
179+
.map(|ty| (ty, &**fndecl));
160180
}
161181
}
162182
None

src/librustc/infer/error_reporting/named_anon_conflict.rs

+25-27
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,17 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
3535
// only introduced anonymous regions in parameters) as well as a
3636
// version new_ty of its type where the anonymous region is replaced
3737
// with the named one.//scope_def_id
38-
let (named, anon_arg_info, region_info) =
38+
let (named, anon, anon_arg_info, region_info) =
3939
if self.is_named_region(sub) && self.is_suitable_region(sup).is_some() &&
4040
self.find_arg_with_region(sup, sub).is_some() {
4141
(sub,
42+
sup,
4243
self.find_arg_with_region(sup, sub).unwrap(),
4344
self.is_suitable_region(sup).unwrap())
4445
} else if self.is_named_region(sup) && self.is_suitable_region(sub).is_some() &&
4546
self.find_arg_with_region(sub, sup).is_some() {
4647
(sup,
48+
sub,
4749
self.find_arg_with_region(sub, sup).unwrap(),
4850
self.is_suitable_region(sub).unwrap())
4951
} else {
@@ -76,33 +78,29 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
7678
return false;
7779
}
7880

79-
if self.is_return_type_anon(scope_def_id, br) {
80-
debug!("try_report_named_anon_conflict: is_return_type_anon({:?}, {:?}) = true",
81-
scope_def_id,
82-
br);
83-
return false;
84-
} else if self.is_self_anon(is_first, scope_def_id) {
85-
debug!("try_report_named_anon_conflict: is_self_anon({:?}, {:?}) = true",
86-
is_first,
87-
scope_def_id);
88-
return false;
81+
if let Some((_, fndecl)) = self.find_anon_type(anon, &br) {
82+
if self.is_return_type_anon(scope_def_id, br, fndecl).is_some() ||
83+
self.is_self_anon(is_first, scope_def_id) {
84+
return false;
85+
}
86+
}
87+
88+
let (error_var, span_label_var) = if let Some(simple_name) = arg.pat.simple_name() {
89+
(format!("the type of `{}`", simple_name), format!("the type of `{}`", simple_name))
8990
} else {
90-
let (error_var, span_label_var) = if let Some(simple_name) = arg.pat.simple_name() {
91-
(format!("the type of `{}`", simple_name), format!("the type of `{}`", simple_name))
92-
} else {
93-
("parameter type".to_owned(), "type".to_owned())
94-
};
91+
("parameter type".to_owned(), "type".to_owned())
92+
};
93+
94+
struct_span_err!(self.tcx.sess,
95+
span,
96+
E0621,
97+
"explicit lifetime required in {}",
98+
error_var)
99+
.span_label(arg.pat.span,
100+
format!("consider changing {} to `{}`", span_label_var, new_ty))
101+
.span_label(span, format!("lifetime `{}` required", named))
102+
.emit();
103+
return true;
95104

96-
struct_span_err!(self.tcx.sess,
97-
span,
98-
E0621,
99-
"explicit lifetime required in {}",
100-
error_var)
101-
.span_label(arg.pat.span,
102-
format!("consider changing {} to `{}`", span_label_var, new_ty))
103-
.span_label(span, format!("lifetime `{}` required", named))
104-
.emit();
105-
return true;
106-
}
107105
}
108106
}

src/librustc/infer/error_reporting/util.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use infer::InferCtxt;
1515
use ty::{self, Region, Ty};
1616
use hir::def_id::DefId;
1717
use hir::map as hir_map;
18+
use syntax_pos::Span;
1819

1920
macro_rules! or_false {
2021
($v:expr) => {
@@ -163,20 +164,24 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
163164
// Here, we check for the case where the anonymous region
164165
// is in the return type.
165166
// FIXME(#42703) - Need to handle certain cases here.
166-
pub fn is_return_type_anon(&self, scope_def_id: DefId, br: ty::BoundRegion) -> bool {
167+
pub fn is_return_type_anon(&self,
168+
scope_def_id: DefId,
169+
br: ty::BoundRegion,
170+
decl: &hir::FnDecl)
171+
-> Option<Span> {
167172
let ret_ty = self.tcx.type_of(scope_def_id);
168173
match ret_ty.sty {
169174
ty::TyFnDef(_, _) => {
170175
let sig = ret_ty.fn_sig(self.tcx);
171176
let late_bound_regions = self.tcx
172177
.collect_referenced_late_bound_regions(&sig.output());
173178
if late_bound_regions.iter().any(|r| *r == br) {
174-
return true;
179+
return Some(decl.output.span());
175180
}
176181
}
177182
_ => {}
178183
}
179-
false
184+
None
180185
}
181186
// Here we check for the case where anonymous region
182187
// corresponds to self and if yes, we display E0312.

src/test/compile-fail/object-lifetime-default-mybox.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn load1<'a,'b>(a: &'a MyBox<SomeTrait>,
3434
b: &'b MyBox<SomeTrait>)
3535
-> &'b MyBox<SomeTrait>
3636
{
37-
a //~ ERROR E0312
37+
a //~ ERROR lifetime mismatch
3838
}
3939

4040
fn load2<'a>(ss: &MyBox<SomeTrait+'a>) -> MyBox<SomeTrait+'a> {

0 commit comments

Comments
 (0)