Use from_fn for non-Copyable Rust types#192
Merged
Merged
Conversation
f11f71d to
71c227e
Compare
nunoplopes
reviewed
Jun 12, 2026
| } | ||
| } | ||
| unsafe fn main_0() -> i32 { | ||
| let mut arr: [NonCopy; 3] = std::array::from_fn::<_, 3, _>(|_| <NonCopy>::default()); |
Contributor
There was a problem hiding this comment.
Uhm, I think we can use just let _arr: [NonCopy; 3] = Default::default();. No need for from_fn?
Contributor
Author
There was a problem hiding this comment.
Unfortunately no, Default::default() only works for array sizes < 32: https://godbolt.org/z/c5T6boGbq
71c227e to
4c56665
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In Rust, a struct is Copyable if it explicitly derives or implements Copy. All fields being Copy is not enough.
Add the
derivesattribute in the IR that lists all traits that a mapped Rust type derives. For example rustls_slice_bytes, a struct with Copyable fields, doesn't derive Copy. Soderivescontains only Default.User defined types and builtin types also have their
derivesfield filled. The entire PR is built around the following check:Arrays of non-copyable types cannot be initialized with
[non_copyable_default, N], they have to use thestd::array:from_fnformat.