Skip to content

Make Files::new const for static initialization - #420

Open
RanaPriyansh wants to merge 1 commit into
brendanzab:masterfrom
RanaPriyansh:feat/const-files-new-347
Open

RanaPriyansh wants to merge 1 commit into
brendanzab:masterfrom
RanaPriyansh:feat/const-files-new-347

Conversation

@RanaPriyansh

Copy link
Copy Markdown

Files::new() cannot currently initialize static storage. Make it a const fn that initializes the existing empty file vector, preserving runtime construction and Default.

Addresses #347 for Files::new.

Validation:

  • The identical static-initializer regression fails on the baseline with E0015 and passes after the fix.
  • The static fixture and public file/line queries pass on Rust 1.67 and Rust 1.88.
  • Workspace tests, reporting feature checks, LSP tests, formatting, and Clippy pass on Rust 1.88. Clippy emits warnings in reporting code. The full workspace was not tested on Rust 1.67.

Codex used.

@RanaPriyansh

Copy link
Copy Markdown
Author

The current-head CI run for 5f3cde8049a2a78c25084a720c23576af89d6270 is action_required, with no jobs created. Could a maintainer inspect the run and approve it if required?

The published source is unchanged. The local package, feature, workspace, formatting, and Clippy results remain as described above. No hosted test result is available yet.

Codex used.

@kaleidawave

Copy link
Copy Markdown
Collaborator

I think this is a good change. But I wonder under what scenario this is useful? Maybe if you want to reference some source text that is known at compile time?

Additionally, is there a clippy lint that could flag this in the future?

@RanaPriyansh

Copy link
Copy Markdown
Author

A concrete use is a shared file database that starts empty and receives source files at runtime:

static FILES: std::sync::Mutex<codespan::Files<String>> =
    std::sync::Mutex::new(codespan::Files::new());

This permits static initialization without a lazy initializer. The source text does not need to be known at compile time. Files::add still runs at runtime, so this change does not make a populated file database a compile-time value.

Clippy has missing_const_for_fn, an opt-in nursery lint. It can be enabled with -W clippy::missing_const_for_fn. Its documentation notes that non-const callees can prevent a suggestion. The previous constructor called non-const Default::default, so this lint would not necessarily identify the original implementation. The direct Vec::new() construction removes that obstacle.

Codex used.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants