2023-08-18 19:15:41 +01:00
|
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
|
|
|
|
|
|
#include "Luau/Subtyping.h"
|
|
|
|
|
|
|
|
|
|
#include "Luau/Common.h"
|
2023-08-25 18:23:55 +01:00
|
|
|
|
#include "Luau/Error.h"
|
2023-08-18 19:15:41 +01:00
|
|
|
|
#include "Luau/Normalize.h"
|
2023-09-15 18:26:59 +01:00
|
|
|
|
#include "Luau/Scope.h"
|
2023-08-25 18:23:55 +01:00
|
|
|
|
#include "Luau/StringUtils.h"
|
|
|
|
|
#include "Luau/ToString.h"
|
2023-08-18 19:15:41 +01:00
|
|
|
|
#include "Luau/Type.h"
|
2023-08-25 18:23:55 +01:00
|
|
|
|
#include "Luau/TypeArena.h"
|
2023-08-18 19:15:41 +01:00
|
|
|
|
#include "Luau/TypePack.h"
|
2023-10-21 02:10:30 +01:00
|
|
|
|
#include "Luau/TypePath.h"
|
2023-08-18 19:15:41 +01:00
|
|
|
|
#include "Luau/TypeUtils.h"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
|
{
|
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
|
struct VarianceFlipper
|
2023-08-18 19:15:41 +01:00
|
|
|
|
{
|
2023-08-25 18:23:55 +01:00
|
|
|
|
Subtyping::Variance* variance;
|
|
|
|
|
Subtyping::Variance oldValue;
|
|
|
|
|
|
|
|
|
|
VarianceFlipper(Subtyping::Variance* v)
|
|
|
|
|
: variance(v)
|
|
|
|
|
, oldValue(*v)
|
|
|
|
|
{
|
|
|
|
|
switch (oldValue)
|
|
|
|
|
{
|
2023-09-15 18:26:59 +01:00
|
|
|
|
case Subtyping::Variance::Covariant:
|
|
|
|
|
*variance = Subtyping::Variance::Contravariant;
|
|
|
|
|
break;
|
|
|
|
|
case Subtyping::Variance::Contravariant:
|
|
|
|
|
*variance = Subtyping::Variance::Covariant;
|
|
|
|
|
break;
|
2023-08-25 18:23:55 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~VarianceFlipper()
|
|
|
|
|
{
|
|
|
|
|
*variance = oldValue;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-21 02:10:30 +01:00
|
|
|
|
bool SubtypingReasoning::operator==(const SubtypingReasoning& other) const
|
|
|
|
|
{
|
2023-11-17 18:46:18 +00:00
|
|
|
|
return subPath == other.subPath && superPath == other.superPath && variance == other.variance;
|
2023-10-21 02:10:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-10 21:10:07 +00:00
|
|
|
|
size_t SubtypingReasoningHash::operator()(const SubtypingReasoning& r) const
|
|
|
|
|
{
|
2023-11-17 18:46:18 +00:00
|
|
|
|
return TypePath::PathHash()(r.subPath) ^ (TypePath::PathHash()(r.superPath) << 1) ^ (static_cast<size_t>(r.variance) << 1);
|
2023-11-10 21:10:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 18:26:59 +01:00
|
|
|
|
SubtypingResult& SubtypingResult::andAlso(const SubtypingResult& other)
|
2023-08-25 18:23:55 +01:00
|
|
|
|
{
|
2023-11-10 21:10:07 +00:00
|
|
|
|
// If the other result is not a subtype, we want to join all of its
|
|
|
|
|
// reasonings to this one. If this result already has reasonings of its own,
|
|
|
|
|
// those need to be attributed here.
|
|
|
|
|
if (!other.isSubtype)
|
|
|
|
|
{
|
|
|
|
|
for (const SubtypingReasoning& r : other.reasoning)
|
|
|
|
|
reasoning.insert(r);
|
|
|
|
|
}
|
2023-10-21 02:10:30 +01:00
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
|
isSubtype &= other.isSubtype;
|
|
|
|
|
// `|=` is intentional here, we want to preserve error related flags.
|
|
|
|
|
isErrorSuppressing |= other.isErrorSuppressing;
|
|
|
|
|
normalizationTooComplex |= other.normalizationTooComplex;
|
2023-10-13 21:20:12 +01:00
|
|
|
|
isCacheable &= other.isCacheable;
|
|
|
|
|
|
2023-09-15 18:26:59 +01:00
|
|
|
|
return *this;
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 18:26:59 +01:00
|
|
|
|
SubtypingResult& SubtypingResult::orElse(const SubtypingResult& other)
|
2023-08-18 19:15:41 +01:00
|
|
|
|
{
|
2023-11-10 21:10:07 +00:00
|
|
|
|
// If this result is a subtype, we do not join the reasoning lists. If this
|
|
|
|
|
// result is not a subtype, but the other is a subtype, we want to _clear_
|
|
|
|
|
// our reasoning list. If both results are not subtypes, we join the
|
|
|
|
|
// reasoning lists.
|
|
|
|
|
if (!isSubtype)
|
|
|
|
|
{
|
|
|
|
|
if (other.isSubtype)
|
|
|
|
|
reasoning.clear();
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (const SubtypingReasoning& r : other.reasoning)
|
|
|
|
|
reasoning.insert(r);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-21 02:10:30 +01:00
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
|
isSubtype |= other.isSubtype;
|
|
|
|
|
isErrorSuppressing |= other.isErrorSuppressing;
|
|
|
|
|
normalizationTooComplex |= other.normalizationTooComplex;
|
2023-10-13 21:20:12 +01:00
|
|
|
|
isCacheable &= other.isCacheable;
|
|
|
|
|
|
2023-09-15 18:26:59 +01:00
|
|
|
|
return *this;
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 02:10:30 +01:00
|
|
|
|
SubtypingResult& SubtypingResult::withBothComponent(TypePath::Component component)
|
|
|
|
|
{
|
|
|
|
|
return withSubComponent(component).withSuperComponent(component);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SubtypingResult& SubtypingResult::withSubComponent(TypePath::Component component)
|
|
|
|
|
{
|
2023-11-10 21:10:07 +00:00
|
|
|
|
if (reasoning.empty())
|
|
|
|
|
reasoning.insert(SubtypingReasoning{Path(component), TypePath::kEmpty});
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (auto& r : reasoning)
|
|
|
|
|
r.subPath = r.subPath.push_front(component);
|
|
|
|
|
}
|
2023-10-21 02:10:30 +01:00
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SubtypingResult& SubtypingResult::withSuperComponent(TypePath::Component component)
|
|
|
|
|
{
|
2023-11-10 21:10:07 +00:00
|
|
|
|
if (reasoning.empty())
|
|
|
|
|
reasoning.insert(SubtypingReasoning{TypePath::kEmpty, Path(component)});
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (auto& r : reasoning)
|
|
|
|
|
r.superPath = r.superPath.push_front(component);
|
|
|
|
|
}
|
2023-10-21 02:10:30 +01:00
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SubtypingResult& SubtypingResult::withBothPath(TypePath::Path path)
|
|
|
|
|
{
|
|
|
|
|
return withSubPath(path).withSuperPath(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SubtypingResult& SubtypingResult::withSubPath(TypePath::Path path)
|
|
|
|
|
{
|
2023-11-10 21:10:07 +00:00
|
|
|
|
if (reasoning.empty())
|
|
|
|
|
reasoning.insert(SubtypingReasoning{path, TypePath::kEmpty});
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (auto& r : reasoning)
|
|
|
|
|
r.subPath = path.append(r.subPath);
|
|
|
|
|
}
|
2023-10-21 02:10:30 +01:00
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SubtypingResult& SubtypingResult::withSuperPath(TypePath::Path path)
|
|
|
|
|
{
|
2023-11-10 21:10:07 +00:00
|
|
|
|
if (reasoning.empty())
|
|
|
|
|
reasoning.insert(SubtypingReasoning{TypePath::kEmpty, path});
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (auto& r : reasoning)
|
|
|
|
|
r.superPath = path.append(r.superPath);
|
|
|
|
|
}
|
2023-10-21 02:10:30 +01:00
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-17 18:46:18 +00:00
|
|
|
|
SubtypingResult& SubtypingResult::withVariance(SubtypingVariance variance)
|
|
|
|
|
{
|
|
|
|
|
if (reasoning.empty())
|
|
|
|
|
reasoning.insert(SubtypingReasoning{TypePath::kEmpty, TypePath::kEmpty, variance});
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (auto& r : reasoning)
|
|
|
|
|
r.variance = variance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-01 18:58:27 +01:00
|
|
|
|
SubtypingResult SubtypingResult::negate(const SubtypingResult& result)
|
|
|
|
|
{
|
|
|
|
|
return SubtypingResult{
|
|
|
|
|
!result.isSubtype,
|
|
|
|
|
result.isErrorSuppressing,
|
|
|
|
|
result.normalizationTooComplex,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
|
SubtypingResult SubtypingResult::all(const std::vector<SubtypingResult>& results)
|
2023-08-18 19:15:41 +01:00
|
|
|
|
{
|
2023-08-25 18:23:55 +01:00
|
|
|
|
SubtypingResult acc{true, false};
|
|
|
|
|
for (const SubtypingResult& current : results)
|
|
|
|
|
acc.andAlso(current);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
return acc;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
|
SubtypingResult SubtypingResult::any(const std::vector<SubtypingResult>& results)
|
2023-08-18 19:15:41 +01:00
|
|
|
|
{
|
2023-08-25 18:23:55 +01:00
|
|
|
|
SubtypingResult acc{false, false};
|
|
|
|
|
for (const SubtypingResult& current : results)
|
|
|
|
|
acc.orElse(current);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
return acc;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
Subtyping::Subtyping(NotNull<BuiltinTypes> builtinTypes, NotNull<TypeArena> typeArena, NotNull<Normalizer> normalizer,
|
|
|
|
|
NotNull<InternalErrorReporter> iceReporter, NotNull<Scope> scope)
|
|
|
|
|
: builtinTypes(builtinTypes)
|
|
|
|
|
, arena(typeArena)
|
|
|
|
|
, normalizer(normalizer)
|
|
|
|
|
, iceReporter(iceReporter)
|
|
|
|
|
, scope(scope)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
|
SubtypingResult Subtyping::isSubtype(TypeId subTy, TypeId superTy)
|
|
|
|
|
{
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingEnvironment env;
|
2023-08-25 18:23:55 +01:00
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult result = isCovariantWith(env, subTy, superTy);
|
2023-08-25 18:23:55 +01:00
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
for (const auto& [subTy, bounds] : env.mappedGenerics)
|
2023-08-25 18:23:55 +01:00
|
|
|
|
{
|
|
|
|
|
const auto& lb = bounds.lowerBound;
|
|
|
|
|
const auto& ub = bounds.upperBound;
|
|
|
|
|
|
|
|
|
|
TypeId lowerBound = makeAggregateType<UnionType>(lb, builtinTypes->neverType);
|
|
|
|
|
TypeId upperBound = makeAggregateType<IntersectionType>(ub, builtinTypes->unknownType);
|
|
|
|
|
|
2023-10-06 20:02:32 +01:00
|
|
|
|
const NormalizedType* nt = normalizer->normalize(upperBound);
|
|
|
|
|
if (!nt)
|
|
|
|
|
result.normalizationTooComplex = true;
|
|
|
|
|
else if (!normalizer->isInhabited(nt))
|
|
|
|
|
{
|
|
|
|
|
/* If the normalized upper bound we're mapping to a generic is
|
|
|
|
|
* uninhabited, then we must consider the subtyping relation not to
|
|
|
|
|
* hold.
|
|
|
|
|
*
|
|
|
|
|
* This happens eg in <T>() -> (T, T) <: () -> (string, number)
|
|
|
|
|
*
|
|
|
|
|
* T appears in covariant position and would have to be both string
|
|
|
|
|
* and number at once.
|
|
|
|
|
*
|
|
|
|
|
* No actual value is both a string and a number, so the test fails.
|
|
|
|
|
*
|
|
|
|
|
* TODO: We'll need to add explanitory context here.
|
|
|
|
|
*/
|
|
|
|
|
result.isSubtype = false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result.andAlso(isCovariantWith(env, lowerBound, upperBound));
|
2023-08-25 18:23:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
/* TODO: We presently don't store subtype test results in the persistent
|
|
|
|
|
* cache if the left-side type is a generic function.
|
|
|
|
|
*
|
|
|
|
|
* The implementation would be a bit tricky and we haven't seen any material
|
|
|
|
|
* impact on benchmarks.
|
|
|
|
|
*
|
|
|
|
|
* What we would want to do is to remember points within the type where
|
|
|
|
|
* mapped generics are introduced. When all the contingent generics are
|
|
|
|
|
* introduced at which we're doing the test, we can mark the result as
|
|
|
|
|
* cacheable.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (result.isCacheable)
|
|
|
|
|
resultCache[{subTy, superTy}] = result;
|
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SubtypingResult Subtyping::isSubtype(TypePackId subTp, TypePackId superTp)
|
|
|
|
|
{
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingEnvironment env;
|
|
|
|
|
return isCovariantWith(env, subTp, superTp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SubtypingResult Subtyping::cache(SubtypingEnvironment& env, SubtypingResult result, TypeId subTy, TypeId superTy)
|
|
|
|
|
{
|
|
|
|
|
const std::pair<TypeId, TypeId> p{subTy, superTy};
|
|
|
|
|
if (result.isCacheable)
|
|
|
|
|
resultCache[p] = result;
|
|
|
|
|
else
|
|
|
|
|
env.ephemeralCache[p] = result;
|
|
|
|
|
|
|
|
|
|
return result;
|
2023-08-25 18:23:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
struct SeenSetPopper
|
|
|
|
|
{
|
|
|
|
|
Subtyping::SeenSet* seenTypes;
|
|
|
|
|
std::pair<TypeId, TypeId> pair;
|
|
|
|
|
|
|
|
|
|
SeenSetPopper(Subtyping::SeenSet* seenTypes, std::pair<TypeId, TypeId> pair)
|
|
|
|
|
: seenTypes(seenTypes)
|
|
|
|
|
, pair(pair)
|
2023-09-15 18:26:59 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
2023-08-25 18:23:55 +01:00
|
|
|
|
|
|
|
|
|
~SeenSetPopper()
|
|
|
|
|
{
|
|
|
|
|
seenTypes->erase(pair);
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-09-15 18:26:59 +01:00
|
|
|
|
} // namespace
|
2023-08-25 18:23:55 +01:00
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, TypeId subTy, TypeId superTy)
|
2023-08-18 19:15:41 +01:00
|
|
|
|
{
|
|
|
|
|
subTy = follow(subTy);
|
|
|
|
|
superTy = follow(superTy);
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult* cachedResult = resultCache.find({subTy, superTy});
|
|
|
|
|
if (cachedResult)
|
|
|
|
|
return *cachedResult;
|
|
|
|
|
|
|
|
|
|
cachedResult = env.ephemeralCache.find({subTy, superTy});
|
|
|
|
|
if (cachedResult)
|
|
|
|
|
return *cachedResult;
|
|
|
|
|
|
2023-08-18 19:15:41 +01:00
|
|
|
|
// TODO: Do we care about returning a proof that this is error-suppressing?
|
|
|
|
|
// e.g. given `a | error <: a | error` where both operands are pointer equal,
|
|
|
|
|
// then should it also carry the information that it's error-suppressing?
|
|
|
|
|
// If it should, then `error <: error` should also do the same.
|
|
|
|
|
if (subTy == superTy)
|
|
|
|
|
return {true};
|
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
|
std::pair<TypeId, TypeId> typePair{subTy, superTy};
|
2023-11-10 21:10:07 +00:00
|
|
|
|
if (!seenTypes.insert(typePair))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
{
|
|
|
|
|
/* TODO: Caching results for recursive types is really tricky to think
|
|
|
|
|
* about.
|
|
|
|
|
*
|
|
|
|
|
* We'd like to cache at the outermost level where we encounter the
|
|
|
|
|
* recursive type, but we do not want to cache interior results that
|
|
|
|
|
* involve the cycle.
|
|
|
|
|
*
|
|
|
|
|
* Presently, we stop at cycles and assume that the subtype check will
|
|
|
|
|
* succeed because we'll eventually get there if it won't. However, if
|
|
|
|
|
* that cyclic type turns out not to have the asked-for subtyping
|
|
|
|
|
* relation, then all the intermediate cached results that were
|
|
|
|
|
* contingent on that assumption need to be evicted from the cache, or
|
|
|
|
|
* not entered into the cache, or something.
|
|
|
|
|
*
|
|
|
|
|
* For now, we do the conservative thing and refuse to cache anything
|
|
|
|
|
* that touches a cycle.
|
|
|
|
|
*/
|
|
|
|
|
SubtypingResult res;
|
|
|
|
|
res.isSubtype = true;
|
|
|
|
|
res.isCacheable = false;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2023-08-25 18:23:55 +01:00
|
|
|
|
|
|
|
|
|
SeenSetPopper ssp{&seenTypes, typePair};
|
2023-08-18 19:15:41 +01:00
|
|
|
|
|
2023-09-15 18:26:59 +01:00
|
|
|
|
// Within the scope to which a generic belongs, that generic should be
|
|
|
|
|
// tested as though it were its upper bounds. We do not yet support bounded
|
|
|
|
|
// generics, so the upper bound is always unknown.
|
|
|
|
|
if (auto subGeneric = get<GenericType>(subTy); subGeneric && subsumes(subGeneric->scope, scope))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
return isCovariantWith(env, builtinTypes->unknownType, superTy);
|
2023-09-15 18:26:59 +01:00
|
|
|
|
if (auto superGeneric = get<GenericType>(superTy); superGeneric && subsumes(superGeneric->scope, scope))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
return isCovariantWith(env, subTy, builtinTypes->unknownType);
|
|
|
|
|
|
|
|
|
|
SubtypingResult result;
|
2023-09-15 18:26:59 +01:00
|
|
|
|
|
2023-09-01 18:58:27 +01:00
|
|
|
|
if (auto subUnion = get<UnionType>(subTy))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = isCovariantWith(env, subUnion, superTy);
|
2023-09-01 18:58:27 +01:00
|
|
|
|
else if (auto superUnion = get<UnionType>(superTy))
|
2023-11-03 23:45:04 +00:00
|
|
|
|
{
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = isCovariantWith(env, subTy, superUnion);
|
2023-11-03 23:45:04 +00:00
|
|
|
|
if (!result.isSubtype && !result.isErrorSuppressing && !result.normalizationTooComplex)
|
|
|
|
|
{
|
|
|
|
|
SubtypingResult semantic = isCovariantWith(env, normalizer->normalize(subTy), normalizer->normalize(superTy));
|
|
|
|
|
if (semantic.isSubtype)
|
|
|
|
|
result = semantic;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-18 19:15:41 +01:00
|
|
|
|
else if (auto superIntersection = get<IntersectionType>(superTy))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = isCovariantWith(env, subTy, superIntersection);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
else if (auto subIntersection = get<IntersectionType>(subTy))
|
|
|
|
|
{
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = isCovariantWith(env, subIntersection, superTy);
|
|
|
|
|
if (!result.isSubtype && !result.isErrorSuppressing && !result.normalizationTooComplex)
|
2023-11-03 23:45:04 +00:00
|
|
|
|
{
|
|
|
|
|
SubtypingResult semantic = isCovariantWith(env, normalizer->normalize(subTy), normalizer->normalize(superTy));
|
|
|
|
|
if (semantic.isSubtype)
|
|
|
|
|
result = semantic;
|
|
|
|
|
}
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
else if (get<AnyType>(superTy))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = {true};
|
2023-08-18 19:15:41 +01:00
|
|
|
|
else if (get<AnyType>(subTy))
|
|
|
|
|
{
|
|
|
|
|
// any = unknown | error, so we rewrite this to match.
|
|
|
|
|
// As per TAPL: A | B <: T iff A <: T && B <: T
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = isCovariantWith(env, builtinTypes->unknownType, superTy).andAlso(isCovariantWith(env, builtinTypes->errorType, superTy));
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
2023-08-25 18:23:55 +01:00
|
|
|
|
else if (get<UnknownType>(superTy))
|
2023-08-18 19:15:41 +01:00
|
|
|
|
{
|
2023-09-15 18:26:59 +01:00
|
|
|
|
LUAU_ASSERT(!get<AnyType>(subTy)); // TODO: replace with ice.
|
|
|
|
|
LUAU_ASSERT(!get<UnionType>(subTy)); // TODO: replace with ice.
|
2023-08-18 19:15:41 +01:00
|
|
|
|
LUAU_ASSERT(!get<IntersectionType>(subTy)); // TODO: replace with ice.
|
|
|
|
|
|
|
|
|
|
bool errorSuppressing = get<ErrorType>(subTy);
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = {!errorSuppressing, errorSuppressing};
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
else if (get<NeverType>(subTy))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = {true};
|
2023-08-18 19:15:41 +01:00
|
|
|
|
else if (get<ErrorType>(superTy))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = {false, true};
|
2023-08-18 19:15:41 +01:00
|
|
|
|
else if (get<ErrorType>(subTy))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = {false, true};
|
2023-09-15 18:26:59 +01:00
|
|
|
|
else if (auto p = get2<NegationType, NegationType>(subTy, superTy))
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = isCovariantWith(env, p.first->ty, p.second->ty).withBothComponent(TypePath::TypeField::Negated);
|
2023-09-15 18:26:59 +01:00
|
|
|
|
else if (auto subNegation = get<NegationType>(subTy))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = isCovariantWith(env, subNegation, superTy);
|
2023-09-15 18:26:59 +01:00
|
|
|
|
else if (auto superNegation = get<NegationType>(superTy))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = isCovariantWith(env, subTy, superNegation);
|
2023-08-25 18:23:55 +01:00
|
|
|
|
else if (auto subGeneric = get<GenericType>(subTy); subGeneric && variance == Variance::Covariant)
|
|
|
|
|
{
|
2023-10-13 21:20:12 +01:00
|
|
|
|
bool ok = bindGeneric(env, subTy, superTy);
|
|
|
|
|
result.isSubtype = ok;
|
|
|
|
|
result.isCacheable = false;
|
2023-08-25 18:23:55 +01:00
|
|
|
|
}
|
|
|
|
|
else if (auto superGeneric = get<GenericType>(superTy); superGeneric && variance == Variance::Contravariant)
|
|
|
|
|
{
|
2023-10-13 21:20:12 +01:00
|
|
|
|
bool ok = bindGeneric(env, subTy, superTy);
|
|
|
|
|
result.isSubtype = ok;
|
|
|
|
|
result.isCacheable = false;
|
2023-08-25 18:23:55 +01:00
|
|
|
|
}
|
2023-08-18 19:15:41 +01:00
|
|
|
|
else if (auto p = get2<PrimitiveType, PrimitiveType>(subTy, superTy))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = isCovariantWith(env, p);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
else if (auto p = get2<SingletonType, PrimitiveType>(subTy, superTy))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = isCovariantWith(env, p);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
else if (auto p = get2<SingletonType, SingletonType>(subTy, superTy))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = isCovariantWith(env, p);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
else if (auto p = get2<FunctionType, FunctionType>(subTy, superTy))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = isCovariantWith(env, p);
|
2023-08-25 18:23:55 +01:00
|
|
|
|
else if (auto p = get2<TableType, TableType>(subTy, superTy))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = isCovariantWith(env, p);
|
2023-09-01 18:58:27 +01:00
|
|
|
|
else if (auto p = get2<MetatableType, MetatableType>(subTy, superTy))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = isCovariantWith(env, p);
|
2023-09-01 18:58:27 +01:00
|
|
|
|
else if (auto p = get2<MetatableType, TableType>(subTy, superTy))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = isCovariantWith(env, p);
|
2023-09-01 18:58:27 +01:00
|
|
|
|
else if (auto p = get2<ClassType, ClassType>(subTy, superTy))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = isCovariantWith(env, p);
|
2023-09-01 18:58:27 +01:00
|
|
|
|
else if (auto p = get2<ClassType, TableType>(subTy, superTy))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = isCovariantWith(env, p);
|
2023-09-01 18:58:27 +01:00
|
|
|
|
else if (auto p = get2<PrimitiveType, TableType>(subTy, superTy))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = isCovariantWith(env, p);
|
2023-09-01 18:58:27 +01:00
|
|
|
|
else if (auto p = get2<SingletonType, TableType>(subTy, superTy))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result = isCovariantWith(env, p);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
return cache(env, result, subTy, superTy);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, TypePackId subTp, TypePackId superTp)
|
2023-08-18 19:15:41 +01:00
|
|
|
|
{
|
|
|
|
|
subTp = follow(subTp);
|
|
|
|
|
superTp = follow(superTp);
|
|
|
|
|
|
|
|
|
|
auto [subHead, subTail] = flatten(subTp);
|
|
|
|
|
auto [superHead, superTail] = flatten(superTp);
|
|
|
|
|
|
|
|
|
|
const size_t headSize = std::min(subHead.size(), superHead.size());
|
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
|
std::vector<SubtypingResult> results;
|
2023-08-18 19:15:41 +01:00
|
|
|
|
results.reserve(std::max(subHead.size(), superHead.size()) + 1);
|
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
|
if (subTp == superTp)
|
|
|
|
|
return {true};
|
|
|
|
|
|
2023-08-18 19:15:41 +01:00
|
|
|
|
// Match head types pairwise
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < headSize; ++i)
|
|
|
|
|
{
|
2023-10-21 02:10:30 +01:00
|
|
|
|
results.push_back(isCovariantWith(env, subHead[i], superHead[i]).withBothComponent(TypePath::Index{i}));
|
2023-08-18 19:15:41 +01:00
|
|
|
|
if (!results.back().isSubtype)
|
2023-10-21 02:10:30 +01:00
|
|
|
|
return results.back();
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle mismatched head sizes
|
|
|
|
|
|
|
|
|
|
if (subHead.size() < superHead.size())
|
|
|
|
|
{
|
|
|
|
|
if (subTail)
|
|
|
|
|
{
|
|
|
|
|
if (auto vt = get<VariadicTypePack>(*subTail))
|
|
|
|
|
{
|
|
|
|
|
for (size_t i = headSize; i < superHead.size(); ++i)
|
2023-10-21 02:10:30 +01:00
|
|
|
|
results.push_back(isCovariantWith(env, vt->ty, superHead[i])
|
|
|
|
|
.withSubComponent(TypePath::TypeField::Variadic)
|
|
|
|
|
.withSuperComponent(TypePath::Index{i}));
|
2023-08-25 18:23:55 +01:00
|
|
|
|
}
|
|
|
|
|
else if (auto gt = get<GenericTypePack>(*subTail))
|
|
|
|
|
{
|
|
|
|
|
if (variance == Variance::Covariant)
|
2023-08-18 19:15:41 +01:00
|
|
|
|
{
|
2023-08-25 18:23:55 +01:00
|
|
|
|
// For any non-generic type T:
|
|
|
|
|
//
|
|
|
|
|
// <X>(X) -> () <: (T) -> ()
|
|
|
|
|
|
|
|
|
|
// Possible optimization: If headSize == 0 then we can just use subTp as-is.
|
2023-09-15 18:26:59 +01:00
|
|
|
|
std::vector<TypeId> headSlice(begin(superHead), begin(superHead) + headSize);
|
2023-08-25 18:23:55 +01:00
|
|
|
|
TypePackId superTailPack = arena->addTypePack(std::move(headSlice), superTail);
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
if (TypePackId* other = env.mappedGenericPacks.find(*subTail))
|
2023-10-21 02:10:30 +01:00
|
|
|
|
// TODO: TypePath can't express "slice of a pack + its tail".
|
|
|
|
|
results.push_back(isCovariantWith(env, *other, superTailPack).withSubComponent(TypePath::PackField::Tail));
|
2023-08-25 18:23:55 +01:00
|
|
|
|
else
|
2023-10-13 21:20:12 +01:00
|
|
|
|
env.mappedGenericPacks.try_insert(*subTail, superTailPack);
|
2023-08-25 18:23:55 +01:00
|
|
|
|
|
|
|
|
|
// FIXME? Not a fan of the early return here. It makes the
|
|
|
|
|
// control flow harder to reason about.
|
|
|
|
|
return SubtypingResult::all(results);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// For any non-generic type T:
|
|
|
|
|
//
|
|
|
|
|
// (T) -> () </: <X>(X) -> ()
|
|
|
|
|
//
|
2023-10-21 02:10:30 +01:00
|
|
|
|
return SubtypingResult{false}.withSubComponent(TypePath::PackField::Tail);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2023-08-25 18:23:55 +01:00
|
|
|
|
unexpected(*subTail);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return {false};
|
|
|
|
|
}
|
|
|
|
|
else if (subHead.size() > superHead.size())
|
|
|
|
|
{
|
|
|
|
|
if (superTail)
|
|
|
|
|
{
|
|
|
|
|
if (auto vt = get<VariadicTypePack>(*superTail))
|
|
|
|
|
{
|
|
|
|
|
for (size_t i = headSize; i < subHead.size(); ++i)
|
2023-10-21 02:10:30 +01:00
|
|
|
|
results.push_back(isCovariantWith(env, subHead[i], vt->ty)
|
|
|
|
|
.withSubComponent(TypePath::Index{i})
|
|
|
|
|
.withSuperComponent(TypePath::TypeField::Variadic));
|
2023-08-25 18:23:55 +01:00
|
|
|
|
}
|
|
|
|
|
else if (auto gt = get<GenericTypePack>(*superTail))
|
|
|
|
|
{
|
|
|
|
|
if (variance == Variance::Contravariant)
|
|
|
|
|
{
|
|
|
|
|
// For any non-generic type T:
|
|
|
|
|
//
|
|
|
|
|
// <X...>(X...) -> () <: (T) -> ()
|
|
|
|
|
|
|
|
|
|
// Possible optimization: If headSize == 0 then we can just use subTp as-is.
|
2023-09-15 18:26:59 +01:00
|
|
|
|
std::vector<TypeId> headSlice(begin(subHead), begin(subHead) + headSize);
|
2023-08-25 18:23:55 +01:00
|
|
|
|
TypePackId subTailPack = arena->addTypePack(std::move(headSlice), subTail);
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
if (TypePackId* other = env.mappedGenericPacks.find(*superTail))
|
2023-10-21 02:10:30 +01:00
|
|
|
|
// TODO: TypePath can't express "slice of a pack + its tail".
|
|
|
|
|
results.push_back(isCovariantWith(env, *other, subTailPack).withSuperComponent(TypePath::PackField::Tail));
|
2023-08-25 18:23:55 +01:00
|
|
|
|
else
|
2023-10-13 21:20:12 +01:00
|
|
|
|
env.mappedGenericPacks.try_insert(*superTail, subTailPack);
|
2023-08-25 18:23:55 +01:00
|
|
|
|
|
|
|
|
|
// FIXME? Not a fan of the early return here. It makes the
|
|
|
|
|
// control flow harder to reason about.
|
|
|
|
|
return SubtypingResult::all(results);
|
|
|
|
|
}
|
|
|
|
|
else
|
2023-08-18 19:15:41 +01:00
|
|
|
|
{
|
2023-08-25 18:23:55 +01:00
|
|
|
|
// For any non-generic type T:
|
|
|
|
|
//
|
|
|
|
|
// () -> T </: <X...>() -> X...
|
2023-10-21 02:10:30 +01:00
|
|
|
|
return SubtypingResult{false}.withSuperComponent(TypePath::PackField::Tail);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2023-08-25 18:23:55 +01:00
|
|
|
|
unexpected(*superTail);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return {false};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle tails
|
|
|
|
|
|
|
|
|
|
if (subTail && superTail)
|
|
|
|
|
{
|
|
|
|
|
if (auto p = get2<VariadicTypePack, VariadicTypePack>(*subTail, *superTail))
|
|
|
|
|
{
|
2023-10-21 02:10:30 +01:00
|
|
|
|
// Variadic component is added by the isCovariantWith
|
|
|
|
|
// implementation; no need to add it here.
|
|
|
|
|
results.push_back(isCovariantWith(env, p).withBothComponent(TypePath::PackField::Tail));
|
2023-08-25 18:23:55 +01:00
|
|
|
|
}
|
|
|
|
|
else if (auto p = get2<GenericTypePack, GenericTypePack>(*subTail, *superTail))
|
|
|
|
|
{
|
2023-10-13 21:20:12 +01:00
|
|
|
|
bool ok = bindGeneric(env, *subTail, *superTail);
|
2023-10-21 02:10:30 +01:00
|
|
|
|
results.push_back(SubtypingResult{ok}.withBothComponent(TypePath::PackField::Tail));
|
2023-08-25 18:23:55 +01:00
|
|
|
|
}
|
|
|
|
|
else if (get2<VariadicTypePack, GenericTypePack>(*subTail, *superTail))
|
|
|
|
|
{
|
|
|
|
|
if (variance == Variance::Contravariant)
|
|
|
|
|
{
|
|
|
|
|
// <A...>(A...) -> number <: (...number) -> number
|
2023-10-13 21:20:12 +01:00
|
|
|
|
bool ok = bindGeneric(env, *subTail, *superTail);
|
2023-10-21 02:10:30 +01:00
|
|
|
|
results.push_back(SubtypingResult{ok}.withBothComponent(TypePath::PackField::Tail));
|
2023-08-25 18:23:55 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// (number) -> ...number </: <A...>(number) -> A...
|
2023-10-21 02:10:30 +01:00
|
|
|
|
results.push_back(SubtypingResult{false}.withBothComponent(TypePath::PackField::Tail));
|
2023-08-25 18:23:55 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (get2<GenericTypePack, VariadicTypePack>(*subTail, *superTail))
|
|
|
|
|
{
|
|
|
|
|
if (variance == Variance::Contravariant)
|
|
|
|
|
{
|
|
|
|
|
// (...number) -> number </: <A...>(A...) -> number
|
2023-10-21 02:10:30 +01:00
|
|
|
|
results.push_back(SubtypingResult{false}.withBothComponent(TypePath::PackField::Tail));
|
2023-08-25 18:23:55 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// <A...>() -> A... <: () -> ...number
|
2023-10-13 21:20:12 +01:00
|
|
|
|
bool ok = bindGeneric(env, *subTail, *superTail);
|
2023-10-21 02:10:30 +01:00
|
|
|
|
results.push_back(SubtypingResult{ok}.withBothComponent(TypePath::PackField::Tail));
|
2023-08-25 18:23:55 +01:00
|
|
|
|
}
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
2023-09-15 18:26:59 +01:00
|
|
|
|
iceReporter->ice(
|
|
|
|
|
format("Subtyping::isSubtype got unexpected type packs %s and %s", toString(*subTail).c_str(), toString(*superTail).c_str()));
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
else if (subTail)
|
|
|
|
|
{
|
|
|
|
|
if (get<VariadicTypePack>(*subTail))
|
|
|
|
|
{
|
2023-10-21 02:10:30 +01:00
|
|
|
|
return SubtypingResult{false}.withSubComponent(TypePath::PackField::Tail);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
2023-08-25 18:23:55 +01:00
|
|
|
|
else if (get<GenericTypePack>(*subTail))
|
|
|
|
|
{
|
2023-10-13 21:20:12 +01:00
|
|
|
|
bool ok = bindGeneric(env, *subTail, builtinTypes->emptyTypePack);
|
2023-10-21 02:10:30 +01:00
|
|
|
|
return SubtypingResult{ok}.withSubComponent(TypePath::PackField::Tail);
|
2023-08-25 18:23:55 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
unexpected(*subTail);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
else if (superTail)
|
|
|
|
|
{
|
|
|
|
|
if (get<VariadicTypePack>(*superTail))
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* A variadic type pack ...T can be thought of as an infinite union of finite type packs.
|
|
|
|
|
* () | (T) | (T, T) | (T, T, T) | ...
|
|
|
|
|
*
|
|
|
|
|
* And, per TAPL:
|
|
|
|
|
* T <: A | B iff T <: A or T <: B
|
|
|
|
|
*
|
|
|
|
|
* All variadic type packs are therefore supertypes of the empty type pack.
|
|
|
|
|
*/
|
|
|
|
|
}
|
2023-08-25 18:23:55 +01:00
|
|
|
|
else if (get<GenericTypePack>(*superTail))
|
|
|
|
|
{
|
|
|
|
|
if (variance == Variance::Contravariant)
|
|
|
|
|
{
|
2023-10-13 21:20:12 +01:00
|
|
|
|
bool ok = bindGeneric(env, builtinTypes->emptyTypePack, *superTail);
|
2023-10-21 02:10:30 +01:00
|
|
|
|
results.push_back(SubtypingResult{ok}.withSuperComponent(TypePath::PackField::Tail));
|
2023-08-25 18:23:55 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
2023-10-21 02:10:30 +01:00
|
|
|
|
results.push_back(SubtypingResult{false}.withSuperComponent(TypePath::PackField::Tail));
|
2023-08-25 18:23:55 +01:00
|
|
|
|
}
|
2023-08-18 19:15:41 +01:00
|
|
|
|
else
|
2023-09-22 20:12:15 +01:00
|
|
|
|
iceReporter->ice("Subtyping test encountered the unexpected type pack: " + toString(*superTail));
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
|
return SubtypingResult::all(results);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename SubTy, typename SuperTy>
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isContravariantWith(SubtypingEnvironment& env, SubTy&& subTy, SuperTy&& superTy)
|
2023-09-15 18:26:59 +01:00
|
|
|
|
{
|
2023-10-21 02:10:30 +01:00
|
|
|
|
SubtypingResult result = isCovariantWith(env, superTy, subTy);
|
|
|
|
|
// If we don't swap the paths here, we will end up producing an invalid path
|
|
|
|
|
// whenever we involve contravariance. We'll end up appending path
|
|
|
|
|
// components that should belong to the supertype to the subtype, and vice
|
|
|
|
|
// versa.
|
2023-11-10 21:10:07 +00:00
|
|
|
|
for (auto& reasoning : result.reasoning)
|
|
|
|
|
std::swap(reasoning.subPath, reasoning.superPath);
|
2023-10-21 02:10:30 +01:00
|
|
|
|
|
|
|
|
|
return result;
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename SubTy, typename SuperTy>
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isInvariantWith(SubtypingEnvironment& env, SubTy&& subTy, SuperTy&& superTy)
|
2023-09-15 18:26:59 +01:00
|
|
|
|
{
|
2023-11-17 18:46:18 +00:00
|
|
|
|
return isCovariantWith(env, subTy, superTy).andAlso(isContravariantWith(env, subTy, superTy)).withVariance(SubtypingVariance::Invariant);
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename SubTy, typename SuperTy>
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const TryPair<const SubTy*, const SuperTy*>& pair)
|
2023-09-15 18:26:59 +01:00
|
|
|
|
{
|
2023-10-13 21:20:12 +01:00
|
|
|
|
return isCovariantWith(env, pair.first, pair.second);
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename SubTy, typename SuperTy>
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isContravariantWith(SubtypingEnvironment& env, const TryPair<const SubTy*, const SuperTy*>& pair)
|
2023-09-15 18:26:59 +01:00
|
|
|
|
{
|
2023-10-13 21:20:12 +01:00
|
|
|
|
return isCovariantWith(env, pair.second, pair.first);
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename SubTy, typename SuperTy>
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isInvariantWith(SubtypingEnvironment& env, const TryPair<const SubTy*, const SuperTy*>& pair)
|
2023-08-18 19:15:41 +01:00
|
|
|
|
{
|
2023-11-17 18:46:18 +00:00
|
|
|
|
return isCovariantWith(env, pair).andAlso(isContravariantWith(pair)).withVariance(SubtypingVariance::Invariant);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This is much simpler than the Unifier implementation because we don't
|
|
|
|
|
* actually care about potential "cross-talk" between union parts that match the
|
|
|
|
|
* left side.
|
|
|
|
|
*
|
|
|
|
|
* In fact, we're very limited in what we can do: If multiple choices match, but
|
|
|
|
|
* all of them have non-overlapping constraints, then we're stuck with an "or"
|
|
|
|
|
* conjunction of constraints. Solving this in the general case is quite
|
|
|
|
|
* difficult.
|
|
|
|
|
*
|
|
|
|
|
* For example, we cannot dispatch anything from this constraint:
|
|
|
|
|
*
|
|
|
|
|
* {x: number, y: string} <: {x: number, y: 'a} | {x: 'b, y: string}
|
|
|
|
|
*
|
|
|
|
|
* From this constraint, we can know that either string <: 'a or number <: 'b,
|
|
|
|
|
* but we don't know which!
|
|
|
|
|
*
|
|
|
|
|
* However:
|
|
|
|
|
*
|
|
|
|
|
* {x: number, y: string} <: {x: number, y: 'a} | {x: number, y: string}
|
|
|
|
|
*
|
|
|
|
|
* We can dispatch this constraint because there is no 'or' conjunction. One of
|
|
|
|
|
* the arms requires 0 matches.
|
|
|
|
|
*
|
|
|
|
|
* {x: number, y: string, z: boolean} | {x: number, y: 'a, z: 'b} | {x: number,
|
|
|
|
|
* y: string, z: 'b}
|
|
|
|
|
*
|
|
|
|
|
* Here, we have two matches. One asks for string ~ 'a and boolean ~ 'b. The
|
|
|
|
|
* other just asks for boolean ~ 'b. We can dispatch this and only commit
|
|
|
|
|
* boolean ~ 'b. This constraint does not teach us anything about 'a.
|
|
|
|
|
*/
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, TypeId subTy, const UnionType* superUnion)
|
2023-08-18 19:15:41 +01:00
|
|
|
|
{
|
|
|
|
|
// As per TAPL: T <: A | B iff T <: A || T <: B
|
2023-08-25 18:23:55 +01:00
|
|
|
|
std::vector<SubtypingResult> subtypings;
|
2023-10-21 02:10:30 +01:00
|
|
|
|
size_t i = 0;
|
2023-08-18 19:15:41 +01:00
|
|
|
|
for (TypeId ty : superUnion)
|
2023-10-21 02:10:30 +01:00
|
|
|
|
subtypings.push_back(isCovariantWith(env, subTy, ty).withSuperComponent(TypePath::Index{i++}));
|
2023-08-25 18:23:55 +01:00
|
|
|
|
return SubtypingResult::any(subtypings);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const UnionType* subUnion, TypeId superTy)
|
2023-08-18 19:15:41 +01:00
|
|
|
|
{
|
|
|
|
|
// As per TAPL: A | B <: T iff A <: T && B <: T
|
2023-08-25 18:23:55 +01:00
|
|
|
|
std::vector<SubtypingResult> subtypings;
|
2023-10-21 02:10:30 +01:00
|
|
|
|
size_t i = 0;
|
2023-08-18 19:15:41 +01:00
|
|
|
|
for (TypeId ty : subUnion)
|
2023-10-21 02:10:30 +01:00
|
|
|
|
subtypings.push_back(isCovariantWith(env, ty, superTy).withSubComponent(TypePath::Index{i++}));
|
2023-08-25 18:23:55 +01:00
|
|
|
|
return SubtypingResult::all(subtypings);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, TypeId subTy, const IntersectionType* superIntersection)
|
2023-08-18 19:15:41 +01:00
|
|
|
|
{
|
|
|
|
|
// As per TAPL: T <: A & B iff T <: A && T <: B
|
2023-08-25 18:23:55 +01:00
|
|
|
|
std::vector<SubtypingResult> subtypings;
|
2023-10-21 02:10:30 +01:00
|
|
|
|
size_t i = 0;
|
2023-08-18 19:15:41 +01:00
|
|
|
|
for (TypeId ty : superIntersection)
|
2023-10-21 02:10:30 +01:00
|
|
|
|
subtypings.push_back(isCovariantWith(env, subTy, ty).withSuperComponent(TypePath::Index{i++}));
|
2023-08-25 18:23:55 +01:00
|
|
|
|
return SubtypingResult::all(subtypings);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const IntersectionType* subIntersection, TypeId superTy)
|
2023-08-18 19:15:41 +01:00
|
|
|
|
{
|
|
|
|
|
// As per TAPL: A & B <: T iff A <: T || B <: T
|
2023-08-25 18:23:55 +01:00
|
|
|
|
std::vector<SubtypingResult> subtypings;
|
2023-10-21 02:10:30 +01:00
|
|
|
|
size_t i = 0;
|
2023-08-18 19:15:41 +01:00
|
|
|
|
for (TypeId ty : subIntersection)
|
2023-10-21 02:10:30 +01:00
|
|
|
|
subtypings.push_back(isCovariantWith(env, ty, superTy).withSubComponent(TypePath::Index{i++}));
|
2023-08-25 18:23:55 +01:00
|
|
|
|
return SubtypingResult::any(subtypings);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const NegationType* subNegation, TypeId superTy)
|
2023-09-15 18:26:59 +01:00
|
|
|
|
{
|
|
|
|
|
TypeId negatedTy = follow(subNegation->ty);
|
|
|
|
|
|
2023-10-21 02:10:30 +01:00
|
|
|
|
SubtypingResult result;
|
|
|
|
|
|
2023-09-15 18:26:59 +01:00
|
|
|
|
// In order to follow a consistent codepath, rather than folding the
|
|
|
|
|
// isCovariantWith test down to its conclusion here, we test the subtyping test
|
|
|
|
|
// of the result of negating the type for never, unknown, any, and error.
|
|
|
|
|
if (is<NeverType>(negatedTy))
|
|
|
|
|
{
|
|
|
|
|
// ¬never ~ unknown
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = isCovariantWith(env, builtinTypes->unknownType, superTy);
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
else if (is<UnknownType>(negatedTy))
|
|
|
|
|
{
|
|
|
|
|
// ¬unknown ~ never
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = isCovariantWith(env, builtinTypes->neverType, superTy);
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
else if (is<AnyType>(negatedTy))
|
|
|
|
|
{
|
|
|
|
|
// ¬any ~ any
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = isCovariantWith(env, negatedTy, superTy);
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
else if (auto u = get<UnionType>(negatedTy))
|
|
|
|
|
{
|
|
|
|
|
// ¬(A ∪ B) ~ ¬A ∩ ¬B
|
|
|
|
|
// follow intersection rules: A & B <: T iff A <: T && B <: T
|
|
|
|
|
std::vector<SubtypingResult> subtypings;
|
|
|
|
|
|
|
|
|
|
for (TypeId ty : u)
|
|
|
|
|
{
|
|
|
|
|
NegationType negatedTmp{ty};
|
2023-10-13 21:20:12 +01:00
|
|
|
|
subtypings.push_back(isCovariantWith(env, &negatedTmp, superTy));
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = SubtypingResult::all(subtypings);
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
else if (auto i = get<IntersectionType>(negatedTy))
|
|
|
|
|
{
|
|
|
|
|
// ¬(A ∩ B) ~ ¬A ∪ ¬B
|
|
|
|
|
// follow union rules: A | B <: T iff A <: T || B <: T
|
|
|
|
|
std::vector<SubtypingResult> subtypings;
|
|
|
|
|
|
|
|
|
|
for (TypeId ty : i)
|
|
|
|
|
{
|
|
|
|
|
if (auto negatedPart = get<NegationType>(follow(ty)))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
subtypings.push_back(isCovariantWith(env, negatedPart->ty, superTy));
|
2023-09-15 18:26:59 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NegationType negatedTmp{ty};
|
2023-10-13 21:20:12 +01:00
|
|
|
|
subtypings.push_back(isCovariantWith(env, &negatedTmp, superTy));
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = SubtypingResult::any(subtypings);
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
else if (is<ErrorType, FunctionType, TableType, MetatableType>(negatedTy))
|
|
|
|
|
{
|
|
|
|
|
iceReporter->ice("attempting to negate a non-testable type");
|
|
|
|
|
}
|
|
|
|
|
// negating a different subtype will get you a very wide type that's not a
|
|
|
|
|
// subtype of other stuff.
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = {false};
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
2023-10-21 02:10:30 +01:00
|
|
|
|
|
|
|
|
|
return result.withSubComponent(TypePath::TypeField::Negated);
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const TypeId subTy, const NegationType* superNegation)
|
2023-09-15 18:26:59 +01:00
|
|
|
|
{
|
|
|
|
|
TypeId negatedTy = follow(superNegation->ty);
|
|
|
|
|
|
2023-10-21 02:10:30 +01:00
|
|
|
|
SubtypingResult result;
|
|
|
|
|
|
2023-09-15 18:26:59 +01:00
|
|
|
|
if (is<NeverType>(negatedTy))
|
|
|
|
|
{
|
|
|
|
|
// ¬never ~ unknown
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = isCovariantWith(env, subTy, builtinTypes->unknownType);
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
else if (is<UnknownType>(negatedTy))
|
|
|
|
|
{
|
|
|
|
|
// ¬unknown ~ never
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = isCovariantWith(env, subTy, builtinTypes->neverType);
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
else if (is<AnyType>(negatedTy))
|
|
|
|
|
{
|
|
|
|
|
// ¬any ~ any
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = isSubtype(subTy, negatedTy);
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
else if (auto u = get<UnionType>(negatedTy))
|
|
|
|
|
{
|
|
|
|
|
// ¬(A ∪ B) ~ ¬A ∩ ¬B
|
|
|
|
|
// follow intersection rules: A & B <: T iff A <: T && B <: T
|
|
|
|
|
std::vector<SubtypingResult> subtypings;
|
|
|
|
|
|
|
|
|
|
for (TypeId ty : u)
|
|
|
|
|
{
|
|
|
|
|
if (auto negatedPart = get<NegationType>(follow(ty)))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
subtypings.push_back(isCovariantWith(env, subTy, negatedPart->ty));
|
2023-09-15 18:26:59 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NegationType negatedTmp{ty};
|
2023-10-13 21:20:12 +01:00
|
|
|
|
subtypings.push_back(isCovariantWith(env, subTy, &negatedTmp));
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = SubtypingResult::all(subtypings);
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
else if (auto i = get<IntersectionType>(negatedTy))
|
|
|
|
|
{
|
|
|
|
|
// ¬(A ∩ B) ~ ¬A ∪ ¬B
|
|
|
|
|
// follow union rules: A | B <: T iff A <: T || B <: T
|
|
|
|
|
std::vector<SubtypingResult> subtypings;
|
|
|
|
|
|
|
|
|
|
for (TypeId ty : i)
|
|
|
|
|
{
|
|
|
|
|
if (auto negatedPart = get<NegationType>(follow(ty)))
|
2023-10-13 21:20:12 +01:00
|
|
|
|
subtypings.push_back(isCovariantWith(env, subTy, negatedPart->ty));
|
2023-09-15 18:26:59 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NegationType negatedTmp{ty};
|
2023-10-13 21:20:12 +01:00
|
|
|
|
subtypings.push_back(isCovariantWith(env, subTy, &negatedTmp));
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = SubtypingResult::any(subtypings);
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
else if (auto p = get2<PrimitiveType, PrimitiveType>(subTy, negatedTy))
|
|
|
|
|
{
|
|
|
|
|
// number <: ¬boolean
|
|
|
|
|
// number </: ¬number
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = {p.first->type != p.second->type};
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
else if (auto p = get2<SingletonType, PrimitiveType>(subTy, negatedTy))
|
|
|
|
|
{
|
|
|
|
|
// "foo" </: ¬string
|
|
|
|
|
if (get<StringSingleton>(p.first) && p.second->type == PrimitiveType::String)
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = {false};
|
2023-09-15 18:26:59 +01:00
|
|
|
|
// false </: ¬boolean
|
|
|
|
|
else if (get<BooleanSingleton>(p.first) && p.second->type == PrimitiveType::Boolean)
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = {false};
|
2023-09-15 18:26:59 +01:00
|
|
|
|
// other cases are true
|
|
|
|
|
else
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = {true};
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
else if (auto p = get2<PrimitiveType, SingletonType>(subTy, negatedTy))
|
|
|
|
|
{
|
|
|
|
|
if (p.first->type == PrimitiveType::String && get<StringSingleton>(p.second))
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = {false};
|
2023-09-15 18:26:59 +01:00
|
|
|
|
else if (p.first->type == PrimitiveType::Boolean && get<BooleanSingleton>(p.second))
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = {false};
|
2023-09-15 18:26:59 +01:00
|
|
|
|
else
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = {true};
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
// the top class type is not actually a primitive type, so the negation of
|
|
|
|
|
// any one of them includes the top class type.
|
|
|
|
|
else if (auto p = get2<ClassType, PrimitiveType>(subTy, negatedTy))
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = {true};
|
2023-09-15 18:26:59 +01:00
|
|
|
|
else if (auto p = get<PrimitiveType>(negatedTy); p && is<TableType, MetatableType>(subTy))
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = {p->type != PrimitiveType::Table};
|
2023-09-15 18:26:59 +01:00
|
|
|
|
else if (auto p = get2<FunctionType, PrimitiveType>(subTy, negatedTy))
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = {p.second->type != PrimitiveType::Function};
|
2023-09-15 18:26:59 +01:00
|
|
|
|
else if (auto p = get2<SingletonType, SingletonType>(subTy, negatedTy))
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = {*p.first != *p.second};
|
2023-09-15 18:26:59 +01:00
|
|
|
|
else if (auto p = get2<ClassType, ClassType>(subTy, negatedTy))
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = SubtypingResult::negate(isCovariantWith(env, p.first, p.second));
|
2023-09-15 18:26:59 +01:00
|
|
|
|
else if (get2<FunctionType, ClassType>(subTy, negatedTy))
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result = {true};
|
2023-09-15 18:26:59 +01:00
|
|
|
|
else if (is<ErrorType, FunctionType, TableType, MetatableType>(negatedTy))
|
|
|
|
|
iceReporter->ice("attempting to negate a non-testable type");
|
2023-10-21 02:10:30 +01:00
|
|
|
|
else
|
|
|
|
|
result = {false};
|
2023-09-15 18:26:59 +01:00
|
|
|
|
|
2023-10-21 02:10:30 +01:00
|
|
|
|
return result.withSuperComponent(TypePath::TypeField::Negated);
|
2023-09-15 18:26:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const PrimitiveType* subPrim, const PrimitiveType* superPrim)
|
2023-08-18 19:15:41 +01:00
|
|
|
|
{
|
|
|
|
|
return {subPrim->type == superPrim->type};
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const SingletonType* subSingleton, const PrimitiveType* superPrim)
|
2023-08-18 19:15:41 +01:00
|
|
|
|
{
|
|
|
|
|
if (get<StringSingleton>(subSingleton) && superPrim->type == PrimitiveType::String)
|
|
|
|
|
return {true};
|
|
|
|
|
else if (get<BooleanSingleton>(subSingleton) && superPrim->type == PrimitiveType::Boolean)
|
|
|
|
|
return {true};
|
|
|
|
|
else
|
|
|
|
|
return {false};
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const SingletonType* subSingleton, const SingletonType* superSingleton)
|
2023-08-18 19:15:41 +01:00
|
|
|
|
{
|
|
|
|
|
return {*subSingleton == *superSingleton};
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const TableType* subTable, const TableType* superTable)
|
2023-08-18 19:15:41 +01:00
|
|
|
|
{
|
2023-08-25 18:23:55 +01:00
|
|
|
|
SubtypingResult result{true};
|
|
|
|
|
|
2023-09-22 20:12:15 +01:00
|
|
|
|
if (subTable->props.empty() && !subTable->indexer && superTable->indexer)
|
|
|
|
|
return {false};
|
|
|
|
|
|
2023-09-15 18:26:59 +01:00
|
|
|
|
for (const auto& [name, prop] : superTable->props)
|
2023-08-25 18:23:55 +01:00
|
|
|
|
{
|
2023-09-22 20:12:15 +01:00
|
|
|
|
std::vector<SubtypingResult> results;
|
|
|
|
|
if (auto it = subTable->props.find(name); it != subTable->props.end())
|
2023-10-21 02:10:30 +01:00
|
|
|
|
results.push_back(isInvariantWith(env, it->second.type(), prop.type())
|
|
|
|
|
.withBothComponent(TypePath::Property(name)));
|
2023-09-22 20:12:15 +01:00
|
|
|
|
|
|
|
|
|
if (subTable->indexer)
|
|
|
|
|
{
|
2023-10-13 21:20:12 +01:00
|
|
|
|
if (isInvariantWith(env, subTable->indexer->indexType, builtinTypes->stringType).isSubtype)
|
2023-10-21 02:10:30 +01:00
|
|
|
|
results.push_back(isInvariantWith(env, subTable->indexer->indexResultType, prop.type())
|
|
|
|
|
.withSubComponent(TypePath::TypeField::IndexResult)
|
|
|
|
|
.withSuperComponent(TypePath::Property(name)));
|
2023-09-22 20:12:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (results.empty())
|
2023-10-21 02:10:30 +01:00
|
|
|
|
return SubtypingResult{false};
|
2023-09-22 20:12:15 +01:00
|
|
|
|
|
|
|
|
|
result.andAlso(SubtypingResult::all(results));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (superTable->indexer)
|
|
|
|
|
{
|
|
|
|
|
if (subTable->indexer)
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result.andAlso(isInvariantWith(env, *subTable->indexer, *superTable->indexer));
|
2023-08-25 18:23:55 +01:00
|
|
|
|
else
|
2023-09-22 20:12:15 +01:00
|
|
|
|
return {false};
|
2023-08-25 18:23:55 +01:00
|
|
|
|
}
|
2023-08-18 19:15:41 +01:00
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
|
return result;
|
2023-08-18 19:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const MetatableType* subMt, const MetatableType* superMt)
|
2023-09-01 18:58:27 +01:00
|
|
|
|
{
|
2023-10-21 02:10:30 +01:00
|
|
|
|
return isCovariantWith(env, subMt->table, superMt->table)
|
|
|
|
|
.andAlso(isCovariantWith(env, subMt->metatable, superMt->metatable).withBothComponent(TypePath::TypeField::Metatable));
|
2023-09-01 18:58:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const MetatableType* subMt, const TableType* superTable)
|
2023-09-01 18:58:27 +01:00
|
|
|
|
{
|
2023-11-17 18:46:18 +00:00
|
|
|
|
if (auto subTable = get<TableType>(follow(subMt->table)))
|
2023-09-15 18:26:59 +01:00
|
|
|
|
{
|
2023-09-01 18:58:27 +01:00
|
|
|
|
// Metatables cannot erase properties from the table they're attached to, so
|
|
|
|
|
// the subtyping rule for this is just if the table component is a subtype
|
|
|
|
|
// of the supertype table.
|
|
|
|
|
//
|
|
|
|
|
// There's a flaw here in that if the __index metamethod contributes a new
|
|
|
|
|
// field that would satisfy the subtyping relationship, we'll erronously say
|
|
|
|
|
// that the metatable isn't a subtype of the table, even though they have
|
|
|
|
|
// compatible properties/shapes. We'll revisit this later when we have a
|
|
|
|
|
// better understanding of how important this is.
|
2023-10-13 21:20:12 +01:00
|
|
|
|
return isCovariantWith(env, subTable, superTable);
|
2023-09-01 18:58:27 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// TODO: This may be a case we actually hit?
|
|
|
|
|
return {false};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const ClassType* subClass, const ClassType* superClass)
|
2023-09-01 18:58:27 +01:00
|
|
|
|
{
|
|
|
|
|
return {isSubclass(subClass, superClass)};
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const ClassType* subClass, const TableType* superTable)
|
2023-09-01 18:58:27 +01:00
|
|
|
|
{
|
|
|
|
|
SubtypingResult result{true};
|
|
|
|
|
|
2023-09-15 18:26:59 +01:00
|
|
|
|
for (const auto& [name, prop] : superTable->props)
|
2023-09-01 18:58:27 +01:00
|
|
|
|
{
|
|
|
|
|
if (auto classProp = lookupClassProp(subClass, name))
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result.andAlso(isInvariantWith(env, prop.type(), classProp->type()).withBothComponent(TypePath::Property(name)));
|
2023-09-01 18:58:27 +01:00
|
|
|
|
else
|
|
|
|
|
return SubtypingResult{false};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const FunctionType* subFunction, const FunctionType* superFunction)
|
2023-08-25 18:23:55 +01:00
|
|
|
|
{
|
|
|
|
|
SubtypingResult result;
|
|
|
|
|
{
|
|
|
|
|
VarianceFlipper vf{&variance};
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result.orElse(isContravariantWith(env, subFunction->argTypes, superFunction->argTypes).withBothComponent(TypePath::PackField::Arguments));
|
2023-08-25 18:23:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result.andAlso(isCovariantWith(env, subFunction->retTypes, superFunction->retTypes).withBothComponent(TypePath::PackField::Returns));
|
2023-08-25 18:23:55 +01:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const PrimitiveType* subPrim, const TableType* superTable)
|
2023-09-01 18:58:27 +01:00
|
|
|
|
{
|
|
|
|
|
SubtypingResult result{false};
|
|
|
|
|
if (subPrim->type == PrimitiveType::String)
|
|
|
|
|
{
|
|
|
|
|
if (auto metatable = getMetatable(builtinTypes->stringType, builtinTypes))
|
|
|
|
|
{
|
|
|
|
|
if (auto mttv = get<TableType>(follow(metatable)))
|
|
|
|
|
{
|
|
|
|
|
if (auto it = mttv->props.find("__index"); it != mttv->props.end())
|
|
|
|
|
{
|
|
|
|
|
if (auto stringTable = get<TableType>(it->second.type()))
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result.orElse(
|
|
|
|
|
isCovariantWith(env, stringTable, superTable).withSubPath(TypePath::PathBuilder().mt().prop("__index").build()));
|
2023-09-01 18:58:27 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const SingletonType* subSingleton, const TableType* superTable)
|
2023-09-01 18:58:27 +01:00
|
|
|
|
{
|
|
|
|
|
SubtypingResult result{false};
|
|
|
|
|
if (auto stringleton = get<StringSingleton>(subSingleton))
|
|
|
|
|
{
|
|
|
|
|
if (auto metatable = getMetatable(builtinTypes->stringType, builtinTypes))
|
|
|
|
|
{
|
|
|
|
|
if (auto mttv = get<TableType>(follow(metatable)))
|
|
|
|
|
{
|
|
|
|
|
if (auto it = mttv->props.find("__index"); it != mttv->props.end())
|
|
|
|
|
{
|
|
|
|
|
if (auto stringTable = get<TableType>(it->second.type()))
|
2023-10-21 02:10:30 +01:00
|
|
|
|
result.orElse(
|
|
|
|
|
isCovariantWith(env, stringTable, superTable).withSubPath(TypePath::PathBuilder().mt().prop("__index").build()));
|
2023-09-01 18:58:27 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const TableIndexer& subIndexer, const TableIndexer& superIndexer)
|
2023-09-22 20:12:15 +01:00
|
|
|
|
{
|
2023-10-13 21:20:12 +01:00
|
|
|
|
return isInvariantWith(env, subIndexer.indexType, superIndexer.indexType)
|
2023-10-21 02:10:30 +01:00
|
|
|
|
.withBothComponent(TypePath::TypeField::IndexLookup)
|
|
|
|
|
.andAlso(isInvariantWith(env, superIndexer.indexResultType, subIndexer.indexResultType).withBothComponent(TypePath::TypeField::IndexResult));
|
2023-09-22 20:12:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const NormalizedType* subNorm, const NormalizedType* superNorm)
|
2023-08-18 19:15:41 +01:00
|
|
|
|
{
|
|
|
|
|
if (!subNorm || !superNorm)
|
|
|
|
|
return {false, true, true};
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult result = isCovariantWith(env, subNorm->tops, superNorm->tops);
|
|
|
|
|
result.andAlso(isCovariantWith(env, subNorm->booleans, superNorm->booleans));
|
|
|
|
|
result.andAlso(isCovariantWith(env, subNorm->classes, superNorm->classes).orElse(isCovariantWith(env, subNorm->classes, superNorm->tables)));
|
|
|
|
|
result.andAlso(isCovariantWith(env, subNorm->errors, superNorm->errors));
|
|
|
|
|
result.andAlso(isCovariantWith(env, subNorm->nils, superNorm->nils));
|
|
|
|
|
result.andAlso(isCovariantWith(env, subNorm->numbers, superNorm->numbers));
|
|
|
|
|
result.andAlso(isCovariantWith(env, subNorm->strings, superNorm->strings));
|
|
|
|
|
result.andAlso(isCovariantWith(env, subNorm->strings, superNorm->tables));
|
|
|
|
|
result.andAlso(isCovariantWith(env, subNorm->threads, superNorm->threads));
|
|
|
|
|
result.andAlso(isCovariantWith(env, subNorm->tables, superNorm->tables));
|
|
|
|
|
result.andAlso(isCovariantWith(env, subNorm->functions, superNorm->functions));
|
2023-09-15 18:26:59 +01:00
|
|
|
|
// isCovariantWith(subNorm->tyvars, superNorm->tyvars);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const NormalizedClassType& subClass, const NormalizedClassType& superClass)
|
2023-09-01 18:58:27 +01:00
|
|
|
|
{
|
|
|
|
|
for (const auto& [subClassTy, _] : subClass.classes)
|
|
|
|
|
{
|
|
|
|
|
SubtypingResult result;
|
|
|
|
|
|
|
|
|
|
for (const auto& [superClassTy, superNegations] : superClass.classes)
|
|
|
|
|
{
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result.orElse(isCovariantWith(env, subClassTy, superClassTy));
|
2023-09-01 18:58:27 +01:00
|
|
|
|
if (!result.isSubtype)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
for (TypeId negation : superNegations)
|
|
|
|
|
{
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result.andAlso(SubtypingResult::negate(isCovariantWith(env, subClassTy, negation)));
|
2023-09-01 18:58:27 +01:00
|
|
|
|
if (result.isSubtype)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 18:26:59 +01:00
|
|
|
|
if (!result.isSubtype)
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {true};
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const NormalizedClassType& subClass, const TypeIds& superTables)
|
2023-09-15 18:26:59 +01:00
|
|
|
|
{
|
|
|
|
|
for (const auto& [subClassTy, _] : subClass.classes)
|
|
|
|
|
{
|
|
|
|
|
SubtypingResult result;
|
2023-09-01 18:58:27 +01:00
|
|
|
|
|
|
|
|
|
for (TypeId superTableTy : superTables)
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result.orElse(isCovariantWith(env, subClassTy, superTableTy));
|
2023-09-01 18:58:27 +01:00
|
|
|
|
|
|
|
|
|
if (!result.isSubtype)
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {true};
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const NormalizedStringType& subString, const NormalizedStringType& superString)
|
2023-09-15 18:26:59 +01:00
|
|
|
|
{
|
|
|
|
|
bool isSubtype = Luau::isSubtype(subString, superString);
|
|
|
|
|
return {isSubtype};
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const NormalizedStringType& subString, const TypeIds& superTables)
|
2023-09-15 18:26:59 +01:00
|
|
|
|
{
|
|
|
|
|
if (subString.isNever())
|
|
|
|
|
return {true};
|
|
|
|
|
|
|
|
|
|
if (subString.isCofinite)
|
|
|
|
|
{
|
|
|
|
|
SubtypingResult result;
|
|
|
|
|
for (const auto& superTable : superTables)
|
|
|
|
|
{
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result.orElse(isCovariantWith(env, builtinTypes->stringType, superTable));
|
2023-09-15 18:26:59 +01:00
|
|
|
|
if (result.isSubtype)
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Finite case
|
|
|
|
|
// S = s1 | s2 | s3 ... sn <: t1 | t2 | ... | tn
|
|
|
|
|
// iff for some ti, S <: ti
|
|
|
|
|
// iff for all sj, sj <: ti
|
|
|
|
|
for (const auto& superTable : superTables)
|
|
|
|
|
{
|
|
|
|
|
SubtypingResult result{true};
|
|
|
|
|
for (const auto& [_, subString] : subString.singletons)
|
|
|
|
|
{
|
2023-10-13 21:20:12 +01:00
|
|
|
|
result.andAlso(isCovariantWith(env, subString, superTable));
|
2023-09-15 18:26:59 +01:00
|
|
|
|
if (!result.isSubtype)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!result.isSubtype)
|
|
|
|
|
continue;
|
|
|
|
|
else
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {false};
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(
|
|
|
|
|
SubtypingEnvironment& env, const NormalizedFunctionType& subFunction, const NormalizedFunctionType& superFunction)
|
2023-09-08 01:13:49 +01:00
|
|
|
|
{
|
|
|
|
|
if (subFunction.isNever())
|
|
|
|
|
return {true};
|
|
|
|
|
else if (superFunction.isTop)
|
|
|
|
|
return {true};
|
|
|
|
|
else
|
2023-10-13 21:20:12 +01:00
|
|
|
|
return isCovariantWith(env, subFunction.parts, superFunction.parts);
|
2023-09-08 01:13:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const TypeIds& subTypes, const TypeIds& superTypes)
|
2023-09-01 18:58:27 +01:00
|
|
|
|
{
|
|
|
|
|
std::vector<SubtypingResult> results;
|
|
|
|
|
|
2023-10-21 02:10:30 +01:00
|
|
|
|
size_t i = 0;
|
2023-09-01 18:58:27 +01:00
|
|
|
|
for (TypeId subTy : subTypes)
|
|
|
|
|
{
|
|
|
|
|
results.emplace_back();
|
|
|
|
|
for (TypeId superTy : superTypes)
|
2023-10-21 02:10:30 +01:00
|
|
|
|
results.back().orElse(isCovariantWith(env, subTy, superTy).withBothComponent(TypePath::Index{i++}));
|
2023-09-01 18:58:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return SubtypingResult::all(results);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const VariadicTypePack* subVariadic, const VariadicTypePack* superVariadic)
|
2023-09-01 18:58:27 +01:00
|
|
|
|
{
|
2023-10-21 02:10:30 +01:00
|
|
|
|
return isCovariantWith(env, subVariadic->ty, superVariadic->ty).withBothComponent(TypePath::TypeField::Variadic);
|
2023-09-01 18:58:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
bool Subtyping::bindGeneric(SubtypingEnvironment& env, TypeId subTy, TypeId superTy)
|
2023-08-25 18:23:55 +01:00
|
|
|
|
{
|
|
|
|
|
if (variance == Variance::Covariant)
|
|
|
|
|
{
|
|
|
|
|
if (!get<GenericType>(subTy))
|
|
|
|
|
return false;
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
env.mappedGenerics[subTy].upperBound.insert(superTy);
|
2023-08-25 18:23:55 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!get<GenericType>(superTy))
|
|
|
|
|
return false;
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
env.mappedGenerics[superTy].lowerBound.insert(subTy);
|
2023-08-25 18:23:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If, when performing a subtyping test, we encounter a generic on the left
|
|
|
|
|
* side, it is permissible to tentatively bind that generic to the right side
|
|
|
|
|
* type.
|
|
|
|
|
*/
|
2023-10-13 21:20:12 +01:00
|
|
|
|
bool Subtyping::bindGeneric(SubtypingEnvironment& env, TypePackId subTp, TypePackId superTp)
|
2023-08-25 18:23:55 +01:00
|
|
|
|
{
|
|
|
|
|
if (variance == Variance::Contravariant)
|
|
|
|
|
std::swap(superTp, subTp);
|
|
|
|
|
|
|
|
|
|
if (!get<GenericTypePack>(subTp))
|
|
|
|
|
return false;
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
if (TypePackId* m = env.mappedGenericPacks.find(subTp))
|
2023-08-25 18:23:55 +01:00
|
|
|
|
return *m == superTp;
|
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
|
env.mappedGenericPacks[subTp] = superTp;
|
2023-08-25 18:23:55 +01:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 18:26:59 +01:00
|
|
|
|
template<typename T, typename Container>
|
2023-08-25 18:23:55 +01:00
|
|
|
|
TypeId Subtyping::makeAggregateType(const Container& container, TypeId orElse)
|
|
|
|
|
{
|
|
|
|
|
if (container.empty())
|
|
|
|
|
return orElse;
|
|
|
|
|
else if (container.size() == 1)
|
|
|
|
|
return *begin(container);
|
|
|
|
|
else
|
|
|
|
|
return arena->addType(T{std::vector<TypeId>(begin(container), end(container))});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Subtyping::unexpected(TypePackId tp)
|
|
|
|
|
{
|
|
|
|
|
iceReporter->ice(format("Unexpected type pack %s", toString(tp).c_str()));
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 19:15:41 +01:00
|
|
|
|
} // namespace Luau
|