Skip to content

Commit 18f3f5f

Browse files
Enable unsafe_ops_in_unsafe_fn lint in all workspaces (#3044)
1 parent d536613 commit 18f3f5f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+3039
-2407
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ both `raw_window_handle::HasRawWindowHandle` and `raw_window_handle::HasRawDispl
229229
- Don't use `PhantomData` for `IdentityManager`'s `Input` type. By @jimblandy in [#2972](https://github.com/gfx-rs/wgpu/pull/2972)
230230
- Changed Naga variant in ShaderSource to `Cow<'static, Module>`, to allow loading global variables by @daxpedda in [#2903](https://github.com/gfx-rs/wgpu/pull/2903)
231231
- Updated the maximum binding index to match the WebGPU specification by @nical in [#2957](https://github.com/gfx-rs/wgpu/pull/2957)
232+
- Add `unsafe_op_in_unsafe_fn` to Clippy lints in the entire workspace. By @ErichDonGubler in [#3044](https://github.com/gfx-rs/wgpu/pull/3044).
232233

233234
#### Metal
234235

deno_webgpu/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
22

3+
#![warn(unsafe_op_in_unsafe_fn)]
4+
35
use deno_core::error::AnyError;
46
use deno_core::include_js_files;
57
use deno_core::op;

player/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* so that we don't accidentally try to use the same ID.
77
!*/
88

9+
#![warn(unsafe_op_in_unsafe_fn)]
10+
911
use wgc::device::trace;
1012

1113
use std::{borrow::Cow, fmt::Debug, fs, marker::PhantomData, path::Path};

wgpu-core/src/command/bundle.rs

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ impl<A: HalApi> RenderBundle<A> {
763763
let mut offsets = self.base.dynamic_offsets.as_slice();
764764
let mut pipeline_layout_id = None::<id::Valid<id::PipelineLayoutId>>;
765765
if let Some(ref label) = self.base.label {
766-
raw.begin_debug_marker(label);
766+
unsafe { raw.begin_debug_marker(label) };
767767
}
768768

769769
for command in self.base.commands.iter() {
@@ -774,17 +774,19 @@ impl<A: HalApi> RenderBundle<A> {
774774
bind_group_id,
775775
} => {
776776
let bind_group = bind_group_guard.get(bind_group_id).unwrap();
777-
raw.set_bind_group(
778-
&pipeline_layout_guard[pipeline_layout_id.unwrap()].raw,
779-
index as u32,
780-
&bind_group.raw,
781-
&offsets[..num_dynamic_offsets as usize],
782-
);
777+
unsafe {
778+
raw.set_bind_group(
779+
&pipeline_layout_guard[pipeline_layout_id.unwrap()].raw,
780+
index as u32,
781+
&bind_group.raw,
782+
&offsets[..num_dynamic_offsets as usize],
783+
)
784+
};
783785
offsets = &offsets[num_dynamic_offsets as usize..];
784786
}
785787
RenderCommand::SetPipeline(pipeline_id) => {
786788
let pipeline = pipeline_guard.get(pipeline_id).unwrap();
787-
raw.set_render_pipeline(&pipeline.raw);
789+
unsafe { raw.set_render_pipeline(&pipeline.raw) };
788790

789791
pipeline_layout_id = Some(pipeline.layout_id.value);
790792
}
@@ -805,7 +807,7 @@ impl<A: HalApi> RenderBundle<A> {
805807
offset,
806808
size,
807809
};
808-
raw.set_index_buffer(bb, index_format);
810+
unsafe { raw.set_index_buffer(bb, index_format) };
809811
}
810812
RenderCommand::SetVertexBuffer {
811813
slot,
@@ -824,7 +826,7 @@ impl<A: HalApi> RenderBundle<A> {
824826
offset,
825827
size,
826828
};
827-
raw.set_vertex_buffer(slot, bb);
829+
unsafe { raw.set_vertex_buffer(slot, bb) };
828830
}
829831
RenderCommand::SetPushConstant {
830832
stages,
@@ -841,18 +843,22 @@ impl<A: HalApi> RenderBundle<A> {
841843
let data_slice = &self.base.push_constant_data
842844
[(values_offset as usize)..values_end_offset];
843845

844-
raw.set_push_constants(&pipeline_layout.raw, stages, offset, data_slice)
846+
unsafe {
847+
raw.set_push_constants(&pipeline_layout.raw, stages, offset, data_slice)
848+
}
845849
} else {
846850
super::push_constant_clear(
847851
offset,
848852
size_bytes,
849853
|clear_offset, clear_data| {
850-
raw.set_push_constants(
851-
&pipeline_layout.raw,
852-
stages,
853-
clear_offset,
854-
clear_data,
855-
);
854+
unsafe {
855+
raw.set_push_constants(
856+
&pipeline_layout.raw,
857+
stages,
858+
clear_offset,
859+
clear_data,
860+
)
861+
};
856862
},
857863
);
858864
}
@@ -863,7 +869,7 @@ impl<A: HalApi> RenderBundle<A> {
863869
first_vertex,
864870
first_instance,
865871
} => {
866-
raw.draw(first_vertex, vertex_count, first_instance, instance_count);
872+
unsafe { raw.draw(first_vertex, vertex_count, first_instance, instance_count) };
867873
}
868874
RenderCommand::DrawIndexed {
869875
index_count,
@@ -872,13 +878,15 @@ impl<A: HalApi> RenderBundle<A> {
872878
base_vertex,
873879
first_instance,
874880
} => {
875-
raw.draw_indexed(
876-
first_index,
877-
index_count,
878-
base_vertex,
879-
first_instance,
880-
instance_count,
881-
);
881+
unsafe {
882+
raw.draw_indexed(
883+
first_index,
884+
index_count,
885+
base_vertex,
886+
first_instance,
887+
instance_count,
888+
)
889+
};
882890
}
883891
RenderCommand::MultiDrawIndirect {
884892
buffer_id,
@@ -892,7 +900,7 @@ impl<A: HalApi> RenderBundle<A> {
892900
.raw
893901
.as_ref()
894902
.ok_or(ExecutionError::DestroyedBuffer(buffer_id))?;
895-
raw.draw_indirect(buffer, offset, 1);
903+
unsafe { raw.draw_indirect(buffer, offset, 1) };
896904
}
897905
RenderCommand::MultiDrawIndirect {
898906
buffer_id,
@@ -906,7 +914,7 @@ impl<A: HalApi> RenderBundle<A> {
906914
.raw
907915
.as_ref()
908916
.ok_or(ExecutionError::DestroyedBuffer(buffer_id))?;
909-
raw.draw_indexed_indirect(buffer, offset, 1);
917+
unsafe { raw.draw_indexed_indirect(buffer, offset, 1) };
910918
}
911919
RenderCommand::MultiDrawIndirect { .. }
912920
| RenderCommand::MultiDrawIndirectCount { .. } => {
@@ -931,7 +939,7 @@ impl<A: HalApi> RenderBundle<A> {
931939
}
932940

933941
if let Some(_) = self.base.label {
934-
raw.end_debug_marker();
942+
unsafe { raw.end_debug_marker() };
935943
}
936944

937945
Ok(())
@@ -1439,13 +1447,15 @@ pub mod bundle_ffi {
14391447
offsets: *const DynamicOffset,
14401448
offset_length: usize,
14411449
) {
1442-
let redundant = bundle.current_bind_groups.set_and_check_redundant(
1443-
bind_group_id,
1444-
index,
1445-
&mut bundle.base.dynamic_offsets,
1446-
offsets,
1447-
offset_length,
1448-
);
1450+
let redundant = unsafe {
1451+
bundle.current_bind_groups.set_and_check_redundant(
1452+
bind_group_id,
1453+
index,
1454+
&mut bundle.base.dynamic_offsets,
1455+
offsets,
1456+
offset_length,
1457+
)
1458+
};
14491459

14501460
if redundant {
14511461
return;
@@ -1522,7 +1532,7 @@ pub mod bundle_ffi {
15221532
0,
15231533
"Push constant size must be aligned to 4 bytes."
15241534
);
1525-
let data_slice = slice::from_raw_parts(data, size_bytes as usize);
1535+
let data_slice = unsafe { slice::from_raw_parts(data, size_bytes as usize) };
15261536
let value_offset = pass.base.push_constant_data.len().try_into().expect(
15271537
"Ran out of push constant space. Don't set 4gb of push constants per RenderBundle.",
15281538
);

wgpu-core/src/command/compute.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -800,13 +800,15 @@ pub mod compute_ffi {
800800
offsets: *const DynamicOffset,
801801
offset_length: usize,
802802
) {
803-
let redundant = pass.current_bind_groups.set_and_check_redundant(
804-
bind_group_id,
805-
index,
806-
&mut pass.base.dynamic_offsets,
807-
offsets,
808-
offset_length,
809-
);
803+
let redundant = unsafe {
804+
pass.current_bind_groups.set_and_check_redundant(
805+
bind_group_id,
806+
index,
807+
&mut pass.base.dynamic_offsets,
808+
offsets,
809+
offset_length,
810+
)
811+
};
810812

811813
if redundant {
812814
return;
@@ -854,7 +856,7 @@ pub mod compute_ffi {
854856
0,
855857
"Push constant size must be aligned to 4 bytes."
856858
);
857-
let data_slice = slice::from_raw_parts(data, size_bytes as usize);
859+
let data_slice = unsafe { slice::from_raw_parts(data, size_bytes as usize) };
858860
let value_offset = pass.base.push_constant_data.len().try_into().expect(
859861
"Ran out of push constant space. Don't set 4gb of push constants per ComputePass.",
860862
);
@@ -905,7 +907,7 @@ pub mod compute_ffi {
905907
label: RawString,
906908
color: u32,
907909
) {
908-
let bytes = ffi::CStr::from_ptr(label).to_bytes();
910+
let bytes = unsafe { ffi::CStr::from_ptr(label) }.to_bytes();
909911
pass.base.string_data.extend_from_slice(bytes);
910912

911913
pass.base.commands.push(ComputeCommand::PushDebugGroup {
@@ -929,7 +931,7 @@ pub mod compute_ffi {
929931
label: RawString,
930932
color: u32,
931933
) {
932-
let bytes = ffi::CStr::from_ptr(label).to_bytes();
934+
let bytes = unsafe { ffi::CStr::from_ptr(label) }.to_bytes();
933935
pass.base.string_data.extend_from_slice(bytes);
934936

935937
pass.base.commands.push(ComputeCommand::InsertDebugMarker {

wgpu-core/src/command/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,8 @@ impl BindGroupStateChange {
526526
if let Some(current_bind_group) = self.last_states.get_mut(index as usize) {
527527
current_bind_group.reset();
528528
}
529-
dynamic_offsets.extend_from_slice(slice::from_raw_parts(offsets, offset_length));
529+
dynamic_offsets
530+
.extend_from_slice(unsafe { slice::from_raw_parts(offsets, offset_length) });
530531
}
531532
false
532533
}

wgpu-core/src/command/render.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,13 +2113,15 @@ pub mod render_ffi {
21132113
offsets: *const DynamicOffset,
21142114
offset_length: usize,
21152115
) {
2116-
let redundant = pass.current_bind_groups.set_and_check_redundant(
2117-
bind_group_id,
2118-
index,
2119-
&mut pass.base.dynamic_offsets,
2120-
offsets,
2121-
offset_length,
2122-
);
2116+
let redundant = unsafe {
2117+
pass.current_bind_groups.set_and_check_redundant(
2118+
bind_group_id,
2119+
index,
2120+
&mut pass.base.dynamic_offsets,
2121+
offsets,
2122+
offset_length,
2123+
)
2124+
};
21232125

21242126
if redundant {
21252127
return;
@@ -2239,7 +2241,7 @@ pub mod render_ffi {
22392241
0,
22402242
"Push constant size must be aligned to 4 bytes."
22412243
);
2242-
let data_slice = slice::from_raw_parts(data, size_bytes as usize);
2244+
let data_slice = unsafe { slice::from_raw_parts(data, size_bytes as usize) };
22432245
let value_offset = pass.base.push_constant_data.len().try_into().expect(
22442246
"Ran out of push constant space. Don't set 4gb of push constants per RenderPass.",
22452247
);
@@ -2402,7 +2404,7 @@ pub mod render_ffi {
24022404
label: RawString,
24032405
color: u32,
24042406
) {
2405-
let bytes = ffi::CStr::from_ptr(label).to_bytes();
2407+
let bytes = unsafe { ffi::CStr::from_ptr(label) }.to_bytes();
24062408
pass.base.string_data.extend_from_slice(bytes);
24072409

24082410
pass.base.commands.push(RenderCommand::PushDebugGroup {
@@ -2426,7 +2428,7 @@ pub mod render_ffi {
24262428
label: RawString,
24272429
color: u32,
24282430
) {
2429-
let bytes = ffi::CStr::from_ptr(label).to_bytes();
2431+
let bytes = unsafe { ffi::CStr::from_ptr(label) }.to_bytes();
24302432
pass.base.string_data.extend_from_slice(bytes);
24312433

24322434
pass.base.commands.push(RenderCommand::InsertDebugMarker {
@@ -2478,7 +2480,9 @@ pub mod render_ffi {
24782480
render_bundle_ids: *const id::RenderBundleId,
24792481
render_bundle_ids_length: usize,
24802482
) {
2481-
for &bundle_id in slice::from_raw_parts(render_bundle_ids, render_bundle_ids_length) {
2483+
for &bundle_id in
2484+
unsafe { slice::from_raw_parts(render_bundle_ids, render_bundle_ids_length) }
2485+
{
24822486
pass.base
24832487
.commands
24842488
.push(RenderCommand::ExecuteBundle(bundle_id));

wgpu-core/src/device/life.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,61 +132,61 @@ impl<A: hal::Api> NonReferencedResources<A> {
132132
if !self.buffers.is_empty() {
133133
profiling::scope!("destroy_buffers");
134134
for raw in self.buffers.drain(..) {
135-
device.destroy_buffer(raw);
135+
unsafe { device.destroy_buffer(raw) };
136136
}
137137
}
138138
if !self.textures.is_empty() {
139139
profiling::scope!("destroy_textures");
140140
for raw in self.textures.drain(..) {
141-
device.destroy_texture(raw);
141+
unsafe { device.destroy_texture(raw) };
142142
}
143143
}
144144
if !self.texture_views.is_empty() {
145145
profiling::scope!("destroy_texture_views");
146146
for raw in self.texture_views.drain(..) {
147-
device.destroy_texture_view(raw);
147+
unsafe { device.destroy_texture_view(raw) };
148148
}
149149
}
150150
if !self.samplers.is_empty() {
151151
profiling::scope!("destroy_samplers");
152152
for raw in self.samplers.drain(..) {
153-
device.destroy_sampler(raw);
153+
unsafe { device.destroy_sampler(raw) };
154154
}
155155
}
156156
if !self.bind_groups.is_empty() {
157157
profiling::scope!("destroy_bind_groups");
158158
for raw in self.bind_groups.drain(..) {
159-
device.destroy_bind_group(raw);
159+
unsafe { device.destroy_bind_group(raw) };
160160
}
161161
}
162162
if !self.compute_pipes.is_empty() {
163163
profiling::scope!("destroy_compute_pipelines");
164164
for raw in self.compute_pipes.drain(..) {
165-
device.destroy_compute_pipeline(raw);
165+
unsafe { device.destroy_compute_pipeline(raw) };
166166
}
167167
}
168168
if !self.render_pipes.is_empty() {
169169
profiling::scope!("destroy_render_pipelines");
170170
for raw in self.render_pipes.drain(..) {
171-
device.destroy_render_pipeline(raw);
171+
unsafe { device.destroy_render_pipeline(raw) };
172172
}
173173
}
174174
if !self.bind_group_layouts.is_empty() {
175175
profiling::scope!("destroy_bind_group_layouts");
176176
for raw in self.bind_group_layouts.drain(..) {
177-
device.destroy_bind_group_layout(raw);
177+
unsafe { device.destroy_bind_group_layout(raw) };
178178
}
179179
}
180180
if !self.pipeline_layouts.is_empty() {
181181
profiling::scope!("destroy_pipeline_layouts");
182182
for raw in self.pipeline_layouts.drain(..) {
183-
device.destroy_pipeline_layout(raw);
183+
unsafe { device.destroy_pipeline_layout(raw) };
184184
}
185185
}
186186
if !self.query_sets.is_empty() {
187187
profiling::scope!("destroy_query_sets");
188188
for raw in self.query_sets.drain(..) {
189-
device.destroy_query_set(raw);
189+
unsafe { device.destroy_query_set(raw) };
190190
}
191191
}
192192
}

0 commit comments

Comments
 (0)