Fix #5888: Disallow SAM type creation of final and sealed classes#5896
Fix #5888: Disallow SAM type creation of final and sealed classes#5896odersky merged 1 commit intoscala:masterfrom
Conversation
allanrenucci
left a comment
There was a problem hiding this comment.
My intuition would be that SAM type creation of sealed classes should be allowed within the same file, however scalac prevents it.
| 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)) { |
There was a problem hiding this comment.
Should we move this check into the SAMType extractor?
There was a problem hiding this comment.
No I think it's better here. An extractor is for extracting, not reporting errors.
There was a problem hiding this comment.
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|
Even anonymous subclasses of sealed types are problematic. So I think it's OK to not allow SAM type creation. |
| 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)) { |
There was a problem hiding this comment.
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
No description provided.