-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Use annotation to enable -explain locally, similar to @nowarn("verbose")
#22413
-
Adding an annotation is a lot more convenient than changing the build to add a compiler flag.
Scala 3 supports @nowarn("verbose") to display applicable message filters ("Matching filters..." section):
scala> @nowarn("v") def f = try 1 1 warning found -- [E002] Syntax Warning: ------------------------------------------------------ 1 |@nowarn("v") def f = try 1 | ^^^^^ | A try without catch or finally is equivalent to putting | its body in a block; no exceptions are handled. |Matching filters for @nowarn or -Wconf: | - id=E2 | - name=EmptyCatchAndFinallyBlock | | longer explanation available when compiling with `-explain`
It would be practical if -explain could be enabled in the same way, not only for warnings but also for errors.
Reusing @nowarn("v") would be trivial, but maybe a different annotation is desired?
https://github.com/scala/scala3/compare/main...lrytz:scala3:nowarnExplain?expand=1
scala> def f: Int = "" -- [E007] Type Mismatch Error: ------------------------------------------------- 1 |def f: Int = "" | ^^ | Found: ("" : String) | Required: Int | | longer explanation available when compiling with `-explain` 1 error found scala> @annotation.nowarn("v") def f: Int = "" -- [E007] Type Mismatch Error: ------------------------------------------------- 1 |@annotation.nowarn("v") def f: Int = "" | ^^ | Found: ("" : String) | Required: Int |----------------------------------------------------------------------------- | Explanation (enabled by `-explain`) |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | | Tree: "" | I tried to show that | ("" : String) | conforms to | Int | but none of the attempts shown below succeeded: | | ==> ("" : String) <: Int | ==> String <: Int = false | | The tests were made under the empty constraint ----------------------------------------------------------------------------- 1 error found
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments
-
Alternative, suggested by @dwijnand: make the compiler understand //> using options -explain in individual source files.
Beta Was this translation helpful? Give feedback.
All reactions
-
I needed this feature today. (For a test where I don't want to explain everything, but I want to show certain explanations.)
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 1