<feed xmlns='http://www.w3.org/2005/Atom'>
<title>slang.git/tests/bugs/paren-insertion-bug.slang.expected.txt, branch master</title>
<subtitle>Making it easier to work with shaders</subtitle>
<id>https://git.yummers.dev/slang.git/atom?h=master</id>
<link rel='self' href='https://git.yummers.dev/slang.git/atom?h=master'/>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/'/>
<updated>2018-10-31T14:01:07+00:00</updated>
<entry>
<title>Fix a precedence bug in code emit (#705)</title>
<updated>2018-10-31T14:01:07+00:00</updated>
<author>
<name>Tim Foley</name>
<email>tfoleyNV@users.noreply.github.com</email>
</author>
<published>2018-10-31T14:01:07+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=2993dd8a49d912e04d5d45dcc9a5757d32d2ba7a'/>
<id>urn:sha1:2993dd8a49d912e04d5d45dcc9a5757d32d2ba7a</id>
<content type='text'>
* Fix a precedence bug in code emit

Given code like the following:

```hlsl
float a = ...;
float3 b = pow(a, 2.0);
float3 c = b.xyz;
```

There is an implicit cast from `float` to `float3` in the computation of `b`, that Slang will always make explicit in the output.
Slang will also tend to pull the computation of `b` into the next expression if it has no other use sites in the same function.

When it does, the compiler was failing to parenthesize the result correctly, and yielded (more or less):

```hlsl
float a = ...;
float3 c = (float3) pow(a,2.0).xyz;
```

As you can see, the swizzle ended up attached to the `pow()` call instead of the cast, and the downstream compiler luckily complained that we couldn't apply an `.xyz` swizzle to a scalar value.

This change adds the missing parentheses-insertion logic for that case of emitting a cast expression, so that we instead get:

```hlsl
float a = ...;
float3 c = ((float3) pow(a,2.0)).xyz;
```

I added a test case to catch this specific issue, but there is of course no guarantee that we haven't missed other cases in the emit logic. This is why I held out so long on getting to the "why so many parentheses?" complaints...

* remove commented-out code from test program
</content>
</entry>
</feed>
