-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #4247: Do not crash if self is of Array type #4251
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Foo[U] { self : Array[U] & Nothing => | ||
val s = self(0) // error: Array type conflict with Foo | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Foo[U] { self : Array[U] & Nothing => | ||
self(0) = ??? // error: Array type conflict with Foo | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Foo[U] { self : Array[U] & Nothing => | ||
self.length // error: Array type conflict with Foo | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Scalac allows both the following:
We need to justify why Dotty should deviate in such cases.
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.
We will never be able to instantiate a Foo because Array is final. The code is nonsensical as no subclass of Foo can mixin Array.
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'm thinking it's the same case as
def f(x: Nothing): Unit = 3
. The code is valid, though the function can not be called. If we have checks for realization of self types during instantiation, can we just reuse that mechanism?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.
Maybe we could. But this patch should still be there to ensure there are no compiler crashes on arrays in case that checker fails to detect it early.
Also, It is possible that we may create some other kind of unrealisable array with something that is not the self type.
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 tend to agree with @liufengyun. We should fix https://github.com/lampepfl/dotty/blob/4c8a2bd319437f7ab22303a162f5922bc000134d/compiler/src/dotty/tools/dotc/typer/Checking.scala#L148
Otherwise we will reject
class Foo[U] { self : Array[U] & Nothing => self(0) }
but acceptclass Foo[U] { self : Array[U] & Nothing => }
.Wouldn't that be a bug? Then crashing is the right think to do
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.
That was a different issue.
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.
The
& Nothing
is unrelated tho this particular issue.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.
Sorry, my point was that we should also reject
test[U](x: Array[U] & Nothing) = x(0)
, if we rejectself: Array[U] & Nothing => self(0)
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.
BTW, those checks on instantiation are spec-mandated (#4252), so I think I agree with @allanrenucci and @liufengyun.
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.
The check on instantiation are done and work as expected. The issue is that for this particular case we do have a problem before instantiation. This is a particular exception due to the way we erase Arrays into java arrays.
Additionally the self type Array is completely valid as far as this class is conserened and could be realizable. Though Array would be the only class that could extend it.