Michael-F-Bryan / include_dir

The logical evolution of the include_str macro for embedding a directory tree into your binary.
https://michael-f-bryan.github.io/include_dir
MIT License
339 stars 38 forks source link

include_dir generates function calls that need promotion, leading to issues with Rust 1.79+ #99

Closed RalfJung closed 3 months ago

RalfJung commented 3 months ago

Apparently, this crate generates code like

Some(include_dir::Dir::new(
        "",
        &[include_dir::DirEntry::File(include_dir::File::new(
            "test_file.txt",
            b"",
        ))],
    ))

It then relies on the part in &[...] to be const-promoted. However, const-promotion of arbitrary function calls is problematic, and in Rust 1.79 it has been limited to only occur in straight-line code, causing compilation failures for some users of this crate.

The intended way to obtain 'static references to things computed by const fn is to make this explicitly const -- either via const { ... } blocks, or (when support for older versions of Rust is required), via explicit const items:

Some(include_dir::Dir::new(
        "",
        {
            const ENTRIES: &'static [DirEntry<'static>] = &[include_dir::DirEntry::File(include_dir::File::new(
                "test_file.txt",
                b"",
            ))];
            ENTRIES
        },
    ))

Would be good to get this crate updated to avoid problems related to promotion of function calls. :)

Michael-F-Bryan commented 3 months ago

This was odd. I tried running the crate's tests with 1.79 and nightly, and it built just fine 🤔

I've made the fix anyway. Let's see if it resolves things for other people.

RalfJung commented 3 months ago

Thanks for the quick fix!

This was odd. I tried running the crate's tests with 1.79 and nightly, and it built just fine 🤔

Yeah, https://github.com/rust-lang/rust/pull/121557 is rather odd, making it tricky to write a good test. This still works fine:

pub static ASSETS_DIR: Option<include_dir::Dir<'_>> = Some(include_dir::include_dir!("$CARGO_MANIFEST_DIR/assets"));

but putting the include_dir! inside an if breaks it. It's kind of a hack but for backcompat reasons it's hard to do better...