Skip to content

Commit d16c723

Browse files
BKDaughertyslyedoc
authored andcommitted
fix(wgsl-in): Handle Unterminated Block Comments (gfx-rs#9356)
1 parent 0f3c83c commit d16c723

6 files changed

Lines changed: 44 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Bottom level categories:
8989
- 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).
9090
- 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).
9191
- 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).
92+
- Fixed handling of unterminated block comments. By @BKDaugherty in [#9356](https://github.com/gfx-rs/wgpu/pull/9356).
9293

9394
#### dx12
9495

cts_runner/test.lst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ webgpu:shader,validation,parse,blankspace:bom:*
447447
webgpu:shader,validation,parse,blankspace:null_characters:contains_null=false;*
448448
webgpu:shader,validation,parse,blankspace:null_characters:contains_null=true;placement="delimiter"
449449
webgpu:shader,validation,parse,blankspace:null_characters:contains_null=true;placement="eol"
450+
webgpu:shader,validation,parse,comments:unterminated_block_comment:terminated=false
450451
webgpu:shader,validation,parse,enable:*
451452
webgpu:shader,validation,parse,identifiers:*
452453
webgpu:shader,validation,parse,requires:*

naga/src/front/wgsl/error.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ pub(crate) enum Error<'a> {
442442
},
443443
UnexpectedExprForTypeExpression(Span),
444444
MissingIncomingPayload(Span),
445+
UnterminatedBlockComment(Span),
445446
}
446447

447448
impl From<ConflictingDiagnosticRuleError> for Error<'_> {
@@ -516,6 +517,7 @@ impl<'a> Error<'a> {
516517
Token::DocComment(s) => format!("doc comment ('{s}')"),
517518
Token::ModuleDocComment(s) => format!("module doc comment ('{s}')"),
518519
Token::End => "end".to_string(),
520+
Token::UnterminatedBlockComment(s) => format!("unterminated doc comment ('{s}'")
519521
},
520522
ExpectedToken::Identifier => "identifier".to_string(),
521523
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> {
15111513
)],
15121514
notes: vec![],
15131515
},
1516+
Error::UnterminatedBlockComment(span) => ParseError {
1517+
message: "unterminated block comment".to_string(),
1518+
labels: vec![(
1519+
span,
1520+
"must be closed with `*/`".into(),
1521+
)],
1522+
notes: vec![],
1523+
}
15141524
}
15151525
}
15161526
}

naga/src/front/wgsl/parse/lexer.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ pub enum Token<'a> {
8989
/// A module-level doc comment, beginning with `//!` or `/*!`.
9090
ModuleDocComment(&'a str),
9191

92+
/// A block comment that is incomplete, and has not been closed with */.
93+
///
94+
/// It's expected that the parser will consider this to be an error.
95+
UnterminatedBlockComment(&'a str),
96+
9297
/// The end of the input.
9398
End,
9499
}
@@ -360,7 +365,7 @@ fn consume_token(
360365
}
361366
}
362367

363-
(Token::End, "")
368+
(Token::UnterminatedBlockComment(input), "")
364369
}
365370
Some('=') => (Token::AssignmentOperation(cur), chars.as_str()),
366371
_ => (Token::Operation(cur), og_chars),
@@ -1263,3 +1268,11 @@ fn test_doc_comments_module() {
12631268
],
12641269
);
12651270
}
1271+
1272+
#[test]
1273+
fn test_block_comment_unclosed() {
1274+
sub_test_with_and_without_doc_comments(
1275+
"/** Unclosed Doc Comment",
1276+
&[Token::UnterminatedBlockComment("/** Unclosed Doc Comment")],
1277+
);
1278+
}

naga/src/front/wgsl/parse/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,6 +2192,9 @@ impl Parser {
21922192
Some(ast::GlobalDeclKind::ConstAssert(condition))
21932193
}
21942194
(Token::End, _) => return Ok(()),
2195+
(Token::UnterminatedBlockComment(_), span) => {
2196+
return Err(Box::new(Error::UnterminatedBlockComment(span)))
2197+
}
21952198
other => {
21962199
return Err(Box::new(Error::Unexpected(
21972200
other.1,

naga/tests/naga/wgsl_errors.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5237,3 +5237,18 @@ fn bitwise_shift_errors() {
52375237
naga::valid::Capabilities::SHADER_INT64
52385238
}
52395239
}
5240+
5241+
#[test]
5242+
fn unterminated_block_comment_errors() {
5243+
check_success("/* Closed */");
5244+
5245+
check_error_matches("/* unterminated", "unterminated block comment");
5246+
check_error_matches(
5247+
"/* unterminated /* terimated inner */",
5248+
"unterminated block comment",
5249+
);
5250+
check_error_matches(
5251+
"const N: u32 = 1u; /* Trailing unterminated",
5252+
"unterminated block comment",
5253+
)
5254+
}

0 commit comments

Comments
 (0)