Skip to content

[Sema] Ensure that NameAliasTypes record the right substitutions. #16172

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
Apr 27, 2018
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
39 changes: 18 additions & 21 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6938,29 +6938,26 @@ void TypeChecker::validateDecl(ValueDecl *D) {
if (!aliasDecl->isGeneric()) {
aliasDecl->setGenericEnvironment(proto->getGenericEnvironment());

// If the underlying alias declaration has a type parameter,
// we have unresolved dependent member types we will need to deal
// with. Wipe out the types and validate them again.
// FIXME: We never should have recorded such a type in the first
// place.
if (!aliasDecl->getUnderlyingTypeLoc().getType() ||
aliasDecl->getUnderlyingTypeLoc().getType()
->findUnresolvedDependentMemberType()) {
aliasDecl->getUnderlyingTypeLoc().setType(Type(),
/*validated=*/false);
validateAccessControl(aliasDecl);

// Check generic parameters, if needed.
bool validated = aliasDecl->hasValidationStarted();
// The generic environment didn't exist until now, we may have
// unresolved types we will need to deal with, and need to record the
// appropriate substitutions for that environment. Wipe out the types
// and validate them again.
aliasDecl->getUnderlyingTypeLoc().setType(Type(),
/*validated=*/false);
aliasDecl->setInterfaceType(Type());

validateAccessControl(aliasDecl);

// Check generic parameters, if needed.
bool validated = aliasDecl->hasValidationStarted();
if (!validated)
aliasDecl->setIsBeingValidated();
SWIFT_DEFER {
if (!validated)
aliasDecl->setIsBeingValidated();
SWIFT_DEFER {
if (!validated)
aliasDecl->setIsBeingValidated(false);
};
aliasDecl->setIsBeingValidated(false);
};

validateTypealiasType(*this, aliasDecl);
}
validateTypealiasType(*this, aliasDecl);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions lib/Sema/TypeCheckGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@ Type CompleteGenericTypeResolver::resolveDependentMemberType(
// base type into it.
auto concrete = ref->getBoundDecl();
tc.validateDeclForNameLookup(concrete);

if (auto typeAlias = dyn_cast<TypeAliasDecl>(concrete)) {
if (auto protocol = dyn_cast<ProtocolDecl>(typeAlias->getDeclContext())) {
// We need to make sure the generic environment of a surrounding protocol
// propagates to the typealias, since the former may not have existed when
// the typealiases type was first computed.
// FIXME: See the comment in the ProtocolDecl case of validateDecl().
tc.validateDecl(protocol);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you just unconditionally call validateDecl() and remove the validateDeclForNameLookup() call above?

}
}
if (!concrete->hasInterfaceType())
return ErrorType::get(tc.Context);
if (baseTy->isTypeParameter()) {
Expand Down
11 changes: 11 additions & 0 deletions validation-test/compiler_crashers_2_fixed/0156-rdar39636312.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %target-swift-frontend %s -emit-module

protocol P2 {
func f1<T : P1>(_: T) -> T.T1
}

public struct S1<U> {}

protocol P1 {
typealias T1 = S1<Self>
}