-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #1442: add new phase, SelectStatic #1445
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
9 commits
Select commit
Hold shift + click to select a range
594e8dd
Fix #1442: add new phase, SelectStatic
DarkDimius c13a7ce
Test that #1442 is fixed.
DarkDimius 2ada4cc
Fix NoDenotation.owner in SelectStatic.
DarkDimius a1a35c9
Fix SelectStatic: do not lift java statics to free idents.
DarkDimius debc5fd
SelectStatic: do not create blocks that are qualifier of select\apply
DarkDimius 67bb2f7
SelectStatic: also normalise TypeApply nodes.
DarkDimius 92e13fe
SelectStatic: retain symbols on overloaded selects
DarkDimius 8fcbd0a
SelectStatic: do not promote types-qualifiers.
DarkDimius 376fa06
t4859: update the check file.
DarkDimius 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,60 @@ | ||
package dotty.tools.dotc | ||
package transform | ||
|
||
import dotty.tools.dotc.ast.Trees._ | ||
import dotty.tools.dotc.ast.tpd | ||
import dotty.tools.dotc.core.Contexts.Context | ||
import dotty.tools.dotc.core.DenotTransformers.IdentityDenotTransformer | ||
import dotty.tools.dotc.core.Flags._ | ||
import dotty.tools.dotc.core.Symbols._ | ||
import dotty.tools.dotc.core._ | ||
import dotty.tools.dotc.transform.TreeTransforms._ | ||
|
||
/** Removes selects that would be compiled into GetStatic | ||
* otherwise backend needs to be aware that some qualifiers need to be dropped. | ||
* Similar transformation seems to be performed by flatten in nsc | ||
* @author Dmytro Petrashko | ||
*/ | ||
class SelectStatic extends MiniPhaseTransform with IdentityDenotTransformer { thisTransform => | ||
import ast.tpd._ | ||
|
||
override def phaseName: String = "selectStatic" | ||
private val isPackage = FlagConjunction(PackageCreationFlags.bits) | ||
|
||
override def transformSelect(tree: tpd.Select)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = { | ||
val sym = tree.symbol | ||
val r1 = | ||
if (!sym.is(isPackage) && !sym.maybeOwner.is(isPackage) && | ||
( | ||
((sym is Flags.Module) && sym.maybeOwner.isStaticOwner) || | ||
(sym is Flags.JavaStatic) || | ||
(sym.maybeOwner is Flags.ImplClass) || | ||
sym.hasAnnotation(ctx.definitions.ScalaStaticAnnot) | ||
) | ||
) | ||
if (!tree.qualifier.symbol.is(JavaModule) && !tree.qualifier.isType) | ||
Block(List(tree.qualifier), ref(sym)) | ||
else tree | ||
else tree | ||
|
||
normalize(r1) | ||
} | ||
|
||
private def normalize(t: Tree)(implicit ctx: Context) = t match { | ||
case Select(Block(stats, qual), nm) => | ||
Block(stats, cpy.Select(t)(qual, nm)) | ||
case Apply(Block(stats, qual), nm) => | ||
Block(stats, Apply(qual, nm)) | ||
case TypeApply(Block(stats, qual), nm) => | ||
Block(stats, TypeApply(qual, nm)) | ||
case _ => t | ||
} | ||
|
||
override def transformApply(tree: tpd.Apply)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = { | ||
normalize(tree) | ||
} | ||
|
||
override def transformTypeApply(tree: tpd.TypeApply)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = { | ||
normalize(tree) | ||
} | ||
} |
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,24 @@ | ||
object Test1442 { | ||
final def sumMinimized[B](num: Numeric[B]): Int = { | ||
var cse: scala.math.Numeric.type = null.asInstanceOf[scala.math.Numeric.type] | ||
({cse = scala.math.Numeric; num eq cse.IntIsIntegral} || | ||
(num eq cse.DoubleAsIfIntegral)) | ||
2 | ||
} | ||
|
||
final def sum[B](implicit num: Numeric[B]): B = { | ||
// arithmetic series formula can be used for regular addition | ||
var cse: scala.math.Numeric.type = null.asInstanceOf[scala.math.Numeric.type] | ||
if ({cse = scala.math.Numeric; num eq cse.IntIsIntegral}|| | ||
(num eq cse.BigIntIsIntegral)|| | ||
(num eq cse.ShortIsIntegral)|| | ||
(num eq cse.ByteIsIntegral)|| | ||
(num eq cse.CharIsIntegral)|| | ||
(num eq cse.LongIsIntegral)|| | ||
(num eq cse.FloatAsIfIntegral)|| | ||
(num eq cse.BigDecimalIsFractional)|| | ||
(num eq cse.DoubleAsIfIntegral)) { | ||
null.asInstanceOf[B] | ||
} else null.asInstanceOf[B] | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
Outer | ||
Inner | ||
Inner.i | ||
About to reference Inner.i | ||
Outer | ||
Inner.i | ||
About to reference O.N | ||
About to reference O.N | ||
|
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.
Any particular reason not to copy
Apply
andTypeApply
as you do withSelect
?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.
Copying does not copy the tree here, as it always has a different structure.
cpy.Select can preserve the symbol though, in case method is overloaded. This is not needed for Apply\TypeApply as by the time they are applied overloads are already resolved.
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.
Yes, I asked because I ran into the overloading issues some time ago. Great!