Skip to content

Commit 075429f

Browse files
committed
Recover when resolution did not resolve lifetimes.
1 parent b5caa5a commit 075429f

File tree

5 files changed

+92
-11
lines changed

5 files changed

+92
-11
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -1169,15 +1169,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11691169
TyKind::Ptr(ref mt) => hir::TyKind::Ptr(self.lower_mt(mt, itctx)),
11701170
TyKind::Rptr(ref region, ref mt) => {
11711171
let region = region.unwrap_or_else(|| {
1172-
let Some(LifetimeRes::ElidedAnchor { start, end }) = self.resolver.get_lifetime_res(t.id) else {
1173-
panic!()
1172+
let id = if let Some(LifetimeRes::ElidedAnchor { start, end }) =
1173+
self.resolver.get_lifetime_res(t.id)
1174+
{
1175+
debug_assert_eq!(start.plus(1), end);
1176+
start
1177+
} else {
1178+
self.resolver.next_node_id()
11741179
};
1175-
debug_assert_eq!(start.plus(1), end);
11761180
let span = self.sess.source_map().next_point(t.span.shrink_to_lo());
1177-
Lifetime {
1178-
ident: Ident::new(kw::UnderscoreLifetime, span),
1179-
id: start,
1180-
}
1181+
Lifetime { ident: Ident::new(kw::UnderscoreLifetime, span), id }
11811182
});
11821183
let lifetime = self.lower_lifetime(&region);
11831184
hir::TyKind::Rptr(lifetime, self.lower_mt(mt, itctx))
@@ -1836,10 +1837,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18361837
fn lower_lifetime(&mut self, l: &Lifetime) -> hir::Lifetime {
18371838
let span = self.lower_span(l.ident.span);
18381839
let ident = self.lower_ident(l.ident);
1839-
let res = self
1840-
.resolver
1841-
.get_lifetime_res(l.id)
1842-
.unwrap_or_else(|| panic!("Missing resolution for lifetime {:?} at {:?}", l, span));
1840+
let res = self.resolver.get_lifetime_res(l.id).unwrap_or(LifetimeRes::Error);
18431841
self.new_named_lifetime_with_res(l.id, span, ident, res)
18441842
}
18451843

src/test/ui/lifetimes/issue-97193.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extern "C" {
2+
fn a(&mut self) {
3+
//~^ ERROR incorrect function inside `extern` block
4+
//~| ERROR `self` parameter is only allowed in associated functions
5+
fn b(buf: &Self) {}
6+
}
7+
}
8+
9+
fn main() {}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: incorrect function inside `extern` block
2+
--> $DIR/issue-97193.rs:2:8
3+
|
4+
LL | extern "C" {
5+
| ---------- `extern` blocks define existing foreign functions and functions inside of them cannot have a body
6+
LL | fn a(&mut self) {
7+
| ________^____________-
8+
| | |
9+
| | cannot have a body
10+
LL | |
11+
LL | |
12+
LL | | fn b(buf: &Self) {}
13+
LL | | }
14+
| |_____- help: remove the invalid body: `;`
15+
|
16+
= help: you might have meant to write a function accessible through FFI, which can be done by writing `extern fn` outside of the `extern` block
17+
= note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html
18+
19+
error: `self` parameter is only allowed in associated functions
20+
--> $DIR/issue-97193.rs:2:10
21+
|
22+
LL | fn a(&mut self) {
23+
| ^^^^^^^^^ not semantically valid as function parameter
24+
|
25+
= note: associated functions are those in `impl` or `trait` definitions
26+
27+
error: aborting due to 2 previous errors
28+

src/test/ui/lifetimes/issue-97194.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
extern "C" {
2+
fn bget(&self, index: [usize; Self::DIM]) -> bool {
3+
//~^ ERROR incorrect function inside `extern` block
4+
//~| ERROR `self` parameter is only allowed in associated functions
5+
//~| ERROR use of undeclared type `Self`
6+
type T<'a> = &'a str;
7+
}
8+
}
9+
10+
fn main() {}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error: incorrect function inside `extern` block
2+
--> $DIR/issue-97194.rs:2:8
3+
|
4+
LL | extern "C" {
5+
| ---------- `extern` blocks define existing foreign functions and functions inside of them cannot have a body
6+
LL | fn bget(&self, index: [usize; Self::DIM]) -> bool {
7+
| ________^^^^___________________________________________-
8+
| | |
9+
| | cannot have a body
10+
LL | |
11+
LL | |
12+
LL | |
13+
LL | | type T<'a> = &'a str;
14+
LL | | }
15+
| |_____- help: remove the invalid body: `;`
16+
|
17+
= help: you might have meant to write a function accessible through FFI, which can be done by writing `extern fn` outside of the `extern` block
18+
= note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html
19+
20+
error: `self` parameter is only allowed in associated functions
21+
--> $DIR/issue-97194.rs:2:13
22+
|
23+
LL | fn bget(&self, index: [usize; Self::DIM]) -> bool {
24+
| ^^^^^ not semantically valid as function parameter
25+
|
26+
= note: associated functions are those in `impl` or `trait` definitions
27+
28+
error[E0433]: failed to resolve: use of undeclared type `Self`
29+
--> $DIR/issue-97194.rs:2:35
30+
|
31+
LL | fn bget(&self, index: [usize; Self::DIM]) -> bool {
32+
| ^^^^ use of undeclared type `Self`
33+
34+
error: aborting due to 3 previous errors
35+
36+
For more information about this error, try `rustc --explain E0433`.

0 commit comments

Comments
 (0)