Skip to content

comptime read files experiments - #165159

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

lambinoo wants to merge 11 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

rust-bors Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

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

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

4 participants