Skip to content

Commit

Permalink
Merge pull request bevyengine#5 from robtfm/3-unwrap-none-when-creati…
Browse files Browse the repository at this point in the history
…ng-a-naga-module-with-missing-import

add check for imports when creating shader
  • Loading branch information
robtfm authored Oct 26, 2022
2 parents 5982631 + f7f8218 commit d2814df
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/compose/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,24 @@ impl Composer {
let name = name.unwrap_or_default();
let substituted_source = Self::sanitize_and_substitute_shader_string(source, &imports);

// make sure imports have been added
for (import_name, offset) in imports
.iter()
.map(|id| (&id.definition.import, id.offset))
.chain(additional_imports.iter().map(|ai| (&ai.import, 0)))
{
if self.module_sets.get(import_name).is_none() {
return Err(ComposerError {
inner: ComposerErrorInner::ImportNotFound(import_name.clone(), offset),
source: ErrSource::Constructing {
path: file_path.to_owned(),
source: substituted_source.clone(),
offset: 0,
},
});
}
}

self.ensure_imports(
imports.iter().map(|import| &import.definition),
&shader_defs,
Expand Down
21 changes: 20 additions & 1 deletion src/compose/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ mod test {
}

#[test]
fn missing_import() {
fn missing_import_in_module() {
let mut composer = Composer::default();

let error = composer
Expand All @@ -268,6 +268,25 @@ mod test {
assert_eq!(text, include_str!("tests/expected/missing_import.txt"));
}

#[test]
fn missing_import_in_shader() {
let mut composer = Composer::default();

let error = composer
.make_naga_module(NagaModuleDescriptor {
source: include_str!("tests/error_test/include.wgsl"),
file_path: "tests/error_test/include.wgsl",
..Default::default()
})
.err()
.unwrap();
let text = error.emit_to_string(&composer);
// let mut f = std::fs::File::create("missing_import.txt").unwrap();
// f.write_all(text.as_bytes()).unwrap();
// drop(f);
assert_eq!(text, include_str!("tests/expected/missing_import.txt"));
}

#[test]
fn wgsl_call_glsl() {
let mut composer = Composer::default();
Expand Down

0 comments on commit d2814df

Please sign in to comment.