Skip to content

Fix #8150: Improve screening of legal refinements #8166

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
Feb 6, 2020
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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Flags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ object Flags {
}

/** The list of non-empty names of flags that are set in the given flag set */
def flagStrings(privateWithin: String): Seq[String] = {
def flagStrings(privateWithin: String = ""): Seq[String] = {
var rawStrings = (2 to MaxFlag).flatMap(x.flagString(_)) // DOTTY problem: cannot drop with (_)
if (!privateWithin.isEmpty && !x.is(Protected))
rawStrings = rawStrings :+ "private"
Expand Down
28 changes: 15 additions & 13 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3844,19 +3844,21 @@ object Parsers {
*/
def refineStatSeq(): List[Tree] = {
val stats = new ListBuffer[Tree]
def checkLegal(tree: Tree): List[Tree] = {
val isLegal = tree match {
case tree: ValDef => tree.rhs.isEmpty && !tree.mods.flags.is(Mutable)
case tree: DefDef => tree.rhs.isEmpty
case tree: TypeDef => true
case _ => false
}
if (isLegal) tree :: Nil
else {
syntaxError("illegal refinement", tree.span)
Nil
}
}
def checkLegal(tree: Tree): List[Tree] =
val problem = tree match
case tree: MemberDef if !(tree.mods.flags & ModifierFlags).isEmpty =>
i"refinement cannot be ${(tree.mods.flags & ModifierFlags).flagStrings().mkString("`", "`, `", "`")}"
case tree: ValOrDefDef =>
if tree.rhs.isEmpty then ""
else "refinement in cannot have a right-hand side"
case tree: TypeDef =>
if !tree.isClassDef then ""
else "refinement cannot be a class or trait"
case _ =>
"this kind of definition cannot be a refinement"
if problem.isEmpty then tree :: Nil
else { syntaxError(problem, tree.span); Nil }

while (!isStatSeqEnd) {
if (isDclIntro)
stats ++= checkLegal(defOrDcl(in.offset, Modifiers()))
Expand Down
3 changes: 3 additions & 0 deletions tests/neg/i8150.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
trait A
trait B
type T = {given(given a: A) as B} // error: refinement cannot be `given`