Skip to content

slice::get_mut() followed by slice::copy_from_slice generates unreachable panic branch #98294

Description

@bugadani

In code like the following, the compiler misses a possible optimization: in f1, the length of dst is equal to the length of bytes, yet the compiler generates a call to len_mismatch_fail.

pub fn f1(a: &mut [u8], offset: usize, bytes: &[u8]) {
    if let Some(dst) = a.get_mut(offset..offset + bytes.len()) {
        dst.copy_from_slice(bytes);
    }
}

The compiler knows the lengths are equal (and thus, the panic is unreachable), because the following snippet optimizes the panic away:

pub fn f2(a: &mut [u8], offset: usize, bytes: &[u8]) {
    if let Some(dst) = a.get_mut(offset..offset + bytes.len()) {
        assert!(dst.len() == bytes.len());
        dst.copy_from_slice(bytes);
    }
}

This is a regression between rustc versions 1.51 and 1.52.

https://rust.godbolt.org/z/YhEY78E9P

Activity

  1. changed the title [-]`slice::get[_mut]()` followed by `slice::copy_from_slice` generates unreachable panic branch[/-] [+]`slice::get_mut()` followed by `slice::copy_from_slice` generates unreachable panic branch[/+] on Jun 20, 2022
  2. added
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Jul 27, 2022
  3. steffahn commented on Jul 27, 2022

    @steffahn
    Member

    @rustbot label I-slow, E-needs-bisection

  4. added
    E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc
    I-slowIssue: Problems and improvements with respect to performance of generated code.
    on Jul 27, 2022
  5. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Aug 3, 2022
  6. steffahn commented on Aug 30, 2022

    @steffahn
    Member

    This seems to be fixed on nightly. @rustbot label E-needs-test

    Bisection would still be interesting IMO, both for the regression and the fix.

  7. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Aug 30, 2022
  8. steffahn commented on Aug 30, 2022

    @steffahn
    Member

    Alright, that’s pretty boring in terms of rustc-bisection: it was fixed by #99464 (Update to LLVM 15), and it regressed in nightly-2021-03-05 which includes #81451 (Upgrade1 to LLVM 12).

    @rustbot label -E-needs-bisection, +A-llvm.

    Footnotes

    1. what’s up with the inconsistent naming, upgrade vs. update? ↩

  9. added
    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.
    and removed
    E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc
    on Aug 30, 2022
  10. removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Aug 31, 2022
  11. added a commit that references this issue on Aug 31, 2022
  12. added a commit that references this issue on Sep 3, 2022
  13. added a commit that references this issue on Sep 5, 2022
  14. added a commit that references this issue on Sep 7, 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-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.I-slowIssue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions