Skip to content

Commit 8f2fbdc

Browse files
committed
Make function parameters and refutable patterns always immutable
All refutable patterns and function parameters marked with 'var' is now an error. - Using explicit 'let' keyword on function parameters causes a warning. - Don't suggest making function parameters mutable - Remove uses in the standard library - Update tests rdar://problem/23378003
1 parent 22ba61b commit 8f2fbdc

File tree

99 files changed

+729
-579
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+729
-579
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,10 @@ ERROR(untyped_pattern_ellipsis,pattern_parsing,none,
641641
"'...' cannot be applied to a subpattern which is not explicitly typed", ())
642642
ERROR(non_func_decl_pattern_init,pattern_parsing,none,
643643
"default argument is only permitted for a non-curried function parameter",())
644-
WARNING(var_not_allowed_in_pattern,pattern_parsing, none,
645-
"Use of '%select{var|let}0' binding here is deprecated and will be removed in a future version of Swift", (unsigned))
644+
ERROR(var_not_allowed_in_pattern,pattern_parsing, none,
645+
"Use of 'var' binding here is not allowed", ())
646+
WARNING(let_on_param_is_redundant,pattern_parsing, none,
647+
"'let' keyword is unnecessary; function parameters are immutable by default", (unsigned))
646648
ERROR(var_pattern_in_var,pattern_parsing,none,
647649
"'%select{var|let}0' cannot appear nested inside another 'var' or "
648650
"'let' pattern", (unsigned))

lib/AST/Decl.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3268,26 +3268,6 @@ void VarDecl::emitLetToVarNoteIfSimple(DeclContext *UseDC) const {
32683268
.fixItReplace(PBD->getLoc(), "var");
32693269
return;
32703270
}
3271-
3272-
// If this is an argument pattern, suggest adding 'var' to the pattern.
3273-
if (auto *PD = dyn_cast<ParamDecl>(this)) {
3274-
if (auto *P = PD->getParamParentPattern()) {
3275-
if (P->getStartLoc().isValid() && !PD->isImplicit()) {
3276-
// The pattern is: (pattern_let (pattern_typed (pattern_named)))
3277-
// where the outside let pattern may be missing. If present, this is a
3278-
// change, otherwise this is an addition of "var"
3279-
auto &d = getASTContext().Diags;
3280-
if (auto *VP = dyn_cast<VarPattern>(P)) {
3281-
d.diagnose(VP->getLoc(), diag::change_let_to_var_param)
3282-
.fixItReplace(VP->getLoc(), "var");
3283-
} else {
3284-
d.diagnose(P->getStartLoc(), diag::mark_param_var)
3285-
.fixItInsert(P->getStartLoc(), "var ");
3286-
}
3287-
return;
3288-
}
3289-
}
3290-
}
32913271
}
32923272

32933273

lib/Parse/ParsePattern.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,15 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
179179
param.LetVarInOutLoc = consumeToken();
180180
param.SpecifierKind = ParsedParameter::InOut;
181181
} else if (Tok.is(tok::kw_let)) {
182-
diagnose(Tok.getLoc(), diag::var_not_allowed_in_pattern,
182+
diagnose(Tok.getLoc(), diag::let_on_param_is_redundant,
183183
Tok.is(tok::kw_let)).fixItRemove(Tok.getLoc());
184184
param.LetVarInOutLoc = consumeToken();
185185
param.SpecifierKind = ParsedParameter::Let;
186186
} else if (Tok.is(tok::kw_var)) {
187-
diagnose(Tok.getLoc(), diag::var_not_allowed_in_pattern,
188-
Tok.is(tok::kw_let)).fixItRemove(Tok.getLoc());
187+
diagnose(Tok.getLoc(), diag::var_not_allowed_in_pattern)
188+
.fixItRemove(Tok.getLoc());
189189
param.LetVarInOutLoc = consumeToken();
190-
param.SpecifierKind = ParsedParameter::Var;
190+
param.SpecifierKind = ParsedParameter::Let;
191191
}
192192

193193
// Redundant specifiers are fairly common, recognize, reject, and recover
@@ -855,8 +855,8 @@ ParserResult<Pattern> Parser::parsePattern() {
855855
} else {
856856
// In an always immutable context, `var` is not allowed.
857857
if (alwaysImmutable)
858-
diagnose(varLoc, diag::var_not_allowed_in_pattern, isLetKeyword)
859-
.fixItRemove(varLoc);
858+
diagnose(varLoc, diag::var_not_allowed_in_pattern)
859+
.fixItRemove(varLoc);
860860
}
861861

862862
// In our recursive parse, remember that we're in a var/let pattern.
@@ -1067,7 +1067,7 @@ ParserResult<Pattern> Parser::parseMatchingPatternAsLetOrVar(bool isLet,
10671067
diagnose(varLoc, diag::let_pattern_in_immutable_context);
10681068

10691069
if (!isLet && InVarOrLetPattern == IVOLP_AlwaysImmutable)
1070-
diagnose(varLoc, diag::var_not_allowed_in_pattern, isLet)
1070+
diagnose(varLoc, diag::var_not_allowed_in_pattern)
10711071
.fixItReplace(varLoc, "let");
10721072

10731073
// In our recursive parse, remember that we're in a var/let pattern.

stdlib/private/StdlibUnittest/CheckCollectionType.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ extension TestSuite {
287287
Collection.SubSequence.SubSequence == Collection.SubSequence,
288288
CollectionWithEquatableElement.Generator.Element : Equatable
289289
>(
290-
var testNamePrefix: String = "",
290+
testNamePrefix: String = "",
291291
makeCollection: ([Collection.Generator.Element]) -> Collection,
292292
wrapValue: (OpaqueValue<Int>) -> Collection.Generator.Element,
293293
extractValue: (Collection.Generator.Element) -> OpaqueValue<Int>,
@@ -302,6 +302,8 @@ extension TestSuite {
302302
outOfBoundsSubscriptOffset: Int = 1
303303
) {
304304

305+
var testNamePrefix = testNamePrefix
306+
305307
if checksAdded.value.contains(__FUNCTION__) {
306308
return
307309
}
@@ -786,7 +788,7 @@ self.test("\(testNamePrefix).removeFirst(n: Int)/slice/removeTooMany/semantics")
786788
CollectionWithEquatableElement.Index : BidirectionalIndexType,
787789
CollectionWithEquatableElement.Generator.Element : Equatable
788790
>(
789-
var testNamePrefix: String = "",
791+
testNamePrefix: String = "",
790792
makeCollection: ([Collection.Generator.Element]) -> Collection,
791793
wrapValue: (OpaqueValue<Int>) -> Collection.Generator.Element,
792794
extractValue: (Collection.Generator.Element) -> OpaqueValue<Int>,
@@ -800,6 +802,9 @@ self.test("\(testNamePrefix).removeFirst(n: Int)/slice/removeTooMany/semantics")
800802
outOfBoundsIndexOffset: Int = 1,
801803
outOfBoundsSubscriptOffset: Int = 1
802804
) {
805+
806+
var testNamePrefix = testNamePrefix
807+
803808
if checksAdded.value.contains(__FUNCTION__) {
804809
return
805810
}
@@ -1088,7 +1093,7 @@ self.test("\(testNamePrefix).suffix/semantics") {
10881093
CollectionWithEquatableElement.Index : RandomAccessIndexType,
10891094
CollectionWithEquatableElement.Generator.Element : Equatable
10901095
>(
1091-
var testNamePrefix: String = "",
1096+
testNamePrefix: String = "",
10921097
makeCollection: ([Collection.Generator.Element]) -> Collection,
10931098
wrapValue: (OpaqueValue<Int>) -> Collection.Generator.Element,
10941099
extractValue: (Collection.Generator.Element) -> OpaqueValue<Int>,
@@ -1103,6 +1108,8 @@ self.test("\(testNamePrefix).suffix/semantics") {
11031108
outOfBoundsSubscriptOffset: Int = 1
11041109
) {
11051110

1111+
var testNamePrefix = testNamePrefix
1112+
11061113
if checksAdded.value.contains(__FUNCTION__) {
11071114
return
11081115
}

stdlib/private/StdlibUnittest/CheckMutableCollectionType.swift.gyb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ extension TestSuite {
7979
CollectionWithEquatableElement.Generator.Element : Equatable,
8080
CollectionWithComparableElement.Generator.Element : Comparable
8181
>(
82-
var testNamePrefix: String = "",
82+
testNamePrefix: String = "",
8383
makeCollection: ([Collection.Generator.Element]) -> Collection,
8484
wrapValue: (OpaqueValue<Int>) -> Collection.Generator.Element,
8585
extractValue: (Collection.Generator.Element) -> OpaqueValue<Int>,
@@ -99,6 +99,8 @@ extension TestSuite {
9999
withUnsafeMutableBufferPointerIsSupported: Bool
100100
) {
101101

102+
var testNamePrefix = testNamePrefix
103+
102104
if checksAdded.value.contains(__FUNCTION__) {
103105
return
104106
}
@@ -383,7 +385,7 @@ self.test("\(testNamePrefix).sort/${'Predicate' if predicate else 'WhereElementI
383385
CollectionWithComparableElement.Index : BidirectionalIndexType,
384386
CollectionWithComparableElement.Generator.Element : Comparable
385387
>(
386-
var testNamePrefix: String = "",
388+
testNamePrefix: String = "",
387389
makeCollection: ([Collection.Generator.Element]) -> Collection,
388390
wrapValue: (OpaqueValue<Int>) -> Collection.Generator.Element,
389391
extractValue: (Collection.Generator.Element) -> OpaqueValue<Int>,
@@ -403,6 +405,8 @@ self.test("\(testNamePrefix).sort/${'Predicate' if predicate else 'WhereElementI
403405
withUnsafeMutableBufferPointerIsSupported: Bool
404406
) {
405407

408+
var testNamePrefix = testNamePrefix
409+
406410
if checksAdded.value.contains(__FUNCTION__) {
407411
return
408412
}
@@ -500,7 +504,7 @@ if resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior != .None {
500504
CollectionWithComparableElement.Index : RandomAccessIndexType,
501505
CollectionWithComparableElement.Generator.Element : Comparable
502506
>(
503-
var testNamePrefix: String = "",
507+
testNamePrefix: String = "",
504508
makeCollection: ([Collection.Generator.Element]) -> Collection,
505509
wrapValue: (OpaqueValue<Int>) -> Collection.Generator.Element,
506510
extractValue: (Collection.Generator.Element) -> OpaqueValue<Int>,
@@ -520,6 +524,8 @@ if resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior != .None {
520524
withUnsafeMutableBufferPointerIsSupported: Bool
521525
) {
522526

527+
var testNamePrefix = testNamePrefix
528+
523529
if checksAdded.value.contains(__FUNCTION__) {
524530
return
525531
}

stdlib/private/StdlibUnittest/CheckRangeReplaceableCollectionType.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ extension TestSuite {
348348
Collection.SubSequence.SubSequence == Collection.SubSequence,
349349
CollectionWithEquatableElement.Generator.Element : Equatable
350350
>(
351-
var testNamePrefix: String = "",
351+
testNamePrefix: String = "",
352352
makeCollection: ([Collection.Generator.Element]) -> Collection,
353353
wrapValue: (OpaqueValue<Int>) -> Collection.Generator.Element,
354354
extractValue: (Collection.Generator.Element) -> OpaqueValue<Int>,
@@ -362,6 +362,8 @@ extension TestSuite {
362362
outOfBoundsIndexOffset: Int = 1
363363
) {
364364

365+
var testNamePrefix = testNamePrefix
366+
365367
if checksAdded.value.contains(__FUNCTION__) {
366368
return
367369
}
@@ -1108,7 +1110,7 @@ self.test("\(testNamePrefix).OperatorPlus") {
11081110
CollectionWithEquatableElement.Index : BidirectionalIndexType,
11091111
CollectionWithEquatableElement.Generator.Element : Equatable
11101112
>(
1111-
var testNamePrefix: String = "",
1113+
testNamePrefix: String = "",
11121114
makeCollection: ([Collection.Generator.Element]) -> Collection,
11131115
wrapValue: (OpaqueValue<Int>) -> Collection.Generator.Element,
11141116
extractValue: (Collection.Generator.Element) -> OpaqueValue<Int>,
@@ -1122,6 +1124,8 @@ self.test("\(testNamePrefix).OperatorPlus") {
11221124
outOfBoundsIndexOffset: Int = 1
11231125
) {
11241126

1127+
var testNamePrefix = testNamePrefix
1128+
11251129
if checksAdded.value.contains(__FUNCTION__) {
11261130
return
11271131
}
@@ -1233,7 +1237,7 @@ self.test("\(testNamePrefix).removeLast(n: Int)/whereIndexIsBidirectional/remove
12331237
CollectionWithEquatableElement.Index : RandomAccessIndexType,
12341238
CollectionWithEquatableElement.Generator.Element : Equatable
12351239
>(
1236-
var testNamePrefix: String = "",
1240+
testNamePrefix: String = "",
12371241
makeCollection: ([Collection.Generator.Element]) -> Collection,
12381242
wrapValue: (OpaqueValue<Int>) -> Collection.Generator.Element,
12391243
extractValue: (Collection.Generator.Element) -> OpaqueValue<Int>,
@@ -1247,6 +1251,8 @@ self.test("\(testNamePrefix).removeLast(n: Int)/whereIndexIsBidirectional/remove
12471251
outOfBoundsIndexOffset: Int = 1
12481252
) {
12491253

1254+
var testNamePrefix = testNamePrefix
1255+
12501256
if checksAdded.value.contains(__FUNCTION__) {
12511257
return
12521258
}

stdlib/private/StdlibUnittest/CheckRangeReplaceableSliceType.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extension TestSuite {
2020
Collection.SubSequence == Collection,
2121
CollectionWithEquatableElement.Generator.Element : Equatable
2222
>(
23-
var testNamePrefix: String = "",
23+
testNamePrefix: String = "",
2424
makeCollection: ([Collection.Generator.Element]) -> Collection,
2525
wrapValue: (OpaqueValue<Int>) -> Collection.Generator.Element,
2626
extractValue: (Collection.Generator.Element) -> OpaqueValue<Int>,
@@ -34,6 +34,8 @@ extension TestSuite {
3434
outOfBoundsIndexOffset: Int = 1
3535
) {
3636

37+
var testNamePrefix = testNamePrefix
38+
3739
// Don't run the same tests twice.
3840
if checksAdded.value.contains(__FUNCTION__) {
3941
return
@@ -150,7 +152,7 @@ extension TestSuite {
150152
CollectionWithEquatableElement.SubSequence == CollectionWithEquatableElement,
151153
CollectionWithEquatableElement.Generator.Element : Equatable
152154
>(
153-
var testNamePrefix: String = "",
155+
testNamePrefix: String = "",
154156
makeCollection: ([Collection.Generator.Element]) -> Collection,
155157
wrapValue: (OpaqueValue<Int>) -> Collection.Generator.Element,
156158
extractValue: (Collection.Generator.Element) -> OpaqueValue<Int>,
@@ -164,6 +166,8 @@ extension TestSuite {
164166
outOfBoundsIndexOffset: Int = 1
165167
) {
166168

169+
var testNamePrefix = testNamePrefix
170+
167171
// Don't run the same tests twice.
168172
if checksAdded.value.contains(__FUNCTION__) {
169173
return
@@ -294,7 +298,7 @@ extension TestSuite {
294298
CollectionWithEquatableElement.SubSequence == CollectionWithEquatableElement,
295299
CollectionWithEquatableElement.Generator.Element : Equatable
296300
>(
297-
var testNamePrefix: String = "",
301+
testNamePrefix: String = "",
298302
makeCollection: ([Collection.Generator.Element]) -> Collection,
299303
wrapValue: (OpaqueValue<Int>) -> Collection.Generator.Element,
300304
extractValue: (Collection.Generator.Element) -> OpaqueValue<Int>,
@@ -308,6 +312,8 @@ extension TestSuite {
308312
outOfBoundsIndexOffset: Int = 1
309313
) {
310314

315+
var testNamePrefix = testNamePrefix
316+
311317
// Don't run the same tests twice.
312318
if checksAdded.value.contains(__FUNCTION__) {
313319
return

stdlib/private/StdlibUnittest/CheckSequenceType.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,7 @@ extension TestSuite {
14391439
Sequence.SubSequence.Generator.Element == Sequence.Generator.Element,
14401440
Sequence.SubSequence.SubSequence == Sequence.SubSequence
14411441
>(
1442-
var testNamePrefix: String = "",
1442+
testNamePrefix: String = "",
14431443
makeSequence: ([Sequence.Generator.Element]) -> Sequence,
14441444
wrapValue: (OpaqueValue<Int>) -> Sequence.Generator.Element,
14451445
extractValue: (Sequence.Generator.Element) -> OpaqueValue<Int>,
@@ -1452,6 +1452,8 @@ extension TestSuite {
14521452
resiliencyChecks: CollectionMisuseResiliencyChecks = .all
14531453
) {
14541454

1455+
var testNamePrefix = testNamePrefix
1456+
14551457
if checksAdded.value.contains(__FUNCTION__) {
14561458
return
14571459
}

stdlib/private/StdlibUnittest/OpaqueIdentityFunctions.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ func _blackHolePtr<T>(x: UnsafePointer<T>) {
2727
_stdlib_getPointer(COpaquePointer(x))
2828
}
2929

30-
public func _blackHole<T>(var x: T) {
30+
public func _blackHole<T>(x: T) {
31+
var x = x
3132
_blackHolePtr(&x)
3233
}
3334

stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2008,7 +2008,8 @@ public func check${traversal}Collection<
20082008
x + offset0 >= 0 && x + offset1 <= count
20092009
}
20102010

2011-
func nextN(n: C.Index.Distance, var _ i: C.Index) -> C.Index {
2011+
func nextN(n: C.Index.Distance, _ i: C.Index) -> C.Index {
2012+
var i = i
20122013
if n < 0 {
20132014
for _ in 0 ..< -(n.toIntMax()) {
20142015
--i

stdlib/private/SwiftPrivateSerialization/MsgPackSerialization.swift.gyb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
public struct MsgPackSerializer {
1818
public static func serialize<
1919
T : SerializableFixedDictionaryType
20-
>(var value: T) -> [UInt8] {
20+
>(value: T) -> [UInt8] {
21+
var value = value
2122
// FIXME(performance): we could serialize directly into a byte stream
2223
// without creating an intermediate data structure if our MsgPack
2324
// serializer could accept arrays and dictionaries of unknown length and
@@ -76,7 +77,8 @@ internal func _toMsgPackVariant<
7677
}
7778
% end
7879
return .Array(MsgPackVariantArray(value.map {
79-
(var element) -> MsgPackVariant in
80+
element -> MsgPackVariant in
81+
var element = element
8082
var serializerImpl = _MsgPackSerializerImpl()
8183
element.serializeDeserialize(&serializerImpl)
8284
return serializerImpl._value!

stdlib/public/SDK/AssetsLibrary/AssetsLibrary.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
//===----------------------------------------------------------------------===//
1818
extension ALAssetsLibrary {
1919
@nonobjc
20-
public func enumerateGroupsWithTypes(var types: UInt32,
20+
public func enumerateGroupsWithTypes(types: UInt32,
2121
usingBlock enumerationBlock: ALAssetsLibraryGroupsEnumerationResultsBlock!,
2222
failureBlock: ALAssetsLibraryAccessFailureBlock!) {
23+
var types = types
2324
if (types == ALAssetsGroupAll) {
2425
types = ALAssetsGroupLibrary | ALAssetsGroupAlbum | ALAssetsGroupEvent |
2526
ALAssetsGroupFaces | ALAssetsGroupSavedPhotos |

stdlib/public/core/Arrays.swift.gyb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,9 @@ public func _allocateUninitializedArray<Element>(_count: Builtin.Word)
484484
@warn_unused_result
485485
@_semantics("array.dealloc_uninitialized")
486486
public func _deallocateUninitialized${Self}<Element>(
487-
var array: ${Self}<Element>
487+
array: ${Self}<Element>
488488
) {
489+
var array = array
489490
array._deallocateUninitialized()
490491
}
491492
%end

stdlib/public/core/CollectionAlgorithms.swift.gyb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ ${partitionDocComment}
129129
${orderingRequirementForPredicate}
130130
public mutating func partition(
131131
range: Range<Index>,
132-
var isOrderedBefore: (${GElement}, ${GElement}) -> Bool
132+
isOrderedBefore: (${GElement}, ${GElement}) -> Bool
133133
) -> Index {
134+
var isOrderedBefore = isOrderedBefore
134135

135136
% else:
136137

0 commit comments

Comments
 (0)