Skip to content

Commit b8069fb

Browse files
committed
Update tests to remove @autoclosure in decl position
Also restore some diagnostics in TypeCheckType that should have migrated with the attributes. In particular, diagnose cases where we have @autoclosure with any function type repr with a non-Void input type. Resolves SR-5296
1 parent ab580a3 commit b8069fb

File tree

8 files changed

+43
-49
lines changed

8 files changed

+43
-49
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -993,9 +993,6 @@ ERROR(missing_initializer_def,PointsToFirstBadToken,
993993
"initializer requires a body", ())
994994

995995
// Attributes
996-
ERROR(attr_decl_attr_now_on_type,none,
997-
"%0 is now an attribute on a parameter type, "
998-
"instead of on the parameter itself", (StringRef))
999996

1000997
ERROR(operator_not_func,none,
1001998
"operators must be declared with 'func'", ())
@@ -2183,20 +2180,15 @@ ERROR(rethrows_without_throwing_parameter,none,
21832180
"'rethrows' function must take a throwing function argument", ())
21842181

21852182
ERROR(autoclosure_function_type,none,
2186-
"@autoclosure may only be applied to values of function type",
2183+
"@autoclosure attribute only applies to function types",
21872184
())
21882185
ERROR(autoclosure_function_input_nonunit,none,
2189-
"autoclosure argument type must be '()'", ())
2186+
"argument type of @autoclosure parameter must be '()'", ())
21902187

21912188
// FIXME: drop these when we drop @noescape
2192-
ERROR(noescape_function_type,none,
2193-
"@noescape may only be applied to parameters of function type",
2194-
())
21952189
ERROR(noescape_implied_by_autoclosure,none,
21962190
"@noescape is implied by @autoclosure and should not be "
21972191
"redundantly specified", ())
2198-
ERROR(noescape_conflicts_escaping_autoclosure,none,
2199-
"@noescape conflicts with @autoclosure(escaping)", ())
22002192

22012193
ERROR(escaping_non_function_parameter,none,
22022194
"@escaping attribute may only be used in function parameter position", ())

include/swift/Migrator/FixitFilter.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ struct FixitFilter {
111111
Info.ID == diag::function_type_no_parens.ID ||
112112
Info.ID == diag::convert_let_to_var.ID ||
113113
Info.ID == diag::parameter_extraneous_double_up.ID ||
114-
Info.ID == diag::attr_decl_attr_now_on_type.ID ||
115114
Info.ID == diag::noescape_parameter.ID ||
116115
Info.ID == diag::where_inside_brackets.ID ||
117116
Info.ID == diag::selector_construction_suggest.ID ||

lib/Sema/TypeCheckType.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,7 +2064,14 @@ Type TypeResolver::resolveAttributedType(TypeAttributes &attrs,
20642064
// Function attributes require a syntactic function type.
20652065
auto *fnRepr = dyn_cast<FunctionTypeRepr>(repr);
20662066

2067-
if (hasFunctionAttr && fnRepr && (options & TR_SILType)) {
2067+
if (!fnRepr) {
2068+
if (attrs.has(TAK_autoclosure)) {
2069+
TC.diagnose(attrs.getLoc(TAK_autoclosure),
2070+
diag::autoclosure_function_type);
2071+
attrs.clearAttribute(TAK_autoclosure);
2072+
}
2073+
// Fall through to diagnose below.
2074+
} else if (hasFunctionAttr && (options & TR_SILType)) {
20682075
SILFunctionType::Representation rep;
20692076

20702077
auto calleeConvention = ParameterConvention::Direct_Unowned;
@@ -2107,8 +2114,7 @@ Type TypeResolver::resolveAttributedType(TypeAttributes &attrs,
21072114

21082115
ty = resolveSILFunctionType(fnRepr, options, extInfo, calleeConvention);
21092116
if (!ty || ty->hasError()) return ty;
2110-
} else if (hasFunctionAttr && fnRepr) {
2111-
2117+
} else if (hasFunctionAttr) {
21122118
FunctionType::Representation rep = FunctionType::Representation::Swift;
21132119
if (attrs.hasConvention()) {
21142120
auto parsedRep =
@@ -2136,9 +2142,17 @@ Type TypeResolver::resolveAttributedType(TypeAttributes &attrs,
21362142
: diag::attr_only_on_parameters, "@autoclosure");
21372143
attrs.clearAttribute(TAK_autoclosure);
21382144
}
2145+
2146+
auto *FuncTyInput = dyn_cast<TupleTypeRepr>(fnRepr->getArgsTypeRepr());
2147+
if ((!FuncTyInput || FuncTyInput->getNumElements() != 0)
2148+
&& attrs.has(TAK_autoclosure)) {
2149+
TC.diagnose(attrs.getLoc(TAK_autoclosure),
2150+
diag::autoclosure_function_input_nonunit);
2151+
attrs.clearAttribute(TAK_autoclosure);
2152+
}
21392153

21402154
// @noreturn has been replaced with a 'Never' return type.
2141-
if (fnRepr && attrs.has(TAK_noreturn)) {
2155+
if (attrs.has(TAK_noreturn)) {
21422156
auto loc = attrs.getLoc(TAK_noreturn);
21432157
auto attrRange = getTypeAttrRangeWithAt(TC, loc);
21442158
auto resultRange = fnRepr->getResultTypeRepr()->getSourceRange();
@@ -2179,8 +2193,7 @@ Type TypeResolver::resolveAttributedType(TypeAttributes &attrs,
21792193
isVariadicFunctionParam && Context.isSwiftVersion3();
21802194

21812195
// The attribute is meaningless except on parameter types.
2182-
bool shouldDiagnose = !isFunctionParam && !skipDiagnostic;
2183-
if (shouldDiagnose) {
2196+
if (!isFunctionParam && !skipDiagnostic) {
21842197
auto loc = attrs.getLoc(TAK_escaping);
21852198
auto attrRange = getTypeAttrRangeWithAt(TC, loc);
21862199

test/IDE/complete_decl_attribute.swift

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=AVAILABILITY1 | %FileCheck %s -check-prefix=AVAILABILITY1
22
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=AVAILABILITY2 | %FileCheck %s -check-prefix=AVAILABILITY2
3-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=KEYWORD1 | %FileCheck %s -check-prefix=KEYWORD1
43
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=KEYWORD2 | %FileCheck %s -check-prefix=KEYWORD2
54
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=KEYWORD3 | %FileCheck %s -check-prefix=KEYWORD3
65
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=KEYWORD4 | %FileCheck %s -check-prefix=KEYWORD4
@@ -31,15 +30,6 @@
3130
// AVAILABILITY2-NEXT: Keyword/None: deprecated: [#Specify version number#]; name=deprecated{{$}}
3231
// AVAILABILITY2-NEXT: End completions
3332

34-
35-
36-
func method(@#^KEYWORD1^#) {}
37-
38-
// KEYWORD1: Begin completions, 2 items
39-
// KEYWORD1-NEXT: Keyword/None: autoclosure[#Param Attribute#]; name=autoclosure{{$}}
40-
// KEYWORD1-NEXT: Keyword/None: noescape[#Param Attribute#]; name=noescape{{$}}
41-
// KEYWORD1-NEXT: End completions
42-
4333
@#^KEYWORD2^#
4434
func method(){}
4535

@@ -86,7 +76,7 @@ struct S{}
8676

8777
@#^KEYWORD_LAST^#
8878

89-
// KEYWORD_LAST: Begin completions, 21 items
79+
// KEYWORD_LAST: Begin completions, 19 items
9080
// KEYWORD_LAST-NEXT: Keyword/None: available[#Declaration Attribute#]; name=available{{$}}
9181
// KEYWORD_LAST-NEXT: Keyword/None: objc[#Declaration Attribute#]; name=objc{{$}}
9282
// KEYWORD_LAST-NEXT: Keyword/None: noreturn[#Declaration Attribute#]; name=noreturn{{$}}
@@ -99,8 +89,6 @@ struct S{}
9989
// KEYWORD_LAST-NEXT: Keyword/None: UIApplicationMain[#Declaration Attribute#]; name=UIApplicationMain{{$}}
10090
// KEYWORD_LAST-NEXT: Keyword/None: inline[#Declaration Attribute#]; name=inline{{$}}
10191
// KEYWORD_LAST-NEXT: Keyword/None: requires_stored_property_inits[#Declaration Attribute#]; name=requires_stored_property_inits{{$}}
102-
// KEYWORD_LAST-NEXT: Keyword/None: autoclosure[#Declaration Attribute#]; name=autoclosure{{$}}
103-
// KEYWORD_LAST-NEXT: Keyword/None: noescape[#Declaration Attribute#]; name=noescape{{$}}
10492
// KEYWORD_LAST-NEXT: Keyword/None: nonobjc[#Declaration Attribute#]; name=nonobjc{{$}}
10593
// KEYWORD_LAST-NEXT: Keyword/None: objcMembers[#Declaration Attribute#]; name=objcMembers{{$}}
10694
// KEYWORD_LAST-NEXT: Keyword/None: NSApplicationMain[#Declaration Attribute#]; name=NSApplicationMain{{$}}

test/IDE/complete_value_expr.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ struct FooStruct {
187187
mutating
188188
func instanceFunc8(_ a: (Int, Int)) {}
189189
mutating
190-
func instanceFunc9(@autoclosure a: () -> Int) {}
190+
func instanceFunc9(a: @autoclosure () -> Int) {}
191191

192192
mutating
193193
func varargInstanceFunc0(_ v: Int...) {}

test/Parse/type_expr.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ func testFunctionCollectionTypes() {
254254
_ = [(Int) throws -> Int]()
255255
_ = [@convention(swift) (Int) throws -> Int]().count
256256
_ = [(inout Int) throws -> (inout () -> Void) -> Void]().count
257-
_ = [String: (@autoclosure (Int) -> Int32) -> Void]().keys
257+
_ = [String: (@autoclosure (Int) -> Int32) -> Void]().keys // expected-error {{argument type of @autoclosure parameter must be '()'}}
258258
let _ = [(Int) -> throws Int]() // expected-error{{'throws' may only occur before '->'}}
259259
let _ = [Int throws Int](); // expected-error{{'throws' may only occur before '->'}} expected-error {{consecutive statements on a line must be separated by ';'}}
260260
}

test/attr/attr_autoclosure.swift

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Simple case.
44
var fn : @autoclosure () -> Int = 4 // expected-error {{@autoclosure may only be used on parameters}} expected-error {{cannot convert value of type 'Int' to specified type '() -> Int'}}
55

6-
@autoclosure func func1() {} // expected-error {{@autoclosure may only be used on 'parameter' declarations}}
6+
@autoclosure func func1() {} // expected-error {{attribute can only be applied to types, not declarations}}
77

88
func func1a(_ v1 : @autoclosure Int) {} // expected-error {{@autoclosure attribute only applies to function types}}
99

@@ -14,24 +14,26 @@ func func3(fp fpx : @autoclosure () -> Int) {func3(fp: 0)}
1414
func func4(fp : @autoclosure () -> Int) {func4(fp: 0)}
1515
func func6(_: @autoclosure () -> Int) {func6(0)}
1616

17-
// autoclosure + inout don't make sense.
17+
// autoclosure + inout doesn't make sense.
1818
func func8(_ x: inout @autoclosure () -> Bool) -> Bool { // expected-error {{@autoclosure may only be used on parameters}}
1919
}
2020

21+
func func9(_ x: @autoclosure (Int) -> Bool) {} // expected-error {{argument type of @autoclosure parameter must be '()'}}
22+
func func10(_ x: @autoclosure (Int, String, Int) -> Void) {} // expected-error {{argument type of @autoclosure parameter must be '()'}}
2123

2224
// <rdar://problem/19707366> QoI: @autoclosure declaration change fixit
2325
let migrate4 : (@autoclosure() -> ()) -> ()
2426

2527

2628
struct SomeStruct {
27-
@autoclosure let property : () -> Int // expected-error {{@autoclosure may only be used on 'parameter' declarations}} {{3-16=}}
29+
@autoclosure let property : () -> Int // expected-error {{attribute can only be applied to types, not declarations}}
2830

2931
init() {
3032
}
3133
}
3234

3335
class BaseClass {
34-
@autoclosure var property : () -> Int // expected-error {{@autoclosure may only be used on 'parameter' declarations}} {{3-16=}}
36+
@autoclosure var property : () -> Int // expected-error {{attribute can only be applied to types, not declarations}}
3537
init() {}
3638
}
3739

@@ -63,13 +65,12 @@ struct S : P2 {
6365

6466

6567
struct AutoclosureEscapeTest {
66-
@autoclosure let delayed: () -> Int // expected-error {{@autoclosure may only be used on 'parameter' declarations}} {{3-16=}}
68+
@autoclosure let delayed: () -> Int // expected-error {{attribute can only be applied to types, not declarations}}
6769
}
6870

6971
// @autoclosure(escaping)
70-
// expected-error @+1 {{@autoclosure is now an attribute on a parameter type, instead of on the parameter itself}} {{13-34=}} {{38-38=@autoclosure @escaping }}
71-
func func10(@autoclosure(escaping _: () -> ()) { } // expected-error{{expected ')' in @autoclosure}}
72-
// expected-note@-1{{to match this opening '('}}
72+
// expected-error @+1 {{attribute can only be applied to types, not declarations}}
73+
func func10(@autoclosure(escaping _: () -> ()) { } // expected-error{{expected parameter name followed by ':'}}
7374
7475
func func11(_: @autoclosure(escaping) @noescape () -> ()) { } // expected-error{{@escaping conflicts with @noescape}}
7576
// expected-warning@-1{{@autoclosure(escaping) is deprecated; use @autoclosure @escaping instead}} {{28-38= @escaping}}
@@ -133,12 +134,13 @@ let _ : (@autoclosure(escaping) () -> ()) -> ()
133134

134135
// escaping is the name of param type
135136
let _ : (@autoclosure(escaping) -> ()) -> () // expected-error {{use of undeclared type 'escaping'}}
137+
// expected-error@-1 {{argument type of @autoclosure parameter must be '()'}}
136138

137139
// Migration
138-
// expected-error @+1 {{@autoclosure is now an attribute on a parameter type, instead of on the parameter itself}} {{16-28=}} {{32-32=@autoclosure }}
140+
// expected-error @+1 {{attribute can only be applied to types, not declarations}}
139141
func migrateAC(@autoclosure _: () -> ()) { }
140142

141-
// expected-error @+1 {{@autoclosure is now an attribute on a parameter type, instead of on the parameter itself}} {{17-39=}} {{43-43=@autoclosure @escaping }}
143+
// expected-error @+1 {{attribute can only be applied to types, not declarations}}
142144
func migrateACE(@autoclosure(escaping) _: () -> ()) { }
143145

144146
func takesAutoclosure(_ fn: @autoclosure () -> Int) {}

test/attr/attr_noescape.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %target-typecheck-verify-swift
22

3-
@noescape var fn : () -> Int = { 4 } // expected-error {{@noescape may only be used on 'parameter' declarations}} {{1-11=}}
3+
@noescape var fn : () -> Int = { 4 } // expected-error {{attribute can only be applied to types, not declarations}}
44

55
func conflictingAttrs(_ fn: @noescape @escaping () -> Int) {} // expected-error {{@escaping conflicts with @noescape}}
66
// expected-warning@-1{{@noescape is the default and is deprecated}} {{29-39=}}
@@ -327,17 +327,17 @@ enum r19997577Type {
327327
}
328328

329329
// type attribute and decl attribute
330-
func noescapeD(@noescape f: @escaping () -> Bool) {} // expected-error {{@noescape is now an attribute on a parameter type, instead of on the parameter itself}} {{16-25=}} {{29-29=@noescape }}
330+
func noescapeD(@noescape f: @escaping () -> Bool) {} // expected-error {{attribute can only be applied to types, not declarations}}
331331
func noescapeT(f: @noescape () -> Bool) {} // expected-warning{{@noescape is the default and is deprecated}} {{19-29=}}
332-
func noescapeG<T>(@noescape f: () -> T) {} // expected-error{{@noescape is now an attribute on a parameter type, instead of on the parameter itself}}
332+
func noescapeG<T>(@noescape f: () -> T) {} // expected-error{{attribute can only be applied to types, not declarations}}
333333

334-
func autoclosureD(@autoclosure f: () -> Bool) {} // expected-error {{@autoclosure is now an attribute on a parameter type, instead of on the parameter itself}} {{19-31=}} {{35-35=@autoclosure }}
334+
func autoclosureD(@autoclosure f: () -> Bool) {} // expected-error {{attribute can only be applied to types, not declarations}}
335335
func autoclosureT(f: @autoclosure () -> Bool) {} // ok
336-
func autoclosureG<T>(@autoclosure f: () -> T) {} // expected-error{{@autoclosure is now an attribute on a parameter type, instead of on the parameter itself}}
336+
func autoclosureG<T>(@autoclosure f: () -> T) {} // expected-error{{attribute can only be applied to types, not declarations}}
337337

338-
func noescapeD_noescapeT(@noescape f: @noescape () -> Bool) {} // expected-error {{@noescape is now an attribute on a parameter type, instead of on the parameter itself}}
338+
func noescapeD_noescapeT(@noescape f: @noescape () -> Bool) {} // expected-error {{attribute can only be applied to types, not declarations}}
339339
// expected-warning@-1{{@noescape is the default and is deprecated}} {{39-49=}}
340340

341-
func autoclosureD_noescapeT(@autoclosure f: @noescape () -> Bool) {} // expected-error {{@autoclosure is now an attribute on a parameter type, instead of on the parameter itself}} {{29-41=}} {{45-45=@autoclosure }}
341+
func autoclosureD_noescapeT(@autoclosure f: @noescape () -> Bool) {} // expected-error {{attribute can only be applied to types, not declarations}}
342342
// expected-warning@-1{{@noescape is the default and is deprecated}} {{45-55=}}
343343

0 commit comments

Comments
 (0)