# compiler: do not propagate result type to \`try\` operand · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/3924f173af51c50ac18be2166fe115555241778e)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 3924f173af51c50ac18be2166fe115555241778e

Requested commit: 3924f173af51c50ac18be2166fe115555241778e

Commit: 3924f173af51c50ac18be2166fe115555241778e

Tree: ee3fa65cc85dd6022af6533068dc3fb4f46461ab

Author: mlugg

Committer: Matthew Lugg

## Message

```
compiler: do not propagate result type to `try` operand

This commit effectively reverts 9e683f0, and hence un-accepts #19777.
While nice in theory, this proposal turned out to have a few problems.

Firstly, supplying a result type implicitly coerces the operand to this
type -- that's the main point of result types! But for `try`, this is
actually a bad idea; we want a redundant `try` to be a compile error,
not to silently coerce the non-error value to an error union. In
practice, this didn't always happen, because the implementation was
buggy anyway; but when it did, it was really quite silly. For instance,
`try try ... try .{ ... }` was an accepted expression, with the inner
initializer being initially coerced to `E!E!...E!T`.

Secondly, the result type inference here didn't play nicely with
`return`. If you write `return try`, the operand would actually receive
a result type of `E!E!T`, since the `return` gave a result type of `E!T`
and the `try` wrapped it in *another* error union. More generally, the
problem here is that `try` doesn't know when it should or shouldn't
nest error unions. This occasionally broke code which looked like it
should work.

So, this commit prevents `try` from propagating result types through to
its operand. A key motivation for the original proposal here was decl
literals; so, as a special case, `try .foo(...)` is still an allowed
syntax form, caught by AstGen and specially lowered. This does open the
doors to allowing other special cases for decl literals in future, such
as `.foo(...) catch ...`, but those proposals are for another time.

Resolves: #21991
Resolves: #22633

```

## Parents

- [c225b780e3944014300555c3b45eaccd7bc7c8ae](https://git.cafe/gitcafe/zig/commit/c225b780e3944014300555c3b45eaccd7bc7c8ae?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/3924f173af51c50ac18be2166fe115555241778e?format=markdown)
