-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[TypeContextInfo/ConformingMethods] Map type out of context #25130
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,12 +104,17 @@ void ContextInfoCallbacks::doneParsing() { | |
for (auto T : Info.getPossibleTypes()) { | ||
if (T->is<ErrorType>() || T->is<UnresolvedType>()) | ||
continue; | ||
if (auto env = CurDeclContext->getGenericEnvironmentOfContext()) | ||
T = env->mapTypeIntoContext(T); | ||
|
||
T = T->getRValueType(); | ||
if (T->hasArchetype()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this ever true? In the old code we would map into context unconditionally. Or is env always null? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it just used to lack tests for generic environment 😞 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha. |
||
T = T->mapTypeOutOfContext(); | ||
|
||
// TODO: Do we need '.none' for Optionals? | ||
auto objT = T->lookThroughAllOptionalTypes(); | ||
|
||
if (auto env = CurDeclContext->getGenericEnvironmentOfContext()) | ||
objT = env->mapTypeIntoContext(T); | ||
|
||
if (!seenTypes.insert(objT->getCanonicalType()).second) | ||
continue; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
protocol Proto {} | ||
struct ConcreteProto : Proto {} | ||
struct ConcreteProtoGen<T> : Proto {} | ||
|
||
struct S<T> : Proto { | ||
func methodForProto1(x: T) -> ConcreteProto {} | ||
func methodForProto2<U>(x: U) -> ConcreteProtoGen<U> {} | ||
func methodForProto3(x: Self) -> ConcreteProtoGen<T> {} | ||
func methodForProto4() -> Self {} | ||
func methodForInt() -> Int { return 1 } | ||
mutating func test() { | ||
self. | ||
} | ||
} | ||
|
||
func test<X>(value: S<X>) { | ||
value. | ||
} | ||
|
||
// RUN: %sourcekitd-test -req=conformingmethods -pos=12:10 %s -req-opts=expectedtypes='$s8MyModule5ProtoPD' -- -module-name MyModule %s > %t.response.1 | ||
// RUN: diff -u %s.response.1 %t.response.1 | ||
// RUN: %sourcekitd-test -req=conformingmethods -pos=17:8 %s -req-opts=expectedtypes='$s8MyModule5ProtoPD' -- -module-name MyModule %s > %t.response.2 | ||
// RUN: diff -u %s.response.2 %t.response.2 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
key.typename: "S<T>", | ||
key.typeusr: "$s8MyModule1SVyxGD", | ||
key.members: [ | ||
{ | ||
key.name: "methodForProto1(x:)", | ||
key.sourcetext: "methodForProto1(x: <#T##T#>)", | ||
key.description: "methodForProto1(x: T)", | ||
key.typename: "ConcreteProto", | ||
key.typeusr: "$s8MyModule13ConcreteProtoVD" | ||
}, | ||
{ | ||
key.name: "methodForProto2(x:)", | ||
key.sourcetext: "methodForProto2(x: <#T##U#>)", | ||
key.description: "methodForProto2(x: U)", | ||
key.typename: "ConcreteProtoGen<U>", | ||
key.typeusr: "$s8MyModule16ConcreteProtoGenVyqd__GD" | ||
}, | ||
{ | ||
key.name: "methodForProto3(x:)", | ||
key.sourcetext: "methodForProto3(x: <#T##S<T>#>)", | ||
key.description: "methodForProto3(x: S<T>)", | ||
key.typename: "ConcreteProtoGen<T>", | ||
key.typeusr: "$s8MyModule16ConcreteProtoGenVyxGD" | ||
}, | ||
{ | ||
key.name: "methodForProto4()", | ||
key.sourcetext: "methodForProto4()", | ||
key.description: "methodForProto4()", | ||
key.typename: "S<T>", | ||
key.typeusr: "$s8MyModule1SVyxGD" | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
key.typename: "S<X>", | ||
key.typeusr: "$s8MyModule1SVyxGD", | ||
key.members: [ | ||
{ | ||
key.name: "methodForProto1(x:)", | ||
key.sourcetext: "methodForProto1(x: <#T##X#>)", | ||
key.description: "methodForProto1(x: X)", | ||
key.typename: "ConcreteProto", | ||
key.typeusr: "$s8MyModule13ConcreteProtoVD" | ||
}, | ||
{ | ||
key.name: "methodForProto2(x:)", | ||
key.sourcetext: "methodForProto2(x: <#T##U#>)", | ||
key.description: "methodForProto2(x: U)", | ||
key.typename: "ConcreteProtoGen<U>", | ||
key.typeusr: "$s8MyModule16ConcreteProtoGenVyqd__GD" | ||
}, | ||
{ | ||
key.name: "methodForProto3(x:)", | ||
key.sourcetext: "methodForProto3(x: <#T##S<X>#>)", | ||
key.description: "methodForProto3(x: S<X>)", | ||
key.typename: "ConcreteProtoGen<X>", | ||
key.typeusr: "$s8MyModule16ConcreteProtoGenVyxGD" | ||
}, | ||
{ | ||
key.name: "methodForProto4()", | ||
key.sourcetext: "methodForProto4()", | ||
key.description: "methodForProto4()", | ||
key.typename: "S<X>", | ||
key.typeusr: "$s8MyModule1SVyxGD" | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
struct S<T> { | ||
func foo<U>(x: U) {} | ||
func bar<V>(x: S<V>) {} | ||
func baz(x: Self) {} | ||
|
||
func test() { | ||
foo(x: ) | ||
bar(x: ) | ||
self.baz(x: ) | ||
} | ||
|
||
func test2<X>(x: X) { | ||
var value = S<X>() | ||
value.foo(x: ) | ||
value.bar(x: ) | ||
value.baz(x: ) | ||
} | ||
|
||
static var instance: Self = S<T>() | ||
} | ||
|
||
// RUN: %sourcekitd-test -req=typecontextinfo -pos=7:12 %s -- %s > %t.response.1 | ||
// RUN: diff -u %s.response.1 %t.response.1 | ||
// RUN: %sourcekitd-test -req=typecontextinfo -pos=8:12 %s -- %s > %t.response.2 | ||
// RUN: diff -u %s.response.2 %t.response.2 | ||
// RUN: %sourcekitd-test -req=typecontextinfo -pos=9:17 %s -- %s > %t.response.3 | ||
// RUN: diff -u %s.response.3 %t.response.3 | ||
|
||
// RUN: %sourcekitd-test -req=typecontextinfo -pos=14:18 %s -- %s > %t.response.4 | ||
// RUN: diff -u %s.response.4 %t.response.4 | ||
// RUN: %sourcekitd-test -req=typecontextinfo -pos=15:18 %s -- %s > %t.response.5 | ||
// RUN: diff -u %s.response.5 %t.response.5 | ||
// RUN: %sourcekitd-test -req=typecontextinfo -pos=16:18 %s -- %s > %t.response.6 | ||
// RUN: diff -u %s.response.6 %t.response.6 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
key.results: [ | ||
{ | ||
key.typename: "U", | ||
key.typeusr: "$sqd__D", | ||
key.implicitmembers: [ | ||
] | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
key.results: [ | ||
{ | ||
key.typename: "S<V>", | ||
key.typeusr: "$s20typecontext_generics1SVyqd__GD", | ||
key.implicitmembers: [ | ||
{ | ||
key.name: "instance", | ||
key.sourcetext: "instance", | ||
key.description: "instance" | ||
} | ||
] | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
key.results: [ | ||
{ | ||
key.typename: "S<T>", | ||
key.typeusr: "$s20typecontext_generics1SVyxGD", | ||
key.implicitmembers: [ | ||
{ | ||
key.name: "instance", | ||
key.sourcetext: "instance", | ||
key.description: "instance" | ||
} | ||
] | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
key.results: [ | ||
{ | ||
key.typename: "U", | ||
key.typeusr: "$sqd__D", | ||
key.implicitmembers: [ | ||
] | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
key.results: [ | ||
{ | ||
key.typename: "S<V>", | ||
key.typeusr: "$s20typecontext_generics1SVyqd__GD", | ||
key.implicitmembers: [ | ||
{ | ||
key.name: "instance", | ||
key.sourcetext: "instance", | ||
key.description: "instance" | ||
} | ||
] | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
key.results: [ | ||
{ | ||
key.typename: "S<X>", | ||
key.typeusr: "$s20typecontext_generics1SVyqd__GD", | ||
key.implicitmembers: [ | ||
{ | ||
key.name: "instance", | ||
key.sourcetext: "instance", | ||
key.description: "instance" | ||
} | ||
] | ||
} | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the hasArchetype() check is an optimization, you don’t really need it because mapTypeOutOfContext() should already short circuit in this case. If it’s because you might have archetypes or type parameters here, it’s worth untangling that. I would try making it unconditional and seeing if the tests pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's safe to check this, just in case it have
hasTypeParameter()
. (There's is an invariant thathasArchetype()
andhasTypeParameter()
are mutually exclusive, right?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing enforces they're mututally exclusive, that's the problem. This is why I like to establish the invariant by using mapTypeIntoContext()/OutOfContext() unconditionally where possible, and fixing callers to only pass one or the other consistently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand. Let me try that in followups.