Skip to content

Function item should not be used as const arg#157962

Open
Shourya742 wants to merge 2 commits into
rust-lang:mainfrom
Shourya742:2026-06-16-function-x-cannot-be-used-as-const
Open

Function item should not be used as const arg#157962
Shourya742 wants to merge 2 commits into
rust-lang:mainfrom
Shourya742:2026-06-16-function-x-cannot-be-used-as-const

Conversation

@Shourya742

@Shourya742 Shourya742 commented Jun 16, 2026

Copy link
Copy Markdown
Member

closes: #138088

r? @BoxyUwU

…ef const after scheduling. That works only when a later phase emits a real diagnostics. For function item (associated fn), the lowering needs parent trait and self args.
@rustbot

rustbot commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

HIR ty lowering was modified

cc @fmease

@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 Jun 16, 2026
@rustbot

rustbot commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

BoxyUwU is currently at their maximum review capacity.
They may take a while to respond.

Comment on lines +2794 to +2800
Res::Def(DefKind::AssocFn, _) => {
let guar =
self.dcx().span_err(span, "function items cannot be used as const arguments");
Const::new_error(tcx, guar)
}
// FIXME(const_generics): create real const to allow fn items as const paths
Res::Def(DefKind::Fn | DefKind::AssocFn, did) => {
Res::Def(DefKind::Fn, did) => {

@Shourya742 Shourya742 Jun 16, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Previous both free function and associated function, where lowering to zero_sized fn def which would later emit diagnostics, but to lower the assoc it required the parent trait. This now emits an error, instead of lowering.

View changes since the review

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job aarch64-gnu-llvm-21-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
test [crashes] tests/crashes/136666.rs ... ok
test [crashes] tests/crashes/136766.rs ... ok
test [crashes] tests/crashes/137187.rs ... ok
test [crashes] tests/crashes/137049.rs ... ok
2026-06-16T13:15:29.230202Z ERROR compiletest::runtest: fatal error, panic: "crashtest no longer crashes/triggers ICE, hooray! Please give it a meaningful name, add a doc-comment to the start of the test explaining why it exists and move it to tests/ui or wherever you see fit. Adding 'Fixes #<issueNr>' to your PR description ensures that the corresponding ticket is auto-closed upon merge. If you want to see verbose output, set `COMPILETEST_VERBOSE_CRASHES=1`."
test [crashes] tests/crashes/138088.rs ... FAILED
test [crashes] tests/crashes/138008.rs ... ok
test [crashes] tests/crashes/138009.rs ... ok
test [crashes] tests/crashes/138156.rs ... ok
test [crashes] tests/crashes/138534.rs ... ok
---

---- [crashes] tests/crashes/138088.rs stdout ----
------rustc stdout------------------------------

------rustc stderr------------------------------
error[E0601]: `main` function not found in crate `138088`
##[error] --> /checkout/tests/crashes/138088.rs:5:2
  |
5 | }
  |  ^ consider adding a `main` function to `/checkout/tests/crashes/138088.rs`

error: function items cannot be used as const arguments
##[error] --> /checkout/tests/crashes/138088.rs:4:26
  |
4 |     fn x(&self) -> [i32; Bar::x];
  |                          ^^^^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0601`.

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

error: crashtest no longer crashes/triggers ICE, hooray! Please give it a meaningful name, add a doc-comment to the start of the test explaining why it exists and move it to tests/ui or wherever you see fit. Adding 'Fixes #<issueNr>' to your PR description ensures that the corresponding ticket is auto-closed upon merge. If you want to see verbose output, set `COMPILETEST_VERBOSE_CRASHES=1`.

thread '[crashes] tests/crashes/138088.rs' panicked at src/tools/compiletest/src/runtest/crashes.rs:16:18:
fatal error
stack backtrace:
   8: __rustc::rust_begin_unwind

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ICE: assertion failed: !parent_args.is_empty()

4 participants