Skip to content

Better enforce GenericFunctionType TypeVariableType invariant #64335

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
Mar 14, 2023
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4178,6 +4178,13 @@ GenericFunctionType *GenericFunctionType::get(GenericSignature sig,
Type result,
Optional<ExtInfo> info) {
assert(sig && "no generic signature for generic function type?!");

// We do not allow type variables in GenericFunctionTypes. Note that if this
// ever changes, we'll need to setup arena-specific allocation for
// GenericFunctionTypes.
assert(llvm::none_of(params, [](Param param) {
return param.getPlainType()->hasTypeVariable();
}));
assert(!result->hasTypeVariable());

llvm::FoldingSetNodeID id;
Expand Down
61 changes: 32 additions & 29 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2364,6 +2364,13 @@ ConstraintSystem::getTypeOfMemberReference(
return { openedType, openedType, memberTy, memberTy };
}

if (isa<AbstractFunctionDecl>(value) || isa<EnumElementDecl>(value)) {
if (value->getInterfaceType()->is<ErrorType>()) {
auto genericErrorTy = ErrorType::get(getASTContext());
return { genericErrorTy, genericErrorTy, genericErrorTy, genericErrorTy };
}
}

// Figure out the declaration context to use when opening this type.
DeclContext *innerDC = value->getInnermostDeclContext();
DeclContext *outerDC = value->getDeclContext();
Expand All @@ -2372,18 +2379,22 @@ ConstraintSystem::getTypeOfMemberReference(
Type openedType;
OpenedTypeMap localReplacements;
auto &replacements = replacementsPtr ? *replacementsPtr : localReplacements;
unsigned numRemovedArgumentLabels = getNumRemovedArgumentLabels(
value, /*isCurriedInstanceReference*/ !hasAppliedSelf, functionRefKind);

AnyFunctionType *funcType;
// If we have a generic signature, open the parameters. We delay opening
// requirements to allow contextual types to affect the situation.
auto genericSig = innerDC->getGenericSignatureOfContext();
if (genericSig)
openGenericParameters(outerDC, genericSig, replacements, locator);

if (isa<AbstractFunctionDecl>(value) || isa<EnumElementDecl>(value)) {
if (auto ErrorTy = value->getInterfaceType()->getAs<ErrorType>()) {
auto genericErrorTy = ErrorType::get(ErrorTy->getASTContext());
return { genericErrorTy, genericErrorTy, genericErrorTy, genericErrorTy };
}
// This is the easy case.
funcType = value->getInterfaceType()->castTo<AnyFunctionType>();
openedType = value->getInterfaceType()->castTo<AnyFunctionType>();

if (auto *genericFn = openedType->getAs<GenericFunctionType>()) {
openedType = genericFn->substGenericArgs([&](Type type) {
return openType(type, replacements);
});
}
} else {
// For a property, build a type (Self) -> PropType.
// For a subscript, build a type (Self) -> (Indices...) -> ElementType.
Expand Down Expand Up @@ -2424,29 +2435,21 @@ ConstraintSystem::getTypeOfMemberReference(
!selfTy->hasError())
selfFlags = selfFlags.withInOut(true);

// If the storage is generic, add a generic signature.
// If the storage is generic, open the self and ref types.
if (genericSig) {
selfTy = openType(selfTy, replacements);
refType = openType(refType, replacements);
}
FunctionType::Param selfParam(selfTy, Identifier(), selfFlags);

// FIXME: Verify ExtInfo state is correct, not working by accident.
if (auto sig = innerDC->getGenericSignatureOfContext()) {
GenericFunctionType::ExtInfo info;
funcType = GenericFunctionType::get(sig, {selfParam}, refType, info);
} else {
FunctionType::ExtInfo info;
funcType = FunctionType::get({selfParam}, refType, info);
}
FunctionType::ExtInfo info;
openedType = FunctionType::get({selfParam}, refType, info);
}
assert(!openedType->hasTypeParameter());

// While opening member function type, let's delay opening requirements
// to allow contextual types to affect the situation.
if (auto *genericFn = funcType->getAs<GenericFunctionType>()) {
openGenericParameters(outerDC, genericFn->getGenericSignature(),
replacements, locator);

openedType = genericFn->substGenericArgs(
[&](Type type) { return openType(type, replacements); });
} else {
openedType = funcType;
}
unsigned numRemovedArgumentLabels = getNumRemovedArgumentLabels(
value, /*isCurriedInstanceReference*/ !hasAppliedSelf, functionRefKind);

openedType = openedType->removeArgumentLabels(numRemovedArgumentLabels);

Expand Down Expand Up @@ -2497,9 +2500,9 @@ ConstraintSystem::getTypeOfMemberReference(
// failing we'll get a generic requirement constraint failure
// if mismatch is related to generic parameters which is much
// easier to diagnose.
if (auto *genericFn = funcType->getAs<GenericFunctionType>()) {
if (genericSig) {
openGenericRequirements(
outerDC, genericFn->getGenericSignature(),
outerDC, genericSig,
/*skipProtocolSelfConstraint=*/true, locator,
[&](Type type) { return openType(type, replacements); });
}
Expand Down