現状のrustcのライフタイムアノテーション修正案提示について
- 現状のrustcでライフタイムアノテーションの修正案提示が入っている
fn long<'a, 'b>(a: &'a str, b: &'b str) -> &'b str {
if a.len() < b.len() {
b
} else {
a
}
}
fn main() {
println!("{}", long("a", "bb"));
}
上のをコンパイルすると下の感じに。
error: lifetime may not live long enough
--> test3.rs:5:9
|
1 | fn long<'a, 'b>(a: &'a str, b: &'b str) -> &'b str {
| -- -- lifetime `'b` defined here
| |
| lifetime `'a` defined here
...
5 | a
| ^ function was supposed to return data with lifetime `'b` but it is returning data with lifetime `'a`
|
= help: consider adding the following bound: `'a: 'b`
error: aborting due to 1 previous error
- 実装は rustc_borrowck/src/diagnostics/outlives_suggestion.rs にある。
- 見た感じ、regionのoutlive関係を見て、制約を組み立ててそれに従って提案している。
enum SuggestedConstraint {
/// Outlives(a, [b, c, d, ...]) => 'a: 'b + 'c + 'd + ...
Outlives(RegionName, SmallVec<[RegionName; 2]>),
/// 'a = 'b
Equal(RegionName, RegionName),
/// 'a: 'static i.e. 'a = 'static and the user should just use 'static
Static(RegionName),
}
- これが制約かな