Repository navigation
Taking a shared reference of an array suppresses the unconditional_panic lint #98444
Copy link
Copy link
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.A-mir-optArea: MIR optimizationsArea: MIR optimizationsC-bugCategory: This is a bug.Category: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.L-unconditional_panicLint: unconditional_panicLint: unconditional_panicP-mediumMedium priorityMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant 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.Performance or correctness regression from one stable version to another.
Description
I tried this code:
fn main() {
let xs: [i32; 5] = [1, 2, 3, 4, 5];
let _ = &xs; // this line suppresses the `unconditional_panic` lint
let _ = xs[7];
}I expected to see this happen:
error: this operation will panic at runtime
--> src/main.rs:4:13
|
4 | let _ = xs[7];
| ^^^^^ index out of bounds: the length is 5 but the index is 7
|
= note: `#[deny(unconditional_panic)]` on by default
Instead, this happened:
thread 'main' panicked at 'index out of bounds: the len is 5 but the index is 7', src/main.rs:4:13
Apparently, taking a shared reference of xs will suppress the unconditional_panic lint. This seems like a false negative. While false negatives can't be avoided entirely, this case seems to be avoidable as taking a shared reference shouldn't change the type of xs (which is [i32; 5]) or modify any data in this example.
Note that
fn main() {
let xs: [i32; 5] = [1, 2, 3, 4, 5];
//let _ = &xs;
let _ = xs[7];
}will trigger the lint properly.
Meta
rustc --version --verbose:
rustc 1.63.0-nightly (43347397f 2022-06-23)
binary: rustc
commit-hash: 43347397f7c5ca9a670a3bb3890c7187e24a52ab
commit-date: 2022-06-23
host: x86_64-unknown-freebsd
release: 1.63.0-nightly
LLVM version: 14.0.5
Reacted by Yerkebulan Tulibergenov, Jaanus Varus, Ludwig Stecher, Ciren, Piotr Maks and Benjamin Singleton
Reacted by Daniel White and Evgeniy Dushistov
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.A-mir-optArea: MIR optimizationsArea: MIR optimizationsC-bugCategory: This is a bug.Category: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.L-unconditional_panicLint: unconditional_panicLint: unconditional_panicP-mediumMedium priorityMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant 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.Performance or correctness regression from one stable version to another.
Activity
JanBeh commentedon Jun 24, 2022
See also this thread on URLO.
TheWastl commentedon Jun 24, 2022
@rustbot claim
poliorcetics commentedon Jun 27, 2022
Note this is a problem because the book has an example which is supposed to fail to compile but this bug makes it fail at runtime instead
kmdreko commentedon Jul 15, 2022
In case no one has dug into it yet, the error is suppressed in
1.61but not in1.60and before:eggyal commentedon Jul 20, 2022
@rustbot label +regression-from-stable-to-stable
apiraino commentedon Jul 21, 2022
WG-prioritization assigning priority (Zulip discussion).
@rustbot label -I-prioritize +P-medium
TheWastl commentedon Jul 21, 2022
This is caused by #94934. It only ever worked because the lint ran after optimizations.
I made a fix here. It specializes the length of arrays as constant, even if the array isn't. I'm going to open a PR when I have time, probably tomorrow.
In the long term, I'd like to implement some more extensive tracking of "what parts of a value are constant". For example, the compiler currently assumes that anything that had its reference taken can change, but actually, this can only happen inside
UnsafeCell(as I understand it).8 remaining items
albertlarsan68 commentedon Mar 6, 2023
I claim the add test part
albertlarsan68 commentedon Mar 6, 2023
This lint only appears on full builds (not check), but this will be fixed once #108730 lands.
Rollup merge of rust-lang#108812 - albertlarsan68:test-98444, r=Nilst…
Auto merge of rust-lang#108821 - matthiaskrgr:rollup-cmkbgpr, r=matth…
apiraino commentedon Aug 15, 2023
Reopening because I feel this is not yet solved, see Zulip notes on #114840.
As per the prev. comment, #108730 should nail down this issue?
veera-sivarajan commentedon Aug 24, 2024
MRE:
Also, duplicate of #38035
veera-sivarajan commentedon Sep 28, 2024
Another variant of this same issue: #109260
Rollup merge of rust-lang#129517 - cjgillot:known-panic-array, r=pnkf…
Unrolled build for rust-lang#129517
Rollup merge of #129517 - cjgillot:known-panic-array, r=pnkfelix