Skip to content

Commit e6a466e

Browse files
committed
Fix ICE and find correct return span.
This commit fixes an ICE and determines the correct return span in cases with a method implemented on a struct with an an elided lifetime.
1 parent 653da4f commit e6a466e

File tree

3 files changed

+55
-16
lines changed

3 files changed

+55
-16
lines changed

src/librustc_mir/borrow_check/nll/region_infer/error_reporting/region_name.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -687,22 +687,24 @@ impl<'tcx> RegionInferenceContext<'tcx> {
687687

688688
let mir_node_id = tcx.hir.as_local_node_id(mir_def_id).expect("non-local mir");
689689

690-
let (return_span, mir_description) =
691-
if let hir::ExprKind::Closure(_, _, _, span, gen_move) =
692-
tcx.hir.expect_expr(mir_node_id).node
693-
{
694-
(
695-
tcx.sess.source_map().end_point(span),
696-
if gen_move.is_some() {
697-
" of generator"
698-
} else {
699-
" of closure"
700-
},
701-
)
702-
} else {
703-
// unreachable?
704-
(mir.span, "")
705-
};
690+
let (return_span, mir_description) = match tcx.hir.get(mir_node_id) {
691+
hir::Node::Expr(hir::Expr {
692+
node: hir::ExprKind::Closure(_, _, _, span, gen_move),
693+
..
694+
}) => (
695+
tcx.sess.source_map().end_point(*span),
696+
if gen_move.is_some() {
697+
" of generator"
698+
} else {
699+
" of closure"
700+
},
701+
),
702+
hir::Node::ImplItem(hir::ImplItem {
703+
node: hir::ImplItemKind::Method(method_sig, _),
704+
..
705+
}) => (method_sig.decl.output.span(), ""),
706+
_ => (mir.span, ""),
707+
};
706708

707709
Some(RegionName {
708710
// This counter value will already have been used, so this function will increment it

src/test/ui/nll/issue-55394.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2017 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+
#![feature(nll)]
12+
13+
struct Bar;
14+
15+
struct Foo<'s> {
16+
bar: &'s mut Bar,
17+
}
18+
19+
impl Foo<'_> {
20+
fn new(bar: &mut Bar) -> Self {
21+
Foo { bar }
22+
}
23+
}
24+
25+
fn main() { }

src/test/ui/nll/issue-55394.stderr

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: unsatisfied lifetime constraints
2+
--> $DIR/issue-55394.rs:21:9
3+
|
4+
LL | fn new(bar: &mut Bar) -> Self {
5+
| - ---- return type is Foo<'2>
6+
| |
7+
| let's call the lifetime of this reference `'1`
8+
LL | Foo { bar }
9+
| ^^^^^^^^^^^ returning this value requires that `'1` must outlive `'2`
10+
11+
error: aborting due to previous error
12+

0 commit comments

Comments
 (0)