Files
swift-mirror/test/Sema/diag_self_assign.swift
Dmitri Hrybenko 61c067fe5f Implement a check in Sema to find self-assignment
rdar://14151649 suggests that it should be a warning, but I don’t see a reason
why it should not be an error.  We have no legacy code that relies on this.


Swift SVN r9976
2013-11-05 23:43:54 +00:00

75 lines
1.9 KiB
Swift

// RUN: %swift %s -verify
var sa1_global: Int
sa1_global = sa1_global // expected-error {{assigning a variable to itself}}
class SA1 {
var foo: Int
init(foo: Int) {
foo = foo // expected-error {{assigning a variable to itself}}
self.foo = self.foo // expected-error {{assigning a property to itself}}
foo = self.foo // no-error
self.foo = foo // no-error
}
def f(foo: Int) {
foo = foo // expected-error {{assigning a variable to itself}}
self.foo = self.foo // expected-error {{assigning a property to itself}}
foo = self.foo // no-error
self.foo = foo // no-error
}
}
class SA2 {
var foo: Int {
get:
return 0
set:
}
init(foo: Int) {
foo = foo // expected-error {{assigning a variable to itself}}
self.foo = self.foo // expected-error {{assigning a property to itself}}
foo = self.foo // no-error
self.foo = foo // no-error
}
def f(foo: Int) {
foo = foo // expected-error {{assigning a variable to itself}}
self.foo = self.foo // expected-error {{assigning a property to itself}}
foo = self.foo // no-error
self.foo = foo // no-error
}
}
class SA3 {
var foo: Int {
get:
return foo
set:
foo = foo // expected-error {{assigning a property to itself}}
self.foo = self.foo // expected-error {{assigning a property to itself}}
foo = self.foo // expected-error {{assigning a property to itself}}
self.foo = foo // expected-error {{assigning a property to itself}}
}
}
class SA4 {
var foo: Int {
get:
return foo
set:
value = value // expected-error {{assigning a variable to itself}}
}
}
class SA5 {
var foo: Int
}
def SA5_test(a: SA4, b: SA4) {
a.foo = a.foo // expected-error {{assigning a property to itself}}
a.foo = b.foo
}
def SA6_test(a: Int) {
a = a.0 // expected-error {{assigning a variable to itself}}
a.0 = a // expected-error {{assigning a variable to itself}}
}