-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #4098: Check that refinements have good bounds #4122
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
Changes from all commits
Commits
Show all changes
3 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 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 |
---|---|---|
|
@@ -30,8 +30,8 @@ object CheckRealizable { | |
class NotFinal(sym: Symbol)(implicit ctx: Context) | ||
extends Realizability(i" refers to nonfinal $sym") | ||
|
||
class HasProblemBounds(typ: SingleDenotation)(implicit ctx: Context) | ||
extends Realizability(i" has a member $typ with possibly conflicting bounds ${typ.info.bounds.lo} <: ... <: ${typ.info.bounds.hi}") | ||
class HasProblemBounds(name: Name, info: Type)(implicit ctx: Context) | ||
extends Realizability(i" has a member $name with possibly conflicting bounds ${info.bounds.lo} <: ... <: ${info.bounds.hi}") | ||
|
||
class HasProblemBaseArg(typ: Type, argBounds: TypeBounds)(implicit ctx: Context) | ||
extends Realizability(i" has a base type $typ with possibly conflicting parameter bounds ${argBounds.lo} <: ... <: ${argBounds.hi}") | ||
|
@@ -96,6 +96,14 @@ class CheckRealizable(implicit ctx: Context) { | |
else boundsRealizability(tp).andAlso(memberRealizability(tp)) | ||
} | ||
|
||
private def refinedNames(tp: Type): Set[Name] = tp.dealias match { | ||
case tp: RefinedType => refinedNames(tp.parent) + tp.refinedName | ||
case tp: AndType => refinedNames(tp.tp1) ++ refinedNames(tp.tp2) | ||
case tp: OrType => refinedNames(tp.tp1) ++ refinedNames(tp.tp2) | ||
case tp: TypeProxy => refinedNames(tp.underlying) | ||
case _ => Set.empty | ||
} | ||
|
||
/** `Realizable` if `tp` has good bounds, a `HasProblem...` instance | ||
* pointing to a bad bounds member otherwise. "Has good bounds" means: | ||
* | ||
|
@@ -107,12 +115,22 @@ class CheckRealizable(implicit ctx: Context) { | |
* also lead to base types with bad bounds). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment should be updated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, the comment is now correct. The previous implementation did less than the comment implied. |
||
*/ | ||
private def boundsRealizability(tp: Type) = { | ||
val mbrProblems = | ||
|
||
val memberProblems = | ||
for { | ||
mbr <- tp.nonClassTypeMembers | ||
if !(mbr.info.loBound <:< mbr.info.hiBound) | ||
} | ||
yield new HasProblemBounds(mbr) | ||
yield new HasProblemBounds(mbr.name, mbr.info) | ||
|
||
val refinementProblems = | ||
for { | ||
name <- refinedNames(tp) | ||
if (name.isTypeName) | ||
mbr <- tp.member(name).alternatives | ||
if !(mbr.info.loBound <:< mbr.info.hiBound) | ||
} | ||
yield new HasProblemBounds(name, mbr.info) | ||
|
||
def baseTypeProblems(base: Type) = base match { | ||
case AndType(base1, base2) => | ||
|
@@ -126,12 +144,13 @@ class CheckRealizable(implicit ctx: Context) { | |
val baseProblems = | ||
tp.baseClasses.map(_.baseTypeOf(tp)).flatMap(baseTypeProblems) | ||
|
||
(((Realizable: Realizability) | ||
/: mbrProblems)(_ andAlso _) | ||
((((Realizable: Realizability) | ||
/: memberProblems)(_ andAlso _) | ||
/: refinementProblems)(_ andAlso _) | ||
/: baseProblems)(_ andAlso _) | ||
} | ||
|
||
/** `Realizable` if all of `tp`'s non-struct fields have realizable types, | ||
/** `Realizable` if all of `tp`'s non-strict fields have realizable types, | ||
* a `HasProblemField` instance pointing to a bad field otherwise. | ||
*/ | ||
private def memberRealizability(tp: Type) = { | ||
|
This file contains 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 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,16 @@ | ||
object App { | ||
import scala.reflect.Selectable.reflectiveSelectable | ||
|
||
def coerce[U, V](u: U): V = { | ||
type X = { type R >: U } | ||
type Y = { type R = V } | ||
type Z = X & Y | ||
val u1: Z#R = u // error: not a legal path | ||
u1 | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
val x: Int = coerce[String, Int]("a") | ||
println(x + 1) | ||
} | ||
} |
This file contains 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,16 @@ | ||
object App { | ||
import scala.reflect.Selectable.reflectiveSelectable | ||
|
||
def coerce[U, V](u: U): V = { | ||
type X = { val x: { type R >: U } } | ||
type Y = { val x: { type R = V } } | ||
lazy val z: X & Y = z | ||
val u1: z.x.R = u // error: Object { type R >: U | V <: V } is not stable (with arrows under z.x) | ||
u1 | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
val x: Int = coerce[String, Int]("a") | ||
println(x + 1) | ||
} | ||
} |
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.
It seems that
A | B
would need a test? Confused what happens there with disjoint ranges.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.
refinedNames
only collects names that appear in a top-level refinement. So that's the same for & and |. The bounds checking is done 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.
I meant I don't see a testcase exercising this path.