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 61 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 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
bd7df19
Merge remote-tracking branch 'origin/main' into generic_typealias
xavgru12 Jun 5, 2025
b480ceb
rename struct to fix redeclaration after merge
xavgru12 Jun 5, 2025
ad5d532
remove assert since it breaks 4 tests: Compatibility/stdlib_generic_t…
xavgru12 Jun 5, 2025
cc1b299
remove superfluous getPointer
xavgru12 Jun 5, 2025
04c1f72
add another test case
xavgru12 Jun 5, 2025
8a849b7
remove isInferredType
xavgru12 Jun 5, 2025
87cdebf
minimize diagnostic noise
xavgru12 Jun 5, 2025
be17e78
add test case
xavgru12 Jun 5, 2025
8a71a9a
changed foreach to while
xavgru12 Jun 5, 2025
6fb08c1
update test case with where
xavgru12 Jun 5, 2025
fe2fc6d
remove comments
xavgru12 Jun 5, 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
68 changes: 59 additions & 9 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3025,6 +3025,34 @@ AllMembersRequest::evaluate(
return evaluateMembersRequest(idc, MembersRequestKind::All);
}

static bool isTypeInferredByTypealias(TypeAliasDecl *typealias,
NominalTypeDecl *nominal) {
if (!nominal->isGeneric()){
return false;
}

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

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

if (typealiasBoundGenericType->hasTypeParameter()) {
return false;
}
}

return true;
}

bool TypeChecker::isPassThroughTypealias(TypeAliasDecl *typealias,
NominalTypeDecl *nominal) {
// Pass-through only makes sense when the typealias refers to a nominal
Expand All @@ -3045,17 +3073,39 @@ 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);

while (!nominalGenericParams.empty() &&
nominalGenericParams.back()->getDepth() == maxDepth) {
nominalGenericParams = nominalGenericParams.drop_back();
}

while (!typealiasGenericParams.empty() &&
typealiasGenericParams.back()->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;
}
Comment on lines +3099 to +3105
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to perform this check regardless of whether the original number of generic params matched.

Copy link
Author

Choose a reason for hiding this comment

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

Confused here, added a comment above about this


return isTypeInferredByTypealias(typealias, nominal);
}

// If neither is generic at this level, we have a pass-through typealias.
if (!typealias->isGeneric()) return true;
Expand Down
100 changes: 100 additions & 0 deletions test/decl/ext/specialize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,106 @@ 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 S10<X, Y> {}
typealias InferredSpecializedNestedTypes<X> = S10<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 test() {
let int: Int
let _: X = int // expected-error {{cannot convert value of type 'Int' to specified type 'X'}}
}
}


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> where A == Int
}

extension S5.Alias{
func test() {
let int: Int
let _: A = int // expected-error {{cannot convert value of type 'Int' to specified type 'A'}}
}
}


struct S11<T> {
struct Inner<U> {}
}

struct S12<T> {
struct Inner<U> {}
typealias A1<U> = S11<T>.Inner<U>
typealias A2<U> = S12<T>.Inner<U> where T == Int
}

extension S12<Int>.A1 {
func foo1() {
let int: Int
let _: T = int
}
}

extension S12.A2 {
func foo2() {
let int: Int
let _: T = int
}
}


struct S13<T> {
struct Inner<U> {}
}
struct S14 {
typealias A<T> = S13<T>.Inner<Int>
}
extension S14.A {
func test() {
let int: Int
let _: U = int // error
}
}


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