Skip to content

[5.3][ASTPrinter] Don't print inferred opaque result type witness #31834

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
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
22 changes: 13 additions & 9 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3519,24 +3519,28 @@ static Type getMemberForBaseType(LookupConformanceFn lookupConformances,

// Retrieve the type witness.
auto witness =
conformance.getConcrete()->getTypeWitness(assocType, options);
if (!witness || witness->hasError())
conformance.getConcrete()->getTypeWitnessAndDecl(assocType, options);

auto witnessTy = witness.getWitnessType();
if (!witnessTy || witnessTy->hasError())
return failed();

// This is a hacky feature allowing code completion to migrate to
// using Type::subst() without changing output.
if (options & SubstFlags::DesugarMemberTypes) {
if (auto *aliasType =
dyn_cast<TypeAliasType>(witness.getPointer())) {
if (!aliasType->is<ErrorType>())
witness = aliasType->getSinglyDesugaredType();
}
if (auto *aliasType = dyn_cast<TypeAliasType>(witnessTy.getPointer()))
witnessTy = aliasType->getSinglyDesugaredType();

// Another hack. If the type witness is a opaque result type. They can
// only be referred using the name of the associated type.
if (witnessTy->is<OpaqueTypeArchetypeType>())
witnessTy = witness.getWitnessDecl()->getDeclaredInterfaceType();
}

if (witness->is<ErrorType>())
if (witnessTy->is<ErrorType>())
return failed();

return witness;
return witnessTy;
}

return failed();
Expand Down
36 changes: 36 additions & 0 deletions test/IDE/complete_opaque_result.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=POSTFIX_TestProtocol_DOT | %FileCheck %s -check-prefix=POSTFIX_TestProtocol_DOT
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=POSTFIX_TestProtocol_NODOT | %FileCheck %s -check-prefix=POSTFIX_TestProtocol_NODOT

// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OVERRIDE_TestProtocol2 | %FileCheck %s -check-prefix=OVERRIDE_TestProtocol2
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=POSTFIX_ConcreteTestProtocol2 | %FileCheck %s -check-prefix=POSTFIX_ConcreteTestProtocol2

protocol MyProtocol {
associatedtype Mistery
}
Expand Down Expand Up @@ -200,3 +203,36 @@ func postfixExpr() {
// POSTFIX_TestProtocol_NODOT-DAG: BuiltinOperator/None: = {#TestProtocol#}[#Void#]; name={{.*$}}
// POSTFIX_TestProtocol_NODOT-DAG: Keyword[self]/CurrNominal: .self[#TestProtocol#]; name={{.*$}}
// POSTFIX_TestProtocol_NODOT-DAG: End completions

protocol TestProtocol2 {
associatedtype Assoc: Comparable
func foo() -> Assoc
func bar() -> Assoc
func baz(x: @autoclosure () -> Assoc) -> (Assoc) -> Assoc
}
extension TestProtocol2 {
func inExt() -> Assoc { fatalError() }
}
struct ConcreteTestProtocol2: TestProtocol2 {
func foo() -> some Comparable { 1 }
#^OVERRIDE_TestProtocol2^#
// OVERRIDE_TestProtocol2: Begin completions
// OVERRIDE_TestProtocol2-NOT: foo()
// OVERRIDE_TestProtocol2-NOT: inExt()
// OVERRIDE_TestProtocol2-DAG: Decl[InstanceMethod]/Super: func bar() -> Assoc {|};
// OVERRIDE_TestProtocol2-DAG: Decl[InstanceMethod]/Super: func baz(x: @autoclosure () -> Assoc) -> (Assoc) -> Assoc {|};
// OVERRIDE_TestProtocol2-DAG: Decl[AssociatedType]/Super: typealias Assoc = {#(Type)#};
// OVERRIDE_TestProtocol2-NOT: foo()
// OVERRIDE_TestProtocol2-NOT: inExt()
// OVERRIDE_TestProtocol2: End completions
}
func testUseTestProtocol2(value: ConcreteTestProtocol2) {
value.#^POSTFIX_ConcreteTestProtocol2^#
// POSTFIX_ConcreteTestProtocol2: Begin completions
// POSTFIX_ConcreteTestProtocol2-DAG: Keyword[self]/CurrNominal: self[#ConcreteTestProtocol2#];
// POSTFIX_ConcreteTestProtocol2-DAG: Decl[InstanceMethod]/CurrNominal: foo()[#Comparable#];
// POSTFIX_ConcreteTestProtocol2-DAG: Decl[InstanceMethod]/Super: bar()[#ConcreteTestProtocol2.Assoc#];
// POSTFIX_ConcreteTestProtocol2-DAG: Decl[InstanceMethod]/Super: baz({#x: ConcreteTestProtocol2.Assoc#})[#(ConcreteTestProtocol2.Assoc) -> ConcreteTestProtocol2.Assoc#];
// POSTFIX_ConcreteTestProtocol2-DAG: Decl[InstanceMethod]/Super: inExt()[#ConcreteTestProtocol2.Assoc#];
// POSTFIX_ConcreteTestProtocol2: End completions
}
35 changes: 35 additions & 0 deletions test/SourceKit/CursorInfo/cursor_opaque_result.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
public protocol P {
associatedtype Assoc
func foo() -> Assoc
}
extension P {
func bar() -> Assoc { fatalError() }
}

public struct MyStruct: P {
public func foo() -> some Comparable { 1 }
}
func test(value: MyStruct) {
value.foo()
value.bar()
}

// RUN: %sourcekitd-test -req=cursor -pos=13:9 %s -- %s -module-name MyModule | %FileCheck --check-prefix=OPAQUE %s
// RUN: %sourcekitd-test -req=cursor -pos=14:9 %s -- %s -module-name MyModule | %FileCheck --check-prefix=ASSOC %s

// OPAQUE: foo()
// OPAQUE-NEXT: s:8MyModule0A6StructV3fooQryF
// OPAQUE-NEXT: (MyStruct) -> () -> some Comparable
// OPAQUE-NEXT: $sQrycD
// OPAQUE-NEXT: <Container>$s8MyModule0A6StructVD</Container>
// OPAQUE-NEXT: <Declaration>public func foo() -&gt; some <Type usr="s:SL">Comparable</Type></Declaration>
// OPAQUE-NEXT: <decl.function.method.instance><syntaxtype.keyword>public</syntaxtype.keyword> <syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>foo</decl.name>() -&gt; <decl.function.returntype><syntaxtype.keyword>some</syntaxtype.keyword> <ref.protocol usr="s:SL">Comparable</ref.protocol></decl.function.returntype></decl.function.method.instance>


// ASSOC: bar()
// ASSOC-NEXT: s:8MyModule1PPAAE3bar5AssocQzyF
// ASSOC-NEXT: <Self where Self : P> (Self) -> () -> Self.Assoc
// ASSOC-NEXT: $s5AssocQzycD
// ASSOC-NEXT: <Container>$s8MyModule0A6StructVD</Container>
// ASSOC-NEXT: <Declaration>func bar() -&gt; <Type usr="s:8MyModule0A6StructV5Assoca">Assoc</Type></Declaration>
// ASSOC-NEXT: <decl.function.method.instance><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>bar</decl.name>() -&gt; <decl.function.returntype><ref.typealias usr="s:8MyModule0A6StructV5Assoca">Assoc</ref.typealias></decl.function.returntype></decl.function.method.instance>