Skip to content

Fix #5888: Disallow SAM type creation of final and sealed classes #5896

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 1 commit into from
Feb 15, 2019
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
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Flags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,9 @@ object Flags {
/** value that's final or inline */
final val FinalOrInline: FlagSet = Final | Inline

/** class that's final or sealed */
final val FinalOrSealed: FlagSet = Final | Sealed

/** A covariant type parameter instance */
final val LocalCovariant: FlagConjunction = allOf(Local, Covariant)

Expand Down
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,11 @@ class Typer extends Namer
case SAMType(sam)
if !defn.isFunctionType(pt) && mt <:< sam =>
if (!isFullyDefined(pt, ForceDegree.all))
ctx.error(ex"result type of closure is an underspecified SAM type $pt", tree.sourcePos)
ctx.error(ex"result type of lambda is an underspecified SAM type $pt", tree.sourcePos)
else if (pt.classSymbol.is(FinalOrSealed)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we move this check into the SAMType extractor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No I think it's better here. An extractor is for extracting, not reporting errors.

Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't mean to report an error in the extractor, just have the extractor return None on sealed and final classes. It seemed arbitrary to have a special error message for this. In scalac I get a type mismatch instead.:

scala> sealed trait Foo { def f(x: Int): Int }; val foo: Foo = (x: Int) => x
<console>:11: error: type mismatch;
 found   : Int => Int
 required: Foo
       sealed trait Foo { def f(x: Int): Int }; val foo: Foo = (x: Int) => x

val offendingFlag = pt.classSymbol.flags & FinalOrSealed
ctx.error(ex"lambda cannot implement $offendingFlag ${pt.classSymbol}", tree.sourcePos)
}
TypeTree(pt)
case _ =>
if (mt.isParamDependent) {
Expand Down
8 changes: 8 additions & 0 deletions tests/neg/i5888.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
val x: String <:< Int = _.toInt // error

abstract final class Foo { def f(x: Int): Int }

val foo: Foo = x => x // error

val foo2: Foo = Predef.identity // error