Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Use annotation to enable -explain locally, similar to @nowarn("verbose") #22413

lrytz started this conversation in Feature Requests
Discussion options

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
You must be logged in to vote

Replies: 2 comments

Comment options

lrytz
Feb 25, 2025
Maintainer Author

Alternative, suggested by @dwijnand: make the compiler understand //> using options -explain in individual source files.

You must be logged in to vote
0 replies
Comment options

I needed this feature today. (For a test where I don't want to explain everything, but I want to show certain explanations.)

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /