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
add -Wconf flag for configurable warnings, @nowarn annotation for local suppression #8373
Conversation
Message filtering is performed in The reasons are:
So all warnings in the compiler should go through
The There are a few calls to
|
Added support for local warning suppression using |
This will be immensely useful. Thanks for working on this! |
7729d15
to
9c37e5e
Compare
Removed the WIP flag and update the PR description, this is ready to be reviewed! |
174a43e
to
8aa1750
Compare
@SethTisue would you be interested to review this, at least on a high level? I updated the PR description, and also included a section on significant changes. |
f2ed6db
to
1667f83
Compare
This implements `apiStatus ` annotation as a generic form of `deprecated` annotation. `apiStatus` takes `category` and `defaultAction` parameters, corresponding to configurable warning's category and action. One of the usage is to trigger compiler error from the library when a method is invoked to display migration message. Another usage would be to denote bincompat status of the API as warning. This is a resend of scala#7790 based on the configurable warnings. Ref scala#8373 / https://twitter.com/not_xuwei_k/status/1240354073297268737
This implements `apiStatus` annotation as a generic form of `deprecated` annotation. While deprecated is only able to discourage someone from using the some API, `apiStatus` can be more nuanced about the state (for instance Category.ApiMayChange), and choose the default compile-time actions (Action.Error, Action.Warning, etc). In other words, this gives library authors the lever to trigger compilation warning or compilation errors! One of the usage is to trigger compiler error from the library when a method is invoked to display migration message. Another usage would be to denote bincompat status of the API as warning. This is a resend of scala#7790 based on the configurable warnings. Ref scala#8373 / https://twitter.com/not_xuwei_k/status/1240354073297268737
I tested this feature in Apache Kafka and I have a branch with 0 warnings and
I will try this on a minimized test case and report an issue (hopefully soon), but I wanted to give a heads up since 2.13.2 has not been released yet. |
That's certainly an option. Alternatively, adding it to https://github.com/scala/scala-collection-compat would be lower overhead - would it be an option to add this dependency? For the site filter, it seems to work for me; is it maybe that the leftmost matching rule defines the action? $> cat Test.scala
package a.pu
class K {
def f = 1 → 2
@annotation.nowarn("site=a.pu.K.g")
def g = 1 → 2
}
$>
$> scv 2.13.2-bin-cd56924 -Wconf:cat=deprecation:wv Test.scala
Test.scala:4: warning: [deprecation @ a.pu.K.f | origin=scala.Predef.ArrowAssoc.→ | version=2.13.0] method → in class ArrowAssoc is deprecated (since 2.13.0): Use `->` instead. If you still wish to display it as one character, consider using a font with programming ligatures such as Fira Code.
def f = 1 → 2
^
1 warning
$>
$> scv 2.13.2-bin-cd56924 -Wconf:site=a.pu.K.f:s,cat=deprecation:wv Test.scala
$>
$> scv 2.13.2-bin-cd56924 -Wconf:cat=deprecation:wv,site=a.pu.K.f:s Test.scala
Test.scala:4: warning: [deprecation @ a.pu.K.f | origin=scala.Predef.ArrowAssoc.→ | version=2.13.0] method → in class ArrowAssoc is deprecated (since 2.13.0): Use `->` instead. If you still wish to display it as one character, consider using a font with programming ligatures such as Fira Code.
def f = 1 → 2
^
1 warning
$> |
@lrytz Thanks for the quick response.
I got |
OK, I see how the The following command line worked as expected for me:
|
@lrytz I investigated more and I found the issue. I got a bit unlucky and the first warning I tried to suppress didn't seem to have the
In the Kafka codebase, we had one other example like the above (similar pattern), but all other ones worked fine. The usage of |
I'll take a look if I can reproduce and fix that. For |
(moved my comment to scala/scala-collection-compat#312 (comment) where I intended to post it) |
Thanks! |
|
@charpov https://github.com/scala/scala/blob/v2.13.3/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala#L1046 just says other. (Probably I'd ask on gitter instead of the old PR.) (or the forum. It might be a useful topic to consider what cats are useful.) |
This PR adds a
-Wconf
compiler flag that allows filtering and configuring compiler warnings (silence them, or turn them into errors).It also integrates the fantastic silencer compiler plugin by @ghik into the compiler, which allows suppressing warnings locally using the
@nowarn
annotation.Configurable Warnings
Warnings are assigned a category.
Warnings can be filtered by category, by message regex, by site where they are issued, and by source path. Deprecations can additionally be filtered by origin (deprecated definition) and
since
version.Filtered warnings can be reported as error, warning, info, summary (like deprecations) or silent.
Local Suppression
The
@nowarn
annotation suppresses warnings within the scope covered by the annotation.@nowarn def foo = ...
,@nowarn class C { ... }
: suppress warnings in a definitionexpression: @nowarn
: suppress warnings in a specific expressionThe annotation can be configured to filter selected warnings, for example
@nowarn("cat=deprecation")
only suppresses deprecation warnings. The filter configuration syntax is the same as in-Wconf
.Help Text
Significant changes
scala.annotation.nowarn
-rootdir
compiler flag. It is used to relativize file paths when using source filters (-Wconf:src=some/source/File.scala:s
), there might be other uses for it in the future.warning
methods in currentRun.reporting and context reporter take newCategory
parameter. Theglobal.reporter
is unchanged, warnings reported through it are not filtered byWconf
.@nowarn
annotations.@nowarn
-deprecation
,-feature
and-unchecked
settings are no longer directly used in the compiler, they are shortcuts for specificWconf
configurations. the compiler only looks at-Wconf
.