Skip to content

Possible single_use_lifetime false positive #46873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
leonardo-m opened this issue Dec 20, 2017 · 6 comments
Closed

Possible single_use_lifetime false positive #46873

leonardo-m opened this issue Dec 20, 2017 · 6 comments
Labels
A-lifetimes Area: Lifetimes / regions A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@leonardo-m
Copy link

leonardo-m commented Dec 20, 2017

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

@Manishearth
Copy link
Member

It wants you to say

fn rotations(n: u32, digs: &mut [u32], rots: &'rots mut [u32]) -> &'rots [u32] {

@Manishearth
Copy link
Member

Filed a specific issue on improving the diagnostics at #46944

@leonardo-m
Copy link
Author

leonardo-m commented Dec 22, 2017

Perhaps I'm still missing something, but I am still seeing warnings:

warning: lifetime name `'rots` only used once
  --> ...\test.rs:20:47
   |
20 | fn rotations(n: u32, digs: &mut [u32], rots: &'rots mut [u32]) -> &'rots [u32] {
   |                                               ^^^^^
   |
note: lint level defined here
  --> ...\test.rs:3:9
   |
3  | #![warn(single_use_lifetime)]
   |         ^^^^^^^^^^^^^^^^^^^

warning: lifetime name `'rots` only used once
  --> ...\test.rs:20:47
   |
20 | fn rotations(n: u32, digs: &mut [u32], rots: &'rots mut [u32]) -> &'rots [u32] {
   |                                               ^^^^^

Another possible solution:

warning: lifetime name `'rots` only used once
  --> src/main.rs:20:62
   |
20 | fn rotations(n: u32, digs: &mut [u32], rots: &mut [u32]) -> &'rots [u32] {
   |                                                              ^^^^^
   |
note: lint level defined here
  --> src/main.rs:3:9
   |
3  | #![warn(single_use_lifetime)]
   |         ^^^^^^^^^^^^^^^^^^^

error[E0621]: explicit lifetime required in the type of `rots`
  --> src/main.rs:32:5
   |
20 | fn rotations(n: u32, digs: &mut [u32], rots: &mut [u32]) -> &'rots [u32] {
   |                                        ---- consider changing the type of `rots` to `&'rots mut [u32]`
...
32 |     &rots[.. pos]
   |     ^^^^^^^^^^^^^ lifetime `'rots` required

@leonardo-m
Copy link
Author

How can I reopen this issue?

@petrochenkov petrochenkov reopened this Dec 26, 2017
@Manishearth
Copy link
Member

Ah, looks like I tested it with the thing commented out. My bad.

Reopening.

https://play.rust-lang.org/?gist=73158fdb88e83c6a6f592838e3b066b9&version=nightly

(In the future please give playpen link for bug reports)

@pietroalbini pietroalbini added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-lifetimes Area: Lifetimes / regions T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 30, 2018
@zackmdavis
Copy link
Member

This is fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants