<feed xmlns='http://www.w3.org/2005/Atom'>
<title>slang.git/tests/diagnostics/setter-method.slang.expected, 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>2025-07-15T23:39:22+00:00</updated>
<entry>
<title>Emit additional diagnostic for invalid pointer taking operations (#7663)</title>
<updated>2025-07-15T23:39:22+00:00</updated>
<author>
<name>Gangzheng Tong</name>
<email>tonggangzheng@gmail.com</email>
</author>
<published>2025-07-15T23:39:22+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=21a66267c661a55c8ad27248c0765276dd6f72ea'/>
<id>urn:sha1:21a66267c661a55c8ad27248c0765276dd6f72ea</id>
<content type='text'>
* Emit special diagnostic for invalid pointer taking operations

* Update source/slang/slang-diagnostic-defs.h

Co-authored-by: ArielG-NV &lt;159081215+ArielG-NV@users.noreply.github.com&gt;

* Add OperatorAddressOf KnownBuiltin modifier

* update error message for non-l-value assignment

* update the diagnostics in the tests

* Use enum based KnownBuiltinDeclName

* format code (#7772)

Co-authored-by: slangbot &lt;186143334+slangbot@users.noreply.github.com&gt;

---------

Co-authored-by: ArielG-NV &lt;159081215+ArielG-NV@users.noreply.github.com&gt;
Co-authored-by: slangbot &lt;ellieh+slangbot@nvidia.com&gt;
Co-authored-by: slangbot &lt;186143334+slangbot@users.noreply.github.com&gt;</content>
</entry>
<entry>
<title>Diagnostic location highlighting (#1700)</title>
<updated>2021-02-12T19:31:56+00:00</updated>
<author>
<name>jsmall-nvidia</name>
<email>jsmall@nvidia.com</email>
</author>
<published>2021-02-12T19:31:56+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=369279e91dde1b056d8d0e3bb83e7ba3f96321af'/>
<id>urn:sha1:369279e91dde1b056d8d0e3bb83e7ba3f96321af</id>
<content type='text'>
* #include an absolute path didn't work - because paths were taken to always be relative.

* WIP: First pass in supporting output of line error information.

* Add support for lexing to better be able to indicate SourceLocation information.

* Fix lexer usage in DiagnosticSink in C++ extractor.

* Update diagnostics tests to have line location info.

* Fixed test expected output that now have source location information in them.

* Better handling of tab.

* Fix test expected results for tabbing change.

* DiagnosticLexer -&gt; DiagnosticSink::SourceLocationLexer
Added line continuation tests.

* Fix typo.

* Added String::appendRepeatedChar

* Change to rerun tests.

Co-authored-by: Tim Foley &lt;tfoleyNV@users.noreply.github.com&gt;</content>
</entry>
<entry>
<title>Add basic support for [mutating] methods (#667)</title>
<updated>2018-10-11T16:20:10+00:00</updated>
<author>
<name>Tim Foley</name>
<email>tfoleyNV@users.noreply.github.com</email>
</author>
<published>2018-10-11T16:20:10+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=dcd9f78b972a4b7b88e9c3403bd1e887d628b40a'/>
<id>urn:sha1:dcd9f78b972a4b7b88e9c3403bd1e887d628b40a</id>
<content type='text'>
By default, when writing a "method" (aka "member function") in Slang, the `this` parameter is implicitly an `in` parameter. So this:

```hlsl
struct Foo
{
    int state;
    int getState() { return state; }
    void setState(int s) { state = s; }
};
```

is desugared into something like this:

```hlsl
struct Foo { int state };
int Foo_getState(Foo this) { return this.state; }

// BAD:
void Foo_setState(Foo this, int s) { this.state = s; }
```

That "setter" doesn't really do what was intended. It modifies a local copy of type `Foo`, because `in` parameters in HLSL represent by-value copy-in semantics, and are mutable in the body the function. Slang was updated to give a static error on the original code to catch this kind of mistake (so that `this` parameters are unlike ordinary function parameters, and no longer mutable).

Of course, sometimes users *want* a mutable `this` parameter. Rather than make a mutable `this` the default (there are arguments both for and against this), this change adds a new attribute `[mutating]` that can be put on a method (member function) to indicate that its `this` parameter should be an `in out` parameter:

```hlsl
[mutating] void setState(int s) { state = s; }
```

The above will translate to, more or less:

```hlsl
void Foo_setState(inout Foo this, int s) { this.state = s; }
```

One added detail is that `[mutating]` can also be used on interface requirements, with the same semantics. A `[mutating]` requirement can be satisfied with a `[mutating]` or non-`[mutating]` method, while a non-`[mutating]` requirement can't be satisfied with a `[mutating]` method (the call sites would not expect mutation to happen).

The design of `[mutating]` here is heavily influenced by the equivalent `mutating` keyword in Swift.

Notes on the implementation:

* Adding the new attribute was straightforward using the existing support, but I had to change around where attributes get checked in the overall sequencing of static checks, because attributes were being checked *after* function bodies, but with this change I need to look at semantically-checked attributes to determine the mutability of `this`

* The check to restrict it so that `[mutating]` methods cannot satisfy non-`[mutating]` requirements was easy to add, but it points out the fact that there is a huge TODO comment where the actual checking of method *signatures* is supposed to happen. That is a bug waiting to bite users and needs to be fixed!

* While we had special-case logic to detect attempts to modify state accessed through an immutable `this` (e.g., `this.state = s`), that logic didn't trigger when the mutation happened through a function/operator call (e.g., `this.state += s`), so this change factors out the validation logic for that case and calls through to it from both the assignment and `out` argument cases.

* The error message for the special-case check was updated to note that the user could apply `[mutating]` to their function declaration to get rid of the error.

* The semantic checking logic for an explicit `this` expression was already walking up through the scopes (created during parsing) and looking for a scope that represents an outer type declaration that `this` might be referring to. We simply extend it to note when it passes through the scope for a function or similar declaration (`FunctionDeclBase`) and check for the `[mutating]` attribute. If the attribute is seen, it returns a mutable `this` expression, and otherwise leaves it immutable.

* The IR lowering logic then needed to be updated so that when adding an IR-level parameter to represent `this`, it gives it the appropriate "direction" based on the attributes of the function declaration being lowered. The rest of the IR logic works as-is, because it will treat `this` just like an other parameter (whether it is `in` or `inout`).

* This biggest chunk of work was the "implicit `this`" case, because ordinary name lookup may resolve an expression like `state` into `this.state`, so that the `this` expression comes out of "thin air." To handle this case, I extended the structure of the "breadcrumbs" that come along with a lookup result (the breadcrumbs are used for any case where a single identifier like `state` needs to be embellished to a more complex expression as a result of lookup), so that it can identify whether a `Breadcrumb::Kind::This` node comes from a `[mutating]` context or not. Similar to the logic for an explicit `this`, we handle this by noting when we pass through a `FunctionDeclBase` when moving up through scopes, and look for the `[mutating]` attribute on it. The rest of the work was just plumbing the additional state through.</content>
</entry>
<entry>
<title>Diagnose attempts to write to fields in methods (#530)</title>
<updated>2018-05-02T00:26:20+00:00</updated>
<author>
<name>Tim Foley</name>
<email>tfoleyNV@users.noreply.github.com</email>
</author>
<published>2018-05-02T00:26:20+00:00</published>
<link rel='alternate' type='text/html' href='https://git.yummers.dev/slang.git/commit/?id=d90d73a66d6d5c665736f58096071965734fd15b'/>
<id>urn:sha1:d90d73a66d6d5c665736f58096071965734fd15b</id>
<content type='text'>
* Diagnose attempts to write to fields in methods

Work on #529

This helps to avoid the case where a Slang user writes a struct with helpful `setter` methods, and finds that it doesn't work as expected because the `this` parameter is currently handled like an `in` parameter (passed by value, but mutable in the callee).

Fixing this issue actually involved making a more broad fix to how l-value-ness is propagated. The existing checking logic was assuming that l-value-ness is just a property of a particular member declaration (e.g., a field is either mutable or not), and didn't take into account whether the "base expression" was mutable. This change fixes that oversight, which might lead to additional errors being issued if we aren't correctly making things mutable when we should.

A `ThisExpr` was already immutable by default, so that part didn't actually need to change. Just propagating its immutability through was enough.

As an additional assistance to users, I have added an extra diagnostic that triggers when a "destination of assignment is not an l-value" error occurs and the left-hand-side expression seems to be based on `this` (whether implicitly or explicitly). This will ideally help users to understand that the "setter" idiom is not yet supported.

* Fixed setRadius typo
</content>
</entry>
</feed>
