Description
Perhaps here I am missing something and it's just a mistake of mine, but even in such case, other programmers could trip like me and they could enjoy a better error message.
I have this small program, it compiles without warnings and works:
#![feature(underscore_lifetimes, in_band_lifetimes)]
//#![warn(single_use_lifetime)]
#![allow(dead_code)]
fn reverse_digits(mut n: u32, digs: &mut [u32]) -> &[u32] {
if n == 0 {
digs[0] = 0;
return &digs[0 .. 1];
}
let mut pos = 0;
while n > 0 {
digs[pos] = n % 10;
n /= 10;
pos += 1
}
&digs[.. pos]
}
fn rotations<'a, 'b>(n: u32, digs: &'a mut [u32], rots: &'b mut [u32]) -> &'b [u32] {
let digits = reverse_digits(n, digs);
let ln = digits.len();
let mut pos = 0;
for i in 0 .. ln {
let mut m = 0;
for j in (0 .. ln).rev() {
m = m * 10 + digits[(j + i) % ln];
}
rots[pos] = m;
pos += 1;
}
&rots[.. pos]
}
fn main() {}
I've tried to use the new single_use_lifetime
lint, if I uncomment the #![warn(single_use_lifetime)]
line, I see the warnings/errors:
warning: lifetime name `'b` only used once
--> C:\lavoro\bugs\test2.rs:20:18
|
20 | fn rotations<'a, 'b>(n: u32, digs: &'a mut [u32], rots: &'b mut [u32]) -> &'b [u32] {
| ^^
|
note: lint level defined here
--> C:\lavoro\bugs\test2.rs:3:9
|
3 | #![warn(single_use_lifetime)]
| ^^^^^^^^^^^^^^^^^^^
warning: lifetime name `'a` only used once
--> C:\lavoro\bugs\test2.rs:20:14
|
20 | fn rotations<'a, 'b>(n: u32, digs: &'a mut [u32], rots: &'b mut [u32]) -> &'b [u32] {
| ^^
Using in_band_lifetimes and leaving just 'a gives a similar warning:
warning: lifetime name `'a` only used once
--> C:\lavoro\bugs\test2.rs:20:48
|
20 | fn rotations(n: u32, digs: & mut [u32], rots: &'a mut [u32]) -> &'a [u32] {
| ^^
Not using in_band_lifetimes and leaving just 'a gives the same:
warning: lifetime name `'a` only used once
--> C:\lavoro\bugs\test2.rs:20:14
|
20 | fn rotations<'a>(n: u32, digs: & mut [u32], rots: &'a mut [u32]) -> &'a [u32] {
| ^^
The warning seems to suggest to use the 'a name too, but it doesn't compile:
20 | fn rotations(n: u32, digs: &mut [u32], rots: &mut [u32]) -> &[u32] {
| ^ expected lifetime parameter
|
= help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `digs` or `rots`
If I try to use an underscore the error is similar:
20 | fn rotations(n: u32, digs: & mut [u32], rots: &'_ mut [u32]) -> &'_ [u32] {
| ^^ expected lifetime parameter
|
= help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `digs` or `rots`
So is the message given to me by single_use_lifetime correct? If it's correct then I don't see how to fix the code to remove the warning