Skip to content

Commit 11a6f0a

Browse files
authored
fix(#16610): warn ignored Scaladoc on multiple enum cases (#19555)
close #16610 Before this commit, the compiler ignored Scaladoc comment on multiple enum cases without warning. This is partly expected because the case to which the doc is attached is ambiguous, but we should at least warn users that the comment is ignored by compiler due to ambiguity and they should take an action if they want the doc to be displayed.
1 parent abe7b0f commit 11a6f0a

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ private sealed trait WarningSettings:
168168
val XfatalWarnings: Setting[Boolean] = BooleanSetting("-Werror", "Fail the compilation if there are any warnings.", aliases = List("-Xfatal-warnings"))
169169
val WvalueDiscard: Setting[Boolean] = BooleanSetting("-Wvalue-discard", "Warn when non-Unit expression results are unused.")
170170
val WNonUnitStatement = BooleanSetting("-Wnonunit-statement", "Warn when block statements are non-Unit expressions.")
171+
val WenumCommentDiscard = BooleanSetting("-Wenum-comment-discard", "Warn when a comment ambiguously assigned to multiple enum cases is discarded.")
171172
val WimplausiblePatterns = BooleanSetting("-Wimplausible-patterns", "Warn if comparison with a pattern value looks like it might always fail.")
172173
val WunstableInlineAccessors = BooleanSetting("-WunstableInlineAccessors", "Warn an inline methods has references to non-stable binary APIs.")
173174
val Wunused: Setting[List[ChoiceWithHelp[String]]] = MultiChoiceHelpSetting(

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3997,6 +3997,14 @@ object Parsers {
39973997
if (in.token == COMMA) {
39983998
in.nextToken()
39993999
val ids = commaSeparated(() => termIdent())
4000+
if ctx.settings.WenumCommentDiscard.value then
4001+
in.getDocComment(start).foreach: comm =>
4002+
warning(
4003+
em"""Ambiguous Scaladoc comment on multiple cases is ignored.
4004+
|Remove the comment or make separate cases to add Scaladoc comments to each of them.""",
4005+
comm.span.start
4006+
)
4007+
40004008
PatDef(mods1, id :: ids, TypeTree(), EmptyTree)
40014009
}
40024010
else {

tests/warn/i16610.check

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- Warning: tests/warn/i16610.scala:12:2 -------------------------------------------------------------------------------
2+
12 | /** // warn
3+
| ^
4+
| Ambiguous Scaladoc comment on multiple cases is ignored.
5+
| Remove the comment or make separate cases to add Scaladoc comments to each of them.

tests/warn/i16610.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//> using options -Wenum-comment-discard
2+
/**
3+
* Description of enum
4+
*/
5+
enum MyEnum {
6+
7+
/**
8+
* Description of case 1
9+
*/
10+
case MyCase1
11+
12+
/** // warn
13+
* Description of case 2 and 3
14+
*/
15+
case MyCase2, MyCase3
16+
}

0 commit comments

Comments
 (0)