Skip to content

Commit 3180f1c

Browse files
committed
Fix #107998, avoid ICE when the generic_span is empty
1 parent 5348a89 commit 3180f1c

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

compiler/rustc_lint/src/context.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -837,9 +837,17 @@ pub trait LintContext: Sized {
837837
(use_span, "'_".to_owned())
838838
};
839839
debug!(?deletion_span, ?use_span);
840+
841+
// issue 107998 for the case such as a wrong function pointer type
842+
// `deletion_span` is empty and there is no need to report lifetime uses here
843+
let suggestions = if deletion_span.is_empty() {
844+
vec![(use_span, replace_lt)]
845+
} else {
846+
vec![(deletion_span, String::new()), (use_span, replace_lt)]
847+
};
840848
db.multipart_suggestion(
841849
msg,
842-
vec![(deletion_span, String::new()), (use_span, replace_lt)],
850+
suggestions,
843851
Applicability::MachineApplicable,
844852
);
845853
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![deny(single_use_lifetimes)]
2+
3+
fn with<R>(f: &fn<'a>(x: &'a i32) -> R) -> R {
4+
//~^ ERROR function pointer types may not have generic parameters
5+
//~| ERROR lifetime parameter `'a` only used once
6+
f(&3)
7+
}
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error: function pointer types may not have generic parameters
2+
--> $DIR/issue-107998.rs:3:18
3+
|
4+
LL | fn with<R>(f: &fn<'a>(x: &'a i32) -> R) -> R {
5+
| ^^^^
6+
|
7+
help: consider moving the lifetime parameter to a `for` parameter list
8+
|
9+
LL - fn with<R>(f: &fn<'a>(x: &'a i32) -> R) -> R {
10+
LL + fn with<R>(f: &for<'a> fn(x: &'a i32) -> R) -> R {
11+
|
12+
13+
error: lifetime parameter `'a` only used once
14+
--> $DIR/issue-107998.rs:3:19
15+
|
16+
LL | fn with<R>(f: &fn<'a>(x: &'a i32) -> R) -> R {
17+
| ^^ ---
18+
| | |
19+
| | ...is used only here
20+
| | help: elide the single-use lifetime
21+
| this lifetime...
22+
|
23+
note: the lint level is defined here
24+
--> $DIR/issue-107998.rs:1:9
25+
|
26+
LL | #![deny(single_use_lifetimes)]
27+
| ^^^^^^^^^^^^^^^^^^^^
28+
29+
error: aborting due to 2 previous errors
30+

0 commit comments

Comments
 (0)