Skip to content

Commit fa64f17

Browse files
committed
Improve opaque type with no RHS error message
Fixes #15266
1 parent 9d68778 commit fa64f17

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

+13-4
Original file line numberDiff line numberDiff line change
@@ -1235,18 +1235,27 @@ object desugar {
12351235
rhsOK(rhs)
12361236
}
12371237

1238+
def checkOpaqueAlias(tree: MemberDef)(using Context): MemberDef =
1239+
if !tree.mods.is(Opaque) then tree
1240+
else tree match
1241+
case TypeDef(_, bounds: TypeBoundsTree) if bounds.alias.isEmpty =>
1242+
report.error(i"opaque type must have a right-hand side", tree.srcPos)
1243+
tree.withMods(tree.mods.withoutFlags(Opaque))
1244+
case _ => tree
1245+
12381246
/** Check that modifiers are legal for the definition `tree`.
12391247
* Right now, we only check for `opaque`. TODO: Move other modifier checks here.
12401248
*/
12411249
def checkModifiers(tree: Tree)(using Context): Tree = tree match {
12421250
case tree: MemberDef =>
12431251
var tested: MemberDef = tree
1244-
def checkApplicable(flag: Flag, test: MemberDefTest): Unit =
1252+
def checkApplicable(flag: Flag, test: MemberDefTest): MemberDef =
12451253
if (tested.mods.is(flag) && !test.applyOrElse(tree, (md: MemberDef) => false)) {
12461254
report.error(ModifierNotAllowedForDefinition(flag), tree.srcPos)
1247-
tested = tested.withMods(tested.mods.withoutFlags(flag))
1248-
}
1249-
checkApplicable(Opaque, legalOpaque)
1255+
tested.withMods(tested.mods.withoutFlags(flag))
1256+
} else tested
1257+
tested = checkOpaqueAlias(tested)
1258+
tested = checkApplicable(Opaque, legalOpaque)
12501259
tested
12511260
case _ =>
12521261
tree

tests/neg/i15266.check

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- [E156] Syntax Error: tests/neg/i15266.scala:5:13 --------------------------------------------------------------------
2+
5 |opaque class A // error
3+
|^^^^^^^^^^^^^^
4+
|Modifier opaque is not allowed for this definition
5+
-- Error: tests/neg/i15266.scala:1:12 ----------------------------------------------------------------------------------
6+
1 |opaque type Type0 // error
7+
|^^^^^^^^^^^^^^^^^
8+
|opaque type must have a right-hand side

tests/neg/i15266.scala

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
opaque type Type0 // error
2+
opaque type Type1 = Int
3+
opaque type Type2 <: Int = 2
4+
opaque type Type3 >: 3 <: Int = 3
5+
opaque class A // error

0 commit comments

Comments
 (0)