Skip to content
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

const_prop_lint: Consider array length constant even if array is not #100160

Closed

Conversation

TheWastl
Copy link
Contributor

@TheWastl TheWastl commented Aug 5, 2022

Fixes #98444.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Aug 5, 2022
@rust-highfive
Copy link
Collaborator

r? @compiler-errors

(rust-highfive has picked a reviewer for you, use r? to override)

@rustbot
Copy link
Collaborator

rustbot commented Aug 5, 2022

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 5, 2022
@compiler-errors
Copy link
Member

r? oli-obk

cc @JakobDegen

@RalfJung
Copy link
Member

RalfJung commented Aug 5, 2022

r? @oli-obk

@@ -501,12 +501,23 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
return None;
}

Rvalue::Len(len_place) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should make the same change in the const_prop pass as well:

but that doesn't have to be in this PR 🙂

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

afaicr this is not necessary, because there are optimizations taking care of this before const prop runs

@oli-obk
Copy link
Contributor

oli-obk commented Aug 25, 2022

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Aug 25, 2022

📌 Commit e4a4246 has been approved by oli-obk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 25, 2022
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Aug 25, 2022
…nt, r=oli-obk

const_prop_lint: Consider array length constant even if array is not

Fixes rust-lang#98444.
@JohnTitor
Copy link
Member

Failed in rollup: #101016 (comment)
@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Aug 26, 2022
@rustbot
Copy link
Collaborator

rustbot commented Sep 5, 2022

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-tools failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
  IMAGE: x86_64-gnu-tools
##[endgroup]
From https://github.com/rust-lang/rust
 * branch              master     -> FETCH_HEAD
Searching for toolstate changes between 2dc703fd6e3aaaf343828cc7dd1aec192d24c628 and c83902e575b2da5c0ce00966b6215feadda35d4b
Clippy or rustfmt subtrees were updated
##[group]Run src/ci/scripts/verify-channel.sh
src/ci/scripts/verify-channel.sh
shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
env:
---
diff of stderr:

-error: this operation will panic at runtime
-  --> $DIR/indexing_slicing_index.rs:23:5
-   |
-LL |     x[4]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
-   |     ^^^^ index out of bounds: the length is 4 but the index is 4
-   |
-   = note: `#[deny(unconditional_panic)]` on by default
-error: this operation will panic at runtime
-  --> $DIR/indexing_slicing_index.rs:24:5
-   |
-   |
-LL |     x[1 << 3]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
-   |     ^^^^^^^^^ index out of bounds: the length is 4 but the index is 8
-error: this operation will panic at runtime
-  --> $DIR/indexing_slicing_index.rs:29:5
-   |
-   |
-LL |     x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
-   |     ^^^^^^^^^^^^^^^^^^^ index out of bounds: the length is 4 but the index is 4
-
 error[E0080]: evaluation of `main::{constant#3}` failed
    |
    |
 LL |     const { &ARR[idx4()] }; // Ok, let rustc handle const contexts.
    |              ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
 error[E0080]: erroneous constant used
   --> $DIR/indexing_slicing_index.rs:31:5
    |
    |
 LL |     const { &ARR[idx4()] }; // Ok, let rustc handle const contexts.
 
-error: this operation will panic at runtime
-  --> $DIR/indexing_slicing_index.rs:35:5
-   |
-   |
-LL |     y[4]; // Ok, rustc will handle references too.
-   |     ^^^^ index out of bounds: the length is 4 but the index is 4
-error: this operation will panic at runtime
-  --> $DIR/indexing_slicing_index.rs:44:5
-   |
-   |
-LL |     x[N]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
-   |     ^^^^ index out of bounds: the length is 4 but the index is 15
 error: indexing may panic
   --> $DIR/indexing_slicing_index.rs:22:5
    |
 LL |     x[index];
 LL |     x[index];
    |     ^^^^^^^^
    |
    = note: `-D clippy::indexing-slicing` implied by `-D warnings`
    = help: consider using `.get(n)` or `.get_mut(n)` instead
 error: indexing may panic
   --> $DIR/indexing_slicing_index.rs:38:5
    |
 LL |     v[0];
 LL |     v[0];
    |     ^^^^
    |
    = help: consider using `.get(n)` or `.get_mut(n)` instead
 error: indexing may panic
   --> $DIR/indexing_slicing_index.rs:39:5
    |
 LL |     v[10];
 LL |     v[10];
    |     ^^^^^
    |
    = help: consider using `.get(n)` or `.get_mut(n)` instead
 error: indexing may panic
   --> $DIR/indexing_slicing_index.rs:40:5
    |
    |
 LL |     v[1 << 3];
    |
    |
    = help: consider using `.get(n)` or `.get_mut(n)` instead
 error: indexing may panic
   --> $DIR/indexing_slicing_index.rs:46:5
    |
 LL |     v[N];
 LL |     v[N];
    |     ^^^^
    |
    = help: consider using `.get(n)` or `.get_mut(n)` instead
 error: indexing may panic
   --> $DIR/indexing_slicing_index.rs:47:5
    |
 LL |     v[M];
 LL |     v[M];
    |     ^^^^
    |
    = help: consider using `.get(n)` or `.get_mut(n)` instead
-error: aborting due to 13 previous errors
+error: aborting due to 8 previous errors
 
 For more information about this error, try `rustc --explain E0080`.
---

error: 1 errors occurred comparing output.
status: exit status: 1
error: test failed, to rerun pass '--test compile-test'
command: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/clippy-driver" "tests/ui/indexing_slicing_index.rs" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/test/ui" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "-C" "prefer-dynamic" "-o" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/test/ui/indexing_slicing_index.stage-id" "-A" "unused" "--emit=metadata" "-Dwarnings" "-Zui-testing" "-L" "dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/deps" "-L" "dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/release/deps" "--extern" "tokio=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/deps/libtokio-e0524b7e2611e851.rlib" "--extern" "rustc_semver=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/deps/librustc_semver-963bbd3f89834643.rlib" "--extern" "itertools=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/deps/libitertools-cdd893c121eb00e4.rlib" "--extern" "parking_lot=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/deps/libparking_lot-4554cde6a1339e03.rlib" "--extern" "serde=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/deps/libserde-4b46e2e2788394f2.rlib" "--extern" "clippy_lints=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/deps/libclippy_lints-a74d54f3750f2f75.rlib" "--extern" "serde_derive=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/release/deps/libserde_derive-eed8221ad604f845.so" "--extern" "clippy_utils=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/deps/libclippy_utils-47229815ed3188f9.rlib" "--extern" "if_chain=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/deps/libif_chain-03f75cdc6d4d3afc.rlib" "--extern" "quote=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/deps/libquote-021aec868151835c.rlib" "--extern" "futures=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/deps/libfutures-e92c80438a94fde5.rlib" "--extern" "derive_new=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/release/deps/libderive_new-14dbc812a1f5dba0.so" "--extern" "syn=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/deps/libsyn-c6aa3eacac0eeebb.rlib" "--extern" "regex=/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/deps/libregex-619ac20e364f2b2c.rlib" "--edition=2021" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/test/ui/indexing_slicing_index.stage-id.aux"
------------------------------------------

------------------------------------------
stderr:
stderr:
------------------------------------------
{"message":"evaluation of `main::{constant#3}` failed","code":{"code":"E0080","explanation":"A constant value failed to get evaluated.\n\nErroneous code example:\n\n```compile_fail,E0080\nenum Enum {\n    X = (1 << 500),\n    Y = (1 / 0),\n}\n```\n\nThis error indicates that the compiler was unable to sensibly evaluate a\nconstant expression that had to be evaluated. Attempting to divide by 0\nor causing an integer overflow are two ways to induce this error.\n\nEnsure that the expressions given can be evaluated as the desired integer type.\n\nSee the [Custom Discriminants][custom-discriminants] section of the Reference\nfor more information about setting custom integer types on fieldless enums\nusing the [`repr` attribute][repr-attribute].\n\n[custom-discriminants]: https://doc.rust-lang.org/reference/items/enumerations.html#custom-discriminant-values-for-field-less-enumerations\n[repr-attribute]: https://doc.rust-lang.org/reference/type-layout.html#reprc-enums\n"},"level":"error","spans":[{"file_name":"tests/ui/indexing_slicing_index.rs","byte_start":1157,"byte_end":1168,"line_start":31,"line_end":31,"column_start":14,"column_end":25,"is_primary":true,"text":[{"text":"    const { &ARR[idx4()] }; // Ok, let rustc handle const contexts.","highlight_start":14,"highlight_end":25}],"label":"index out of bounds: the length is 2 but the index is 4","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[],"rendered":"error[E0080]: evaluation of `main::{constant#3}` failed\n  --> tests/ui/indexing_slicing_index.rs:31:14\n   |\nLL |     const { &ARR[idx4()] }; // Ok, let rustc handle const contexts.\n   |              ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4\n\n"}
{"message":"erroneous constant used","code":{"code":"E0080","explanation":"A constant value failed to get evaluated.\n\nErroneous code example:\n\n```compile_fail,E0080\nenum Enum {\n    X = (1 << 500),\n    Y = (1 / 0),\n}\n```\n\nThis error indicates that the compiler was unable to sensibly evaluate a\nconstant expression that had to be evaluated. Attempting to divide by 0\nor causing an integer overflow are two ways to induce this error.\n\nEnsure that the expressions given can be evaluated as the desired integer type.\n\nSee the [Custom Discriminants][custom-discriminants] section of the Reference\nfor more information about setting custom integer types on fieldless enums\nusing the [`repr` attribute][repr-attribute].\n\n[custom-discriminants]: https://doc.rust-lang.org/reference/items/enumerations.html#custom-discriminant-values-for-field-less-enumerations\n[repr-attribute]: https://doc.rust-lang.org/reference/type-layout.html#reprc-enums\n"},"level":"error","spans":[{"file_name":"tests/ui/indexing_slicing_index.rs","byte_start":1148,"byte_end":1170,"line_start":31,"line_end":31,"column_start":5,"column_end":27,"is_primary":true,"text":[{"text":"    const { &ARR[idx4()] }; // Ok, let rustc handle const contexts.","highlight_start":5,"highlight_end":27}],"label":"referenced constant has errors","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[],"rendered":"error[E0080]: erroneous constant used\n  --> tests/ui/indexing_slicing_index.rs:31:5\n   |\nLL |     const { &ARR[idx4()] }; // Ok, let rustc handle const contexts.\n   |     ^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors\n\n"}
{"message":"indexing may panic","code":{"code":"clippy::indexing_slicing","explanation":null},"level":"error","spans":[{"file_name":"tests/ui/indexing_slicing_index.rs","byte_start":633,"byte_end":641,"line_start":22,"line_end":22,"column_start":5,"column_end":13,"is_primary":true,"text":[{"text":"    x[index];","highlight_start":5,"highlight_end":13}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"`-D clippy::indexing-slicing` implied by `-D warnings`","code":null,"level":"note","spans":[],"children":[],"rendered":null},{"message":"consider using `.get(n)` or `.get_mut(n)` instead","code":null,"level":"help","spans":[],"children":[],"rendered":null}],"rendered":"error: indexing may panic\n  --> tests/ui/indexing_slicing_index.rs:22:5\n   |\nLL |     x[index];\n   |     ^^^^^^^^\n   |\n   = note: `-D clippy::indexing-slicing` implied by `-D warnings`\n   = help: consider using `.get(n)` or `.get_mut(n)` instead\n\n"}
{"message":"indexing may panic","code":{"code":"clippy::indexing_slicing","explanation":null},"level":"error","spans":[{"file_name":"tests/ui/indexing_slicing_index.rs","byte_start":1385,"byte_end":1389,"line_start":38,"line_end":38,"column_start":5,"column_end":9,"is_primary":true,"text":[{"text":"    v[0];","highlight_start":5,"highlight_end":9}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"consider using `.get(n)` or `.get_mut(n)` instead","code":null,"level":"help","spans":[],"children":[],"rendered":null}],"rendered":"error: indexing may panic\n  --> tests/ui/indexing_slicing_index.rs:38:5\n   |\nLL |     v[0];\n   |     ^^^^\n   |\n   = help: consider using `.get(n)` or `.get_mut(n)` instead\n\n"}
{"message":"indexing may panic","code":{"code":"clippy::indexing_slicing","explanation":null},"level":"error","spans":[{"file_name":"tests/ui/indexing_slicing_index.rs","byte_start":1395,"byte_end":1400,"line_start":39,"line_end":39,"column_start":5,"column_end":10,"is_primary":true,"text":[{"text":"    v[10];","highlight_start":5,"highlight_end":10}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"consider using `.get(n)` or `.get_mut(n)` instead","code":null,"level":"help","spans":[],"children":[],"rendered":null}],"rendered":"error: indexing may panic\n  --> tests/ui/indexing_slicing_index.rs:39:5\n   |\nLL |     v[10];\n   |     ^^^^^\n   |\n   = help: consider using `.get(n)` or `.get_mut(n)` instead\n\n"}
{"message":"indexing may panic","code":{"code":"clippy::indexing_slicing","explanation":null},"level":"error","spans":[{"file_name":"tests/ui/indexing_slicing_index.rs","byte_start":1406,"byte_end":1415,"line_start":40,"line_end":40,"column_start":5,"column_end":14,"is_primary":true,"text":[{"text":"    v[1 << 3];","highlight_start":5,"highlight_end":14}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"consider using `.get(n)` or `.get_mut(n)` instead","code":null,"level":"help","spans":[],"children":[],"rendered":null}],"rendered":"error: indexing may panic\n  --> tests/ui/indexing_slicing_index.rs:40:5\n   |\nLL |     v[1 << 3];\n   |     ^^^^^^^^^\n   |\n   = help: consider using `.get(n)` or `.get_mut(n)` instead\n\n"}
{"message":"indexing may panic","code":{"code":"clippy::indexing_slicing","explanation":null},"level":"error","spans":[{"file_name":"tests/ui/indexing_slicing_index.rs","byte_start":1636,"byte_end":1640,"line_start":46,"line_end":46,"column_start":5,"column_end":9,"is_primary":true,"text":[{"text":"    v[N];","highlight_start":5,"highlight_end":9}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"consider using `.get(n)` or `.get_mut(n)` instead","code":null,"level":"help","spans":[],"children":[],"rendered":null}],"rendered":"error: indexing may panic\n  --> tests/ui/indexing_slicing_index.rs:46:5\n   |\nLL |     v[N];\n   |     ^^^^\n   |\n   = help: consider using `.get(n)` or `.get_mut(n)` instead\n\n"}
{"message":"indexing may panic","code":{"code":"clippy::indexing_slicing","explanation":null},"level":"error","spans":[{"file_name":"tests/ui/indexing_slicing_index.rs","byte_start":1646,"byte_end":1650,"line_start":47,"line_end":47,"column_start":5,"column_end":9,"is_primary":true,"text":[{"text":"    v[M];","highlight_start":5,"highlight_end":9}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"consider using `.get(n)` or `.get_mut(n)` instead","code":null,"level":"help","spans":[],"children":[],"rendered":null}],"rendered":"error: indexing may panic\n  --> tests/ui/indexing_slicing_index.rs:47:5\n   |\nLL |     v[M];\n   |     ^^^^\n   |\n   = help: consider using `.get(n)` or `.get_mut(n)` instead\n\n"}
{"message":"For more information about this error, try `rustc --explain E0080`.","code":null,"level":"failure-note","spans":[],"children":[],"rendered":"For more information about this error, try `rustc --explain E0080`.\n"}

------------------------------------------

@JohnCSimon
Copy link
Member

Ping from triage:
@TheWastl - can you post your status on this MR?

@bors
Copy link
Contributor

bors commented Oct 8, 2022

☔ The latest upstream changes (presumably #102091) made this pull request unmergeable. Please resolve the merge conflicts.

@JohnCSimon
Copy link
Member

@TheWastl
Ping from triage: I'm closing this due to inactivity, Please reopen when you are ready to continue with this.
Note: if you do please open the PR BEFORE you push to it, else you won't be able to reopen - this is a quirk of github.
Thanks for your contribution.

@rustbot label: +S-inactive

@JohnCSimon JohnCSimon closed this Nov 27, 2022
@rustbot rustbot added the S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. label Nov 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Taking a shared reference of an array suppresses the unconditional_panic lint