mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
adjust autoclosure/noescape printing to print them in their type
position instead of before a parameter. This wraps up the meat of SE-0049.
This commit is contained in:
@@ -2530,32 +2530,6 @@ void PrintAST::printOneParameter(const ParamDecl *param, bool Curried,
|
||||
|
||||
auto TheTypeLoc = param->getTypeLoc();
|
||||
|
||||
// If the parameter is autoclosure, or noescape, print it. This is stored
|
||||
// on the type of the decl, not on the typerepr.
|
||||
if (param->hasType()) {
|
||||
auto bodyCanType = param->getType()->getCanonicalType();
|
||||
if (auto patternType = dyn_cast<AnyFunctionType>(bodyCanType)) {
|
||||
if (patternType->isAutoClosure() || patternType->isNoEscape()) {
|
||||
Printer.callPrintStructurePre(PrintStructureKind::BuiltinAttribute);
|
||||
switch (patternType->isAutoClosure()*2 + patternType->isNoEscape()) {
|
||||
case 1:
|
||||
Printer.printAttrName("@noescape");
|
||||
break;
|
||||
case 2:
|
||||
Printer.printAttrName("@autoclosure");
|
||||
Printer << "(escaping)";
|
||||
break;
|
||||
case 3: Printer.printAttrName("@autoclosure");
|
||||
break;
|
||||
default:
|
||||
llvm_unreachable("impossible autoclosure/noescape");
|
||||
}
|
||||
Printer.printStructurePost(PrintStructureKind::BuiltinAttribute);
|
||||
Printer << " ";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printArgName();
|
||||
|
||||
if (!TheTypeLoc.getTypeRepr() && param->hasType())
|
||||
@@ -2571,16 +2545,6 @@ void PrintAST::printOneParameter(const ParamDecl *param, bool Curried,
|
||||
Options.ExcludeAttrList.end(), Kind));
|
||||
};
|
||||
|
||||
// Since we have already printed @noescape and @autoclosure, we exclude them
|
||||
// when printing the type.
|
||||
auto hasNoEscape = ContainsFunc(DAK_NoEscape);
|
||||
auto hasAutoClosure = ContainsFunc(DAK_AutoClosure);
|
||||
if (!hasNoEscape)
|
||||
Options.ExcludeAttrList.push_back(DAK_NoEscape);
|
||||
if (!hasAutoClosure)
|
||||
Options.ExcludeAttrList.push_back(DAK_AutoClosure);
|
||||
|
||||
|
||||
// If the parameter is variadic, we will print the "..." after it, but we have
|
||||
// to strip off the added array type.
|
||||
if (param->isVariadic() && TheTypeLoc.getType()) {
|
||||
@@ -2593,13 +2557,6 @@ void PrintAST::printOneParameter(const ParamDecl *param, bool Curried,
|
||||
if (param->isVariadic())
|
||||
Printer << "...";
|
||||
|
||||
// After printing the type, we need to restore what the option used to be.
|
||||
if (!hasNoEscape)
|
||||
RemoveFunc(DAK_NoEscape);
|
||||
if (!hasAutoClosure)
|
||||
RemoveFunc(DAK_AutoClosure);
|
||||
|
||||
|
||||
if (param->isDefaultArgument()) {
|
||||
auto defaultArgStr
|
||||
= getDefaultArgumentSpelling(param->getDefaultArgumentKind());
|
||||
@@ -3691,16 +3648,12 @@ public:
|
||||
void printFunctionExtInfo(AnyFunctionType::ExtInfo info) {
|
||||
if (Options.SkipAttributes)
|
||||
return;
|
||||
auto IsAttrExcluded = [&](DeclAttrKind Kind) {
|
||||
return Options.ExcludeAttrList.end() != std::find(Options.ExcludeAttrList.
|
||||
begin(), Options.ExcludeAttrList.end(), Kind);
|
||||
};
|
||||
if (info.isAutoClosure() && !IsAttrExcluded(DAK_AutoClosure)) {
|
||||
if (info.isAutoClosure()) {
|
||||
if (info.isNoEscape())
|
||||
Printer << "@autoclosure ";
|
||||
else
|
||||
Printer << "@autoclosure(escaping) ";
|
||||
} else if (info.isNoEscape() && !IsAttrExcluded(DAK_NoEscape))
|
||||
} else if (info.isNoEscape())
|
||||
// autoclosure implies noescape.
|
||||
Printer << "@noescape ";
|
||||
|
||||
|
||||
@@ -265,7 +265,6 @@ void ErrorTypeRepr::printImpl(ASTPrinter &Printer,
|
||||
void AttributedTypeRepr::printImpl(ASTPrinter &Printer,
|
||||
const PrintOptions &Opts) const {
|
||||
printAttrs(Printer);
|
||||
Printer << " ";
|
||||
printTypeRepr(Ty, Printer, Opts);
|
||||
}
|
||||
|
||||
@@ -277,12 +276,17 @@ void AttributedTypeRepr::printAttrs(llvm::raw_ostream &OS) const {
|
||||
void AttributedTypeRepr::printAttrs(ASTPrinter &Printer) const {
|
||||
const TypeAttributes &Attrs = getAttrs();
|
||||
if (Attrs.has(TAK_noreturn)) Printer << "@noreturn ";
|
||||
if (Attrs.has(TAK_noescape)) Printer << "@noescape ";
|
||||
if (Attrs.has(TAK_autoclosure)) Printer << "@autoclosure ";
|
||||
|
||||
switch (Attrs.has(TAK_autoclosure)*2 + Attrs.has(TAK_noescape)) {
|
||||
case 0: break; // Nothing specified.
|
||||
case 1: Printer << "@noescape "; break;
|
||||
case 2: Printer << "@autoclosure(escaping) "; break;
|
||||
case 3: Printer << "@autoclosure "; break;
|
||||
}
|
||||
if (Attrs.has(TAK_thin)) Printer << "@thin ";
|
||||
if (Attrs.has(TAK_thick)) Printer << "@thick ";
|
||||
if (Attrs.convention.hasValue()) {
|
||||
Printer << "@convention(" << Attrs.convention.getValue() << ")";
|
||||
Printer << "@convention(" << Attrs.convention.getValue() << ") ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1329,17 +1329,17 @@ public struct DiscardableThingy {
|
||||
|
||||
|
||||
// <rdar://problem/19775868> Swift 1.2b1: Header gen puts @autoclosure in the wrong place
|
||||
// PASS_PRINT_AST: public func ParamAttrs1(@autoclosure a: {{.*}}() -> ())
|
||||
// PASS_PRINT_AST: public func ParamAttrs1(a: @autoclosure () -> ())
|
||||
public func ParamAttrs1(a : @autoclosure () -> ()) {
|
||||
a()
|
||||
}
|
||||
|
||||
// PASS_PRINT_AST: public func ParamAttrs2(@autoclosure(escaping) a: {{.*}}() -> ())
|
||||
// PASS_PRINT_AST: public func ParamAttrs2(a: @autoclosure(escaping) () -> ())
|
||||
public func ParamAttrs2(a : @autoclosure(escaping) () -> ()) {
|
||||
a()
|
||||
}
|
||||
|
||||
// PASS_PRINT_AST: public func ParamAttrs3(@noescape a: {{.*}}() -> ())
|
||||
// PASS_PRINT_AST: public func ParamAttrs3(a: @noescape () -> ())
|
||||
public func ParamAttrs3(a : @noescape () -> ()) {
|
||||
a()
|
||||
}
|
||||
|
||||
@@ -38,8 +38,8 @@ public class ImplicitOptionalInitContainer {
|
||||
|
||||
// ATTR1: class AttributeContainer1 {
|
||||
public class AttributeContainer1 {
|
||||
// ATTR1: func m1(@autoclosure a: () -> Int)
|
||||
// ATTR1: func m1(a: @autoclosure () -> Int)
|
||||
public func m1(a : @autoclosure () -> Int) {}
|
||||
// ATTR1: func m2(@noescape a: () -> Int)
|
||||
// ATTR1: func m2(a: @noescape () -> Int)
|
||||
public func m2(a : @noescape () -> Int) {}
|
||||
}
|
||||
|
||||
@@ -605,13 +605,13 @@ typealias MyVoid = ()
|
||||
// CHECK69-NEXT: <decl.typealias><syntaxtype.keyword>typealias</syntaxtype.keyword> <decl.name>MyAlias</decl.name><<decl.generic_type_param usr="{{.*}}"><decl.generic_type_param.name>T</decl.generic_type_param.name></decl.generic_type_param>, <decl.generic_type_param usr="{{.*}}"><decl.generic_type_param.name>U</decl.generic_type_param.name></decl.generic_type_param>> = <tuple>(<tuple.element><tuple.element.type><ref.generic_type_param usr="{{.*}}">T</ref.generic_type_param></tuple.element.type></tuple.element>, <tuple.element><tuple.element.type><ref.generic_type_param usr="{{.*}}">U</ref.generic_type_param></tuple.element.type></tuple.element>, <tuple.element><tuple.element.type><ref.generic_type_param usr="{{.*}}">T</ref.generic_type_param></tuple.element.type></tuple.element>, <tuple.element><tuple.element.type><ref.generic_type_param usr="{{.*}}">U</ref.generic_type_param></tuple.element.type></tuple.element>)</tuple></decl.typealias>
|
||||
|
||||
// RUN: %sourcekitd-test -req=cursor -pos=155:6 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %mcp_opt %s | FileCheck %s -check-prefix=CHECK70
|
||||
// CHECK70: <decl.function.free><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>paramAutoclosureNoescape1</decl.name>(<decl.var.parameter><syntaxtype.attribute.builtin><syntaxtype.attribute.name>@noescape</syntaxtype.attribute.name></syntaxtype.attribute.builtin> <decl.var.parameter.argument_label>_</decl.var.parameter.argument_label> <decl.var.parameter.name>msg</decl.var.parameter.name>: <decl.var.parameter.type>() -> <decl.function.returntype><ref.struct usr="s:SS">String</ref.struct></decl.function.returntype></decl.var.parameter.type></decl.var.parameter>)</decl.function.free>
|
||||
// CHECK70: <decl.function.free><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>paramAutoclosureNoescape1</decl.name>(<decl.var.parameter><decl.var.parameter.argument_label>_</decl.var.parameter.argument_label> <decl.var.parameter.name>msg</decl.var.parameter.name>: <decl.var.parameter.type>() -> <decl.function.returntype><ref.struct usr="s:SS">String</ref.struct></decl.function.returntype></decl.var.parameter.type></decl.var.parameter>)</decl.function.free>
|
||||
|
||||
// RUN: %sourcekitd-test -req=cursor -pos=156:6 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %mcp_opt %s | FileCheck %s -check-prefix=CHECK71
|
||||
// CHECK71: <decl.function.free><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>paramAutoclosureNoescape2</decl.name>(<decl.var.parameter><syntaxtype.attribute.builtin><syntaxtype.attribute.name>@autoclosure</syntaxtype.attribute.name></syntaxtype.attribute.builtin> <decl.var.parameter.argument_label>_</decl.var.parameter.argument_label> <decl.var.parameter.name>msg</decl.var.parameter.name>: <decl.var.parameter.type>() -> <decl.function.returntype><ref.struct usr="s:SS">String</ref.struct></decl.function.returntype></decl.var.parameter.type></decl.var.parameter>)</decl.function.free>
|
||||
// CHECK71: <decl.function.free><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>paramAutoclosureNoescape2</decl.name>(<decl.var.parameter><decl.var.parameter.argument_label>_</decl.var.parameter.argument_label> <decl.var.parameter.name>msg</decl.var.parameter.name>: <decl.var.parameter.type>() -> <decl.function.returntype><ref.struct usr="s:SS">String</ref.struct></decl.function.returntype></decl.var.parameter.type></decl.var.parameter>)</decl.function.free>
|
||||
|
||||
// RUN: %sourcekitd-test -req=cursor -pos=157:6 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %mcp_opt %s | FileCheck %s -check-prefix=CHECK72
|
||||
// CHECK72: <decl.function.free><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>paramAutoclosureNoescape3</decl.name>(<decl.var.parameter><syntaxtype.attribute.builtin><syntaxtype.attribute.name>@autoclosure</syntaxtype.attribute.name>(escaping)</syntaxtype.attribute.builtin> <decl.var.parameter.argument_label>_</decl.var.parameter.argument_label> <decl.var.parameter.name>msg</decl.var.parameter.name>: <decl.var.parameter.type>() -> <decl.function.returntype><ref.struct usr="s:SS">String</ref.struct></decl.function.returntype></decl.var.parameter.type></decl.var.parameter>)</decl.function.free>
|
||||
// CHECK72: <decl.function.free><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>paramAutoclosureNoescape3</decl.name>(<decl.var.parameter><decl.var.parameter.argument_label>_</decl.var.parameter.argument_label> <decl.var.parameter.name>msg</decl.var.parameter.name>: <decl.var.parameter.type>() -> <decl.function.returntype><ref.struct usr="s:SS">String</ref.struct></decl.function.returntype></decl.var.parameter.type></decl.var.parameter>)</decl.function.free>
|
||||
|
||||
// RUN: %sourcekitd-test -req=cursor -pos=159:6 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %mcp_opt %s | FileCheck %s -check-prefix=CHECK73
|
||||
// CHECK73: <decl.function.free>
|
||||
|
||||
@@ -36,9 +36,9 @@ func foo3(a: Float, b: Bool) {}
|
||||
// CHECK-REPLACEMENT1: <Group>Collection/Array</Group>
|
||||
// CHECK-REPLACEMENT1: <Declaration>func sorted() -> [<Type usr="s:Si">Int</Type>]</Declaration>
|
||||
// CHECK-REPLACEMENT1: RELATED BEGIN
|
||||
// CHECK-REPLACEMENT1: sorted(@noescape isOrderedBefore: @noescape (Int, Int) -> Bool) -> [Int]</RelatedName>
|
||||
// CHECK-REPLACEMENT1: sorted(isOrderedBefore: @noescape (Int, Int) -> Bool) -> [Int]</RelatedName>
|
||||
// CHECK-REPLACEMENT1: sorted() -> [Int]</RelatedName>
|
||||
// CHECK-REPLACEMENT1: sorted(@noescape isOrderedBefore: @noescape (Int, Int) -> Bool) -> [Int]</RelatedName>
|
||||
// CHECK-REPLACEMENT1: sorted(isOrderedBefore: @noescape (Int, Int) -> Bool) -> [Int]</RelatedName>
|
||||
// CHECK-REPLACEMENT1: RELATED END
|
||||
|
||||
// RUN: %sourcekitd-test -req=cursor -pos=9:8 %s -- %s %mcp_opt %clang-importer-sdk | FileCheck -check-prefix=CHECK-REPLACEMENT2 %s
|
||||
@@ -47,10 +47,10 @@ func foo3(a: Float, b: Bool) {}
|
||||
|
||||
// RUN: %sourcekitd-test -req=cursor -pos=15:10 %s -- %s %mcp_opt %clang-importer-sdk | FileCheck -check-prefix=CHECK-REPLACEMENT3 %s
|
||||
// CHECK-REPLACEMENT3: <Group>Collection/Array</Group>
|
||||
// CHECK-REPLACEMENT3: func sorted(@noescape isOrderedBefore: @noescape (<Type usr="s:V13cursor_stdlib2S1">S1</Type>
|
||||
// CHECK-REPLACEMENT3: func sorted(isOrderedBefore: @noescape (<Type usr="s:V13cursor_stdlib2S1">S1</Type>
|
||||
// CHECK-REPLACEMENT3: sorted() -> [S1]</RelatedName>
|
||||
// CHECK-REPLACEMENT3: sorted() -> [S1]</RelatedName>
|
||||
// CHECK-REPLACEMENT3: sorted(@noescape isOrderedBefore: @noescape (S1, S1) -> Bool) -> [S1]</RelatedName>
|
||||
// CHECK-REPLACEMENT3: sorted(isOrderedBefore: @noescape (S1, S1) -> Bool) -> [S1]</RelatedName>
|
||||
|
||||
// RUN: %sourcekitd-test -req=cursor -pos=18:8 %s -- %s %mcp_opt %clang-importer-sdk | FileCheck -check-prefix=CHECK-REPLACEMENT4 %s
|
||||
// CHECK-REPLACEMENT4: <Group>Collection/Array</Group>
|
||||
|
||||
Reference in New Issue
Block a user