-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[SourceKit] SourceKit support for opaque result types #24177
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
644b5ec
6743b61
7078862
8055583
3a2454c
b5351a1
1b7d3e4
4773808
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 |
---|---|---|
|
@@ -107,7 +107,8 @@ PrintOptions PrintOptions::printParseableInterfaceFile() { | |
result.FunctionDefinitions = true; | ||
result.CollapseSingleGetterProperty = false; | ||
result.VarInitializers = true; | ||
result.PrintStableReferencesToOpaqueReturnTypes = true; | ||
result.OpaqueReturnTypePrinting = | ||
OpaqueReturnTypePrintingMode::StableReference; | ||
|
||
// We should print __consuming, __owned, etc for the module interface file. | ||
result.SkipUnderscoredKeywords = false; | ||
|
@@ -1713,6 +1714,8 @@ void PrintAST::printMutatingModifiersIfNeeded(const AccessorDecl *accessor) { | |
void PrintAST::printAccessors(const AbstractStorageDecl *ASD) { | ||
if (isa<VarDecl>(ASD) && !Options.PrintPropertyAccessors) | ||
return; | ||
if (isa<SubscriptDecl>(ASD) && !Options.PrintSubscriptAccessors) | ||
return; | ||
|
||
auto impl = ASD->getImplInfo(); | ||
|
||
|
@@ -2533,6 +2536,7 @@ void PrintAST::visitVarDecl(VarDecl *decl) { | |
if (!tyLoc.getTypeRepr()) | ||
tyLoc = TypeLoc::withoutLoc(decl->getInterfaceType()); | ||
|
||
Printer.printDeclResultTypePre(decl, tyLoc); | ||
if (decl->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>()) | ||
printTypeLocForImplicitlyUnwrappedOptional(tyLoc); | ||
else | ||
|
@@ -2803,6 +2807,8 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) { | |
ResultTyLoc = TypeLoc::withoutLoc(ResultTy); | ||
} | ||
Printer << " -> "; | ||
|
||
Printer.printDeclResultTypePre(decl, ResultTyLoc); | ||
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType); | ||
if (decl->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>()) | ||
printTypeLocForImplicitlyUnwrappedOptional(ResultTyLoc); | ||
|
@@ -2930,8 +2936,9 @@ void PrintAST::visitSubscriptDecl(SubscriptDecl *decl) { | |
}); | ||
Printer << " -> "; | ||
|
||
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType); | ||
TypeLoc elementTy = decl->getElementTypeLoc(); | ||
Printer.printDeclResultTypePre(decl, elementTy); | ||
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType); | ||
if (!elementTy.getTypeRepr()) | ||
elementTy = TypeLoc::withoutLoc(decl->getElementInterfaceType()); | ||
if (decl->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>()) | ||
|
@@ -3402,7 +3409,27 @@ class TypePrinter : public TypeVisitor<TypePrinter> { | |
return; | ||
} | ||
|
||
if (T->hasSimpleTypeRepr()) { | ||
bool isSimple = T->hasSimpleTypeRepr(); | ||
if (isSimple && T->is<OpaqueTypeArchetypeType>()) { | ||
auto opaqueTy = T->castTo<OpaqueTypeArchetypeType>(); | ||
auto opaqueDecl = opaqueTy->getDecl(); | ||
if (!opaqueDecl->hasName()) { | ||
switch (Options.OpaqueReturnTypePrinting) { | ||
case PrintOptions::OpaqueReturnTypePrintingMode::StableReference: | ||
case PrintOptions::OpaqueReturnTypePrintingMode::Description: | ||
isSimple = true; | ||
break; | ||
case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword: | ||
isSimple = false; | ||
break; | ||
case PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword: { | ||
isSimple = opaqueTy->getConformsTo().size() < 2; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (isSimple) { | ||
visit(T); | ||
} else { | ||
Printer << "("; | ||
|
@@ -4165,7 +4192,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> { | |
} | ||
|
||
void visitNestedArchetypeType(NestedArchetypeType *T) { | ||
visit(T->getParent()); | ||
printWithParensIfNotSimple(T->getParent()); | ||
Printer << "."; | ||
printArchetypeCommon(T); | ||
} | ||
|
@@ -4175,16 +4202,28 @@ class TypePrinter : public TypeVisitor<TypePrinter> { | |
} | ||
|
||
void visitOpaqueTypeArchetypeType(OpaqueTypeArchetypeType *T) { | ||
// Print the type by referencing the opaque decl's synthetic name, if we | ||
// were asked to. | ||
OpaqueTypeDecl *decl = T->getDecl(); | ||
|
||
if (Options.PrintStableReferencesToOpaqueReturnTypes) { | ||
switch (Options.OpaqueReturnTypePrinting) { | ||
case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword: | ||
Printer << "some "; | ||
LLVM_FALLTHROUGH; | ||
case PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword: { | ||
SmallVector<Type, 2> types; | ||
for (auto proto : T->getConformsTo()) | ||
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. What about T->getSuperclass()? Maybe this should be generalized to get all the constraints from 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. Will fix. |
||
types.push_back(proto->TypeDecl::getDeclaredInterfaceType()); | ||
|
||
// Create and visit temporary ProtocolCompositionType. | ||
auto composition = | ||
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 there a more direct way to turn an opaque type into an existential, @jckarter ? 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. I added 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. @jckarter Thanks! I'll modify ASTPrinter to use that too. |
||
ProtocolCompositionType::get(T->getASTContext(), types, false); | ||
visit(composition); | ||
return; | ||
} | ||
case PrintOptions::OpaqueReturnTypePrintingMode::StableReference: { | ||
// Print the source of the opaque return type as a mangled name. | ||
// We'll use type reconstruction while parsing the attribute to | ||
// turn this back into a reference to the naming decl for the opaque | ||
// type. | ||
Printer << "@_opaqueReturnTypeOf("; | ||
OpaqueTypeDecl *decl = T->getDecl(); | ||
|
||
Printer.printEscapedStringLiteral( | ||
decl->getOpaqueReturnTypeIdentifier().str()); | ||
|
@@ -4195,7 +4234,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> { | |
|
||
Printer << u8") \U0001F9B8"; | ||
printGenericArgs(T->getSubstitutions().getReplacementTypes()); | ||
} else { | ||
return; | ||
} | ||
case PrintOptions::OpaqueReturnTypePrintingMode::Description: { | ||
// TODO(opaque): present opaque types with user-facing syntax. we should | ||
// probably print this as `some P` and record the fact that we printed that | ||
// so that diagnostics can add followup notes. | ||
|
@@ -4209,6 +4250,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> { | |
[&] { Printer << ", "; }); | ||
Printer << '>'; | ||
} | ||
return; | ||
} | ||
} | ||
} | ||
|
||
|
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.
Can an opaque type have a superclass and one conformance? CC @jckarter
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.
Yes, an opaque type can have any combination of constraints.