Skip to content

Improve opaque type with no RHS error message #15285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1235,18 +1235,31 @@ object desugar {
rhsOK(rhs)
}

def checkOpaqueAlias(tree: MemberDef)(using Context): MemberDef =
def check(rhs: Tree): MemberDef = rhs match
case bounds: TypeBoundsTree if bounds.alias.isEmpty =>
report.error(i"opaque type must have a right-hand side", tree.srcPos)
Copy link
Contributor

@griggt griggt May 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
report.error(i"opaque type must have a right-hand side", tree.srcPos)
report.error(i"opaque type must have a right-hand side", tree.srcPos.endPos)

...if we want the caret to be placed where the missing RHS should go?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both approaches seem reasonable to me. I do not have a preference on either option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both approaches seem reasonable to me. I do not have a preference on either option.

tree.withMods(tree.mods.withoutFlags(Opaque))
case LambdaTypeTree(_, body) => check(body)
case _ => tree
if !tree.mods.is(Opaque) then tree
else tree match
case TypeDef(_, rhs) => check(rhs)
case _ => tree

/** Check that modifiers are legal for the definition `tree`.
* Right now, we only check for `opaque`. TODO: Move other modifier checks here.
*/
def checkModifiers(tree: Tree)(using Context): Tree = tree match {
case tree: MemberDef =>
var tested: MemberDef = tree
def checkApplicable(flag: Flag, test: MemberDefTest): Unit =
def checkApplicable(flag: Flag, test: MemberDefTest): MemberDef =
if (tested.mods.is(flag) && !test.applyOrElse(tree, (md: MemberDef) => false)) {
report.error(ModifierNotAllowedForDefinition(flag), tree.srcPos)
tested = tested.withMods(tested.mods.withoutFlags(flag))
}
checkApplicable(Opaque, legalOpaque)
tested.withMods(tested.mods.withoutFlags(flag))
} else tested
tested = checkOpaqueAlias(tested)
tested = checkApplicable(Opaque, legalOpaque)
tested
case _ =>
tree
Expand Down
8 changes: 8 additions & 0 deletions tests/neg/i15266.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- [E156] Syntax Error: tests/neg/i15266.scala:5:13 --------------------------------------------------------------------
5 |opaque class A // error
|^^^^^^^^^^^^^^
|Modifier opaque is not allowed for this definition
-- Error: tests/neg/i15266.scala:1:12 ----------------------------------------------------------------------------------
1 |opaque type Type0 // error
|^^^^^^^^^^^^^^^^^
|opaque type must have a right-hand side
5 changes: 5 additions & 0 deletions tests/neg/i15266.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
opaque type Type0 // error
opaque type Type1 = Int
opaque type Type2 <: Int = 2
opaque type Type3 >: 3 <: Int = 3
opaque class A // error
16 changes: 16 additions & 0 deletions tests/neg/i6055.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Error: tests/neg/i6055.scala:1:12 -----------------------------------------------------------------------------------
1 |opaque type i0 // error: opaque type must have a right-hand side
|^^^^^^^^^^^^^^
|opaque type must have a right-hand side
-- Error: tests/neg/i6055.scala:2:12 -----------------------------------------------------------------------------------
2 |opaque type i2 <: Int // error: opaque type must have a right-hand side
|^^^^^^^^^^^^^^^^^^^^^
|opaque type must have a right-hand side
-- Error: tests/neg/i6055.scala:4:12 -----------------------------------------------------------------------------------
4 |opaque type i1[_] // error: opaque type must have a right-hand side
|^^^^^^^^^^^^^^^^^
|opaque type must have a right-hand side
-- Error: tests/neg/i6055.scala:5:12 -----------------------------------------------------------------------------------
5 |opaque type x[_] <: Int // error: opaque type must have a right-hand side
|^^^^^^^^^^^^^^^^^^^^^^^
|opaque type must have a right-hand side
8 changes: 4 additions & 4 deletions tests/neg/i6055.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
opaque type i0 // error: opaque type must be an alias type
opaque type i2 <: Int // error: opaque type must be an alias type
opaque type i0 // error: opaque type must have a right-hand side
opaque type i2 <: Int // error: opaque type must have a right-hand side

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