Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ash builder pattern is error prone #866

Closed
yshui opened this issue Feb 28, 2024 · 7 comments
Closed

ash builder pattern is error prone #866

yshui opened this issue Feb 28, 2024 · 7 comments

Comments

@yshui
Copy link

yshui commented Feb 28, 2024

Many of the builder methods take references with lifetime, such as DeviceCreateInfoBuilder::queue_create_infos, but have a final .build() which returns something without a lifetime. This leads me to believe falsely the returned struct holds a copy of the passed arguments internally, thus not limited by the source lifetime. But this is not the case, ash converts the passed references to pointers, which means the passed arguments must live longer than the returned value.

No where in documentation was this mentioned. And the API is designed in such a way that makes leveraging the borrow checker impossible. I suggest the return from builders should retain the lifetime of the builder.

@yshui
Copy link
Author

yshui commented Feb 28, 2024

Real example of misuse: vulkano-rs/vulkano#2465

yshui added a commit to yshui/index_camera_passthrough that referenced this issue Feb 28, 2024
@Friz64
Copy link
Contributor

Friz64 commented Feb 28, 2024

This has been fixed in #602, which will be published on crates.io in a future release.

No where in documentation was this mentioned.

It was previously explained in the README, but that was removed in #743 as that information is no longer applicable on the master branch.

@Ralith
Copy link
Collaborator

Ralith commented Feb 28, 2024

It is also warned against in the API docs for every single build method.

@yshui
Copy link
Author

yshui commented Feb 28, 2024

darn I was a bit late then 😅 feel free to close it now or when the change is released.

@tomaka
Copy link

tomaka commented Mar 6, 2024

#602 doesn't really seem to prevent these mistakes, unless I misunderstand something. As an example, the code below compiles just fine (using the latest git commit 92084df):

// Compiles fine but really shouldn't.
fn test() -> ash::vk::SubpassDescription<'static> {
    ash::vk::SubpassDescription::default().color_attachments(&[
        ash::vk::AttachmentReference {
            attachment: 0,
            layout: ash::vk::ImageLayout::UNDEFINED,
        },
    ])
}

@MarijnS95
Copy link
Collaborator

@tomaka what is wrong about that example? The slice has a 'static lifetime, so it is sound to return a ash::vk::SubpassDescription with a pointer to that ever-lasting slice.

@Ralith
Copy link
Collaborator

Ralith commented Mar 6, 2024

In your example, the array literal is getting promoted to having a static lifetime. Try using a dynamic value in it to prevent this, and observe that it will no longer compile.

@MarijnS95 MarijnS95 closed this as not planned Won't fix, can't repro, duplicate, stale Mar 15, 2024
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

No branches or pull requests

5 participants