-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #444: be more conservative on when Block and If types should be recomputed #5958
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -572,15 +572,25 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo { | |
override def Block(tree: Tree)(stats: List[Tree], expr: Tree)(implicit ctx: Context): Block = { | ||
val tree1 = untpdCpy.Block(tree)(stats, expr) | ||
tree match { | ||
case tree: Block if expr.tpe eq tree.expr.tpe => tree1.withTypeUnchecked(tree.tpe) | ||
case tree: Block if (expr.tpe eq tree.expr.tpe) && (expr.tpe eq tree.tpe) => | ||
// the second guard is needed in case avoid somehow widened the type. | ||
// if it did it could potentially need to rewiden it | ||
// eg {val s = ...; s} | ||
// changing type of s should change type of block, though type of expr is unchanged - TermRef(s) | ||
tree1.withTypeUnchecked(tree.tpe) | ||
case _ => ta.assignType(tree1, stats, expr) | ||
} | ||
} | ||
|
||
override def If(tree: Tree)(cond: Tree, thenp: Tree, elsep: Tree)(implicit ctx: Context): If = { | ||
val tree1 = untpdCpy.If(tree)(cond, thenp, elsep) | ||
tree match { | ||
case tree: If if (thenp.tpe eq tree.thenp.tpe) && (elsep.tpe eq tree.elsep.tpe) => tree1.withTypeUnchecked(tree.tpe) | ||
case tree: If if (thenp.tpe eq tree.thenp.tpe) && (elsep.tpe eq tree.elsep.tpe) && | ||
((tree.tpe eq thenp.tpe) || (tree.tpe eq elsep.tpe)) => | ||
// last guard is needed in case previous if had computed a widened ORType that needs to be recomputed | ||
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. New comment: // The last guard is a conservative check similar to the one done in `Block` above,
// if `tree.tpe` is not identical to the type of one of its branch, it might have been
// computed from the widened type of the branches, so the same reasoning than
// in `Block` applies. |
||
// eg {val a = ...; val b = ...; if(...) a else b} | ||
// changing type of a or b should change type of if, though types of both trees remain unchanged | ||
tree1.withTypeUnchecked(tree.tpe) | ||
case _ => ta.assignType(tree1, thenp, elsep) | ||
} | ||
} | ||
|
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.
More detailed comment: