Skip to content

tests: Add a missing typealias test case and verifications for a few FIXMEs #60972

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
Oct 10, 2022
Merged
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
41 changes: 36 additions & 5 deletions test/decl/typealias/generic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

struct MyType<TyA, TyB> { // expected-note {{generic type 'MyType' declared here}}
// expected-note @-1 {{arguments to generic parameter 'TyB' ('S' and 'Int') are expected to be equal}}
// expected-note @-2 4 {{arguments to generic parameter 'TyA' ('Float' and 'Int') are expected to be equal}}
// expected-note @-2 6 {{arguments to generic parameter 'TyA' ('Float' and 'Int') are expected to be equal}}
// expected-note @-3 2 {{arguments to generic parameter 'TyA' ('Float' and 'Double') are expected to be equal}}
// expected-note @-4 2 {{arguments to generic parameter 'TyB' ('Int' and 'Float') are expected to be equal}}
// expected-note @-5 2 {{arguments to generic parameter 'TyB' ('Double' and 'Float') are expected to be equal}}
var a : TyA, b : TyB
}

Expand All @@ -24,6 +27,22 @@ let _: OurType<Int, String>
let _: YourType<Int>
let _: Container.YourType<Int>

// The question of whether a type alias is implicitly generic should not
// arise until after we attempt to resolve the underlying type.
do {
struct Outer<T> {
typealias NotGeneric = Outer
struct Inner<U> {
typealias NotGeneric = Outer
}
}

let _: Outer<Int>.NotGeneric<Bool>
// expected-error@-1 {{cannot specialize non-generic type 'Outer<Int>.NotGeneric' (aka 'Outer<Int>')}}
let _: Outer<Int>.Inner<Never>.NotGeneric<Bool>
// expected-error@-1 {{cannot specialize non-generic type 'Outer<Int>.Inner<Never>.NotGeneric' (aka 'Outer<Int>')}}
}

//
// Bona-fide generic type aliases
//
Expand Down Expand Up @@ -246,8 +265,14 @@ let _: ConcreteStruct.O<Int> = ConcreteStruct.O<Int>(123)
// In the other cases, we manage to fold the UnresolvedSpecializeExpr in the
// precheckExpression() phase, which handles generic typealiases correctly.

let _ = GenericClass.TA<Float>(a: 4.0, b: 1) // FIXME
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these might still be wrong? Can you verify that it produces the correct underlying type vs when the alias appears in type context?

Copy link
Collaborator Author

@AnthonyLatsis AnthonyLatsis Sep 22, 2022

Choose a reason for hiding this comment

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

Oh, good catch, you’re right! What an odd bug though; I will bring back the FIXMEs and add a verification of this error.

struct S<X, Y> {
  var x: X, y: Y
}
class C<T> {
  typealias TA<U> = S<T, U>
}

let s1 = C.TA<Float>(x: 4.0, y: 1)
let s2: S<Double, Float> = s1 // cannot assign value of type 'S<Float, Int>' to type 'S<Double, Float>'

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe the bug is because SpecializeExpr doesn't handle type alias types correctly. It just desugars the underlying type.

let _ = GenericClass.TA<Float>(a: 1, b: 4.0)
do {
let x1 = GenericClass.TA<Float>(a: 4.0, b: 1) // FIXME
let x2 = GenericClass.TA<Float>(a: 1, b: 4.0) // FIXME

// FIXME: Should not diagnose
let _: MyType<Double, Float> = x1 // expected-error {{cannot assign value of type 'MyType<Float, Int>' to type 'MyType<Double, Float>'}}
let _: MyType<Int, Float> = x2 // expected-error {{cannot assign value of type 'MyType<Float, Double>' to type 'MyType<Int, Float>'}}
}

let _ = GenericClass<Int>.TA(a: 4.0, b: 1) // expected-error {{cannot convert value of type 'Double' to expected argument type 'Int'}}
let _ = GenericClass<Int>.TA(a: 1, b: 4.0)
Expand All @@ -258,8 +283,14 @@ let _ = GenericClass<Int>.TA<Float>(a: 1, b: 4.0)
let _: GenericClass.TA = GenericClass.TA(a: 4.0, b: 1)
let _: GenericClass.TA = GenericClass.TA(a: 1, b: 4.0)

let _: GenericClass.TA = GenericClass.TA<Float>(a: 4.0, b: 1) // FIXME
let _: GenericClass.TA = GenericClass.TA<Float>(a: 1, b: 4.0)
do {
let x1: GenericClass.TA = GenericClass.TA<Float>(a: 4.0, b: 1) // FIXME
let x2: GenericClass.TA = GenericClass.TA<Float>(a: 1, b: 4.0) // FIXME

// FIXME: Should not diagnose
let _: MyType<Double, Float> = x1 // expected-error {{cannot assign value of type 'MyType<Float, Int>' to type 'MyType<Double, Float>'}}
let _: MyType<Int, Float> = x2 // expected-error {{cannot assign value of type 'MyType<Float, Double>' to type 'MyType<Int, Float>'}}
}

let _: GenericClass.TA = GenericClass<Int>.TA(a: 4.0, b: 1) // expected-error {{cannot convert value of type 'Double' to expected argument type 'Int'}}
let _: GenericClass.TA = GenericClass<Int>.TA(a: 1, b: 4.0)
Expand Down