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

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Feb 11, 2019

No description provided.

Copy link
Contributor

@allanrenucci allanrenucci left a 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)) {
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

@odersky
Copy link
Contributor Author

odersky commented Feb 11, 2019

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)) {
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

@odersky odersky merged commit aa715fe into scala:master Feb 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants