Skip to content

[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

Merged
merged 1 commit into from
May 30, 2019
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions lib/IDE/ConformingMethodList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ void ConformingMethodListCallbacks::doneParsing() {
if (!T || T->is<ErrorType>() || T->is<UnresolvedType>())
return;

T = T->getRValueType();
if (T->hasArchetype())
T = T->mapTypeOutOfContext();
Copy link
Contributor

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.

Copy link
Member Author

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 that hasArchetype() and hasTypeParameter() are mutually exclusive, right?)

Copy link
Contributor

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.

Copy link
Member Author

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.


llvm::MapVector<ProtocolDecl*, StringRef> expectedProtocols;
resolveProtocolNames(CurDeclContext, ExpectedTypeNames, expectedProtocols);

Expand Down
9 changes: 7 additions & 2 deletions lib/IDE/TypeContextInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it just used to lack tests for generic environment 😞

Copy link
Contributor

Choose a reason for hiding this comment

The 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;

Expand Down
23 changes: 23 additions & 0 deletions test/SourceKit/ConformingMethods/generics.swift
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
34 changes: 34 additions & 0 deletions test/SourceKit/ConformingMethods/generics.swift.response.1
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"
}
]
}
34 changes: 34 additions & 0 deletions test/SourceKit/ConformingMethods/generics.swift.response.2
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"
}
]
}
34 changes: 34 additions & 0 deletions test/SourceKit/TypeContextInfo/typecontext_generics.swift
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"
}
]
}
]
}