Skip to content

Fix #5107: disallow block as Apply.fun #5190

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
Oct 20, 2018
Merged

Conversation

liufengyun
Copy link
Contributor

Fix #5107: disallow block as Apply.fun

@smarter
Copy link
Member

smarter commented Oct 2, 2018

This should make part of https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/Splitter.scala unnecessary (it could be simplified even more if you also disallowed If trees as Apply.fun).

else readaptSimplified(tpd.Apply(tree, args))
else tree match {
case tree: Block =>
readaptSimplified(tpd.Block(tree.stats, tpd.Apply(tree.expr, args)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added an assert in tpd.Apply, it seems this can never happen.

@@ -300,6 +301,12 @@ class TreeChecker extends Phase with SymTransformer {
res
}

def checkApplyNonBlock(tree: untpd.Tree)(implicit ctx: Context) = tree match {
case tree: untpd.Apply => assert(!tree.fun.isInstanceOf[untpd.Block])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really lightweight check, so instead of doing in TreeChecker I would just do it in the constructor of Apply.

ta.assignType(untpd.Apply(fn, args), fn, args)
}

def TypeApply(fn: Tree, args: List[Tree])(implicit ctx: Context): TypeApply =
ta.assignType(untpd.TypeApply(fn, args), fn, args)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could try to do the same for TypeApply.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we could do that. I'm wondering how if/else could possibly appear in fun. But it is always good to have these invariants if they don't incur much performance cost.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will probably not be able to. You could add the assertion to make sure.

@liufengyun liufengyun force-pushed the fix-5107 branch 2 times, most recently from 8b60d82 to 34711c2 Compare October 2, 2018 11:38
@liufengyun
Copy link
Contributor Author

test performance please

@dottybot
Copy link
Member

dottybot commented Oct 2, 2018

performance test scheduled: 1 job(s) in queue, 0 running.

@dottybot
Copy link
Member

dottybot commented Oct 2, 2018

Performance test finished successfully:

Visit http://dotty-bench.epfl.ch/5190/ to see the changes.

Benchmarks is based on merging with master (2b73f9d)

@liufengyun
Copy link
Contributor Author

test performance please

@dottybot
Copy link
Member

dottybot commented Oct 2, 2018

performance test scheduled: 4 job(s) in queue, 1 running.

@dottybot
Copy link
Member

dottybot commented Oct 2, 2018

Performance test finished successfully:

Visit http://dotty-bench.epfl.ch/5190/ to see the changes.

Benchmarks is based on merging with master (e4735d2)

@liufengyun liufengyun assigned odersky and unassigned allanrenucci Oct 8, 2018
@liufengyun liufengyun requested review from odersky and removed request for allanrenucci October 8, 2018 19:39
Copy link
Contributor

@nicolasstucki nicolasstucki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise LGTM

ta.assignType(untpd.Apply(fn, args), fn, args)
}

def TypeApply(fn: Tree, args: List[Tree])(implicit ctx: Context): TypeApply =
ta.assignType(untpd.TypeApply(fn, args), fn, args)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will probably not be able to. You could add the assertion to make sure.

@@ -69,8 +69,7 @@ class Compiler {
new ExplicitOuter, // Add accessors to outer classes from nested ones.
new ExplicitSelf, // Make references to non-trivial self types explicit as casts
new StringInterpolatorOpt, // Optimizes raw and s string interpolators by rewriting them to string concatentations
new CrossCastAnd, // Normalize selections involving intersection types.
new Splitter) :: // Expand selections involving union types into conditionals
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The splitter source file was nod removed

@@ -40,8 +40,11 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
def Super(qual: Tree, mixName: TypeName, inConstrCall: Boolean, mixinClass: Symbol = NoSymbol)(implicit ctx: Context): Super =
Super(qual, if (mixName.isEmpty) untpd.EmptyTypeIdent else untpd.Ident(mixName), inConstrCall, mixinClass)

def Apply(fn: Tree, args: List[Tree])(implicit ctx: Context): Apply =
def Apply(fn: Tree, args: List[Tree])(implicit ctx: Context): Apply = {
assert(!fn.isInstanceOf[Block])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion, but you could be even more radical and assert what fn can be instead of what it cannot be, is assert(fn.isInstanceOf[RefTree] || fn.isInstanceOf[GenericApply]) enough ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for TypeApply.

@liufengyun liufengyun force-pushed the fix-5107 branch 2 times, most recently from 5994cca to 28c2cba Compare October 19, 2018 06:53
@liufengyun
Copy link
Contributor Author

Thanks @nicolasstucki @smarter . I just updated the PR, could you have a look again?


def TypeApply(fn: Tree, args: List[Tree])(implicit ctx: Context): TypeApply =
def TypeApply(fn: Tree, args: List[Tree])(implicit ctx: Context): TypeApply = {
assert(fn.isInstanceOf[RefTree] || fn.isInstanceOf[GenericApply[_]])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have nested TypeApply? Should we check fn.isInstanceOf[Apply[_]] instead of fn.isInstanceOf[GenericApply[_]]?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With named type arguments yes I think so foo[A=Int][String]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... also with curried type lambdas.

@@ -17,7 +17,7 @@ import vulpix.TestConfiguration
class PatmatExhaustivityTest {
val testsDir = "tests/patmat"
// stop-after: patmatexhaust-huge.scala crash compiler
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it still crash the compiler with the new labelled blocks ?

Copy link
Contributor Author

@liufengyun liufengyun Oct 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tried locally, it works now.

Edit: CI not happy, reverted.

@liufengyun liufengyun force-pushed the fix-5107 branch 2 times, most recently from 863c058 to 756c7ea Compare October 19, 2018 11:38
@odersky odersky merged commit 661ed61 into scala:master Oct 20, 2018
@liufengyun liufengyun deleted the fix-5107 branch October 20, 2018 08:00
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.

6 participants