Skip to content

Commit

Permalink
Sync to upstream/release/556 (#782)
Browse files Browse the repository at this point in the history
* The AST JSON encoder will now stringify infinity and NaN according to the JSON5 spec using the tokens `Infinity`, `-Infinity`, and `NaN`.
* Improve autocompletion of table keys if the type of that key is a union of string singletons.
  • Loading branch information
andyfriesen authored Dec 9, 2022
1 parent e9d4ee7 commit fb2f146
Show file tree
Hide file tree
Showing 53 changed files with 1,097 additions and 918 deletions.
9 changes: 7 additions & 2 deletions Analysis/include/Luau/BuiltinDefinitions.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#pragma once

#include "Luau/Frontend.h"
#include "Luau/Scope.h"
#include "Luau/TypeInfer.h"
#include "Luau/TypeVar.h"

#include <optional>

namespace Luau
{

struct Frontend;
struct TypeChecker;
struct TypeArena;

void registerBuiltinTypes(Frontend& frontend);

void registerBuiltinGlobals(TypeChecker& typeChecker);
Expand Down
7 changes: 7 additions & 0 deletions Analysis/include/Luau/Constraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "Luau/Ast.h" // Used for some of the enumerations
#include "Luau/Def.h"
#include "Luau/DenseHash.h"
#include "Luau/NotNull.h"
#include "Luau/TypeVar.h"
#include "Luau/Variant.h"
Expand Down Expand Up @@ -67,6 +68,12 @@ struct BinaryConstraint
TypeId leftType;
TypeId rightType;
TypeId resultType;

// When we dispatch this constraint, we update the key at this map to record
// the overload that we selected.
AstExpr* expr;
DenseHashMap<const AstExpr*, TypeId>* astOriginalCallTypes;
DenseHashMap<const AstExpr*, TypeId>* astOverloadResolvedTypes;
};

// iteratee is iterable
Expand Down
12 changes: 12 additions & 0 deletions Analysis/include/Luau/ConstraintGraphBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,27 @@ struct ConstraintGraphBuilder

// A mapping of AST node to TypeId.
DenseHashMap<const AstExpr*, TypeId> astTypes{nullptr};

// A mapping of AST node to TypePackId.
DenseHashMap<const AstExpr*, TypePackId> astTypePacks{nullptr};

// If the node was applied as a function, this is the unspecialized type of
// that expression.
DenseHashMap<const AstExpr*, TypeId> astOriginalCallTypes{nullptr};

// If overload resolution was performed on this element, this is the
// overload that was selected.
DenseHashMap<const AstExpr*, TypeId> astOverloadResolvedTypes{nullptr};

// Types resolved from type annotations. Analogous to astTypes.
DenseHashMap<const AstType*, TypeId> astResolvedTypes{nullptr};

// Type packs resolved from type annotations. Analogous to astTypePacks.
DenseHashMap<const AstTypePack*, TypePackId> astResolvedTypePacks{nullptr};

// Defining scopes for AST nodes.
DenseHashMap<const AstStatTypeAlias*, ScopePtr> astTypeAliasDefiningScopes{nullptr};

NotNull<const DataFlowGraph> dfg;
ConnectiveArena connectiveArena;

Expand Down
9 changes: 5 additions & 4 deletions Analysis/include/Luau/ConstraintSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

#pragma once

#include "Luau/Error.h"
#include "Luau/Variant.h"
#include "Luau/Constraint.h"
#include "Luau/TypeVar.h"
#include "Luau/ToString.h"
#include "Luau/Error.h"
#include "Luau/Module.h"
#include "Luau/Normalize.h"
#include "Luau/ToString.h"
#include "Luau/TypeVar.h"
#include "Luau/Variant.h"

#include <vector>

Expand Down
6 changes: 3 additions & 3 deletions Analysis/include/Luau/Error.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#pragma once

#include "Luau/FileResolver.h"
#include "Luau/Location.h"
#include "Luau/TypeVar.h"
#include "Luau/Variant.h"
#include "Luau/TypeArena.h"

namespace Luau
{
struct TypeError;

struct FileResolver;
struct TypeArena;
struct TypeError;

struct TypeMismatch
{
Expand Down
1 change: 1 addition & 0 deletions Analysis/include/Luau/Linter.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Luau/Location.h"

#include <memory>
#include <string>
#include <vector>

namespace Luau
Expand Down
22 changes: 5 additions & 17 deletions Analysis/include/Luau/Normalize.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class TypeIds
void retain(const TypeIds& tys);
void clear();

TypeId front() const;
iterator begin();
iterator end();
const_iterator begin() const;
Expand Down Expand Up @@ -107,18 +108,7 @@ namespace Luau
/** A normalized string type is either `string` (represented by `nullopt`) or a
* union of string singletons.
*
* When FFlagLuauNegatedStringSingletons is unset, the representation is as
* follows:
*
* * The `string` data type is represented by the option `singletons` having the
* value `std::nullopt`.
* * The type `never` is represented by `singletons` being populated with an
* empty map.
* * A union of string singletons is represented by a map populated by the names
* and TypeIds of the singletons contained therein.
*
* When FFlagLuauNegatedStringSingletons is set, the representation is as
* follows:
* The representation is as follows:
*
* * A union of string singletons is finite and includes the singletons named by
* the `singletons` field.
Expand All @@ -138,9 +128,7 @@ struct NormalizedStringType
// eg string & ~"a" & ~"b" & ...
bool isCofinite = false;

// TODO: This field cannot be nullopt when FFlagLuauNegatedStringSingletons
// is set. When clipping that flag, we can remove the wrapping optional.
std::optional<std::map<std::string, TypeId>> singletons;
std::map<std::string, TypeId> singletons;

void resetToString();
void resetToNever();
Expand All @@ -161,8 +149,8 @@ struct NormalizedStringType

static const NormalizedStringType never;

NormalizedStringType() = default;
NormalizedStringType(bool isCofinite, std::optional<std::map<std::string, TypeId>> singletons);
NormalizedStringType();
NormalizedStringType(bool isCofinite, std::map<std::string, TypeId> singletons);
};

bool isSubtype(const NormalizedStringType& subStr, const NormalizedStringType& superStr);
Expand Down
1 change: 0 additions & 1 deletion Analysis/include/Luau/Scope.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#pragma once

#include "Luau/Constraint.h"
#include "Luau/Location.h"
#include "Luau/NotNull.h"
#include "Luau/TypeVar.h"
Expand Down
26 changes: 22 additions & 4 deletions Analysis/include/Luau/ToString.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,35 @@
#pragma once

#include "Luau/Common.h"
#include "Luau/TypeVar.h"
#include "Luau/ConstraintGraphBuilder.h"

#include <unordered_map>
#include <optional>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>

LUAU_FASTINT(LuauTableTypeMaximumStringifierLength)
LUAU_FASTINT(LuauTypeMaximumStringifierLength)

namespace Luau
{

class AstExpr;

struct Scope;

struct TypeVar;
using TypeId = const TypeVar*;

struct TypePackVar;
using TypePackId = const TypePackVar*;

struct FunctionTypeVar;
struct Constraint;

struct Position;
struct Location;

struct ToStringNameMap
{
std::unordered_map<TypeId, std::string> typeVars;
Expand Down Expand Up @@ -125,4 +140,7 @@ std::string dump(const std::shared_ptr<Scope>& scope, const char* name);

std::string generateName(size_t n);

std::string toString(const Position& position);
std::string toString(const Location& location);

} // namespace Luau
6 changes: 3 additions & 3 deletions Analysis/include/Luau/TxnLog.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#pragma once

#include <memory>
#include <unordered_map>

#include "Luau/TypeVar.h"
#include "Luau/TypePack.h"

#include <memory>
#include <unordered_map>

namespace Luau
{

Expand Down
4 changes: 2 additions & 2 deletions Analysis/include/Luau/TypeUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Luau/Error.h"
#include "Luau/Location.h"
#include "Luau/TypeVar.h"
#include "Luau/TypePack.h"

#include <memory>
#include <optional>
Expand All @@ -12,15 +13,14 @@ namespace Luau
{

struct TxnLog;
struct TypeArena;

using ScopePtr = std::shared_ptr<struct Scope>;

std::optional<TypeId> findMetatableEntry(
NotNull<SingletonTypes> singletonTypes, ErrorVec& errors, TypeId type, const std::string& entry, Location location);
std::optional<TypeId> findTablePropertyRespectingMeta(
NotNull<SingletonTypes> singletonTypes, ErrorVec& errors, TypeId ty, const std::string& name, Location location);
std::optional<TypeId> getIndexTypeFromType(const ScopePtr& scope, ErrorVec& errors, TypeArena* arena, NotNull<SingletonTypes> singletonTypes,
TypeId type, const std::string& prop, const Location& location, bool addErrors, InternalErrorReporter& handle);

// Returns the minimum and maximum number of types the argument list can accept.
std::pair<size_t, std::optional<size_t>> getParameterExtents(const TxnLog* log, TypePackId tp, bool includeHiddenVariadics = false);
Expand Down
7 changes: 3 additions & 4 deletions Analysis/include/Luau/TypeVar.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,14 @@ using DcrMagicFunction = bool (*)(MagicFunctionCallContext);
struct MagicRefinementContext
{
ScopePtr scope;
NotNull<struct ConstraintGraphBuilder> cgb;
NotNull<const DataFlowGraph> dfg;
NotNull<ConnectiveArena> connectiveArena;
std::vector<ConnectiveId> argumentConnectives;
const class AstExprCall* callSite;
};

using DcrMagicRefinement = std::vector<ConnectiveId> (*)(MagicRefinementContext);
using DcrMagicRefinement = std::vector<ConnectiveId> (*)(const MagicRefinementContext&);

struct FunctionTypeVar
{
Expand Down Expand Up @@ -666,9 +668,6 @@ struct SingletonTypes
const TypePackId errorTypePack;
};

// Clip with FFlagLuauNoMoreGlobalSingletonTypes
SingletonTypes& DEPRECATED_getSingletonTypes();

void persist(TypeId ty);
void persist(TypePackId tp);

Expand Down
27 changes: 24 additions & 3 deletions Analysis/src/AstJsonEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "Luau/StringUtils.h"
#include "Luau/Common.h"

#include <math.h>

namespace Luau
{

Expand Down Expand Up @@ -103,9 +105,28 @@ struct AstJsonEncoder : public AstVisitor

void write(double d)
{
char b[32];
snprintf(b, sizeof(b), "%.17g", d);
writeRaw(b);
switch (fpclassify(d))
{
case FP_INFINITE:
if (d < 0)
writeRaw("-Infinity");
else
writeRaw("Infinity");
break;

case FP_NAN:
writeRaw("NaN");
break;

case FP_NORMAL:
case FP_SUBNORMAL:
case FP_ZERO:
default:
char b[32];
snprintf(b, sizeof(b), "%.17g", d);
writeRaw(b);
break;
}
}

void writeString(std::string_view sv)
Expand Down
22 changes: 18 additions & 4 deletions Analysis/src/AstQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include <algorithm>

LUAU_FASTFLAG(LuauCompleteTableKeysBetter);

namespace Luau
{

Expand All @@ -29,12 +31,24 @@ struct AutocompleteNodeFinder : public AstVisitor

bool visit(AstExpr* expr) override
{
if (expr->location.begin < pos && pos <= expr->location.end)
if (FFlag::LuauCompleteTableKeysBetter)
{
ancestry.push_back(expr);
return true;
if (expr->location.begin <= pos && pos <= expr->location.end)
{
ancestry.push_back(expr);
return true;
}
return false;
}
else
{
if (expr->location.begin < pos && pos <= expr->location.end)
{
ancestry.push_back(expr);
return true;
}
return false;
}
return false;
}

bool visit(AstStat* stat) override
Expand Down
Loading

0 comments on commit fb2f146

Please sign in to comment.