Fix the stack-use-after-scope.

This commit is contained in:
Alexander McCord 2024-02-02 13:20:35 -08:00
parent 88d2b93351
commit 5559c7fbd5
2 changed files with 16 additions and 8 deletions

View File

@ -140,6 +140,8 @@ struct TryPair
template<typename A, typename B, typename Ty> template<typename A, typename B, typename Ty>
TryPair<const A*, const B*> get2(Ty one, Ty two) TryPair<const A*, const B*> get2(Ty one, Ty two)
{ {
static_assert(std::is_pointer_v<Ty>, "argument must be a pointer type");
const A* a = get<A>(one); const A* a = get<A>(one);
const B* b = get<B>(two); const B* b = get<B>(two);
if (a && b) if (a && b)

View File

@ -2400,14 +2400,20 @@ struct TypeChecker2
if (reasoning.subPath.empty() && reasoning.superPath.empty()) if (reasoning.subPath.empty() && reasoning.superPath.empty())
continue; continue;
std::optional<TypeOrPack> subLeaf = traverse(subTy, reasoning.subPath, builtinTypes); std::optional<TypeOrPack> optSubLeaf = traverse(subTy, reasoning.subPath, builtinTypes);
std::optional<TypeOrPack> superLeaf = traverse(superTy, reasoning.superPath, builtinTypes); std::optional<TypeOrPack> optSuperLeaf = traverse(superTy, reasoning.superPath, builtinTypes);
if (!subLeaf || !superLeaf) if (!optSubLeaf || !optSuperLeaf)
ice->ice("Subtyping test returned a reasoning with an invalid path", location); ice->ice("Subtyping test returned a reasoning with an invalid path", location);
auto [subLeafTy, superLeafTy] = get2<TypeId, TypeId>(*subLeaf, *superLeaf); const TypeOrPack& subLeaf = *optSubLeaf;
auto [subLeafTp, superLeafTp] = get2<TypePackId, TypePackId>(*subLeaf, *superLeaf); const TypeOrPack& superLeaf = *optSuperLeaf;
auto subLeafTy = get<TypeId>(subLeaf);
auto superLeafTy = get<TypeId>(superLeaf);
auto subLeafTp = get<TypePackId>(subLeaf);
auto superLeafTp = get<TypePackId>(superLeaf);
if (!subLeafTy && !superLeafTy && !subLeafTp && !superLeafTp) if (!subLeafTy && !superLeafTy && !subLeafTp && !superLeafTp)
ice->ice("Subtyping test returned a reasoning where one path ends at a type and the other ends at a pack.", location); ice->ice("Subtyping test returned a reasoning where one path ends at a type and the other ends at a pack.", location);
@ -2420,10 +2426,10 @@ struct TypeChecker2
std::string reason; std::string reason;
if (reasoning.subPath == reasoning.superPath) if (reasoning.subPath == reasoning.superPath)
reason = "at " + toString(reasoning.subPath) + ", " + toString(*subLeaf) + " is not " + relation + " " + toString(*superLeaf); reason = "at " + toString(reasoning.subPath) + ", " + toString(subLeaf) + " is not " + relation + " " + toString(superLeaf);
else else
reason = "type " + toString(subTy) + toString(reasoning.subPath, /* prefixDot */ true) + " (" + toString(*subLeaf) + ") is not " + reason = "type " + toString(subTy) + toString(reasoning.subPath, /* prefixDot */ true) + " (" + toString(subLeaf) + ") is not " +
relation + " " + toString(superTy) + toString(reasoning.superPath, /* prefixDot */ true) + " (" + toString(*superLeaf) + ")"; relation + " " + toString(superTy) + toString(reasoning.superPath, /* prefixDot */ true) + " (" + toString(superLeaf) + ")";
reasons.push_back(reason); reasons.push_back(reason);