-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/vet: decide on support for type parameters in the printf analyzer #48971
Comments
In my opinion:
While it is true that “the user may have reasonable prior knowledge of the contents of an interface”, it is also the case that type sets can provide much more precision than interface types, and I think |
@bcmills from the user's point of view I agree with you: I'd like to be warned about these cases, to catch real bugs or be nudged towards writing slightly more defensive code. However, I understand that would go against the precedent set by how interfaces are treated conservatively, which I think is following vet's rule to avoid false positives. In any case, in the context of 1.18, I fully agree that we should keep the printf analyzer conservative. I already think 1.18 may be late with all that we're trying to include in it. Even if we agree to add type parameter warnings to printf, I think those should go into 1.19. |
On the flip-side, type sets are not yet as precise as they ought to be. If I understand #45346 (comment) correctly (@griesemer), the type constraint in this example would today be rejected by the compiler: type StringFormattable interface {
~string | ~[]byte | error | fmt.Stringer
}
func _[T StringFormattable](t T) {
fmt.Printf("%s\n", t) // t is definitely string-formattable, but it may or may not implement fmt.Stringer.
} |
@bcmills I think this is the counter argument here:
In fact my first draft of this issue drew the exact conclusion you suggest: we should warn whenever there are any elements of the type parameter type set that doesn't match the verb. However, after some discussion and looking for precedent, I think moving slowly may be wise here. |
Your understanding is correct, and that's a great example, thanks. |
Another consideration: we can include the prototyped additions to the printf analyzer in gopls now. This will allow developers using gopls to benefit from the additional type safety and will give us some experience with the check. |
It’s easier to relax restrictions than it is to add them. A strict check added now will definitely not affect any existing code, and can be adjusted or removed if it produces false positives. When it comes to warnings for dubious use of type parameters, I think it will be better to err on the side of more checks to begin with. |
Closing this as the associated proposal was accepted and implemented. |
This is a separate issue from #48704, because we need to make a non-trivial decision about the printf analyzer for 1.18.
The printf analyzer has come up several times in the context of updating tools for generics, because it is an example of where we might want to interrogate the type set of a type parameter -- see below. After going back-and-forth, in my opinion we should be conservative and treat type parameters like interfaces, at least for 1.18.
For discussion, consider the following examples:
In all of these examples, there are elements of the type parameter type set that do not match the printf verb being used, but in Examples 1 and 2 there may be false positives:
%s
, but we don’t know if it implementsfmt.Stringer
orfmt.Formatter
.fmt.Formatter
.%s
.In the future we may want to report diagnostics in all three examples, but in the interest of caution I think we should allow all three for 1.18. Vet has very high standards for avoiding false positives, and in Examples 1 and 2, the following advice about interfaces from the printf source applies:
We could reasonably produce a diagnostic just for Example 3, but I think it will probably be uncommon to have a finite type set, and it is OK to miss this true positive in the interest of simplifying the implementation.
The counter argument to doing nothing is that with the new embedded elements in interfaces, we can now express type sets more precisely and it is more likely that a mismatching verb is actually a user error. There is some precedent here, in both directions:
error
for %w (cf. cmd/vet: printf analysis no longer flags interfaces that don't implement error #48931)fmt.Formatter
(cf. cmd/vet: false positive for user-defined verbs #36564).This counter argument is compelling, and I think we should consider adding these diagnostics for 1.19 -- in fact I prototyped this in CL 351832 -- but for 1.18 in particular I am concerned that we don’t have a good understanding of how generics will be used, particularly when updating existing code, and having opinionated vet checks here may get in the way.
Regardless, I think this needs to be discussed (hence the issue).
CC @timothy-king @scott-cotton @mvdan
The text was updated successfully, but these errors were encountered: