Skip to content

[Sema] Fix compiler error when extending a typealias of a partially specialized generic type #73169

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

Open
wants to merge 50 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
7025a0d
comment out isPassThroughTypealies
xavgru12 Apr 21, 2024
772995f
merge with main
xavgru12 Jan 17, 2025
0e2ee58
copy isPassThroughTypealias to isTypeInferredByTypealias and comment …
xavgru12 Jan 23, 2025
f95b4a5
add declaration of new function
xavgru12 Jan 23, 2025
f7cbfb4
merge with main
xavgru12 Jan 23, 2025
a6b222b
merge with main
xavgru12 Jan 25, 2025
11bff2a
merge with main
xavgru12 Jan 25, 2025
fb4dcdd
checkout main
xavgru12 Jan 25, 2025
74fc932
comment out isPassThroughTypealies
xavgru12 Apr 21, 2024
6f6f37f
copy isPassThroughTypealias to isTypeInferredByTypealias and comment …
xavgru12 Jan 23, 2025
8f45a15
add declaration of new function
xavgru12 Jan 23, 2025
e1ab6bb
remove oudtdated file
xavgru12 Jan 25, 2025
e513182
first version
xavgru12 Mar 9, 2025
06d2a96
returns only true if no unsupported type conversion is entered
xavgru12 Mar 9, 2025
7c2dae5
add back spaces
xavgru12 Mar 10, 2025
8719c90
remove more unwanted changes
xavgru12 Mar 10, 2025
bd7a393
get rid of unneeded changes
xavgru12 Mar 10, 2025
6a81633
according to github issue, this is supposed to fail, but the error me…
xavgru12 Mar 11, 2025
0a5afa6
add unit test for the new supported case
xavgru12 Mar 11, 2025
2dcb95f
remove unneeded include
xavgru12 Mar 31, 2025
2a53487
commit forced blank lines
xavgru12 Mar 31, 2025
eb49f5a
remove unneeded copy
xavgru12 Mar 31, 2025
86f74ca
remove extra brackets
xavgru12 Mar 31, 2025
6302d7d
remove comment
xavgru12 Mar 31, 2025
871df92
format code
xavgru12 Mar 31, 2025
ea8f12c
change variable name
xavgru12 Mar 31, 2025
2a929de
change location of function
xavgru12 Mar 31, 2025
997bd4e
remove unneeded check§
xavgru12 Mar 31, 2025
b102077
correct location
xavgru12 Mar 31, 2025
b5a899e
Revert "commit forced blank lines"
xavgru12 Mar 31, 2025
491fd95
revert evaluate ternary operator
xavgru12 Mar 31, 2025
99d29cb
whitespace
xavgru12 Mar 31, 2025
b070615
Revert "commit forced blank lines"
xavgru12 Mar 31, 2025
a68d748
whitespace
xavgru12 Mar 31, 2025
aec1481
Revert "commit forced blank lines"
xavgru12 Mar 31, 2025
a18c1b3
add missing bracket
xavgru12 Mar 31, 2025
f42dc3b
Revert "commit forced blank lines"
xavgru12 Mar 31, 2025
9aa17c5
Merge branch 'main' into merge_in_typealias
xavgru12 Apr 3, 2025
7240766
make the new function static
xavgru12 Apr 15, 2025
ed5ddbd
add typealiasBoundGenericType->hasTypeParameter()
xavgru12 Apr 15, 2025
fa9c005
add test case for typealiasBoundGenericType->hasTypeParameter() infer…
xavgru12 Apr 15, 2025
4d26c68
remove redundant check
xavgru12 Apr 15, 2025
de51bf2
fix typo
xavgru12 Apr 15, 2025
6340373
breaking expected fail
xavgru12 Apr 16, 2025
c4eb3a6
add test case for making type check stricter
xavgru12 Apr 17, 2025
3d8aabb
add isGeneric check
xavgru12 Apr 17, 2025
aeb2e73
add assert
xavgru12 Apr 17, 2025
be58943
make sure no exotic cases slip through
xavgru12 May 4, 2025
6d61966
add test case for expected fail
xavgru12 May 4, 2025
98a9f2e
add test case extension for expected pass
xavgru12 May 4, 2025
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
79 changes: 70 additions & 9 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3012,6 +3012,42 @@ AllMembersRequest::evaluate(
return evaluateMembersRequest(idc, MembersRequestKind::All);
}

static bool isTypeInferredByTypealias(TypeAliasDecl *typealias,
NominalTypeDecl *nominal) {
bool isInferredType = false;

if (!nominal->isGeneric()){
return false;
}

auto nominalGenericArguments = nominal->getDeclaredInterfaceType()
.getPointer()
->getAs<BoundGenericType>()
->getGenericArgs();
auto typealiasGenericArguments = typealias->getUnderlyingType()
.getPointer()
->getAs<BoundGenericType>()
->getGenericArgs();

for (size_t i = 0; i < nominalGenericArguments.size(); i++) {
auto nominalBoundGenericType = nominalGenericArguments[i];
auto typealiasBoundGenericType = typealiasGenericArguments[i];
if (nominalBoundGenericType.getPointer()->isEqual(
typealiasBoundGenericType.getPointer())) {
continue;
}

if (!typealiasBoundGenericType->hasTypeParameter()) {
isInferredType = true;
} else {
isInferredType = false;
break;
}
}

return isInferredType;
}

bool TypeChecker::isPassThroughTypealias(TypeAliasDecl *typealias,
NominalTypeDecl *nominal) {
// Pass-through only makes sense when the typealias refers to a nominal
Expand All @@ -3020,6 +3056,7 @@ bool TypeChecker::isPassThroughTypealias(TypeAliasDecl *typealias,

// Check that the nominal type and the typealias are either both generic
// at this level or neither are.
ASSERT(nominal->isGeneric() == typealias->isGeneric());
if (nominal->isGeneric() != typealias->isGeneric())
return false;

Expand All @@ -3032,17 +3069,41 @@ bool TypeChecker::isPassThroughTypealias(TypeAliasDecl *typealias,
// If neither is generic, we're done: it's a pass-through alias.
if (!nominalSig) return true;

// Check that the type parameters are the same the whole way through.
auto nominalGenericParams = nominalSig.getGenericParams();
auto typealiasGenericParams = typealiasSig.getGenericParams();
if (nominalGenericParams.size() != typealiasGenericParams.size())
return false;
if (!std::equal(nominalGenericParams.begin(), nominalGenericParams.end(),
typealiasGenericParams.begin(),
[](GenericTypeParamType *gp1, GenericTypeParamType *gp2) {
return gp1->isEqual(gp2);
}))
return false;

if (nominalGenericParams.size() != typealiasGenericParams.size()) {

unsigned nominalMaxDepth = nominalGenericParams.back()->getDepth();
unsigned typealiasMaxDepth = typealiasGenericParams.back()->getDepth();
unsigned maxDepth = std::max(nominalMaxDepth, typealiasMaxDepth);

for (const auto &type : nominalGenericParams) {
if (type->getDepth() == maxDepth) {
nominalGenericParams = nominalGenericParams.drop_back();
}
}

for (const auto &type : typealiasGenericParams) {
if (type->getDepth() == maxDepth) {
typealiasGenericParams = typealiasGenericParams.drop_back();
}
}

if (nominalGenericParams.size() != typealiasGenericParams.size()) {
return false;
}

if (!std::equal(nominalGenericParams.begin(), nominalGenericParams.end(),
typealiasGenericParams.begin(),
[](GenericTypeParamType *gp1, GenericTypeParamType *gp2) {
return gp1->isEqual(gp2);
})) {
return false;
}

return isTypeInferredByTypealias(typealias, nominal);
}

// If neither is generic at this level, we have a pass-through typealias.
if (!typealias->isGeneric()) return true;
Expand Down
60 changes: 60 additions & 0 deletions test/decl/ext/specialize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,66 @@ extension IntFoo where U == Int {

Foo(x: "test", y: 1).hello()


struct Field<Tag,Value> {
let tag: Tag
let value: Value
}

typealias IntField<Tag> = Field<Tag,Int>

extension IntField {
func adding(_ value: Int) -> Self {
Field(tag: tag, value: self.value + value)
}
}

struct S<X, Y> {}
typealias InferredSpecializedNestedTypes<X> = S<X, (Int, [Int]?)>
extension InferredSpecializedNestedTypes {
func returnTuple(value: Y) -> (Int, [Int]?) {
return value
}
}


struct S2<X,Y,Z> {
let x: X
let y: Y
let z: Z
}

typealias A2<Y,X> = S2<Int,X,Y>

extension A2 {
func adding(_ x: Int) -> Self {
S2(x: self.x + x, y: y, z: z) // expected-error {{binary operator '+' cannot be applied to operands of type 'X' and 'Int'}} expected-note {{overloads for '+' exist with these partially matching parameter lists: (Int, Int)}}
}
}


struct S4<A, B> {
// Generic parameters: <A, B, C>
// Depth: 0 0 1
struct Nested<C> {
let c: C
}
}

struct S5<A> {
// Generic parameters: <A, B, C>
// Depth: 0 1 1
typealias Alias<B, C> = S4<A, B>.Nested<C>

}

extension S5.Alias{
func adding(_ c: Int) -> Self {
S4.Nested(c: self.c + c) //expected-error {{binary operator '+' cannot be applied to operands of type 'C' and 'Int'}} expected-note {{overloads for '+' exist with these partially matching parameter lists: (Int, Int)}}
}
}


struct MyType<TyA, TyB> {
var a : TyA, b : TyB
}
Expand Down