Skip to content

[5.3][ConstraintSystem] Always verify computed/resolved pattern types befo… #31614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 42 additions & 7 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2254,6 +2254,8 @@ namespace {

switch (pattern->getKind()) {
case PatternKind::Paren: {
auto *paren = cast<ParenPattern>(pattern);

// Parentheses don't affect the canonical type, but record them as
// type sugar.
if (externalPatternType &&
Expand All @@ -2262,13 +2264,14 @@ namespace {
->getUnderlyingType();
}

return setType(
ParenType::get(
CS.getASTContext(),
getTypeForPattern(
cast<ParenPattern>(pattern)->getSubPattern(), locator,
externalPatternType,
bindPatternVarsOneWay)));
auto underlyingType =
getTypeForPattern(paren->getSubPattern(), locator,
externalPatternType, bindPatternVarsOneWay);

if (!underlyingType)
return Type();

return setType(ParenType::get(CS.getASTContext(), underlyingType));
}
case PatternKind::Var:
// Var doesn't affect the type.
Expand Down Expand Up @@ -2370,10 +2373,14 @@ namespace {

Type type = TypeChecker::typeCheckPattern(contextualPattern);

if (!type)
return Type();

// Look through reference storage types.
type = type->getReferenceStorageReferent();

Type openedType = CS.openUnboundGenericType(type, locator);
assert(openedType);

auto *subPattern = cast<TypedPattern>(pattern)->getSubPattern();
// Determine the subpattern type. It will be convertible to the
Expand All @@ -2383,6 +2390,9 @@ namespace {
locator.withPathElement(LocatorPathElt::PatternMatch(subPattern)),
Type(), bindPatternVarsOneWay);

if (!subPatternType)
return Type();

CS.addConstraint(
ConstraintKind::Conversion, subPatternType, openedType,
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
Expand Down Expand Up @@ -2425,6 +2435,10 @@ namespace {
eltPattern,
locator.withPathElement(LocatorPathElt::PatternMatch(eltPattern)),
externalEltType, bindPatternVarsOneWay);

if (!eltTy)
return Type();

tupleTypeElts.push_back(TupleTypeElt(eltTy, tupleElt.getLabel()));
}

Expand Down Expand Up @@ -2452,6 +2466,9 @@ namespace {
locator.withPathElement(LocatorPathElt::PatternMatch(subPattern)),
externalPatternType, bindPatternVarsOneWay);

if (!subPatternType)
return Type();

return setType(OptionalType::get(subPatternType));
}

Expand All @@ -2460,16 +2477,25 @@ namespace {

Type castType =
resolveTypeReferenceInExpression(isPattern->getCastTypeLoc());

if (!castType)
return Type();

castType = CS.openUnboundGenericType(
castType,
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));

assert(castType);

auto *subPattern = isPattern->getSubPattern();
Type subPatternType = getTypeForPattern(
subPattern,
locator.withPathElement(LocatorPathElt::PatternMatch(subPattern)),
castType, bindPatternVarsOneWay);

if (!subPatternType)
return Type();

// Make sure we can cast from the subpattern type to the type we're
// checking; if it's impossible, fail.
CS.addConstraint(
Expand Down Expand Up @@ -2502,11 +2528,16 @@ namespace {
Type parentType =
resolveTypeReferenceInExpression(enumPattern->getParentType());

if (!parentType)
return Type();

parentType = CS.openUnboundGenericType(
parentType, CS.getConstraintLocator(
locator, {LocatorPathElt::PatternMatch(pattern),
ConstraintLocator::ParentType}));

assert(parentType);

// Perform member lookup into the parent's metatype.
Type parentMetaType = MetatypeType::get(parentType);
CS.addValueMemberConstraint(
Expand Down Expand Up @@ -2539,6 +2570,10 @@ namespace {
// types.
Type subPatternType = getTypeForPattern(
subPattern, locator, Type(), bindPatternVarsOneWay);

if (!subPatternType)
return Type();

SmallVector<AnyFunctionType::Param, 4> params;
AnyFunctionType::decomposeInput(subPatternType, params);

Expand Down