luau/Analysis/include/Luau/ControlFlow.h
Amber Grace d00e93c82c
Support Control Flow type Refinements for "break" and "continue" statements (#1004)
Fixes: https://github.com/Roblox/luau/issues/913

This PR adds support for type refinements around guard clauses that use
`break` and `continue` statements inside a loop, similar to how guard
clauses with `return` is supported.

I had some free time today, so I figure I'd give a shot at a naïve fix
for this at the very least.

---

## Resulting Change:

Luau now supports type refinements within loops where a `continue` or
`break` guard clause was used.
For example:
```lua
for _, object in objects :: {{value: string?}} do
    if not object.value then
        continue
    end
    local x: string = object.value -- OK; Used to emit "Type 'string?' could not be converted into 'string'"
end
```

---------

Co-authored-by: Alexander McCord <amccord@roblox.com>
2023-09-21 15:28:42 -07:00

36 lines
688 B
C++

// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#pragma once
#include <memory>
namespace Luau
{
struct Scope;
using ScopePtr = std::shared_ptr<Scope>;
enum class ControlFlow
{
None = 0b00001,
Returns = 0b00010,
Throws = 0b00100,
Breaks = 0b01000,
Continues = 0b10000,
};
inline ControlFlow operator&(ControlFlow a, ControlFlow b)
{
return ControlFlow(int(a) & int(b));
}
inline ControlFlow operator|(ControlFlow a, ControlFlow b)
{
return ControlFlow(int(a) | int(b));
}
inline bool matches(ControlFlow a, ControlFlow b)
{
return (a & b) != ControlFlow(0);
}
} // namespace Luau