Skip to content
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: printf analysis no longer flags interfaces that don't implement error #48931

Closed
findleyr opened this issue Oct 12, 2021 · 5 comments
Closed
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@findleyr
Copy link
Contributor

findleyr commented Oct 12, 2021

I'm not sure if this is a bug or a feature.

In CL 177601, it was explicitly noted that %w verbs would not match interface{}.

Subsequently, in CL 217180, printf analyzer logic was changed to account for the fact that any argument of interface type could be a fmt.Formatter. More context in #36564. As a side-effect, we no longer report diagnostics if arguments for %w of interface type do not implement error.

Either one of these outcomes is reasonable to me:

  • the type system allows us to express 'implements' relationships between interfaces, so it is likely a user error if the argument type for %w does not implement error (the same is not true of, say, %d, as we can't currently express that an interface's dynamic type is integral).
  • ...but on the other hand we have very low tolerance for false positives in vet. If reasonable user code is generating a false positive, it should be fixed.

I just wanted to flag this inconsistency. What is correct here?

CC @neild @timothy-king

(FWIW, I'm interested in this as a matter of precedent, as I'm trying to figure out what to do with type parameters, where users may express structural restrictions on the underlying of a type parameter).

@neild
Copy link
Contributor

neild commented Oct 13, 2021

My opinion in CL 177601 was that I couldn't think of any real-world cases where you would want to use the %w verb on an interface value that doesn't implement error, and that you can trivially write a x.(error) type assertion to convert back to an error in the unlikely case that this comes up.

In the time since the check being added, I don't think I've ever heard of a vet false positive for this case.

I think vet should continue to flag using the %w verb with a value that is not an error.

@findleyr
Copy link
Contributor Author

I think vet should continue to flag using the %w verb with a value that is not an error.

To be clear this is not currently the case, as of CL 217180:
https://play.golang.org/p/JHbPwrqf3l5

Vet only reported arguments of interface type in Go 1.14. Per your reasoning, this is a regression. I agree with your logic, but thought perhaps we had encountered reasonable false positives. @kortschak, is this consistent with your expectation? Do you still think that we should exempt arguments of interface type from this check? Should %w be special?

@toothrot toothrot added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Oct 13, 2021
@toothrot toothrot added this to the Backlog milestone Oct 13, 2021
@kortschak
Copy link
Contributor

I think that it's reasonable to make %w special. The difference between this case and the cases in the issue I filed is that fmt.Formatter is an optional interface for all verbs, while an argument associated with a %w must be an error. While errors are just values, they are semantically laden and I think it's a good thing to make it explicit that a value being rendered is an error and not just an interface that also implements error.

@gopherbot
Copy link

Change https://golang.org/cl/355730 mentions this issue: go/analysis/passes/printf: warn against using non-error interface values with %w

@timothy-king
Copy link
Contributor

Bit late to the party. I am having a hard time recalling a real false positive, and I am struggling to think of a realistic false positive. A piece of support for being strict (/ suggesting x.(error) as a replacement) is the documentation for fmt.Errorf:

It is invalid to include more than one %w verb or to supply it with an operand that does not implement the error interface.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

6 participants