Here is a short sample code to show what is happening. Note that the only difference between bar() and baz() is the dot before foo(). The solution for catching such errors is using @warn_unqualified_access
attribute as indicated in the comments:
protocol P {
func foo()
}
struct S1: P { func foo() { print("S1 foo()") } }
struct S2: P {
var s1 = S1()
// Uncomment the following to get a warning:
//@warn_unqualified_access
func foo() { print("S2 foo()") }
func bar() {
s1
.foo()
}
func baz() {
s1
foo() // Warns here when you uncomment @warn_unqualified_access
// To silence the warning you should write `self.foo()` if that is what you mean.
}
}
var s2 = S2()
s2.bar()
s2.baz()
I think SwiftUI should use this attribute on such methods.
I also think we may need to enhance @warn_unqualified_access
with some arguments to give API authors more control over the error message and even let them provide fix-it for it.
It is also possible to teach the compiler to detect this particular pattern (when an unqualified method call follows a statement with an ignored result that would accept the same method signature)
It would also be nice if we could use this attribute on a protocol definition to make it warn for all types that conform to it:
// This does compile, but has no effect:
protocol P {
@warn_unqualified_access func foo()
}