Skip to content
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

Compiler crash on function-return style currying #150

Open
Checkroth opened this issue Aug 8, 2016 · 2 comments
Open

Compiler crash on function-return style currying #150

Checkroth opened this issue Aug 8, 2016 · 2 comments

Comments

@Checkroth
Copy link

Mock testing in Specs2, came across a compiler crash that is easily reproducible.

Mock testing works on curried functions in this style:
def func(arg1: A)(arg2: A): A

But it blows up when using the function return style:
def func(arg1: A): A => A

I have reproduced it in a very simple test, using the exact format my actual code contained:

trait IntFunctions {
    def cf(i: Int): Int => Int
  }

  class Implemented extends IntFunctions {
    override def cf(i: Int) = i2 => {
      i + i2
    }
  }

  "Test for github" >> new MockContext {
    val additionFunc = mock[IntFunctions]

    (additionFunc.cf(_: Int)(_: Int)).expects(*, *)

    val firstStep = additionFunc.cf(1)
    firstStep(2)
  }

Changing the final bit of the test to additionFunc.cf(1)(2) results in the exact same issue.

Here is the compiler crash (snippet):

[error] 
[error]   last tree to typer: This(<$anon: org#15.scalamock#12204.specs2#13246.MockContext#13286>)
[error]        tree position: line 26 of test.scala
[error]             tree tpe: test#8865.anon$1#90473
[error]               symbol: final <$anon: org#15.scalamock#12204.specs2#13246.MockContext#13286> in package tests#8865
[error]    symbol definition: final class anon$1#90473 extends org#15.scalamock#12204.specs2#13246.MockContext#13286 (a ClassSymbol)
[error]       symbol package: tests
[error]        symbol owners: <$anon: org#15.scalamock#12204.specs2#13246.MockContext#13286>
[error]            call site: <$anon: Function0#2089> in package tests#8865 in package tests#8864
[error] 
[error] == Source file context for tree position ==
[error] 
[error]     23   }
[error]     24 
[error]     25   "Test for github" >> new MockContext {
[error]     26     val additionFunc = mock[IntFunctions]
[error]     27 
[error]     28     (additionFunc.cf(_: Int)(_: Int)).expects(*, *)
[error]     29 
[error] Total time: 3 s, completed Aug 8, 2016 4:59:38 PM

But if I change the implementation to what we see in the documentation:

trait IntFunctions {
    def cf(i: Int)(i2: Int) : Int
  }

  class Implemented extends IntFunctions {
    override def cf(i: Int)(i2: Int) = {
      i + i2
    }
  }

  "Test for github" >> new MockContext {
    val additionFunc = mock[IntFunctions]

    (additionFunc.cf(_: Int)(_: Int)).expects(*, *)

    val firstStep = additionFunc.cf(1) _
    firstStep(2)
  }

Then we get a successful test case
[success] Total time: 7 s, completed Aug 8, 2016 5:15:02 PM

@Checkroth
Copy link
Author

Corresponding PR adding a test for this case:

#151

@barkhorn
Copy link
Collaborator

barkhorn commented Dec 6, 2016

able to reproduce this on 3.4.2, but I feel I lack the knowledge of macros to fix this at the moment.

this test compiles and succeeds:

    val additionFunc = mock[IntFunctions]
    val curried = mock[Int => Int]

    (additionFunc.cf(_: Int)).expects(*).returning(curried)
    (curried.apply _).expects(*).returning(5)

    val firstStep = additionFunc.cf(1)
    val r = firstStep(2)
    r === 5

leaving this in triage for now until I understand it better or someone else wants to submit a PR

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

No branches or pull requests

2 participants