Skip to content

Commit c0eab4f

Browse files
committed
Change error message to specify when inout usage is allowed
1 parent d9a03dd commit c0eab4f

File tree

8 files changed

+30
-28
lines changed

8 files changed

+30
-28
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3681,13 +3681,10 @@ ERROR(dynamic_self_default_arg,none,
36813681

36823682
ERROR(attr_only_one_decl_kind,none,
36833683
"%0 may only be used on '%1' declarations", (DeclAttribute,StringRef))
3684+
ERROR(attr_only_valid_on_func_or_init_params,none,
3685+
"'%0' may only be used on function or initializer parameters", (StringRef))
36843686
ERROR(attr_not_on_variadic_parameters,none,
36853687
"'%0' must not be used on variadic parameters", (StringRef))
3686-
ERROR(attr_not_on_subscript_parameters,none,
3687-
"'%0' must not be used on subscript parameters", (StringRef))
3688-
ERROR(attr_not_on_enum_case_parameters,none,
3689-
"'%0' must not be used on enum case parameters", (StringRef))
3690-
36913688
ERROR(attr_not_on_stored_properties,none,
36923689
"'%0' must not be used on stored properties", (DeclAttribute))
36933690

lib/Sema/TypeCheckType.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4294,26 +4294,21 @@ TypeResolver::resolveOwnershipTypeRepr(OwnershipTypeRepr *repr,
42944294
options.hasBase(TypeResolverContext::SubscriptDecl) ||
42954295
options.hasBase(TypeResolverContext::EnumElementDecl)) {
42964296

4297-
auto diagID = diag::attr_only_on_parameters;
4298-
bool removeRepr = true;
4299-
if (options.hasBase(TypeResolverContext::SubscriptDecl)) {
4300-
diagID = diag::attr_not_on_subscript_parameters;
4297+
decltype(diag::attr_only_on_parameters) diagID;
4298+
if (options.hasBase(TypeResolverContext::SubscriptDecl) ||
4299+
options.hasBase(TypeResolverContext::EnumElementDecl)) {
4300+
diagID = diag::attr_only_valid_on_func_or_init_params;
43014301
} else if (options.is(TypeResolverContext::VariadicFunctionInput)) {
43024302
diagID = diag::attr_not_on_variadic_parameters;
4303-
} else if (options.hasBase(TypeResolverContext::EnumElementDecl)) {
4304-
diagID = diag::attr_not_on_enum_case_parameters;
43054303
} else {
43064304
diagID = diag::attr_only_on_parameters;
4307-
removeRepr = false;
43084305
}
4306+
43094307
StringRef name;
43104308
if (ownershipRepr) {
43114309
name = ownershipRepr->getSpecifierSpelling();
43124310
}
4313-
auto diag = diagnoseInvalid(repr, repr->getSpecifierLoc(), diagID, name);
4314-
if (removeRepr)
4315-
diag.fixItRemove(repr->getLoc());
4316-
4311+
diagnoseInvalid(repr, repr->getSpecifierLoc(), diagID, name);
43174312
return ErrorType::get(getASTContext());
43184313
}
43194314

test/Concurrency/actor_inout_isolation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ actor TestActor {
3636
var value2: Int = 1 // expected-note 4{{property declared here}}
3737
var points: [Point] = [] // expected-note {{property declared here}}
3838

39-
subscript(x : inout Int) -> Int { // expected-error {{'inout' must not be used on subscript parameters}}
39+
subscript(x : inout Int) -> Int { // expected-error {{'inout' may only be used on function or initializer parameters}}
4040
x += 1
4141
return x
4242
}

test/Sema/immutability.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,8 +646,8 @@ func f(a : FooClass, b : LetStructMembers) {
646646
class MutableSubscripts {
647647
var x : Int = 0
648648

649-
subscript(x: inout Int) -> () { x += 1 } // expected-error {{'inout' must not be used on subscript parameters}}
650-
subscript<T>(x: inout T) -> () { // expected-error {{'inout' must not be used on subscript parameters}}
649+
subscript(x: inout Int) -> () { x += 1 } // expected-error {{'inout' may only be used on function or initializer parameters}}
650+
subscript<T>(x: inout T) -> () { // expected-error {{'inout' may only be used on function or initializer parameters}}
651651
fatalError()
652652
}
653653

test/attr/attr_inout.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ func h(_ : inout Int) -> (inout Int) -> (inout Int) -> Int { }
77
func ff(x: (inout Int, inout Float)) { } // expected-error 2{{'inout' may only be used on parameters}}
88

99
enum inout_carrier {
10-
case carry(inout Int) // expected-error {{'inout' may only be used on parameters}}
10+
case carry(inout Int) // expected-error {{'inout' may only be used on function or initializer parameters}}
1111
}
1212

1313
func deprecated(inout x: Int) {} // expected-error {{'inout' before a parameter name is not allowed, place it before the parameter type instead}}

test/decl/enum/enumtest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@ func useSynthesizedMember() {
324324

325325
// Non-materializable argument type
326326
enum Lens<T> {
327-
case foo(inout T) // expected-error {{'inout' may only be used on parameters}}
328-
case bar(inout T, Int) // expected-error {{'inout' may only be used on parameters}}
327+
case foo(inout T) // expected-error {{'inout' may only be used on function or initializer parameters}}
328+
case bar(inout T, Int) // expected-error {{'inout' may only be used on function or initializer parameters}}
329329

330330
case baz((inout T) -> ()) // ok
331331
case quux((inout T, inout T) -> ()) // ok

test/decl/subscript/subscripting.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,15 +464,15 @@ do {
464464

465465
struct InOutSubscripts {
466466
subscript(x1: inout Int) -> Int { return 0 }
467-
// expected-error@-1 {{'inout' must not be used on subscript parameters}}
467+
// expected-error@-1 {{'inout' may only be used on function or initializer parameters}}
468468

469469
subscript(x2: inout Int, y2: inout Int) -> Int { return 0 }
470-
// expected-error@-1 2{{'inout' must not be used on subscript parameters}}
470+
// expected-error@-1 2{{'inout' may only be used on function or initializer parameters}}
471471

472472
subscript(x3: (inout Int) -> ()) -> Int { return 0 } // ok
473473
subscript(x4: (inout Int, inout Int) -> ()) -> Int { return 0 } // ok
474474

475475
subscript(inout x5: Int) -> Int { return 0 }
476476
// expected-error@-1 {{'inout' before a parameter name is not allowed, place it before the parameter type instead}}
477-
// expected-error@-2 {{'inout' must not be used on subscript parameters}}
477+
// expected-error@-2 {{'inout' may only be used on function or initializer parameters}}
478478
}

test/type/types.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,17 @@ func rdar94888357() {
208208

209209
// https://github.com/apple/swift/issues/68417
210210
enum E {
211-
subscript(x: inout Int) -> Bool { true } // expected-error {{'inout' must not be used on subscript parameters}} {{16-22=}}
212-
case c(x: inout Int) // expected-error {{'inout' must not be used on enum case parameters}} {{13-19=}}
213-
func d(x: inout Int ...) // expected-error {{'inout' must not be used on variadic parameters}} {{13-19=}}
211+
subscript(x: inout Int) -> Bool { true } // expected-error {{'inout' may only be used on function or initializer parameters}}
212+
case c(x: inout Int) // expected-error {{'inout' may only be used on function or initializer parameters}}
213+
func d(x: inout Int ...) {} // expected-error {{'inout' must not be used on variadic parameters}}
214+
func e(x: inout Int) {} // ok
215+
init(x: inout Int) {} // ok
214216
}
217+
218+
do {
219+
struct Test {
220+
init(_: inout Int...) {} // expected-error {{'inout' must not be used on variadic parameters}}
221+
func test(_: inout String...) {} // expected-error {{'inout' must not be used on variadic parameters}}
222+
subscript(_: inout Double...) -> Bool { true } // expected-error {{'inout' may only be used on function or initializer parameters}}
223+
}
224+
}

0 commit comments

Comments
 (0)