fix(wgsl-in): Handle Unterminated Block Comments (#9356)

This commit is contained in:
Brendon Daugherty authored and GitHub committed 2026-04-02 13:26:21 -06:00
1 parent e46eda2e4f
commit 1080b4faf3
6 files changed
+44 -1

No files matched your search

+1
View File
@@ -89,6 +89,7 @@ Bottom level categories:
- Fixed overflow detection and argument domain validation for `acosh`, `length`, `normalize`, and `pow` in constant evaluation. By @ecoricemon in [#9249](https://github.com/gfx-rs/wgpu/pull/9249).
- Naga no longer allows derivative operations on `f16`. WGSL does not currently allow this, although [it may be added in the future](https://github.com/gpuweb/gpuweb/issues/5482). By @andyleiserson in [#9154](https://github.com/gfx-rs/wgpu/pull/9154).
- Disallow direct access to atomic variables in WGSL front-end (e.g. `let x = myAtomic;`). By @ecoricemon in [#9262](https://github.com/gfx-rs/wgpu/pull/9262).
- Fixed handling of unterminated block comments. By @BKDaugherty in [#9356](https://github.com/gfx-rs/wgpu/pull/9356).
#### dx12
+1
View File
@@ -447,6 +447,7 @@ webgpu:shader,validation,parse,blankspace:bom:*
webgpu:shader,validation,parse,blankspace:null_characters:contains_null=false;*
webgpu:shader,validation,parse,blankspace:null_characters:contains_null=true;placement="delimiter"
webgpu:shader,validation,parse,blankspace:null_characters:contains_null=true;placement="eol"
webgpu:shader,validation,parse,comments:unterminated_block_comment:terminated=false
webgpu:shader,validation,parse,enable:*
webgpu:shader,validation,parse,identifiers:*
webgpu:shader,validation,parse,requires:*
+10
View File
@@ -442,6 +442,7 @@ pub(crate) enum Error<'a> {
},
UnexpectedExprForTypeExpression(Span),
MissingIncomingPayload(Span),
UnterminatedBlockComment(Span),
}
impl From<ConflictingDiagnosticRuleError> for Error<'_> {
@@ -516,6 +517,7 @@ impl<'a> Error<'a> {
Token::DocComment(s) => format!("doc comment ('{s}')"),
Token::ModuleDocComment(s) => format!("module doc comment ('{s}')"),
Token::End => "end".to_string(),
Token::UnterminatedBlockComment(s) => format!("unterminated doc comment ('{s}'")
},
ExpectedToken::Identifier => "identifier".to_string(),
ExpectedToken::LhsExpression => "LHS expression (identifier component_or_swizzle_specifier?, (`lhs_expression`) component_or_swizzle_specifier?, &`lhs_expression`, *`lhs_expression`)".to_string(),
@@ -1511,6 +1513,14 @@ impl<'a> Error<'a> {
)],
notes: vec![],
},
Error::UnterminatedBlockComment(span) => ParseError {
message: "unterminated block comment".to_string(),
labels: vec![(
span,
"must be closed with `*/`".into(),
)],
notes: vec![],
}
}
}
}
+14 -1
View File
@@ -89,6 +89,11 @@ pub enum Token<'a> {
/// A module-level doc comment, beginning with `//!` or `/*!`.
ModuleDocComment(&'a str),
/// A block comment that is incomplete, and has not been closed with */.
///
/// It's expected that the parser will consider this to be an error.
UnterminatedBlockComment(&'a str),
/// The end of the input.
End,
}
@@ -360,7 +365,7 @@ fn consume_token(
}
}
(Token::End, "")
(Token::UnterminatedBlockComment(input), "")
}
Some('=') => (Token::AssignmentOperation(cur), chars.as_str()),
_ => (Token::Operation(cur), og_chars),
@@ -1263,3 +1268,11 @@ fn test_doc_comments_module() {
],
);
}
#[test]
fn test_block_comment_unclosed() {
sub_test_with_and_without_doc_comments(
"/** Unclosed Doc Comment",
&[Token::UnterminatedBlockComment("/** Unclosed Doc Comment")],
);
}
+3
View File
@@ -2192,6 +2192,9 @@ impl Parser {
Some(ast::GlobalDeclKind::ConstAssert(condition))
}
(Token::End, _) => return Ok(()),
(Token::UnterminatedBlockComment(_), span) => {
return Err(Box::new(Error::UnterminatedBlockComment(span)))
}
other => {
return Err(Box::new(Error::Unexpected(
other.1,
+15
View File
@@ -5237,3 +5237,18 @@ fn bitwise_shift_errors() {
naga::valid::Capabilities::SHADER_INT64
}
}
#[test]
fn unterminated_block_comment_errors() {
check_success("/* Closed */");
check_error_matches("/* unterminated", "unterminated block comment");
check_error_matches(
"/* unterminated /* terimated inner */",
"unterminated block comment",
);
check_error_matches(
"const N: u32 = 1u; /* Trailing unterminated",
"unterminated block comment",
)
}