diff --git a/src/rewriter.fs b/src/rewriter.fs index 6fec8fb9..daf9c003 100644 --- a/src/rewriter.fs +++ b/src/rewriter.fs @@ -478,7 +478,7 @@ module private RewriterImpl = let simplifyStmt = function | Block [] as e -> e | Block b -> match simplifyBlock b with - | [stmt] -> stmt + | [stmt] as b when hasNoDecl b -> stmt | stmts -> Block stmts | Decl (ty, li) -> Decl (rwType ty, declsNotToInline li) | ForD ((ty, d), cond, inc, body) -> ForD((rwType ty, declsNotToInline d), cond, inc, squeezeBlockWithComma body) diff --git a/tests/unit/shadowing.frag b/tests/unit/shadowing.frag index 7cafb283..bbca6780 100644 --- a/tests/unit/shadowing.frag +++ b/tests/unit/shadowing.frag @@ -12,4 +12,51 @@ float g(float f) f+=g; } return f; +} + +int m1() +{ + int x = 1; + if (rand() >.5) + { + int x = 2, y = x; // y is initialized to 2 + return y; + } +} + +struct S{ int x; }; + +S m2() +{ + S S = S(0); // 'S' is only visible as a struct and constructor + return S; // 'S' is now visible as a variable +} + +void m4(int k) +{ + //int k = k + 3; // redeclaration error of the name k + { + int k = k + 3; // 2nd k is parameter, initializing nested first k + int m = k; // use of new k, which is hiding the parameter + } + return k; + + //int x = x; // Error if x has not been previously defined. + // If the previous definition of x was in this + // same scope, this causes a redeclaration error. +} + +void m5(float k) +{ + float m = 9.; + m = 14.; + { + float k = k + 3.; // 2nd k is parameter, initializing nested first k + m = k; // use of new k, which is hiding the parameter + } + return k + 2. * m; +} +void m6() +{ + m5(55.); } \ No newline at end of file diff --git a/tests/unit/shadowing.frag.expected b/tests/unit/shadowing.frag.expected index 7e840d30..167f2e64 100644 --- a/tests/unit/shadowing.frag.expected +++ b/tests/unit/shadowing.frag.expected @@ -13,3 +13,28 @@ float g(float f) } return f; } +int m1() +{ + if(rand()>.5) + return 2; +} +struct S{int x;}; +S m2() +{ + return S(0); +} +void m4(int k) +{ + return k; +} +void m5() +{ + float m=9.; + m=14.; + m=58.; + return 55.+2.*m; +} +void m6() +{ + m5(); +}