Skip to content

[NLL] Lifetimes errors in a function can mask later errors in same function #96331

Open

Description

@marmeladema

For test src/test/ui/regions/region-multiple-lifetime-bounds-on-fns-where-clause.rs, under NLL, a lifetime may not live long enough error is missing.
This is a behavior change which should be investigated.

See comment posted by @jackh726 in #96212 (comment)

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Apr 23, 2022
  2. marmeladema commented on Apr 23, 2022

    @marmeladema
    ContributorAuthor

    @rustbot label +A-NLL +NLL-diagnostics

  3. added
    A-NLLArea: Non-lexical lifetimes (NLL)
    NLL-diagnosticsWorking towards the "diagnostic parity" goal
    on Apr 23, 2022
  4. m-ou-se commented on May 17, 2022

    @m-ou-se
    Member

    Reduced test case:

    fn b<'a, 'b, 'c>(x: &mut &'a isize, y: &mut &'b isize, z: &mut &'c isize) {
        *x = *y;
        *z = *y;
    }

    Rustc output:

    error[E0623]: lifetime mismatch
     --> src/main.rs:2:10
      |
    1 | fn b<'a, 'b, 'c>(x: &mut &'a isize, y: &mut &'b isize, z: &mut &'c isize) {
      |                          ---------          --------- these two types are declared with different lifetimes...
    2 |     *x = *y;
      |          ^^ ...but data from `y` flows into `x` here
    
    error[E0623]: lifetime mismatch
     --> src/main.rs:3:10
      |
    1 | fn b<'a, 'b, 'c>(x: &mut &'a isize, y: &mut &'b isize, z: &mut &'c isize) {
      |                                             ---------          ---------
      |                                             |
      |                                             these two types are declared with different lifetimes...
    2 |     *x = *y;
    3 |     *z = *y;
      |          ^^ ...but data from `y` flows into `z` here
    
    error: aborting due to 2 previous errors
    

    Rustc output with -Z borrowck=mir:

    error: lifetime may not live long enough
     --> src/main.rs:2:5
      |
    1 | fn b<'a, 'b, 'c>(x: &mut &'a isize, y: &mut &'b isize, z: &mut &'c isize) {
      |      --  -- lifetime `'b` defined here
      |      |
      |      lifetime `'a` defined here
    2 |     *x = *y;
      |     ^^^^^^^ assignment requires that `'b` must outlive `'a`
      |
      = help: consider adding the following bound: `'b: 'a`
    
    error: aborting due to previous error
    

    It doesn't give an error about the *z = *y; line.

    However, if you remove the *x = *y; line, it doesn't succeed. It then produces the right error:

    error: lifetime may not live long enough
     --> src/main.rs:2:5
      |
    1 | fn b<'a, 'b, 'c>(x: &mut &'a isize, y: &mut &'b isize, z: &mut &'c isize) {
      |          --  -- lifetime `'c` defined here
      |          |
      |          lifetime `'b` defined here
    2 |     *z = *y;
      |     ^^^^^^^ assignment requires that `'b` must outlive `'c`
      |
      = help: consider adding the following bound: `'b: 'c`
    

    So, this doesn't seem like a case where invalid code is accepted. Just a case where diagnostics are incomplete.

  5. m-ou-se commented on May 17, 2022

    @m-ou-se
    Member

    When using two unrelated variables/lifetimes, it does produce both errors:

    fn b<'a, 'b, 'c, 'd>(x: &mut &'a isize, y: &mut &'b isize, z: &mut &'c isize, z2: &mut &'d isize) {
        *x = *y;
        *z = *z2;
    }
    
  6. m-ou-se commented on May 17, 2022

    @m-ou-se
    Member

    When doing *x = *y; *x = *y; (the same thing twice), you only get the error once. It seems that in the new mode, that 'deduplicating' effect also applies to *x = *y; *z = *y;.

    (*x = *y; *y = *z; (last assignment flipped) does produce two errors with and without nll.)

  7. jackh726 commented on May 17, 2022

    @jackh726
    Member

    Thanks @m-ou-se for looking at this!

    I think based on this, it should be okay to move forward without "fixing" this. Seems like only a diagnostics thing instead of accidently allowing more code.

  8. changed the title [-][NLL] Investigate behavior difference with non-nll for `region-multiple-lifetime-bounds-on-fns-where-clause.rs` test[/-] [+][NLL] Lifetimes errors in a function can mask later errors in same function[/+] on May 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)A-diagnosticsArea: Messages for errors, warnings, and lintsNLL-diagnosticsWorking towards the "diagnostic parity" goalT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions