-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[Feature Request] Add a way to provide a custom error message for using a missing parameter #43942
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
Comments
I would think the error message would look like:
|
cc @devoncarew @bwilkerson This feels like maybe something that could be/should be done in the tooling, if we do anything? There could be a general language feature here, but if feels more like something that we provide on the side. cc @lrhn @natebosch @mit-mit is this something we would potentially use in the Dart sdk or Dart owned packages? @devoncarew @bwilkerson I wonder whether this might not just fit naturally into something like the refactoring tool you've been working on? |
I definitely wouldn't want to hard-code Flutter framework deprecation messages in the VM. That seems like a layering violation. (I'm not a great fan of how we do the widget line number feature for the same reason.) In principle the framework is not special. Anyone should be able to create a framework like ours and it should work just as well. |
The data file used by data-driven fixes will have enough information in it to allow the error reporter to detect that the parameter used to be declared, so that's a start. The file doesn't currently have any way to express the 'obsolete' message, but that could be added. It would also require some refactoring. Currently the data-driven fixes support is in the The other possibility that was mentioned is to provide some way to add metadata to the function that could be used by the analyzer. I think the annotation would need to be on the function, rather than in the parameter list (where there might not be a parameter to associate it with), but it could potentially look something like
I assume that the Flutter team would want to have some kind of policy governing how long such a notice lived in the code base. There might also be implications for dartdoc, but I don't know whether it displays all annotations or only those that it cares about. |
cc @Piinks who is working on related policies. |
As I showed in the description, I would also like the annotation to include the type of the removed parameter: it could then also give hints when the type changes and someone supplies an incompatible old type of the parameter, not just when it is removed. |
I suspect that I don't understand what you're asking for. If you change the type of a parameter and there is code that's no longer passing in the correct type, then there's already an error with a message like The argument type 'String' can't be assigned to the parameter type 'int'. Were you thinking of something different, or did I completely misunderstand? |
I was just thinking that in addition to "The argument type 'String' can't be assigned to the parameter type 'int'.", we could give a hint as to what is an appropriate replacement, and how to migrate.
|
It would only apply if there was an annotation on the API that included the old type, and the caller was trying to call it with the old type. Calling the example above with an enum, for instance, wouldn't trigger the hint. |
Ok, thanks. I'm not sure how often that case will arise, but I think it would be sufficient to have a |
The annotation should be on the function (it can't go in the Maybe something general like specifying a specific name parameter have been removed: @BreakingChange.namedParameterRemoved(#nullOk, since: "2.5", message: """
Instead of setting the 'nullOk' parameter, consider whether you want to use 'of' or 'maybeOf' instead
""")
void theMethod(...) ... or specifying the previous entire method signature if the change is more than a single named parameter. @BreakingChange.signature<int Function(int, int, {bool nullOk})>(since: "2.5", message: """
Instead of setting the 'nullOk' parameter, consider whether you want to use 'of' or 'maybeOf' instead
"""
void theMethod(...)... (at least if we had generic constructors, until then we'll probably need We could also have And we could also report It is something that can be useful whenever a package makes breaking changes. For packages, breaking changes are usually heralded by a major version increment, but even in a major version bump, most of a package is usually left unchanged. This could help users figure out what has changed (and you can even generate a "change report" from the code and the annotations, without having to look at the previous version, at least if the annotations are complete). For SDK changes, it requires the annotations to be in the Dart SDK. The Flutter changes is in a package, so they can depend on annotations in |
Unfortunately, we can't currently use type arguments in an annotation, which is why I proposed a
I'll just note that the data file for the data-driven fixes work actually has even more information than that, allowing analyzer to know things like when a declaration was renamed or completely removed, when parameters were added, when type parameters were changed, etc. It also contains enough information to allow the analysis server to fix the users code in many cases. I'm not convinced that we want to re-invent support that we are already implementing. The reason we chose to put this information into a separate file is because of the case of removing an API. In that case there isn't anywhere to hang an annotation that could provide analyzer with the information it needs. |
I completely forgot that you can't use type arguments in annotations. We should remove that restriction as long as it's syntactically simple (not making parsing much harder), and I think it is that simple (there is no need for Having the information on the side is a good thing, it means that you can (basically) know about all versions of the code. |
(Note: we added generic metadata, so those ideas are possible.) |
Uh oh!
There was an error while loading. Please reload this page.
In flutter/flutter#68736 (and bunch of other PRs), we're removing a parameter called
nullOk
that used to let the function know if the caller cared that the result was found or not (and hence whether it would return null or throw).We were going to deprecate it, but decided that the failure mode of asserting that the value was false was worse than having a breaking change where we just remove the argument and change the return type to non-nullable, since the assert only happens at runtime, and the breaking change will catch the problem at compile time.
However, when we do that, we'd like to be able to remove the parameter, but have the compiler fail when someone tries to use it with a helpful message that says "use this instead", much like an
@Deprecated
annotation would.I'm not sure what form that would take in the code, but I'm envisioning something like:
cc/ @leafpetersen @Hixie
The text was updated successfully, but these errors were encountered: