Skip to content

Commit 0ee698e

Browse files
committed
Rollup merge of rust-lang#47668 - nikomatsakis:issue-47511, r=eddyb
do not ICE when return type includes unconstrained anon region It turns out that this *can* happen after all, if the region is only used in projections from the input types. Fixes rust-lang#47511 r? @eddyb
2 parents 014931b + 215d66b commit 0ee698e

File tree

3 files changed

+71
-15
lines changed

3 files changed

+71
-15
lines changed

src/librustc_typeck/astconv.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -1206,22 +1206,27 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
12061206
let output = bare_fn_ty.output();
12071207
let late_bound_in_ret = tcx.collect_referenced_late_bound_regions(&output);
12081208
for br in late_bound_in_ret.difference(&late_bound_in_args) {
1209-
let br_name = match *br {
1210-
ty::BrNamed(_, name) => name,
1211-
_ => {
1212-
span_bug!(
1213-
decl.output.span(),
1214-
"anonymous bound region {:?} in return but not args",
1215-
br);
1216-
}
1209+
let lifetime_name = match *br {
1210+
ty::BrNamed(_, name) => format!("lifetime `{}`,", name),
1211+
ty::BrAnon(_) | ty::BrFresh(_) | ty::BrEnv => format!("an anonymous lifetime"),
12171212
};
1218-
struct_span_err!(tcx.sess,
1219-
decl.output.span(),
1220-
E0581,
1221-
"return type references lifetime `{}`, \
1222-
which does not appear in the fn input types",
1223-
br_name)
1224-
.emit();
1213+
let mut err = struct_span_err!(tcx.sess,
1214+
decl.output.span(),
1215+
E0581,
1216+
"return type references {} \
1217+
which is not constrained by the fn input types",
1218+
lifetime_name);
1219+
if let ty::BrAnon(_) = *br {
1220+
// The only way for an anonymous lifetime to wind up
1221+
// in the return type but **also** be unconstrained is
1222+
// if it only appears in "associated types" in the
1223+
// input. See #47511 for an example. In this case,
1224+
// though we can easily give a hint that ought to be
1225+
// relevant.
1226+
err.note("lifetimes appearing in an associated type \
1227+
are not considered constrained");
1228+
}
1229+
err.emit();
12251230
}
12261231

12271232
bare_fn_ty

src/test/ui/issue-47511.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2015 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+
// Regression test for #47511: anonymous lifetimes can appear
12+
// unconstrained in a return type, but only if they appear just once
13+
// in the input, as the input to a projection.
14+
15+
fn f(_: X) -> X {
16+
//~^ ERROR return type references an anonymous lifetime
17+
unimplemented!()
18+
}
19+
20+
fn g<'a>(_: X<'a>) -> X<'a> {
21+
//~^ ERROR return type references lifetime `'a`, which is not constrained
22+
unimplemented!()
23+
}
24+
25+
type X<'a> = <&'a () as Trait>::Value;
26+
27+
trait Trait {
28+
type Value;
29+
}
30+
31+
impl<'a> Trait for &'a () {
32+
type Value = ();
33+
}
34+
35+
fn main() {}

src/test/ui/issue-47511.stderr

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0581]: return type references an anonymous lifetime which is not constrained by the fn input types
2+
--> $DIR/issue-47511.rs:15:15
3+
|
4+
15 | fn f(_: X) -> X {
5+
| ^
6+
|
7+
= note: lifetimes appearing in an associated type are not considered constrained
8+
9+
error[E0581]: return type references lifetime `'a`, which is not constrained by the fn input types
10+
--> $DIR/issue-47511.rs:20:23
11+
|
12+
20 | fn g<'a>(_: X<'a>) -> X<'a> {
13+
| ^^^^^
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)