204: Fix rustc warnings r=kvark a=krolli

This PR fixes warnings encountered with stable 1.40.0 rust toolchain. The most interesting issue is likely switch from `mem::zeroed()` and related functions to `MaybeUninit`.

While `mem::zeroed()` is not marked deprecated in the same way as `mem::uninitialized()` is, compiler still threw warning on it. However, in this case it may have been causing undefined behavior, as `EntryPoint` contains references which may not be null. Another option here would be using `Option` instead of `MaybeUninit`, which would be completely safe with some extra checks. Since original code didn't, and because valid Vulkan use requires exactly one vertex shader, I chose to use `MaybeUninit` instead.

Co-authored-by: Martin Krošlák <kroslakma@gmail.com>
This commit is contained in:
bors[bot] 2020-01-23 13:07:35 +00:00 committed by GitHub
commit 4958223f8e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 17 deletions

View file

@ -37,7 +37,7 @@ impl<T> HandleAllocation<T> {
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
{ {
use std::intrinsics::type_name; use std::intrinsics::type_name;
let name = unsafe { type_name::<T>() }; let name = type_name::<T>();
REGISTRY.lock().unwrap().insert(ptr as _, name); REGISTRY.lock().unwrap().insert(ptr as _, name);
} }
Handle(ptr) Handle(ptr)

View file

@ -62,6 +62,8 @@ pub extern "C" fn gfxCreateInstance(
let _ = env_logger::try_init(); let _ = env_logger::try_init();
} }
#[allow(unused_mut)]
// Metal branch performs mutation, so we silence the warning on other backends.
let mut backend = let mut backend =
back::Instance::create("portability", 1).expect("failed to create backend instance"); back::Instance::create("portability", 1).expect("failed to create backend instance");
@ -2074,13 +2076,11 @@ pub extern "C" fn gfxCreateGraphicsPipelines(
}; };
let shaders = { let shaders = {
let mut set = pso::GraphicsShaderSet { let mut vertex = mem::MaybeUninit::uninit();
vertex: unsafe { mem::zeroed() }, // fake entry point let mut hull = None;
hull: None, let mut domain = None;
domain: None, let mut geometry = None;
geometry: None, let mut fragment = None;
fragment: None,
};
let stages = unsafe { slice::from_raw_parts(info.pStages, info.stageCount as _) }; let stages = unsafe { slice::from_raw_parts(info.pStages, info.stageCount as _) };
@ -2109,26 +2109,31 @@ pub extern "C" fn gfxCreateGraphicsPipelines(
match stage.stage { match stage.stage {
VK_SHADER_STAGE_VERTEX_BIT => { VK_SHADER_STAGE_VERTEX_BIT => {
let fake_vs_entry = mem::replace(&mut set.vertex, entry_point); vertex = mem::MaybeUninit::new(entry_point);
mem::forget(fake_vs_entry);
} }
VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT => { VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT => {
set.hull = Some(entry_point); hull = Some(entry_point);
} }
VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT => { VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT => {
set.domain = Some(entry_point); domain = Some(entry_point);
} }
VK_SHADER_STAGE_GEOMETRY_BIT => { VK_SHADER_STAGE_GEOMETRY_BIT => {
set.geometry = Some(entry_point); geometry = Some(entry_point);
} }
VK_SHADER_STAGE_FRAGMENT_BIT if !rasterizer_discard => { VK_SHADER_STAGE_FRAGMENT_BIT if !rasterizer_discard => {
set.fragment = Some(entry_point); fragment = Some(entry_point);
} }
stage => panic!("Unexpected shader stage: {:?}", stage), stage => panic!("Unexpected shader stage: {:?}", stage),
} }
} }
set pso::GraphicsShaderSet {
vertex: unsafe { vertex.assume_init() },
hull,
domain,
geometry,
fragment,
}
}; };
let (vertex_buffers, attributes) = { let (vertex_buffers, attributes) = {
@ -4413,8 +4418,8 @@ pub extern "C" fn gfxGetPhysicalDeviceSurfacePresentModesKHR(
#[inline] #[inline]
pub extern "C" fn gfxGetPhysicalDeviceWin32PresentationSupportKHR( pub extern "C" fn gfxGetPhysicalDeviceWin32PresentationSupportKHR(
adapter: VkPhysicalDevice, _adapter: VkPhysicalDevice,
queueFamilyIndex: u32, _queueFamilyIndex: u32,
) -> VkBool32 { ) -> VkBool32 {
VK_TRUE VK_TRUE
} }
@ -4443,6 +4448,8 @@ pub extern "C" fn gfxCreateSwapchainKHR(
image_layers: 1, image_layers: 1,
image_usage: conv::map_image_usage(info.imageUsage), image_usage: conv::map_image_usage(info.imageUsage),
}; };
#[allow(unused_mut)] // Metal branch performs mutation.
let (mut swapchain, backbuffers) = match unsafe { let (mut swapchain, backbuffers) = match unsafe {
gpu.device.create_swapchain( gpu.device.create_swapchain(
&mut info.surface.clone(), &mut info.surface.clone(),