-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My intuition would be that SAM type creation of sealed classes should be allowed within the same file, however scalac
prevents it.
@@ -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)) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Even anonymous subclasses of sealed types are problematic. So I think it's OK to not allow SAM type creation. |
@@ -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)) { |
There was a problem hiding this comment.
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
No description provided.