From 54b3da95f808f14b913cbfe8132e7977246f443b Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Sun, 26 Jun 2022 14:38:30 +0000 Subject: [PATCH] tint/transform: Handle 'const' for Unshadow. Bug: tint:1580 Change-Id: I0af473ac13abde1f1890851aeb92812be6a52933 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/94682 Reviewed-by: David Neto Commit-Queue: Ben Clayton Kokoro: Kokoro --- src/tint/transform/unshadow.cc | 3 + src/tint/transform/unshadow_test.cc | 279 +++++++++++++++++++++++++++- 2 files changed, 279 insertions(+), 3 deletions(-) diff --git a/src/tint/transform/unshadow.cc b/src/tint/transform/unshadow.cc index 6f0292b172..952a88b378 100644 --- a/src/tint/transform/unshadow.cc +++ b/src/tint/transform/unshadow.cc @@ -63,6 +63,9 @@ struct Unshadow::State { [&](const ast::Let*) { return ctx.dst->Let(source, symbol, type, constructor, attributes); }, + [&](const ast::Const*) { + return ctx.dst->Const(source, symbol, type, constructor, attributes); + }, [&](const ast::Parameter*) { return ctx.dst->Param(source, symbol, type, attributes); }, diff --git a/src/tint/transform/unshadow_test.cc b/src/tint/transform/unshadow_test.cc index 30e1db55e5..271626ab17 100644 --- a/src/tint/transform/unshadow_test.cc +++ b/src/tint/transform/unshadow_test.cc @@ -34,14 +34,16 @@ TEST_F(UnshadowTest, Noop) { auto* src = R"( var a : i32; -let b : i32 = 1; +const b : i32 = 1; fn F(c : i32) { var d : i32; let e : i32 = 1; + const f : i32 = 2; { - var f : i32; - let g : i32 = 1; + var g : i32; + let h : i32 = 1; + const i : i32 = 2; } } )"; @@ -64,6 +66,10 @@ fn X() { fn Y() { let a = true; } + +fn Z() { + const a = true; +} )"; auto* expect = R"( @@ -76,6 +82,10 @@ fn X() { fn Y() { let a_2 = true; } + +fn Z() { + const a_3 = true; +} )"; auto got = Run(src); @@ -93,6 +103,10 @@ fn Y() { let a = true; } +fn Z() { + const a = true; +} + type a = i32; )"; @@ -105,6 +119,10 @@ fn Y() { let a_2 = true; } +fn Z() { + const a_3 = true; +} + type a = i32; )"; @@ -126,6 +144,10 @@ fn X() { fn Y() { let a = false; } + +fn Z() { + const a = false; +} )"; auto* expect = R"( @@ -140,6 +162,10 @@ fn X() { fn Y() { let a_2 = false; } + +fn Z() { + const a_3 = false; +} )"; auto got = Run(src); @@ -157,6 +183,10 @@ fn Y() { let a = false; } +fn Z() { + const a = false; +} + struct a { m : i32, }; @@ -172,6 +202,10 @@ fn Y() { let a_2 = false; } +fn Z() { + const a_3 = false; +} + struct a { m : i32, } @@ -187,11 +221,19 @@ TEST_F(UnshadowTest, LocalShadowsFunction) { fn a() { var a = true; var b = false; + var c = true; } fn b() { let a = true; let b = false; + let c = true; +} + +fn c() { + const a = true; + const b = false; + const c = true; } )"; @@ -199,11 +241,19 @@ fn b() { fn a() { var a_1 = true; var b_1 = false; + var c_1 = true; } fn b() { let a_2 = true; let b_2 = false; + let c_2 = true; +} + +fn c() { + const a_3 = true; + const b_3 = false; + const c_3 = true; } )"; @@ -217,24 +267,39 @@ TEST_F(UnshadowTest, LocalShadowsFunction_OutOfOrder) { fn b() { let a = true; let b = false; + let c = true; } fn a() { var a = true; var b = false; + var c = true; } +fn c() { + const a = true; + const b = false; + const c = true; +} )"; auto* expect = R"( fn b() { let a_1 = true; let b_1 = false; + let c_1 = true; } fn a() { var a_2 = true; var b_2 = false; + var c_2 = true; +} + +fn c() { + const a_3 = true; + const b_3 = false; + const c_3 = true; } )"; @@ -254,6 +319,10 @@ fn X() { fn Y() { let a = (a == 321); } + +fn Z() { + const a = 321; +} )"; auto* expect = R"( @@ -266,6 +335,10 @@ fn X() { fn Y() { let a_2 = (a == 321); } + +fn Z() { + const a_3 = 321; +} )"; auto got = Run(src); @@ -283,6 +356,10 @@ fn Y() { let a = (a == 321); } +fn Z() { + const a = 321; +} + var a : i32; )"; @@ -295,6 +372,10 @@ fn Y() { let a_2 = (a == 321); } +fn Z() { + const a_3 = 321; +} + var a : i32; )"; @@ -314,6 +395,10 @@ fn X() { fn Y() { let a = (a == 321); } + +fn Z() { + const a = 321; +} )"; auto* expect = R"( @@ -326,6 +411,10 @@ fn X() { fn Y() { let a_2 = (a == 321); } + +fn Z() { + const a_3 = 321; +} )"; auto got = Run(src); @@ -343,6 +432,10 @@ fn Y() { let a = (a == 321); } +fn Z() { + const a = 321; +} + let a : i32 = 1; )"; @@ -355,6 +448,10 @@ fn Y() { let a_2 = (a == 321); } +fn Z() { + const a_3 = 321; +} + let a : i32 = 1; )"; @@ -363,6 +460,82 @@ let a : i32 = 1; EXPECT_EQ(expect, str(got)); } +TEST_F(UnshadowTest, LocalShadowsGlobalConst) { + auto* src = R"( +const a : i32 = 1; + +fn X() { + var a = (a == 123); +} + +fn Y() { + let a = (a == 321); +} + +fn Z() { + const a = a; +} +)"; + + auto* expect = R"( +const a : i32 = 1; + +fn X() { + var a_1 = (a == 123); +} + +fn Y() { + let a_2 = (a == 321); +} + +fn Z() { + const a_3 = a; +} +)"; + + auto got = Run(src); + + EXPECT_EQ(expect, str(got)); +} + +TEST_F(UnshadowTest, LocalShadowsGlobalConst_OutOfOrder) { + auto* src = R"( +fn X() { + var a = (a == 123); +} + +fn Y() { + let a = (a == 321); +} + +fn Z() { + const a = a; +} + +const a : i32 = 1; +)"; + + auto* expect = R"( +fn X() { + var a_1 = (a == 123); +} + +fn Y() { + let a_2 = (a == 321); +} + +fn Z() { + const a_3 = a; +} + +const a : i32 = 1; +)"; + + auto got = Run(src); + + EXPECT_EQ(expect, str(got)); +} + TEST_F(UnshadowTest, LocalShadowsLocalVar) { auto* src = R"( fn X() { @@ -373,6 +546,9 @@ fn X() { { let a = (a == 321); } + { + const a = 321; + } } )"; @@ -385,6 +561,9 @@ fn X() { { let a_2 = (a == 321); } + { + const a_3 = 321; + } } )"; @@ -403,6 +582,9 @@ fn X() { { let a = (a == 321); } + { + const a = 321; + } } )"; @@ -415,6 +597,45 @@ fn X() { { let a_2 = (a == 321); } + { + const a_3 = 321; + } +} +)"; + + auto got = Run(src); + + EXPECT_EQ(expect, str(got)); +} + +TEST_F(UnshadowTest, LocalShadowsLocalConst) { + auto* src = R"( +fn X() { + const a = 1; + { + var a = (a == 123); + } + { + let a = (a == 321); + } + { + const a = a; + } +} +)"; + + auto* expect = R"( +fn X() { + const a = 1; + { + var a_1 = (a == 123); + } + { + let a_2 = (a == 321); + } + { + const a_3 = a; + } } )"; @@ -432,6 +653,9 @@ fn F(a : i32) { { let a = (a == 321); } + { + const a = 321; + } } )"; @@ -443,6 +667,9 @@ fn F(a : i32) { { let a_2 = (a == 321); } + { + const a_3 = 321; + } } )"; @@ -460,6 +687,9 @@ fn a(a : i32) { { let a = (a == 321); } + { + const a = 321; + } } )"; @@ -471,6 +701,9 @@ fn a(a_1 : i32) { { let a_3 = (a_1 == 321); } + { + const a_4 = 321; + } } )"; @@ -539,6 +772,46 @@ let a : i32 = 1; EXPECT_EQ(expect, str(got)); } +TEST_F(UnshadowTest, ParamShadowsGlobalConst) { + auto* src = R"( +const a : i32 = 1; + +fn F(a : bool) { +} +)"; + + auto* expect = R"( +const a : i32 = 1; + +fn F(a_1 : bool) { +} +)"; + + auto got = Run(src); + + EXPECT_EQ(expect, str(got)); +} + +TEST_F(UnshadowTest, ParamShadowsGlobalConst_OutOfOrder) { + auto* src = R"( +fn F(a : bool) { +} + +const a : i32 = 1; +)"; + + auto* expect = R"( +fn F(a_1 : bool) { +} + +const a : i32 = 1; +)"; + + auto got = Run(src); + + EXPECT_EQ(expect, str(got)); +} + TEST_F(UnshadowTest, ParamShadowsAlias) { auto* src = R"( type a = i32;