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

Annotation sometimes not processed on anonymous classes #7203

Closed
scabug opened this issue Mar 2, 2013 · 2 comments
Closed

Annotation sometimes not processed on anonymous classes #7203

scabug opened this issue Mar 2, 2013 · 2 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Mar 2, 2013

I ran across this problem when the compiler failed to process a @tailrec annotation on a method inside an anonymous Runnable class. I hadn't imported @tailrec and I got no compile-time warning about this at all - just a StackOverflowException at runtime.

The Runnable was created like so:

new Thread(new Runnable { ... @tailrec ... }).start()

However I eventually realised the problem and found that I get a proper compile error when I rearranged the code like so.

val t = new Thread(new Runnable { ... @tailrec ... })
t.start()

According to my investigation calling new Thread(...).start() means that annotations aren't processed. Note that the parens on the start() seem to be quite important for some reason.

Simplified test case below.

// CORRECT - complains about annotation
// "not found: type nosuchannotation"
// donothing has parens, returns unit, but not called
object Example1 {
  class Wrapper(x: AnyRef) {
    def donothing {}
  }

  new Wrapper(new AnyRef {
    @nosuchannotation def method() {}
  })
}

// CORRECT - complains about annotation
// "not found: type nosuchannotation"
// donothing doesn't have parens, returns unit, called without parens
object Example2 {
  class Wrapper(x: AnyRef) {
    def donothing {}
  }

  new Wrapper(new AnyRef {
    @nosuchannotation def method() {}
  }).donothing
}

// CORRECT - complains about annotation
// "not found: type nosuchannotation"
// donothing has parens, returns unit, called without parens
object Example3 {
  class Wrapper(x: AnyRef) {
    def donothing() {}
  }

  new Wrapper(new AnyRef {
    @nosuchannotation def method() {}
  }).donothing
}

// INCORRECT - compiles without complaint
// donothing has parens, returns unit, called with parens
object Example4 {
  class Wrapper(x: AnyRef) {
    def donothing() {}
  }

  new Wrapper(new AnyRef {
    @nosuchannotation def method() {}
  }).donothing()
}

// INCORRECT - compiles without complaint
// donothing has parens, returns int, called with parens
object Example5 {
  class Wrapper(x: AnyRef) {
    def donothing(): Int = 1
  }

  new Wrapper(new AnyRef {
    @nosuchannotation def method() {}
  }).donothing()
}
@scabug
Copy link
Author

scabug commented Mar 2, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7203?orig=1
Reporter: @richdougherty
Affected Versions: 2.10.0
Duplicates #6558

@scabug
Copy link
Author

scabug commented Mar 2, 2013

@retronym said:
Duplicate of #6558.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants