diff --git a/src/front/glsl/parser/functions.rs b/src/front/glsl/parser/functions.rs index 35ecfa4833..797e48dea5 100644 --- a/src/front/glsl/parser/functions.rs +++ b/src/front/glsl/parser/functions.rs @@ -42,17 +42,28 @@ impl<'source> ParsingContext<'source> { body: &mut Block, terminator: &mut Option, ) -> Result> { - // TODO: This prevents snippets like the following from working - // ```glsl - // vec4(1.0); - // ``` - // But this would require us to add lookahead to also support - // declarations and since this statement is very unlikely and most - // likely an error, for now we don't support it - if self.peek_type_name(parser) || self.peek_type_qualifier(parser) { + // Type qualifiers always identify a declaration statement + if self.peek_type_qualifier(parser) { return self.parse_declaration(parser, ctx, body, false); } + // Type names can identify either declaration statements or type constructors + // depending on wether the token following the type name is a `(` (LeftParen) + if self.peek_type_name(parser) { + // Start by consuming the type name so that we can peek the token after it + let token = self.bump(parser)?; + // Peek the next token and check if it's a `(` (LeftParen) if so the statement + // is a constructor, otherwise it's a declaration. We need to do the check + // beforehand and not in the if since we will backtrack before the if + let declaration = TokenValue::LeftParen != self.expect_peek(parser)?.value; + + self.backtrack(token)?; + + if declaration { + return self.parse_declaration(parser, ctx, body, false); + } + } + let new_break = || { let mut block = Block::new(); block.push(Statement::Break, crate::Span::default()); diff --git a/tests/in/glsl/expressions.frag b/tests/in/glsl/expressions.frag index 1448d54d80..66a5c0f252 100644 --- a/tests/in/glsl/expressions.frag +++ b/tests/in/glsl/expressions.frag @@ -109,6 +109,10 @@ void testArrayConstructor() { float tree[1] = float[1](0.0); } +void testFreestandingConstructor() { + vec4(1.0); +} + float global; void privatePointer(inout float a) {} diff --git a/tests/out/wgsl/expressions-frag.wgsl b/tests/out/wgsl/expressions-frag.wgsl index 6ed532f023..434d24b6a1 100644 --- a/tests/out/wgsl/expressions-frag.wgsl +++ b/tests/out/wgsl/expressions-frag.wgsl @@ -282,6 +282,10 @@ fn testArrayConstructor() { } +fn testFreestandingConstructor() { + return; +} + fn privatePointer(a_18: ptr) { return; }