-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Clean up extension validation #19386
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
slavapestov
merged 5 commits into
swiftlang:master
from
slavapestov:validate-extension-cleanup
Sep 19, 2018
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7eed6c4
AST: Remove LazyResolver::bindExtension()
slavapestov f466aeb
Sema: Don't call bindExtension() in inferTypeWitnessesViaValueWitness…
slavapestov 26fddb7
Sema: bindExtensions() doesn't need to diagnose extensions right away
slavapestov ef82cd7
Sema: Merge bindExtensionDecl() into validateExtension()
slavapestov 4c7e889
Sema: isPassThroughTypealias() is only used in one file
slavapestov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4689,7 +4689,25 @@ void TypeChecker::finalizeDecl(ValueDecl *decl) { | |
(void)decl->isDynamic(); | ||
} | ||
|
||
bool swift::isPassThroughTypealias(TypeAliasDecl *typealias) { | ||
/// Determine whether this is a "pass-through" typealias, which has the | ||
/// same type parameters as the nominal type it references and specializes | ||
/// the underlying nominal type with exactly those type parameters. | ||
/// For example, the following typealias \c GX is a pass-through typealias: | ||
/// | ||
/// \code | ||
/// struct X<T, U> { } | ||
/// typealias GX<A, B> = X<A, B> | ||
/// \endcode | ||
/// | ||
/// whereas \c GX2 and \c GX3 are not pass-through because \c GX2 has | ||
/// different type parameters and \c GX3 doesn't pass its type parameters | ||
/// directly through. | ||
/// | ||
/// \code | ||
/// typealias GX2<A> = X<A, A> | ||
/// typealias GX3<A, B> = X<B, A> | ||
/// \endcode | ||
static bool isPassThroughTypealias(TypeAliasDecl *typealias) { | ||
// Pass-through only makes sense when the typealias refers to a nominal | ||
// type. | ||
Type underlyingType = typealias->getUnderlyingTypeLoc().getType(); | ||
|
@@ -4877,31 +4895,78 @@ void TypeChecker::validateExtension(ExtensionDecl *ext) { | |
if (ext->hasValidationStarted()) | ||
return; | ||
|
||
bindExtension(ext); | ||
|
||
DeclValidationRAII IBV(ext); | ||
|
||
// If the extension is already known to be invalid, we're done. | ||
if (ext->isInvalid()) | ||
auto dc = ext->getDeclContext(); | ||
|
||
// If we didn't parse a type, fill in an error type and bail out. | ||
if (!ext->getExtendedTypeLoc().getTypeRepr()) { | ||
ext->setInvalid(); | ||
ext->getExtendedTypeLoc().setInvalidType(Context); | ||
return; | ||
} | ||
|
||
// Validate the extended type. | ||
TypeResolutionOptions options(TypeResolverContext::ExtensionBinding); | ||
options |= TypeResolutionFlags::AllowUnboundGenerics; | ||
if (validateType(ext->getExtendedTypeLoc(), | ||
TypeResolution::forContextual(dc), options)) { | ||
ext->setInvalid(); | ||
ext->getExtendedTypeLoc().setInvalidType(Context); | ||
return; | ||
} | ||
|
||
// FIXME: We need to check whether anything is specialized, because | ||
// the innermost extended type might itself be a non-generic type | ||
// within a generic type. | ||
// Dig out the extended type. | ||
auto extendedType = ext->getExtendedType(); | ||
|
||
if (extendedType.isNull() || extendedType->hasError()) | ||
// Hack to allow extending a generic typealias. | ||
if (auto *unboundGeneric = extendedType->getAs<UnboundGenericType>()) { | ||
if (auto *aliasDecl = dyn_cast<TypeAliasDecl>(unboundGeneric->getDecl())) { | ||
auto extendedNominal = aliasDecl->getDeclaredInterfaceType()->getAnyNominal(); | ||
if (extendedNominal) { | ||
extendedType = extendedNominal->getDeclaredType(); | ||
if (!isPassThroughTypealias(aliasDecl)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose this is where we should diagnose non-passthrough |
||
ext->getExtendedTypeLoc().setType(extendedType); | ||
} | ||
} | ||
} | ||
|
||
// Cannot extend a metatype. | ||
if (extendedType->is<AnyMetatypeType>()) { | ||
diagnose(ext->getLoc(), diag::extension_metatype, extendedType) | ||
.highlight(ext->getExtendedTypeLoc().getSourceRange()); | ||
ext->setInvalid(); | ||
ext->getExtendedTypeLoc().setInvalidType(Context); | ||
return; | ||
} | ||
|
||
// Validate the nominal type declaration being extended. | ||
NominalTypeDecl *nominal = extendedType->getAnyNominal(); | ||
if (!nominal) { | ||
auto unbound = cast<UnboundGenericType>(extendedType.getPointer()); | ||
auto typealias = cast<TypeAliasDecl>(unbound->getDecl()); | ||
validateDecl(typealias); | ||
// Cannot extend a bound generic type. | ||
if (extendedType->isSpecialized()) { | ||
diagnose(ext->getLoc(), diag::extension_specialization, | ||
extendedType->getAnyNominal()->getName()) | ||
.highlight(ext->getExtendedTypeLoc().getSourceRange()); | ||
ext->setInvalid(); | ||
ext->getExtendedTypeLoc().setInvalidType(Context); | ||
return; | ||
} | ||
|
||
nominal = typealias->getUnderlyingTypeLoc().getType()->getAnyNominal(); | ||
auto *nominal = extendedType->getAnyNominal(); | ||
|
||
// Cannot extend function types, tuple types, etc. | ||
if (nominal == nullptr) { | ||
diagnose(ext->getLoc(), diag::non_nominal_extension, extendedType) | ||
.highlight(ext->getExtendedTypeLoc().getSourceRange()); | ||
ext->setInvalid(); | ||
ext->getExtendedTypeLoc().setInvalidType(Context); | ||
return; | ||
} | ||
|
||
// Extensions nested inside other declarations are invalid and we | ||
// do not bind them. | ||
if (!isa<SourceFile>(dc)) | ||
return; | ||
|
||
// Validate the nominal type declaration being extended. | ||
validateDecl(nominal); | ||
|
||
if (nominal->getGenericParamsOfContext()) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When does this come up for a protocol extension?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Invalid code, like when an extension is inside of another decl, we don't build the generic parameter list for it anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we still bind the underlying type? Okay.