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

Properly emit const fn UnstableInStable lint when reasonable #117571

Closed

Conversation

clarfonthey
Copy link
Contributor

@clarfonthey clarfonthey commented Nov 4, 2023

Right now, some cases of calling const-unstable functions in const-stable functions will emit a generic const-unstable lint instead of the unstable-in-stable lint. This is less than ideal, since these cases also don't explain what feature is causing the error.

This hopefully updates these cases to emit the proper lint, which provides better errors.

The code that interjects and adds unstable-in-stable lints feels a bit jank, and I'm sure that a refactor is due at some point, but I mostly just was frustrated with the compiler giving me less-than-helpful errors and wanted to fix that, so, this is the result.

Currently marked as draft because not all the tests are passing yet, and I need to do some more debugging. Still figured I'd share what I have anyway.

@rustbot
Copy link
Collaborator

rustbot commented Nov 4, 2023

r? @petrochenkov

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 4, 2023
@rust-log-analyzer

This comment has been minimized.

@petrochenkov petrochenkov 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-review Status: Awaiting review from the assignee but also interested parties. labels Nov 8, 2023
@petrochenkov petrochenkov marked this pull request as ready for review November 8, 2023 17:53
@petrochenkov
Copy link
Contributor

(Removed the draft status, our waiting-on labels are a better alternative.)

@clarfonthey
Copy link
Contributor Author

I mean, I personally went for draft to indicate that it hasn't been finished initially, whereas "waiting on author" doesn't necessarily mean that.

But, sure. You're the reviewer, so, do what you want to make it easier to filter out until it's done.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Dec 23, 2023

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

@Dylan-DPC
Copy link
Member

@clarfonthey any updates on this? Thanks

@clarfonthey
Copy link
Contributor Author

Apologies for taking a while to get to this. Currently looking into rebasing the changes I have so far and actually completing this.

@rust-log-analyzer
Copy link
Collaborator

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

Click to see the possible cause of the failure (guessed by this bot)
GITHUB_ACTION=__run_7
GITHUB_ACTIONS=true
GITHUB_ACTION_REF=
GITHUB_ACTION_REPOSITORY=
GITHUB_ACTOR=clarfonthey
GITHUB_API_URL=https://api.github.com
GITHUB_BASE_REF=master
GITHUB_ENV=/home/runner/work/_temp/_runner_file_commands/set_env_21b87969-d77c-4027-b30c-c93ab76b294b
GITHUB_EVENT_NAME=pull_request
---
GITHUB_SERVER_URL=https://github.com
GITHUB_SHA=73c3390e40c2e8a84637a953677dc909ad8c9dda
GITHUB_STATE=/home/runner/work/_temp/_runner_file_commands/save_state_21b87969-d77c-4027-b30c-c93ab76b294b
GITHUB_STEP_SUMMARY=/home/runner/work/_temp/_runner_file_commands/step_summary_21b87969-d77c-4027-b30c-c93ab76b294b
GITHUB_TRIGGERING_ACTOR=clarfonthey
GITHUB_WORKFLOW_REF=rust-lang/rust/.github/workflows/ci.yml@refs/pull/117571/merge
GITHUB_WORKFLOW_SHA=73c3390e40c2e8a84637a953677dc909ad8c9dda
GITHUB_WORKSPACE=/home/runner/work/rust/rust
GOROOT_1_19_X64=/opt/hostedtoolcache/go/1.19.13/x64
---
#12 writing image sha256:65a605e64391f9ec54f0ba7bcf22d33b2b7c2603d97d1fefeedc663b312e698a done
#12 naming to docker.io/library/rust-ci done
#12 DONE 10.0s
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-16]
##[group]Clock drift check
  local time: Thu Feb 22 04:46:07 UTC 2024
  network time: Thu, 22 Feb 2024 04:46:07 GMT
  network time: Thu, 22 Feb 2024 04:46:07 GMT
##[endgroup]
sccache: Starting the server...
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-16', '--enable-llvm-link-shared', '--set', 'rust.thin-lto-import-instr-limit=10', '--set', 'change-id=99999999', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--set', 'build.optimized-compiler-builtins', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--enable-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-16/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.thin-lto-import-instr-limit := 10
configure: change-id            := 99999999
---
........................................................................................  3344/16206
........................................................................................  3432/16206
........................................................................................  3520/16206
........................................................................................  3608/16206
...............FF...F...................................................................  3696/16206
........................................................................................  3872/16206
......................................i.................................................  3960/16206
i.......................................................................................  4048/16206
........................................................................................  4136/16206
---
---- [ui] tests/ui/consts/min_const_fn/min_const_fn_libstd_stability.rs stdout ----

error: /checkout/tests/ui/consts/min_const_fn/min_const_fn_libstd_stability.rs:24: unexpected error: '24:26: 24:32: `foo2` is not yet stable as a const fn'

error: /checkout/tests/ui/consts/min_const_fn/min_const_fn_libstd_stability.rs:39: unexpected error: '39:32: 39:44: `foo2_gated` is not yet stable as a const fn'

error: /checkout/tests/ui/consts/min_const_fn/min_const_fn_libstd_stability.rs:24: expected error not found: const-stable function cannot use `#[feature(foo2)]`

error: /checkout/tests/ui/consts/min_const_fn/min_const_fn_libstd_stability.rs:39: expected error not found: const-stable function cannot use `#[feature(foo2)]`
error: 2 unexpected errors found, 2 expected errors not found
status: exit status: 1
command: RUSTC_ICE="0" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/consts/min_const_fn/min_const_fn_libstd_stability.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/consts/min_const_fn/min_const_fn_libstd_stability" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/consts/min_const_fn/min_const_fn_libstd_stability/auxiliary"
unexpected errors (from JSON output): [
---
        line_num: 39,
        kind: Some(
            Error,
        ),
        msg: "39:32: 39:44: `foo2_gated` is not yet stable as a const fn",
]

not found errors (from test file): [
    Error {
    Error {
        line_num: 24,
        kind: Some(
            Error,
        ),
        msg: "const-stable function cannot use `#[feature(foo2)]`",
    Error {
        line_num: 39,
        kind: Some(
            Error,
            Error,
        ),
        msg: "const-stable function cannot use `#[feature(foo2)]`",
]

thread '[ui] tests/ui/consts/min_const_fn/min_const_fn_libstd_stability.rs' panicked at src/tools/compiletest/src/runtest.rs:1845:13:
explicit panic
explicit panic
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- [ui] tests/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability.rs stdout ----

error: /checkout/tests/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability.rs:24: unexpected error: '24:42: 24:48: `foo2` is not yet stable as a const fn'

error: /checkout/tests/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability.rs:39: unexpected error: '39:48: 39:60: `foo2_gated` is not yet stable as a const fn'

error: /checkout/tests/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability.rs:24: expected error not found: const-stable function cannot use `#[feature(foo2)]`

error: /checkout/tests/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability.rs:39: expected error not found: const-stable function cannot use `#[feature(foo2)]`
error: 2 unexpected errors found, 2 expected errors not found
status: exit status: 1
command: RUSTC_ICE="0" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability/auxiliary"
unexpected errors (from JSON output): [
---
        line_num: 39,
        kind: Some(
            Error,
        ),
        msg: "39:48: 39:60: `foo2_gated` is not yet stable as a const fn",
]

not found errors (from test file): [
    Error {
    Error {
        line_num: 24,
        kind: Some(
            Error,
        ),
        msg: "const-stable function cannot use `#[feature(foo2)]`",
    Error {
        line_num: 39,
        kind: Some(
            Error,
            Error,
        ),
        msg: "const-stable function cannot use `#[feature(foo2)]`",
]

thread '[ui] tests/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability.rs' panicked at src/tools/compiletest/src/runtest.rs:1845:13:
explicit panic
explicit panic

---- [ui] tests/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability2.rs stdout ----

error: /checkout/tests/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability2.rs:24: unexpected error: '24:33: 24:39: `foo2` is not yet stable as a const fn'

error: /checkout/tests/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability2.rs:33: unexpected error: '33:39: 33:51: `foo2_gated` is not yet stable as a const fn'

error: /checkout/tests/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability2.rs:24: expected error not found: const-stable function cannot use `#[feature(foo)]`

error: /checkout/tests/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability2.rs:33: expected error not found: const-stable function cannot use `#[feature(foo)]`
error: 2 unexpected errors found, 2 expected errors not found
status: exit status: 1
command: RUSTC_ICE="0" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability2.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability2" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability2/auxiliary"
unexpected errors (from JSON output): [
---
        line_num: 33,
        kind: Some(
            Error,
        ),
        msg: "33:39: 33:51: `foo2_gated` is not yet stable as a const fn",
]

not found errors (from test file): [
    Error {
    Error {
        line_num: 24,
        kind: Some(
            Error,
        ),
        msg: "const-stable function cannot use `#[feature(foo)]`",
    Error {
        line_num: 33,
        kind: Some(
            Error,
            Error,
        ),
        msg: "const-stable function cannot use `#[feature(foo)]`",
]

thread '[ui] tests/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability2.rs' panicked at src/tools/compiletest/src/runtest.rs:1845:13:
explicit panic
explicit panic

---- [ui] tests/ui/rfcs/rfc-2632-const-trait-impl/staged-api.rs#unstable stdout ----
diff of stderr:

15    = help: add `#![feature(foo)]` to the crate attributes to enable
17 error: const-stable function cannot use `#[feature(unstable)]`
-   --> $DIR/staged-api.rs:55:5
+   --> $DIR/staged-api.rs:53:5
19    |
19    |
20 LL |     Unstable::func();
21    |     ^^^^^^^^^^^^^^^^

32    |
33 
34 error: const-stable function cannot use `#[feature(foo)]`
+   --> $DIR/staged-api.rs:55:5
36    |
37 LL |     Foo::func();
38    |     ^^^^^^^^^^^
38    |     ^^^^^^^^^^^

49    |
50 
51 error: const-stable function cannot use `#[feature(foo)]`
+   --> $DIR/staged-api.rs:57:5
53    |
54 LL |     const_context_not_const_stable()
55    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
---
69 


The actual stderr differed from the expected stderr.
Actual stderr saved to /checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/rfcs/rfc-2632-const-trait-impl/staged-api.unstable/staged-api.unstable.stderr
To only update this specific test, also pass `--test-args rfcs/rfc-2632-const-trait-impl/staged-api.rs`

error in revision `unstable`: 1 errors occurred comparing output.
status: exit status: 1
status: exit status: 1
command: RUSTC_ICE="0" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/rfcs/rfc-2632-const-trait-impl/staged-api.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--cfg" "unstable" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/rfcs/rfc-2632-const-trait-impl/staged-api.unstable" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/rfcs/rfc-2632-const-trait-impl/staged-api.unstable/auxiliary"
--- stderr -------------------------------
--- stderr -------------------------------
error: `<Foo as staged_api::MyTrait>::func` is not yet stable as a const fn
   |
LL |     Foo::func();
   |     ^^^^^^^^^^^
   |
   |
   = help: add `#![feature(foo)]` to the crate attributes to enable

error: `<Foo as staged_api::MyTrait>::func` is not yet stable as a const fn
   |
LL |     Foo::func();
   |     ^^^^^^^^^^^
   |
   |
   = help: add `#![feature(foo)]` to the crate attributes to enable
error: const-stable function cannot use `#[feature(unstable)]`
##[error]  --> /checkout/tests/ui/rfcs/rfc-2632-const-trait-impl/staged-api.rs:53:5
   |
LL |     Unstable::func();
LL |     Unstable::func();
   |     ^^^^^^^^^^^^^^^^
   |
help: if it is not part of the public API, make this function unstably const
   |
LL + #[rustc_const_unstable(feature = "...", issue = "...")]
   |
   |
help: otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks
   |
LL + #[rustc_allow_const_fn_unstable(unstable)]
   |


error: const-stable function cannot use `#[feature(foo)]`
   |
LL |     Foo::func();
   |     ^^^^^^^^^^^
   |
   |
help: if it is not part of the public API, make this function unstably const
   |
LL + #[rustc_const_unstable(feature = "...", issue = "...")]
   |
   |
help: otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks
   |
LL + #[rustc_allow_const_fn_unstable(foo)]
   |


error: const-stable function cannot use `#[feature(foo)]`
   |
LL |     const_context_not_const_stable()
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   |
help: if it is not part of the public API, make this function unstably const
   |
LL + #[rustc_const_unstable(feature = "...", issue = "...")]
   |
Build completed unsuccessfully in 0:12:56
Build completed unsuccessfully in 0:12:56
help: otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks
   |
LL + #[rustc_allow_const_fn_unstable(foo)]
   |

error: aborting due to 5 previous errors
------------------------------------------
---

37    |
38    = help: add `#![feature(unstable)]` to the crate attributes to enable
39 
- error: `<staged_api::Unstable as staged_api::MyTrait>::func` is not yet stable as a const fn
+ error: const-stable function cannot use `#[feature(unstable)]`
42    |
43 LL |     Unstable::func();

44    |     ^^^^^^^^^^^^^^^^
44    |     ^^^^^^^^^^^^^^^^
45    |
-    = help: const-stable functions can only call other const-stable functions
+ help: if it is not part of the public API, make this function unstably const
+    |
+ LL + #[rustc_const_unstable(feature = "...", issue = "...")]
+ LL | const fn stable_const_context() {
+    |
+ help: otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks
+    |
+ LL + #[rustc_allow_const_fn_unstable(unstable)]
+ LL | const fn stable_const_context() {
47 
48 error: aborting due to 5 previous errors
49 

---
--- stderr -------------------------------
error: trait implementations cannot be const stable yet
##[error]  --> /checkout/tests/ui/rfcs/rfc-2632-const-trait-impl/staged-api.rs:19:1
   |
LL | / impl const MyTrait for Foo {
LL | |     //[stable]~^ ERROR trait implementations cannot be const stable yet
LL | |     fn func() {}
   | |_^
   |
   = note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information


error: function has missing const stability attribute
##[error]  --> /checkout/tests/ui/rfcs/rfc-2632-const-trait-impl/staged-api.rs:41:1
   |
LL | / pub const fn const_context_not_const_stable() {
LL | |     //[stable]~^ ERROR function has missing const stability attribute
LL | |     Unstable::func();
LL | |     //[stable]~^ ERROR not yet stable as a const fn
...  |
LL | |     // ^ fails, because the `foo` feature is not active
   | |_^


error: `<staged_api::Unstable as staged_api::MyTrait>::func` is not yet stable as a const fn
   |
LL |     Unstable::func();
   |     ^^^^^^^^^^^^^^^^
   |
   |
   = help: add `#![feature(unstable)]` to the crate attributes to enable

error: `<staged_api::Unstable as staged_api::MyTrait>::func` is not yet stable as a const fn
   |
LL |     Unstable::func();
   |     ^^^^^^^^^^^^^^^^
   |
---
   |
LL |     Unstable::func();
   |     ^^^^^^^^^^^^^^^^
   |
help: if it is not part of the public API, make this function unstably const
   |
LL + #[rustc_const_unstable(feature = "...", issue = "...")]
   |
   |
help: otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks
   |
LL + #[rustc_allow_const_fn_unstable(unstable)]
   |

error: aborting due to 5 previous errors
------------------------------------------

@clarfonthey
Copy link
Contributor Author

Going to be honest, I've kind of lost the desire to work on this, and the lint seems to now actually do the right thing even though it still breaks in a lot of circumstances. I'm going to close this for now but anyone else is free to pick back up this work and improve the lint themselves.

@clarfonthey clarfonthey deleted the allow-const-fn-unstable branch February 22, 2024 23:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

6 participants