Skip to content

comptime read files experiments - #165159

Open
lambinoo wants to merge 2 commits into
rust-lang:mainfrom
lambinoo:feat-comptime-read-files
Open

lambinoo wants to merge 2 commits into
rust-lang:mainfrom
lambinoo:feat-comptime-read-files

Conversation

@lambinoo

@lambinoo lambinoo commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

@rustbot rustbot added 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. labels Oct 29, 2026
@lambinoo
lambinoo force-pushed the feat-comptime-read-files branch from daef30d to 7da399f Compare October 29, 2026 22:48
Comment thread compiler/rustc_middle/src/queries.rs Resolved

specialization_enabled_in: bool,

// LazyTable maybe??

@oli-obk oli-obk Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

yea, definitely 😅 if the list gets large for anyone the linear search doesn't seem ideal

View changes since the review

@lambinoo lambinoo Nov 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Don't think I can actually use LazyTable here, as I have a hard time (maybe I just do not understand how). I cannot have a Symbol as a key since it does not implement the Idx trait.

Wonder if I could update this to UnordMap. That way, the whole thing can just go in a LazyValue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh hm right. Just a list of entries seems fine, it's usually not gonna be very long anyway

Comment thread compiler/rustc_middle/src/ty/context.rs Resolved
@rustbot

rustbot commented Oct 30, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rust-log-analyzer

This comment has been minimized.

.sess
.opts
.unstable_opts
.experimental_comptime_files

@oli-obk oli-obk Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Use the query everywhere instead of accessing the file list flag directly in other places

View changes since the review

}
}
} else {
tracing::debug!("not allowed to read file");

@oli-obk oli-obk Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should probably just hard error, making the only way to get a None and not fail compilation is by the file not being actually there

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added all proper diagnostics too! But I'm a bit unsure about the handling of the spans I did in the machine interpreter.

match std::fs::read(resolved_path) {
Ok(content) => Some(tcx.arena.alloc_slice(&content)),
Err(err) => {
tracing::debug!(%err, "failed to read file");

@oli-obk oli-obk Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm really unsure what to do with this case. The file not being there is possibly useful as a fallible operation, but all other file reading failures should probably hard error

View changes since the review

@lambinoo lambinoo Nov 7, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I went for the query returning a Result<Option<&[u8]>, ReadFileError<'_>>. That should give us the following property:

  • return None when file is not there, same as the query
  • the caller of the query can emit a clear diagnostic, with the right span, as of why the read failed (not whitelisted, I/O error like PermissionDenied)

Comment thread compiler/rustc_middle/src/queries.rs Outdated
}

/// Query list of files available during const evaluation
query experimental_comptime_list_files(key: CrateNum) -> Option<&'tcx [Symbol]> {

@oli-obk oli-obk Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There isn't really any useful difference between the empty slice and a None here, so we can just leave out the Option part

View changes since the review

@rust-log-analyzer

This comment has been minimized.

@lambinoo
lambinoo force-pushed the feat-comptime-read-files branch from 5c30c45 to b7e2e75 Compare November 7, 2026 19:00
@rust-bors

This comment has been minimized.

@lambinoo lambinoo mentioned this pull request Nov 20, 2026
1 of 11 tasks
@lambinoo
lambinoo force-pushed the feat-comptime-read-files branch from 745adf0 to 254081f Compare November 20, 2026 23:29
feat: add file reading query and rmeta encoding/decoding of comptime files

remove caching and use CLI flags for comptime files

implement intrinsic and add core support for it

use symbol in fetch file query and resolve paths from crate root

feat: properly track comptime files in crates deps

enable proper cross-crate execution of read_file

feat: add file list query

feat: implement intrinsics and lib functions

feat: add error handling for comptime file reading

refactor: use query to get comptime filelist

fix: set comptime fs queries as eval_always

docs: clarify failure modes of read_file

Signed-off-by: Lamb <contact@ltow.me>
@lambinoo
lambinoo force-pushed the feat-comptime-read-files branch from 254081f to 6258416 Compare November 22, 2026 10:19
@lambinoo
lambinoo marked this pull request as ready for review November 23, 2026 17:03
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 23, 2026
@rustbot

rustbot commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter
gets adapted for the changes, if necessary.

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

This branch has not been deployed

No deployments
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.

4 participants