From 7449bbf4e10b5db5b311d11a9ffa698252ee4aa0 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Tue, 10 Oct 2023 14:44:27 -0700 Subject: [PATCH] fixup! add an expression constness tracker --- src/front/glsl/parser/declarations.rs | 27 ++++++++++++------- .../out/wgsl/local-var-init-in-loop.comp.wgsl | 5 ++-- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/front/glsl/parser/declarations.rs b/src/front/glsl/parser/declarations.rs index b53616b8f4..02ee2bedea 100644 --- a/src/front/glsl/parser/declarations.rs +++ b/src/front/glsl/parser/declarations.rs @@ -246,19 +246,26 @@ impl<'source> ParsingContext<'source> { }) .transpose()?; - let (decl_initializer, late_initializer) = if is_global_const { - (init, None) + let decl_initializer; + let late_initializer; + if is_global_const { + decl_initializer = init; + late_initializer = None; } else if ctx.external { - let decl_initializer = + decl_initializer = init.and_then(|expr| ctx.ctx.lift_up_const_expression(expr).ok()); - (decl_initializer, None) + late_initializer = None; + } else if let Some(init) = init { + if ctx.is_inside_loop || !ctx.ctx.expression_constness.is_const(init) { + decl_initializer = None; + late_initializer = Some(init); + } else { + decl_initializer = Some(init); + late_initializer = None; + } } else { - let decl_initializer = - init.filter(|expr| ctx.ctx.expression_constness.is_const(*expr)); - let late_initializer = (decl_initializer.is_none() || ctx.is_inside_loop) - .then_some(init) - .flatten(); - (decl_initializer, late_initializer) + decl_initializer = None; + late_initializer = None; }; let pointer = ctx.add_var(frontend, ty, name, decl_initializer, meta)?; diff --git a/tests/out/wgsl/local-var-init-in-loop.comp.wgsl b/tests/out/wgsl/local-var-init-in-loop.comp.wgsl index 750e32e6d3..7675a54cef 100644 --- a/tests/out/wgsl/local-var-init-in-loop.comp.wgsl +++ b/tests/out/wgsl/local-var-init-in-loop.comp.wgsl @@ -1,7 +1,7 @@ fn main_1() { var sum: vec4 = vec4(0.0); var i: i32 = 0; - var a: vec4 = vec4(1.0); + var a: vec4; loop { let _e6 = i; @@ -9,8 +9,7 @@ fn main_1() { break; } { - let _e15 = vec4(1.0); - a = _e15; + a = vec4(1.0); let _e17 = sum; let _e18 = a; sum = (_e17 + _e18);