Skip to content

Don't rely on promotion of PageTableEntry::new inside a const fn#175

Merged
phil-opp merged 2 commits into
rust-osdev:masterfrom
ecstatic-morse:patch-1
Sep 3, 2020
Merged

Don't rely on promotion of PageTableEntry::new inside a const fn#175
phil-opp merged 2 commits into
rust-osdev:masterfrom
ecstatic-morse:patch-1

Conversation

@ecstatic-morse

@ecstatic-morse ecstatic-morse commented Sep 1, 2020

Copy link
Copy Markdown
Contributor

Hi! Rust compiler team member here. When the const_fn feature is enabled, the implementation of PageTable::new relies on a compiler bug to work. Specifically, PageTableEntry::new should not be eligible for promotion as part of an array initializer. See rust-lang/rust#75502 for more information. This bug may be fixed in the future, which would cause this crate to stop compiling.

Hi! Rust compiler team member here. When the `const_fn` feature is enabled, the implementation of `PageTable::new` relies on a compiler bug to work. Specifically, `PageTableEntry::new` should not be eligible for promotion as part of an array initializer. See rust-lang/rust#75502 for more information. This bug may be fixed in the future, which will cause this crate to stop compiling.
@phil-opp

phil-opp commented Sep 2, 2020

Copy link
Copy Markdown
Member

Thanks a lot for the early warning and for taking the time to fix this for us!

I'm wondering if the following would work too:

    pub const fn new() -> Self {
        const EMPTY: PageTableEntry = PageTableEntry::new();
        PageTable {
            entries: [EMPTY; ENTRY_COUNT],
        }
    }

Here we initialize the array with a const, which should be ok, right? And assigning the result of a const fn to a const should be ok as well, or am I missing something?

The advantage of this implementation would be that it doesn't need access to the private entry field of the PageTableEntry struct, which seems a bit cleaner to me. (I'm happy to implement this change myself, I just wanted to ensure that isn't buggy too.)

@ecstatic-morse

ecstatic-morse commented Sep 2, 2020

Copy link
Copy Markdown
Contributor Author

That works too! The problems that can result from promoting calls to const fn are described in more detail in rust-lang/const-eval#19. In this particular case, promotion of a panicking const fn would actually be fine, since if an array initializer of a non-Copy type cannot be promoted, compilation will fail regardless. However, we're not yet willing to commit to these semantics for the unstable const_in_array_repeat_expressions feature, hence the (possible) breakage.

@phil-opp

phil-opp commented Sep 3, 2020

Copy link
Copy Markdown
Member

Thanks a lot for the explanation!

@phil-opp
phil-opp merged commit ed9fb42 into rust-osdev:master Sep 3, 2020
phil-opp added a commit that referenced this pull request Sep 3, 2020
@phil-opp

phil-opp commented Sep 3, 2020

Copy link
Copy Markdown
Member

Published as v0.11.5.

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