Skip to content

Commit

Permalink
Add Compile Test and Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cwfitzgerald committed Dec 4, 2024
1 parent 432b020 commit c787967
Show file tree
Hide file tree
Showing 13 changed files with 199 additions and 85 deletions.
60 changes: 57 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ serde_json = "1.0.133"
smallvec = "1"
static_assertions = "1.1.0"
strum = { version = "0.25.0", features = ["derive"] }
trybuild = "1"
tracy-client = "0.17"
thiserror = "1.0.69"
wgpu = { version = "23.0.1", path = "./wgpu", default-features = false }
Expand Down
10 changes: 8 additions & 2 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ name = "wgpu-test"
path = "tests/root.rs"
harness = false

[[test]]
name = "wgpu-compile-test"
path = "compile_tests/root.rs"
harness = true

[features]
webgl = ["wgpu/webgl"]

Expand All @@ -27,6 +32,7 @@ bytemuck.workspace = true
cfg-if.workspace = true
ctor.workspace = true
futures-lite.workspace = true
glam.workspace = true
itertools.workspace = true
libtest-mimic.workspace = true
log.workspace = true
Expand All @@ -37,10 +43,10 @@ profiling.workspace = true
serde_json.workspace = true
serde.workspace = true
strum = { workspace = true, features = ["derive"] }
wgpu-macros.workspace = true
trybuild.workspace = true
wgpu = { workspace = true, features = ["wgsl"] }
wgpu-macros.workspace = true
wgt = { workspace = true, features = ["serde"] }
glam.workspace = true

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
env_logger.workspace = true
Expand Down
16 changes: 16 additions & 0 deletions tests/compile_tests/fail/cpass_lifetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Test to ensure that ComputePass without forget_lifetime does not compile
// when the ComputePass is dropped before the CommandBuffer is finished.

fn main() {
let instance = wgpu::Instance::new(Default::default());
let adapter = pollster::block_on(instance.request_adapter(&Default::default())).unwrap();
let (device, queue) =
pollster::block_on(adapter.request_device(&Default::default(), None)).unwrap();

let mut encoder = device.create_command_encoder(&Default::default());
let _compute_pass = encoder.begin_compute_pass(&Default::default());
// set up the compute pass...

let cmd_buffer = encoder.finish();
queue.submit([cmd_buffer]);
}
13 changes: 13 additions & 0 deletions tests/compile_tests/fail/cpass_lifetime.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0505]: cannot move out of `encoder` because it is borrowed
--> compile_tests/fail/cpass_lifetime.rs:14:22
|
10 | let mut encoder = device.create_command_encoder(&Default::default());
| ----------- binding `encoder` declared here
11 | let _compute_pass = encoder.begin_compute_pass(&Default::default());
| ------- borrow of `encoder` occurs here
...
14 | let cmd_buffer = encoder.finish();
| ^^^^^^^ move out of `encoder` occurs here
15 | queue.submit([cmd_buffer]);
16 | }
| - borrow might be used here, when `_compute_pass` is dropped and runs the destructor for type `wgpu::ComputePass<'_>`
16 changes: 16 additions & 0 deletions tests/compile_tests/fail/rpass_lifetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Test to ensure that ComputePass without forget_lifetime does not compile
// when the ComputePass is dropped before the CommandBuffer is finished.

fn main() {
let instance = wgpu::Instance::new(Default::default());
let adapter = pollster::block_on(instance.request_adapter(&Default::default())).unwrap();
let (device, queue) =
pollster::block_on(adapter.request_device(&Default::default(), None)).unwrap();

let mut encoder = device.create_command_encoder(&Default::default());
let _render_pass = encoder.begin_render_pass(&Default::default());
// set up the render pass...

let cmd_buffer = encoder.finish();
queue.submit([cmd_buffer]);
}
13 changes: 13 additions & 0 deletions tests/compile_tests/fail/rpass_lifetime.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0505]: cannot move out of `encoder` because it is borrowed
--> compile_tests/fail/rpass_lifetime.rs:14:22
|
10 | let mut encoder = device.create_command_encoder(&Default::default());
| ----------- binding `encoder` declared here
11 | let _render_pass = encoder.begin_render_pass(&Default::default());
| ------- borrow of `encoder` occurs here
...
14 | let cmd_buffer = encoder.finish();
| ^^^^^^^ move out of `encoder` occurs here
15 | queue.submit([cmd_buffer]);
16 | }
| - borrow might be used here, when `_render_pass` is dropped and runs the destructor for type `wgpu::RenderPass<'_>`
7 changes: 7 additions & 0 deletions tests/compile_tests/root.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Tests that ensure that various constructs that should not compile do not compile.

#[test]
fn compile_fail() {
let t = trybuild::TestCases::new();
t.compile_fail("compile_tests/fail/*.rs");
}
2 changes: 1 addition & 1 deletion wgpu-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ serde = ["dep:serde"]
counters = []

[dependencies]
bitflags.workspace = true
bitflags = { workspace = true, features = ["serde"] }
serde = { workspace = true, features = ["derive"], optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
10 changes: 5 additions & 5 deletions wgpu/src/api/command_encoder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{marker::PhantomData, ops::Range};
use std::ops::Range;

use crate::{
api::{
Expand Down Expand Up @@ -79,8 +79,8 @@ impl CommandEncoder {
) -> RenderPass<'encoder> {
let rpass = self.inner.begin_render_pass(desc);
RenderPass {
inner: RenderPassInner { inner: rpass },
encoder_guard: PhantomData,
inner: rpass,
_encoder_guard: api::PhantomDrop::default(),
}
}

Expand All @@ -100,8 +100,8 @@ impl CommandEncoder {
) -> ComputePass<'encoder> {
let cpass = self.inner.begin_compute_pass(desc);
ComputePass {
inner: ComputePassInner { inner: cpass },
encoder_guard: PhantomData,
inner: cpass,
_encoder_guard: api::PhantomDrop::default(),
}
}

Expand Down
Loading

0 comments on commit c787967

Please sign in to comment.