Skip to content

Sema: Don't allow generic typealiases to have an unbound generic underlying type #21341

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
Dec 15, 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
11 changes: 9 additions & 2 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3197,7 +3197,11 @@ void TypeChecker::typeCheckDecl(Decl *D) {

/// Validate the underlying type of the given typealias.
static void validateTypealiasType(TypeChecker &tc, TypeAliasDecl *typeAlias) {
TypeResolutionOptions options(TypeResolverContext::TypeAliasDecl);
TypeResolutionOptions options(
(typeAlias->getGenericParams() ?
TypeResolverContext::GenericTypeAliasDecl :
TypeResolverContext::TypeAliasDecl));

if (!typeAlias->getDeclContext()->isCascadingContextForLookup(
/*functionsAreNonCascading*/true)) {
options |= TypeResolutionFlags::KnownNonCascadingDependency;
Expand Down Expand Up @@ -4198,7 +4202,10 @@ void TypeChecker::validateDeclForNameLookup(ValueDecl *D) {
if (typealias->isBeingValidated()) return;

auto helper = [&] {
TypeResolutionOptions options(TypeResolverContext::TypeAliasDecl);
TypeResolutionOptions options(
(typealias->getGenericParams() ?
TypeResolverContext::GenericTypeAliasDecl :
TypeResolverContext::TypeAliasDecl));
if (validateType(typealias->getUnderlyingTypeLoc(),
TypeResolution::forStructural(typealias), options)) {
typealias->setInvalid();
Expand Down
1 change: 1 addition & 0 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2914,6 +2914,7 @@ Type TypeResolver::resolveImplicitlyUnwrappedOptionalType(
case TypeResolverContext::EnumElementDecl:
case TypeResolverContext::EnumPatternPayload:
case TypeResolverContext::TypeAliasDecl:
case TypeResolverContext::GenericTypeAliasDecl:
case TypeResolverContext::GenericRequirement:
case TypeResolverContext::ImmediateOptionalTypeArgument:
case TypeResolverContext::InExpression:
Expand Down
6 changes: 5 additions & 1 deletion lib/Sema/TypeCheckType.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,12 @@ enum class TypeResolverContext : uint8_t {
/// Whether this is the payload subpattern of an enum pattern.
EnumPatternPayload,

/// Whether we are checking the underlying type of a typealias.
/// Whether we are checking the underlying type of a non-generic typealias.
TypeAliasDecl,

/// Whether we are checking the underlying type of a generic typealias.
GenericTypeAliasDecl,

/// Whether we are in a requirement of a generic declaration
GenericRequirement,

Expand Down Expand Up @@ -207,6 +210,7 @@ class TypeResolutionOptions {
case Context::EnumElementDecl:
case Context::EnumPatternPayload:
case Context::TypeAliasDecl:
case Context::GenericTypeAliasDecl:
case Context::GenericRequirement:
case Context::ImmediateOptionalTypeArgument:
case Context::AbstractFunctionDecl:
Expand Down
4 changes: 2 additions & 2 deletions test/decl/nested/type_in_type.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,9 @@ extension OuterGeneric.MidGeneric : HasAssocType {
func takesAssocType(first: D, second: F) {}
}

typealias OuterGenericMidGeneric<T> = OuterGeneric<T>.MidGeneric
typealias OuterGenericMidNonGeneric<T> = OuterGeneric<T>.MidNonGeneric

extension OuterGenericMidGeneric {
extension OuterGenericMidNonGeneric {

}

Expand Down
5 changes: 4 additions & 1 deletion test/decl/typealias/generic.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %target-typecheck-verify-swift

struct MyType<TyA, TyB> {
struct MyType<TyA, TyB> { // expected-note {{generic type 'MyType' declared here}}
var a : TyA, b : TyB
}

Expand Down Expand Up @@ -105,6 +105,9 @@ _ = C(a: 42, // expected-error {{'Int' is not convertible to 'String'}}
_ = G(a: "foo", b: 42)
_ = G<Int, String>(a: "foo", b: 42)

// Generic typealias cannot have unbound generic type.
typealias VeryBad1<T> = MyType // expected-error {{reference to generic type 'MyType' requires arguments in <...>}}
typealias VeryBad2<T> = Swift.Array // expected-error {{reference to generic type 'Array' requires arguments in <...>}}

struct MyTypeWithHashable<TyA, TyB : Hashable> {
}
Expand Down