Skip to content

Commit 9f9945f

Browse files
authored
Merge pull request swiftlang#9373 from devincoughlin/static-enforcement-error-in-swift-4-mode
2 parents 7925886 + 0777ccc commit 9f9945f

File tree

5 files changed

+78
-30
lines changed

5 files changed

+78
-30
lines changed

include/swift/AST/DiagnosticsSIL.def

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,22 @@ ERROR(inout_argument_alias,none,
8484
NOTE(previous_inout_alias,none,
8585
"previous aliasing argument", ())
8686

87-
// This is temporarily a warning during staging to make it easier to evaluate.
88-
// The intent is to change it to an error before turning it on by default.
89-
WARNING(exclusivity_access_required,none,
87+
WARNING(exclusivity_access_required_swift3,none,
9088
"simultaneous accesses to %0 %1; "
9189
"%select{initialization|read|modification|deinitialization}2 requires "
9290
"exclusive access", (DescriptiveDeclKind, Identifier, unsigned))
9391

94-
WARNING(exclusivity_access_required_unknown_decl,none,
92+
ERROR(exclusivity_access_required,none,
93+
"simultaneous accesses to %0 %1; "
94+
"%select{initialization|read|modification|deinitialization}2 requires "
95+
"exclusive access", (DescriptiveDeclKind, Identifier, unsigned))
96+
97+
ERROR(exclusivity_access_required_unknown_decl,none,
98+
"simultaneous accesses; "
99+
"%select{initialization|read|modification|deinitialization}0 requires "
100+
"exclusive access", (unsigned))
101+
102+
WARNING(exclusivity_access_required_unknown_decl_swift3,none,
95103
"simultaneous accesses; "
96104
"%select{initialization|read|modification|deinitialization}0 requires "
97105
"exclusive access", (unsigned))

lib/SILOptimizer/Mandatory/DiagnoseStaticExclusivity.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ class AccessInfo {
230230

231231
/// Indicates whether a 'begin_access' requires exclusive access
232232
/// or allows shared access. This needs to be kept in sync with
233-
/// diag::exclusivity_access_required and diag::exclusivity_conflicting_access.
233+
/// diag::exclusivity_access_required, exclusivity_access_required_swift3,
234+
/// and diag::exclusivity_conflicting_access.
234235
enum class ExclusiveOrShared_t : unsigned {
235236
ExclusiveAccess = 0,
236237
SharedAccess = 1
@@ -382,15 +383,22 @@ static void diagnoseExclusivityViolation(const AccessedStorage &Storage,
382383
AccessForMainDiagnostic->getLoc().getSourceRange();
383384

384385
if (const ValueDecl *VD = Storage.getStorageDecl()) {
386+
// We have a declaration, so mention the identifier in the diagnostic.
387+
auto DiagnosticID = (Ctx.LangOpts.isSwiftVersion3() ?
388+
diag::exclusivity_access_required_swift3 :
389+
diag::exclusivity_access_required);
385390
diagnose(Ctx, AccessForMainDiagnostic->getLoc().getSourceLoc(),
386-
diag::exclusivity_access_required,
391+
DiagnosticID,
387392
VD->getDescriptiveKind(),
388393
VD->getName(),
389394
static_cast<unsigned>(AccessForMainDiagnostic->getAccessKind()))
390395
.highlight(rangeForMain);
391396
} else {
397+
auto DiagnosticID = (Ctx.LangOpts.isSwiftVersion3() ?
398+
diag::exclusivity_access_required_unknown_decl_swift3 :
399+
diag::exclusivity_access_required_unknown_decl);
392400
diagnose(Ctx, AccessForMainDiagnostic->getLoc().getSourceLoc(),
393-
diag::exclusivity_access_required_unknown_decl,
401+
DiagnosticID,
394402
static_cast<unsigned>(AccessForMainDiagnostic->getAccessKind()))
395403
.highlight(rangeForMain);
396404
}

test/SILOptimizer/exclusivity_static_diagnostics.sil

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ bb0(%0 : $Int):
3838
%3 = project_box %2 : ${ var Int }, 0
3939
store %0 to [trivial] %3 : $*Int
4040
%4 = function_ref @takesTwoInouts : $@convention(thin) (@inout Int, @inout Int) -> ()
41-
%5 = begin_access [modify] [unknown] %3 : $*Int // expected-warning {{simultaneous accesses; modification requires exclusive access}}
41+
%5 = begin_access [modify] [unknown] %3 : $*Int // expected-error {{simultaneous accesses; modification requires exclusive access}}
4242
%6 = begin_access [modify] [unknown] %3 : $*Int // expected-note {{conflicting access is here}}
4343
%7 = apply %4(%5, %6) : $@convention(thin) (@inout Int, @inout Int) -> ()
4444
end_access %6 : $*Int
@@ -55,7 +55,7 @@ bb0(%0 : $Int):
5555
%3 = project_box %2 : ${ var Int }, 0
5656
store %0 to [trivial] %3 : $*Int
5757
%4 = function_ref @takesTwoInouts : $@convention(thin) (@inout Int, @inout Int) -> ()
58-
%5 = begin_access [modify] [unknown] %3 : $*Int // expected-warning {{simultaneous accesses; modification requires exclusive access}}
58+
%5 = begin_access [modify] [unknown] %3 : $*Int // expected-error {{simultaneous accesses; modification requires exclusive access}}
5959
%6 = begin_access [modify] [unknown] %5 : $*Int
6060
%7 = begin_access [modify] [unknown] %3 : $*Int // expected-note {{conflicting access is here}}
6161
%8 = apply %4(%5, %6) : $@convention(thin) (@inout Int, @inout Int) -> ()
@@ -147,7 +147,7 @@ bb0(%0 : $Int, %1 : $Builtin.Int1):
147147
br bb1
148148
bb1:
149149
// Make sure we don't diagnose twice.
150-
%4 = begin_access [modify] [unknown] %3 : $*Int // expected-warning {{simultaneous accesses; modification requires exclusive access}}
150+
%4 = begin_access [modify] [unknown] %3 : $*Int // expected-error {{simultaneous accesses; modification requires exclusive access}}
151151
%5 = begin_access [modify] [unknown] %3 : $*Int // expected-note {{conflicting access is here}}
152152
end_access %5: $*Int
153153
end_access %4: $*Int
@@ -199,7 +199,7 @@ bb0(%0 : $Int):
199199
%2 = project_box %1 : ${ var Int }, 0
200200
store %0 to [trivial] %2 : $*Int
201201
%4 = begin_access [read] [unknown] %2 : $*Int // expected-note {{conflicting access is here}}
202-
%5 = begin_access [modify] [unknown] %2 : $*Int // expected-warning {{simultaneous accesses; modification requires exclusive access}}
202+
%5 = begin_access [modify] [unknown] %2 : $*Int // expected-error {{simultaneous accesses; modification requires exclusive access}}
203203
end_access %5 : $*Int
204204
end_access %4: $*Int
205205
destroy_value %1 : ${ var Int }
@@ -213,7 +213,7 @@ bb0(%0 : $Int):
213213
%1 = alloc_box ${ var Int }
214214
%2 = project_box %1 : ${ var Int }, 0
215215
store %0 to [trivial] %2 : $*Int
216-
%4 = begin_access [modify] [unknown] %2 : $*Int // expected-warning {{simultaneous accesses; modification requires exclusive access}}
216+
%4 = begin_access [modify] [unknown] %2 : $*Int // expected-error {{simultaneous accesses; modification requires exclusive access}}
217217
%5 = begin_access [read] [unknown] %2 : $*Int // expected-note {{conflicting access is here}}
218218
end_access %5 : $*Int
219219
end_access %4: $*Int
@@ -232,7 +232,7 @@ sil hidden @classStoredProperty : $@convention(thin) (ClassWithStoredProperty) -
232232
bb0(%0 : $ClassWithStoredProperty):
233233
%1 = ref_element_addr %0 : $ClassWithStoredProperty, #ClassWithStoredProperty.f
234234

235-
// expected-warning@+1{{simultaneous accesses to var 'f'; modification requires exclusive access}}
235+
// expected-error@+1{{simultaneous accesses to var 'f'; modification requires exclusive access}}
236236
%2 = begin_access [modify] [dynamic] %1 : $*Int
237237
%3 = ref_element_addr %0 : $ClassWithStoredProperty, #ClassWithStoredProperty.f
238238

@@ -252,7 +252,7 @@ bb0(%0 : $ClassWithStoredProperty):
252252
%2 = begin_borrow %0 : $ClassWithStoredProperty
253253
%3 = ref_element_addr %1 : $ClassWithStoredProperty, #ClassWithStoredProperty.f
254254

255-
// expected-warning@+1{{simultaneous accesses to var 'f'; modification requires exclusive access}}
255+
// expected-error@+1{{simultaneous accesses to var 'f'; modification requires exclusive access}}
256256
%4 = begin_access [modify] [dynamic] %3 : $*Int
257257
%5 = ref_element_addr %2 : $ClassWithStoredProperty, #ClassWithStoredProperty.f
258258

@@ -279,7 +279,7 @@ bb0(%0 : $Int):
279279
store %0 to [trivial] %3 : $*Int
280280
%4 = copy_value %2 : ${ var Int }
281281
%5 = project_box %4 : ${ var Int }, 0
282-
%6 = begin_access [modify] [unknown] %3 : $*Int // expected-warning {{simultaneous accesses; modification requires exclusive access}}
282+
%6 = begin_access [modify] [unknown] %3 : $*Int // expected-error {{simultaneous accesses; modification requires exclusive access}}
283283
%7 = begin_access [modify] [unknown] %5 : $*Int // expected-note {{conflicting access is here}}
284284
end_access %7 : $*Int
285285
end_access %6: $*Int
@@ -297,7 +297,7 @@ bb0(%0 : $Int):
297297
%3 = project_box %2 : ${ var Int }, 0
298298
store %0 to [trivial] %3 : $*Int
299299
%4 = project_box %2 : ${ var Int }, 0
300-
%5 = begin_access [modify] [unknown] %3 : $*Int // expected-warning {{simultaneous accesses; modification requires exclusive access}}
300+
%5 = begin_access [modify] [unknown] %3 : $*Int // expected-error {{simultaneous accesses; modification requires exclusive access}}
301301
%6 = begin_access [modify] [unknown] %4 : $*Int // expected-note {{conflicting access is here}}
302302
end_access %6 : $*Int
303303
end_access %5: $*Int
@@ -315,7 +315,7 @@ sil hidden @modifySameGlobal : $@convention(thin) (Int) -> () {
315315
bb0(%0 : $Int):
316316
%1 = global_addr @global1 :$*Int
317317
%2 = global_addr @global1 :$*Int
318-
%3 = begin_access [modify] [unknown] %1 : $*Int // expected-warning {{simultaneous accesses; modification requires exclusive access}}
318+
%3 = begin_access [modify] [unknown] %1 : $*Int // expected-error {{simultaneous accesses; modification requires exclusive access}}
319319
%4 = begin_access [modify] [unknown] %2 : $*Int // expected-note {{conflicting access is here}}
320320
end_access %4 : $*Int
321321
end_access %3: $*Int
@@ -348,7 +348,7 @@ bb0(%0 : $Int):
348348
store %0 to [trivial] %3 : $*Int
349349
%4 = function_ref @takesTwoInouts : $@convention(thin) (@inout Int, @inout Int) -> ()
350350
%5 = begin_access [read] [unknown] %3 : $*Int // expected-note {{conflicting access is here}}
351-
%6 = begin_access [modify] [unknown] %3 : $*Int // expected-warning {{simultaneous accesses; modification requires exclusive access}}
351+
%6 = begin_access [modify] [unknown] %3 : $*Int // expected-error {{simultaneous accesses; modification requires exclusive access}}
352352
%7 = begin_access [read] [unknown] %3 : $*Int // no-error
353353
%8 = apply %4(%5, %6) : $@convention(thin) (@inout Int, @inout Int) -> ()
354354
end_access %7 : $*Int
@@ -368,7 +368,7 @@ bb0(%0 : $Int):
368368
%3 = project_box %2 : ${ var Int }, 0
369369
store %0 to [trivial] %3 : $*Int
370370
%4 = function_ref @takesTwoInouts : $@convention(thin) (@inout Int, @inout Int) -> ()
371-
%5 = begin_access [modify] [unknown] %3 : $*Int // expected-warning {{simultaneous accesses; modification requires exclusive access}}
371+
%5 = begin_access [modify] [unknown] %3 : $*Int // expected-error {{simultaneous accesses; modification requires exclusive access}}
372372
%6 = begin_access [modify] [unknown] %3 : $*Int // expected-note {{conflicting access is here}}
373373
%7 = begin_access [modify] [unknown] %3 : $*Int // no-error
374374
%8 = apply %4(%5, %6) : $@convention(thin) (@inout Int, @inout Int) -> ()
@@ -392,7 +392,7 @@ bb0(%0 : $Int):
392392
%4 = function_ref @takesTwoInouts : $@convention(thin) (@inout Int, @inout Int) -> ()
393393
%5 = begin_access [modify] [unknown] %3 : $*Int // no-note
394394
end_access %5 : $*Int
395-
%6 = begin_access [modify] [unknown] %3 : $*Int // expected-warning {{simultaneous accesses; modification requires exclusive access}}
395+
%6 = begin_access [modify] [unknown] %3 : $*Int // expected-error {{simultaneous accesses; modification requires exclusive access}}
396396
%7 = begin_access [modify] [unknown] %3 : $*Int // expected-note {{conflicting access is here}}
397397
%8 = apply %4(%5, %6) : $@convention(thin) (@inout Int, @inout Int) -> ()
398398
end_access %7 : $*Int

test/SILOptimizer/exclusivity_static_diagnostics.swift

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -enforce-exclusivity=checked -emit-sil -primary-file %s -o /dev/null -verify
1+
// RUN: %target-swift-frontend -enforce-exclusivity=checked -swift-version 4 -emit-sil -primary-file %s -o /dev/null -verify
22

33
import Swift
44

@@ -7,25 +7,27 @@ func takesTwoInouts<T>(_ p1: inout T, _ p2: inout T) { }
77
func simpleInoutDiagnostic() {
88
var i = 7
99

10+
// FIXME: This diagnostic should be removed if static enforcement is
11+
// turned on by default.
1012
// expected-error@+4{{inout arguments are not allowed to alias each other}}
1113
// expected-note@+3{{previous aliasing argument}}
12-
// expected-warning@+2{{simultaneous accesses to var 'i'; modification requires exclusive access}}
14+
// expected-error@+2{{simultaneous accesses to var 'i'; modification requires exclusive access}}
1315
// expected-note@+1{{conflicting access is here}}
1416
takesTwoInouts(&i, &i)
1517
}
1618

1719
func inoutOnInoutParameter(p: inout Int) {
1820
// expected-error@+4{{inout arguments are not allowed to alias each other}}
1921
// expected-note@+3{{previous aliasing argument}}
20-
// expected-warning@+2{{simultaneous accesses to parameter 'p'; modification requires exclusive access}}
22+
// expected-error@+2{{simultaneous accesses to parameter 'p'; modification requires exclusive access}}
2123
// expected-note@+1{{conflicting access is here}}
2224
takesTwoInouts(&p, &p)
2325
}
2426

2527
func swapNoSuppression(_ i: Int, _ j: Int) {
2628
var a: [Int] = [1, 2, 3]
2729

28-
// expected-warning@+2{{simultaneous accesses to var 'a'; modification requires exclusive access}}
30+
// expected-error@+2{{simultaneous accesses to var 'a'; modification requires exclusive access}}
2931
// expected-note@+1{{conflicting access is here}}
3032
swap(&a[i], &a[j]) // no-warning
3133
}
@@ -40,21 +42,21 @@ struct StructWithMutatingMethodThatTakesSelfInout {
4042
mutating func callMutatingMethodThatTakesSelfInout() {
4143
// expected-error@+4{{inout arguments are not allowed to alias each other}}
4244
// expected-note@+3{{previous aliasing argument}}
43-
// expected-warning@+2{{simultaneous accesses to parameter 'self'; modification requires exclusive access}}
45+
// expected-error@+2{{simultaneous accesses to parameter 'self'; modification requires exclusive access}}
4446
// expected-note@+1{{conflicting access is here}}
4547
mutate(&self)
4648
}
4749

4850
mutating func callMutatingMethodThatTakesSelfStoredPropInout() {
49-
// expected-warning@+2{{simultaneous accesses to parameter 'self'; modification requires exclusive access}}
51+
// expected-error@+2{{simultaneous accesses to parameter 'self'; modification requires exclusive access}}
5052
// expected-note@+1{{conflicting access is here}}
5153
mutate(&self.f)
5254
}
5355
}
5456

5557
var globalStruct1 = StructWithMutatingMethodThatTakesSelfInout()
5658
func callMutatingMethodThatTakesGlobalStoredPropInout() {
57-
// expected-warning@+2{{simultaneous accesses to var 'globalStruct1'; modification requires exclusive access}}
59+
// expected-error@+2{{simultaneous accesses to var 'globalStruct1'; modification requires exclusive access}}
5860
// expected-note@+1{{conflicting access is here}}
5961
globalStruct1.mutate(&globalStruct1.f)
6062
}
@@ -66,13 +68,13 @@ class ClassWithFinalStoredProp {
6668
func callMutatingMethodThatTakesClassStoredPropInout() {
6769
s1.mutate(&s2.f) // no-warning
6870

69-
// expected-warning@+2{{simultaneous accesses to var 's1'; modification requires exclusive access}}
71+
// expected-error@+2{{simultaneous accesses to var 's1'; modification requires exclusive access}}
7072
// expected-note@+1{{conflicting access is here}}
7173
s1.mutate(&s1.f)
7274

7375
let local1 = self
7476

75-
// expected-warning@+2{{simultaneous accesses to var 's1'; modification requires exclusive access}}
77+
// expected-error@+2{{simultaneous accesses to var 's1'; modification requires exclusive access}}
7678
// expected-note@+1{{conflicting access is here}}
7779
local1.s1.mutate(&local1.s1.f)
7880
}
@@ -82,7 +84,7 @@ func violationWithGenericType<T>(_ p: T) {
8284
var local = p
8385
// expected-error@+4{{inout arguments are not allowed to alias each other}}
8486
// expected-note@+3{{previous aliasing argument}}
85-
// expected-warning@+2{{simultaneous accesses to var 'local'; modification requires exclusive access}}
87+
// expected-error@+2{{simultaneous accesses to var 'local'; modification requires exclusive access}}
8688
// expected-note@+1{{conflicting access is here}}
8789
takesTwoInouts(&local, &local)
8890
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %target-swift-frontend -enforce-exclusivity=checked -swift-version 3 -emit-sil -primary-file %s -o /dev/null -verify
2+
3+
import Swift
4+
5+
// In Swift 3 compatibility mode, diagnostics for exclusive accesses are
6+
// warnings not errors.
7+
8+
func takesTwoInouts<T>(_ p1: inout T, _ p2: inout T) { }
9+
10+
func simpleInoutDiagnostic() {
11+
var i = 7
12+
13+
// expected-error@+4{{inout arguments are not allowed to alias each other}}
14+
// expected-note@+3{{previous aliasing argument}}
15+
// expected-warning@+2{{simultaneous accesses to var 'i'; modification requires exclusive access}}
16+
// expected-note@+1{{conflicting access is here}}
17+
takesTwoInouts(&i, &i)
18+
}
19+
20+
struct X {
21+
var f = 12
22+
}
23+
24+
func diagnoseOnSameField() {
25+
var x = X()
26+
27+
// expected-warning@+2{{simultaneous accesses to var 'x'; modification requires exclusive access}}
28+
// expected-note@+1{{conflicting access is here}}
29+
takesTwoInouts(&x.f, &x.f)
30+
}

0 commit comments

Comments
 (0)