scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

Scala does not see type method annotations in macros #9755

Open scabug opened 8 years ago

scabug commented 8 years ago

I faced pretty strange annotations-related behavior (bug?) with scala macro. Its pretty hard to describe it in one message so I decided to create a sample repo to show it.

https://github.com/akozhemiakin/sandbox-one

In brief, here is the test itself:

package sandbox

import org.scalatest.FunSuite
import sandbox.sub.B

class SandboxSpec extends FunSuite {
  class A[T](el: T)

  test("Test 1") {
    // Here we use simple macro to return number of annotations attached to the "foo" method of the A type arg
    assert(Macro.annosOnFoo(new A(new C)) == 1) // Will succeed
  }

  test("Test 2") {
    // Exactly the same case as above. The only difference: B is not in the same package as A and this test
    assert(Macro.annosOnFoo(new A(new B)) == 1) // Will fail
  }
}

So the problem is, that in the second case scala does not see any annotations attached to the target method.

I did my best to find some similar issue but did not succeed, hope it is not a duplicate.

scabug commented 8 years ago

Imported From: https://issues.scala-lang.org/browse/SI-9755?orig=1 Reporter: Artyom Kozhemiakin (akozhemiakin) Affected Versions: 2.11.8, 2.12.0-M3

scabug commented 8 years ago

@som-snytt said: In sbt, it fails after clean, then succeeds subsequently (on editing the test file). I see the same behavior using a Java annotation or ClassfileAnnotation, and also after putting the macro and test in different subprojects.

> clean
[success] Total time: 0 s, completed Apr 15, 2016 9:51:14 AM
> ~ test
[info] Updating {file:/home/apm/tmp/sandbox-one/}sandbox...
[info] Resolving jline#jline;2.12.1 ...
[info] Done updating.
[info] Compiling 1 Scala source to /home/apm/tmp/sandbox-one/target/scala-2.11/classes...
[info] Compiling 4 Scala sources and 1 Java source to /home/apm/tmp/sandbox-one/target/scala-2.11/test-classes...
[info] SandboxSpec:
[info] - Test 1
[info] - Test 2 *** FAILED ***
[info]   0 did not equal 1 (SandboxSpec.scala:16)
[info] Run completed in 249 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 1, canceled 0, ignored 0, pending 0
[info] *** 1 TEST FAILED ***
[error] Failed tests:
[error]     sandbox.SandboxSpec
[error] (test:test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 6 s, completed Apr 15, 2016 9:51:24 AM
1. Waiting for source changes... (press enter to interrupt)
[info] Compiling 1 Scala source to /home/apm/tmp/sandbox-one/target/scala-2.11/test-classes...
[info] SandboxSpec:
[info] - Test 1
[info] - Test 2
[info] - Dummy
[info] Run completed in 137 milliseconds.
[info] Total number of tests run: 3
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 3, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 2 s, completed Apr 15, 2016 9:51:33 AM
scabug commented 8 years ago

Artyom Kozhemiakin (akozhemiakin) said: Yeah, I faced very similar thing while debugging. From time to time it may be compiled successfully without any obvious reason but then fails again.

scabug commented 8 years ago

@som-snytt said: Worked for me today by switching the order of the tests. annotations and imports have unusual completion needs. Maybe the import is a clue.