Skip to content

Commit b3ac017

Browse files
committed
Fix <rdar://23248290> Name lookup: "Cannot convert type 'Int' to expected argument type 'Int'" while trying to initialize ivar of generic type in class scope
Type resolution wasn't looking through property initializer decl contexts to find out whether an unbound generic type reference was referring to the enclosing type. Previously we'd reject this with: error: cannot convert value of type 'Int' to specified type 'Int' private var data: Int = Matrix4.size() ~~~~~~~~^~~~~~ which was super confusing. The problem was that we weren't resolving Matrix4 to Matrix4<T>.
1 parent 4341ef3 commit b3ac017

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,14 @@ Type TypeChecker::resolveTypeInContext(
224224
case DeclContextKind::Module:
225225
case DeclContextKind::FileUnit:
226226
case DeclContextKind::TopLevelCodeDecl:
227-
case DeclContextKind::Initializer:
228227
break;
229228

229+
case DeclContextKind::Initializer:
230+
// If this is a property initializer, we may be referring to the
231+
// type initializing the property.
232+
continue;
233+
234+
230235
case DeclContextKind::NominalTypeDecl:
231236
// If this is our nominal type, return its type within its context.
232237
// FIXME: Just produce the type structure when TR_ResolveStructure.

test/NameBinding/name-binding.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,17 @@ func questionablyValidForwardReference() { print(qvfrVar, terminator: ""); }; va
207207
// FIXME: This should warn too.
208208
print(forwardReferenceVar, terminator: ""); var forwardReferenceVar: Int = 0
209209

210+
211+
212+
// <rdar://problem/23248290> Name lookup: "Cannot convert type 'Int' to expected argument type 'Int'" while trying to initialize ivar of generic type in class scope
213+
// https://gist.github.com/erynofwales/61768899502b7ac83c6e
214+
struct Matrix4<T: FloatingPointType> {
215+
static func size() -> Int {}
216+
217+
private var data: Int = Matrix4.size() // Ok: Matrix4<T>
218+
219+
init() {
220+
data = Matrix4.size() // Ok: Matrix4<T>
221+
}
222+
}
223+

test/expr/delayed-ident/static_var.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ acceptInOutX1(&(.AnX1))
1818

1919
// Generic struct types
2020
struct X2<T> {
21-
static var AnX2 = X2() // expected-error{{generic parameter 'T' could not be inferred}}
21+
static var AnX2 = X2() // expected-error{{static stored properties not yet supported in generic types}}
2222
static var NotAnX2 = 0 // expected-error {{static stored properties not yet supported in generic types}}
2323
}
2424

0 commit comments

Comments
 (0)