"Attempting to store to property within its own willSet" warning not shown if explicit self is used?

I recently came across this incorrect code in a project:

struct Video {
    var progress: Float? {
        willSet {
            guard let value = newValue else {
                self.progress = nil
                return
            }

            self.progress = min(max(value, 0.0), 1.0)
        }
    }
}

The intent was to have progress clamped between 0 and 1 but this code clearly doesn't do that.

These specifics aren't important here but what I find curious is that it wasn't noticed – and I believe that's in part because Xcode gave no warning here. However, if I remove the two uses of explicit self I do get this warning:

Attempting to store to property 'progress' within its own willSet, which is about to be overwritten by the new value

I suspect that the author of that flawed code above would've immediately corrected it had this warning been shown to them.

My question:

Is there any good reason why the use of explicit self should affect whether or not this warning gets shown in this case? Personally I don't see why the use of explicit self here would obfuscate the problem that the warning points out.

looks bug to me. minimal example:

struct S {
	var foo: Int {
		willSet {
			self.foo = 0 // no warning
		}
	}
	var bar: Int {
		willSet {
			bar = 0 // Attempting to store to property 'bar' within its own willSet, which is about to be overwritten by the new value
		}
	}
}
1 Like

Thank you. I suppose I should've just given this minimal example myself instead of the long-winded backstory.

I'm new to this community and its process – is this a known issue or should I report it at https://bugs.swift.org?

1 Like

yes, check if it's not already there and file.

interestingly i found smth very similar: [SR-7281] Swift should diagnose infinite recursion of property observers · Issue #49829 · apple/swift · GitHub