Skip to content

Commit 6619e12

Browse files
committed
Fix the AST printer to properly parenthesize the parameter list of
GenericFunctionType and PolymorphicFunctionType.
1 parent e1bdadd commit 6619e12

File tree

7 files changed

+35
-13
lines changed

7 files changed

+35
-13
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2741,7 +2741,7 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
27412741

27422742
auto &Context = decl->getASTContext();
27432743
Type ResultTy = decl->getResultType();
2744-
if (ResultTy && !ResultTy->isEqual(TupleType::getEmpty(Context))) {
2744+
if (ResultTy && !ResultTy->isVoid()) {
27452745
Printer << " -> ";
27462746
// Use the non-repr external type, but reuse the TypeLoc printing code.
27472747
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType);
@@ -3766,8 +3766,19 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
37663766
printFunctionExtInfo(T->getExtInfo());
37673767
printGenericParams(&T->getGenericParams());
37683768
Printer << " ";
3769-
printWithParensIfNotSimple(T->getInput());
3769+
3770+
bool needsParens =
3771+
!isa<ParenType>(T->getInput().getPointer()) &&
3772+
!T->getInput()->is<TupleType>();
3773+
3774+
if (needsParens)
3775+
Printer << "(";
37703776

3777+
visit(T->getInput());
3778+
3779+
if (needsParens)
3780+
Printer << ")";
3781+
37713782
if (T->throws())
37723783
Printer << " " << tok::kw_throws;
37733784

@@ -3922,7 +3933,18 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
39223933
printFunctionExtInfo(T->getExtInfo());
39233934
printGenericSignature(T->getGenericParams(), T->getRequirements());
39243935
Printer << " ";
3925-
printWithParensIfNotSimple(T->getInput());
3936+
3937+
bool needsParens =
3938+
!isa<ParenType>(T->getInput().getPointer()) &&
3939+
!T->getInput()->is<TupleType>();
3940+
3941+
if (needsParens)
3942+
Printer << "(";
3943+
3944+
visit(T->getInput());
3945+
3946+
if (needsParens)
3947+
Printer << ")";
39263948

39273949
if (T->throws())
39283950
Printer << " " << tok::kw_throws;

test/SILGen/dynamic_lookup.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func direct_to_class(_ obj: AnyObject) {
3232
// CHECK-LABEL: sil hidden @_TF14dynamic_lookup18direct_to_protocol
3333
func direct_to_protocol(_ obj: AnyObject) {
3434
// CHECK: [[OBJ_SELF:%[0-9]+]] = open_existential_ref [[EX:%[0-9]+]] : $AnyObject to $@opened({{.*}}) AnyObject
35-
// CHECK: [[METHOD:%[0-9]+]] = dynamic_method [volatile] [[OBJ_SELF]] : $@opened({{.*}}) AnyObject, #P.g!1.foreign : <Self where Self : P> Self -> () -> (), $@convention(objc_method) (@opened({{.*}}) AnyObject) -> ()
35+
// CHECK: [[METHOD:%[0-9]+]] = dynamic_method [volatile] [[OBJ_SELF]] : $@opened({{.*}}) AnyObject, #P.g!1.foreign : <Self where Self : P> (Self) -> () -> (), $@convention(objc_method) (@opened({{.*}}) AnyObject) -> ()
3636
// CHECK: apply [[METHOD]]([[OBJ_SELF]]) : $@convention(objc_method) (@opened({{.*}}) AnyObject) -> ()
3737
obj.g!()
3838
}

test/SILGen/dynamic_self.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func testDynamicSelfDispatchGeneric(gy: GY<Int>) {
5050
// CHECK: bb0([[GY:%[0-9]+]] : $GY<Int>):
5151
// CHECK: strong_retain [[GY]]
5252
// CHECK: [[GY_AS_GX:%[0-9]+]] = upcast [[GY]] : $GY<Int> to $GX<Array<Int>>
53-
// CHECK: [[GX_F:%[0-9]+]] = class_method [[GY_AS_GX]] : $GX<Array<Int>>, #GX.f!1 : <T> GX<T> -> () -> Self , $@convention(method) <τ_0_0> (@guaranteed GX<τ_0_0>) -> @owned GX<τ_0_0>
53+
// CHECK: [[GX_F:%[0-9]+]] = class_method [[GY_AS_GX]] : $GX<Array<Int>>, #GX.f!1 : <T> (GX<T>) -> () -> Self , $@convention(method) <τ_0_0> (@guaranteed GX<τ_0_0>) -> @owned GX<τ_0_0>
5454
// CHECK: [[GX_RESULT:%[0-9]+]] = apply [[GX_F]]<[Int]>([[GY_AS_GX]]) : $@convention(method) <τ_0_0> (@guaranteed GX<τ_0_0>) -> @owned GX<τ_0_0>
5555
// CHECK: strong_release [[GY_AS_GX]]
5656
// CHECK: [[GY_RESULT:%[0-9]+]] = unchecked_ref_cast [[GX_RESULT]] : $GX<Array<Int>> to $GY<Int>

test/SILGen/objc_generic_class.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Generic<T>: NSObject {
3232

3333
// CHECK-LABEL: sil hidden @_TFC18objc_generic_class11SubGeneric1D : $@convention(method) <U, V> (@owned SubGeneric1<U, V>) -> () {
3434
// CHECK: bb0([[SELF:%.*]] : $SubGeneric1<U, V>):
35-
// CHECK: [[SUPER_DEALLOC:%.*]] = super_method [[SELF]] : $SubGeneric1<U, V>, #Generic.deinit!deallocator.foreign : <T> Generic<T> -> () -> () , $@convention(objc_method) <τ_0_0> (Generic<τ_0_0>) -> ()
35+
// CHECK: [[SUPER_DEALLOC:%.*]] = super_method [[SELF]] : $SubGeneric1<U, V>, #Generic.deinit!deallocator.foreign : <T> (Generic<T>) -> () -> () , $@convention(objc_method) <τ_0_0> (Generic<τ_0_0>) -> ()
3636
// CHECK: [[SUPER:%.*]] = upcast [[SELF:%.*]] : $SubGeneric1<U, V> to $Generic<Int>
3737
// CHECK: apply [[SUPER_DEALLOC]]<Int>([[SUPER]])
3838
class SubGeneric1<U, V>: Generic<Int> {

test/SILGen/objc_imported_generic.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public func genericMethodOnAnyObject(o: AnyObject, b: Bool) -> AnyObject {
1818
}
1919

2020
// CHECK-LABEL: sil @_TF21objc_imported_generic24genericMethodOnAnyObject
21-
// CHECK: dynamic_method [volatile] {{%.*}} : $@opened([[TAG:.*]]) AnyObject, #GenericClass.thing!1.foreign : <T where T : AnyObject> GenericClass<T> -> () -> T?, $@convention(objc_method) @pseudogeneric (@opened([[TAG]]) AnyObject) -> @autoreleased Optional<AnyObject>
21+
// CHECK: dynamic_method [volatile] {{%.*}} : $@opened([[TAG:.*]]) AnyObject, #GenericClass.thing!1.foreign : <T where T : AnyObject> (GenericClass<T>) -> () -> T?, $@convention(objc_method) @pseudogeneric (@opened([[TAG]]) AnyObject) -> @autoreleased Optional<AnyObject>
2222

2323
public func genericMethodOnAnyObjectChained(o: AnyObject, b: Bool) -> AnyObject? {
2424
return o.thing?()

test/SILGen/partial_apply_super.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class GenericChildToFixedGenericOutsideParent<A> : GenericOutsideParent<A> {
188188
// CHECK-LABEL: sil hidden @_TFC19partial_apply_super39GenericChildToFixedGenericOutsideParent6methodfT_T_
189189
// CHECK: [[DOFOO:%[0-9]+]] = function_ref @_TF19partial_apply_super5doFooFFT_T_T_ : $@convention(thin) (@owned @callee_owned () -> ()) -> ()
190190
// CHECK: [[CASTED_SELF:%[0-9]+]] = upcast %0 : $GenericChildToFixedGenericOutsideParent<A> to $GenericOutsideParent<A>
191-
// CHECK: [[SUPER_METHOD:%[0-9]+]] = super_method %0 : $GenericChildToFixedGenericOutsideParent<A>, #GenericOutsideParent.method!1 : <A> GenericOutsideParent<A> -> () -> () , $@convention(method) <τ_0_0> (@guaranteed GenericOutsideParent<τ_0_0>) -> ()
191+
// CHECK: [[SUPER_METHOD:%[0-9]+]] = super_method %0 : $GenericChildToFixedGenericOutsideParent<A>, #GenericOutsideParent.method!1 : <A> (GenericOutsideParent<A>) -> () -> () , $@convention(method) <τ_0_0> (@guaranteed GenericOutsideParent<τ_0_0>) -> ()
192192
// CHECK: [[PARTIAL_APPLY:%[0-9]+]] = partial_apply [[SUPER_METHOD]]<A>([[CASTED_SELF]]) : $@convention(method) <τ_0_0> (@guaranteed GenericOutsideParent<τ_0_0>) -> ()
193193
// CHECK: apply [[DOFOO]]([[PARTIAL_APPLY]]) : $@convention(thin) (@owned @callee_owned () -> ()) -> ()
194194
override func method() {
@@ -198,7 +198,7 @@ class GenericChildToFixedGenericOutsideParent<A> : GenericOutsideParent<A> {
198198
// CHECK-LABEL: sil hidden @_TZFC19partial_apply_super39GenericChildToFixedGenericOutsideParent11classMethodfT_T_
199199
// CHECK: [[DOFOO:%[0-9]+]] = function_ref @_TF19partial_apply_super5doFooFFT_T_T_ : $@convention(thin) (@owned @callee_owned () -> ()) -> ()
200200
// CHECK: [[CASTED_SELF:%[0-9]+]] = upcast %0 : $@thick GenericChildToFixedGenericOutsideParent<A>.Type to $@thick GenericOutsideParent<A>.Type
201-
// CHECK: [[SUPER_METHOD:%[0-9]+]] = super_method %0 : $@thick GenericChildToFixedGenericOutsideParent<A>.Type, #GenericOutsideParent.classMethod!1 : <A> GenericOutsideParent<A>.Type -> () -> () , $@convention(method) <τ_0_0> (@thick GenericOutsideParent<τ_0_0>.Type) -> ()
201+
// CHECK: [[SUPER_METHOD:%[0-9]+]] = super_method %0 : $@thick GenericChildToFixedGenericOutsideParent<A>.Type, #GenericOutsideParent.classMethod!1 : <A> (GenericOutsideParent<A>.Type) -> () -> () , $@convention(method) <τ_0_0> (@thick GenericOutsideParent<τ_0_0>.Type) -> ()
202202
// CHECK: [[PARTIAL_APPLY:%[0-9]+]] = partial_apply [[SUPER_METHOD]]<A>([[CASTED_SELF]]) : $@convention(method) <τ_0_0> (@thick GenericOutsideParent<τ_0_0>.Type) -> ()
203203
// CHECK: apply [[DOFOO]]([[PARTIAL_APPLY]]) : $@convention(thin) (@owned @callee_owned () -> ()) -> ()
204204
override class func classMethod() {
@@ -210,7 +210,7 @@ class GenericChildToResilientGenericOutsideParent<A> : ResilientGenericOutsidePa
210210
// CHECK-LABEL: sil hidden @_TFC19partial_apply_super43GenericChildToResilientGenericOutsideParent6methodfT_T_
211211
// CHECK: [[DOFOO:%[0-9]+]] = function_ref @_TF19partial_apply_super5doFooFFT_T_T_ : $@convention(thin) (@owned @callee_owned () -> ()) -> ()
212212
// CHECK: [[CASTED_SELF:%[0-9]+]] = upcast %0 : $GenericChildToResilientGenericOutsideParent<A> to $ResilientGenericOutsideParent<A>
213-
// CHECK: [[SUPER_METHOD:%[0-9]+]] = super_method %0 : $GenericChildToResilientGenericOutsideParent<A>, #ResilientGenericOutsideParent.method!1 : <A> ResilientGenericOutsideParent<A> -> () -> () , $@convention(method) <τ_0_0> (@guaranteed ResilientGenericOutsideParent<τ_0_0>) -> ()
213+
// CHECK: [[SUPER_METHOD:%[0-9]+]] = super_method %0 : $GenericChildToResilientGenericOutsideParent<A>, #ResilientGenericOutsideParent.method!1 : <A> (ResilientGenericOutsideParent<A>) -> () -> () , $@convention(method) <τ_0_0> (@guaranteed ResilientGenericOutsideParent<τ_0_0>) -> ()
214214
// CHECK: [[PARTIAL_APPLY:%[0-9]+]] = partial_apply [[SUPER_METHOD]]<A>([[CASTED_SELF]]) : $@convention(method) <τ_0_0> (@guaranteed ResilientGenericOutsideParent<τ_0_0>) -> ()
215215
// CHECK: apply [[DOFOO]]([[PARTIAL_APPLY]]) : $@convention(thin) (@owned @callee_owned () -> ()) -> ()
216216
override func method() {
@@ -220,7 +220,7 @@ class GenericChildToResilientGenericOutsideParent<A> : ResilientGenericOutsidePa
220220
// CHECK-LABEL: sil hidden @_TZFC19partial_apply_super43GenericChildToResilientGenericOutsideParent11classMethodfT_T_
221221
// CHECK: [[DOFOO:%[0-9]+]] = function_ref @_TF19partial_apply_super5doFooFFT_T_T_ : $@convention(thin) (@owned @callee_owned () -> ()) -> ()
222222
// CHECK: [[CASTED_SELF:%[0-9]+]] = upcast %0 : $@thick GenericChildToResilientGenericOutsideParent<A>.Type to $@thick ResilientGenericOutsideParent<A>.Type
223-
// CHECK: [[SUPER_METHOD:%[0-9]+]] = super_method %0 : $@thick GenericChildToResilientGenericOutsideParent<A>.Type, #ResilientGenericOutsideParent.classMethod!1 : <A> ResilientGenericOutsideParent<A>.Type -> () -> () , $@convention(method) <τ_0_0> (@thick ResilientGenericOutsideParent<τ_0_0>.Type) -> ()
223+
// CHECK: [[SUPER_METHOD:%[0-9]+]] = super_method %0 : $@thick GenericChildToResilientGenericOutsideParent<A>.Type, #ResilientGenericOutsideParent.classMethod!1 : <A> (ResilientGenericOutsideParent<A>.Type) -> () -> () , $@convention(method) <τ_0_0> (@thick ResilientGenericOutsideParent<τ_0_0>.Type) -> ()
224224
// CHECK: [[PARTIAL_APPLY:%[0-9]+]] = partial_apply [[SUPER_METHOD]]<A>([[CASTED_SELF]]) : $@convention(method) <τ_0_0> (@thick ResilientGenericOutsideParent<τ_0_0>.Type) -> ()
225225
// CHECK: apply [[DOFOO]]([[PARTIAL_APPLY]]) : $@convention(thin) (@owned @callee_owned () -> ()) -> ()
226226
override class func classMethod() {

test/attr/attr_specialize.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ struct FloatElement : HasElt {
6666
typealias Element = Float
6767
}
6868
@_specialize(FloatElement)
69-
@_specialize(IntElement) // expected-error{{'<T : HasElt where T.Element == Float> (T) -> ()' (aka '<T : HasElt where T.Element == Float> T -> ()') requires the types 'Element' (aka 'Int') and 'Float' be equivalent}}
69+
@_specialize(IntElement) // expected-error{{'<T : HasElt where T.Element == Float> (T) -> ()' requires the types 'Element' (aka 'Int') and 'Float' be equivalent}}
7070
func sameTypeRequirement<T : HasElt where T.Element == Float>(_ t: T) {}
7171

7272
class Base {}
7373
class Sub : Base {}
7474
class NonSub {}
7575
@_specialize(Sub)
76-
@_specialize(NonSub) // expected-error{{'<T : Base> (T) -> ()' (aka '<T : Base> T -> ()') requires that 'NonSub' inherit from 'Base'}}
76+
@_specialize(NonSub) // expected-error{{'<T : Base> (T) -> ()' requires that 'NonSub' inherit from 'Base'}}
7777
func superTypeRequirement<T : Base>(_ t: T) {}

0 commit comments

Comments
 (0)