Skip to content

Commit e9de73c

Browse files
authored
Merge pull request #15285 from dotty-staging/fix-15266
Improve opaque type with no RHS error message
2 parents 51308c9 + bccacaf commit e9de73c

File tree

5 files changed

+50
-8
lines changed

5 files changed

+50
-8
lines changed

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

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

1238+
def checkOpaqueAlias(tree: MemberDef)(using Context): MemberDef =
1239+
def check(rhs: Tree): MemberDef = rhs match
1240+
case bounds: TypeBoundsTree if bounds.alias.isEmpty =>
1241+
report.error(i"opaque type must have a right-hand side", tree.srcPos)
1242+
tree.withMods(tree.mods.withoutFlags(Opaque))
1243+
case LambdaTypeTree(_, body) => check(body)
1244+
case _ => tree
1245+
if !tree.mods.is(Opaque) then tree
1246+
else tree match
1247+
case TypeDef(_, rhs) => check(rhs)
1248+
case _ => tree
1249+
12381250
/** Check that modifiers are legal for the definition `tree`.
12391251
* Right now, we only check for `opaque`. TODO: Move other modifier checks here.
12401252
*/
12411253
def checkModifiers(tree: Tree)(using Context): Tree = tree match {
12421254
case tree: MemberDef =>
12431255
var tested: MemberDef = tree
1244-
def checkApplicable(flag: Flag, test: MemberDefTest): Unit =
1256+
def checkApplicable(flag: Flag, test: MemberDefTest): MemberDef =
12451257
if (tested.mods.is(flag) && !test.applyOrElse(tree, (md: MemberDef) => false)) {
12461258
report.error(ModifierNotAllowedForDefinition(flag), tree.srcPos)
1247-
tested = tested.withMods(tested.mods.withoutFlags(flag))
1248-
}
1249-
checkApplicable(Opaque, legalOpaque)
1259+
tested.withMods(tested.mods.withoutFlags(flag))
1260+
} else tested
1261+
tested = checkOpaqueAlias(tested)
1262+
tested = checkApplicable(Opaque, legalOpaque)
12501263
tested
12511264
case _ =>
12521265
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

tests/neg/i6055.check

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- Error: tests/neg/i6055.scala:1:12 -----------------------------------------------------------------------------------
2+
1 |opaque type i0 // error: opaque type must have a right-hand side
3+
|^^^^^^^^^^^^^^
4+
|opaque type must have a right-hand side
5+
-- Error: tests/neg/i6055.scala:2:12 -----------------------------------------------------------------------------------
6+
2 |opaque type i2 <: Int // error: opaque type must have a right-hand side
7+
|^^^^^^^^^^^^^^^^^^^^^
8+
|opaque type must have a right-hand side
9+
-- Error: tests/neg/i6055.scala:4:12 -----------------------------------------------------------------------------------
10+
4 |opaque type i1[_] // error: opaque type must have a right-hand side
11+
|^^^^^^^^^^^^^^^^^
12+
|opaque type must have a right-hand side
13+
-- Error: tests/neg/i6055.scala:5:12 -----------------------------------------------------------------------------------
14+
5 |opaque type x[_] <: Int // error: opaque type must have a right-hand side
15+
|^^^^^^^^^^^^^^^^^^^^^^^
16+
|opaque type must have a right-hand side

tests/neg/i6055.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
opaque type i0 // error: opaque type must be an alias type
2-
opaque type i2 <: Int // error: opaque type must be an alias type
1+
opaque type i0 // error: opaque type must have a right-hand side
2+
opaque type i2 <: Int // error: opaque type must have a right-hand side
33

4-
opaque type i1[_] // error: opaque type must be an alias type
5-
opaque type x[_] <: Int // error: opaque type must be an alias type
4+
opaque type i1[_] // error: opaque type must have a right-hand side
5+
opaque type x[_] <: Int // error: opaque type must have a right-hand side

0 commit comments

Comments
 (0)