[naga] Gracefully handle span lookup with no module source (#7390)

Fixes #7165
This commit is contained in:
Andy Leiserson authored and GitHub committed 2025-03-26 11:19:11 -04:00
1 parent e037f4bc48
commit 90afc88e76
2 files changed
+45 -2

No files matched your search

+1 -1
View File
@@ -239,7 +239,7 @@ impl<E> WithSpan<E> {
/// Return a [`SourceLocation`] for our first span, if we have one.
pub fn location(&self, source: &str) -> Option<SourceLocation> {
if self.spans.is_empty() {
if self.spans.is_empty() || source.is_empty() {
return None;
}
+44 -1
View File
@@ -940,7 +940,7 @@ fn multiple_enables_valid() {
/// Check the result of validating a WGSL program against a pattern.
///
/// Unless you are generating code programmatically, the
/// `check_validation_error` macro will probably be more convenient to
/// `check_validation` macro will probably be more convenient to
/// use.
macro_rules! check_one_validation {
( $source:expr, $pattern:pat $( if $guard:expr )? ) => {
@@ -1353,6 +1353,17 @@ fn invalid_functions() {
}
}
#[test]
fn invalid_return_type() {
check_validation! {
"fn invalid_return_type() -> i32 { return 0u; }":
Err(naga::valid::ValidationError::Function {
source: naga::valid::FunctionError::InvalidReturnType(Some(_)),
..
})
};
}
#[test]
fn pointer_type_equivalence() {
check_validation! {
@@ -3049,3 +3060,35 @@ fn reject_utf8_bom() {
"#,
);
}
#[test]
fn issue7165() {
// Regression test for https://github.com/gfx-rs/wgpu/issues/7165
let shader = "
struct Struct { a: u32 }
fn invalid_return_type(a: Struct) -> i32 { return a; }
";
let module = naga::front::wgsl::parse_str(shader).unwrap();
let err = naga::valid::Validator::new(
naga::valid::ValidationFlags::all(),
naga::valid::Capabilities::default(),
)
.validate(&module)
.unwrap_err();
// This is a proxy for doing the following (with an error
// handler installed so it doesn't immediately panic):
//
// ```
// device.create_shader_module(wgpu::ShaderModuleDescriptor {
// label,
// source: wgpu::ShaderSource::Naga(module),
// });
// ```
//
// `ShaderSource::Naga` causes the implementation to proceed with an empty
// module source, which (prior to the fix for #7165) could panic when
// rendering an error if the module contained spans.
let _location = err.location("");
}