Skip to content

Fix undercompilation when depending on inner class #9694

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, 2020

Conversation

smarter
Copy link
Member

@smarter smarter commented Sep 1, 2020

To communicate to sbt a dependency between a class currently being
compiled and some other class on the classpath, we use the
binaryDependency callback which requires passing the "binary class
name": for a class A in a package pkg, this is just "pkg.A", but for an
inner class B in A, that would be "pkg.A$B", in other words the last
component of the binary class name is the name of the corresponding
classfile.

Before this commit, we computed this name by extracting it from the path
of the associatedFile but it turns out that associatedFile for an
inner Scala class returns the classfile of the corresponding top-level
class! This happens because the compiler never actually reads inner
Scala classfiles since all the information is present in the top-level
.class and .tasty files. This means that under separate compilation a
dependency to an inner class was not recorded which could lead to
undercompilation (see added tests).

This commit fixes this by computing binary class names manually: they're
just made of the fullName of the enclosing package, followed by ".",
followed by the flatName of the current class. This is similar to what
is done in the Scala 2 compiler bridge in Zinc.

@bishabosha
Copy link
Member

bishabosha commented Sep 2, 2020

not sure what Is happening, sbt-dotty/scripted source-dependencies/inner-class is failing for me after a clean:

[info] [info] Compiling 1 Scala source to /private/var/folders/m4/4dsrk6zn74v3zzpk34nj11mh0000gn/T/sbt_e6a419e4/inner-class/project/target/scala-2.12/sbt-1.0/classes ...
[info] [info] Done compiling.
[info] [info] loading settings for project inner-class from build.sbt ...
[info] [info] set current project to inner-class (in build file:/private/var/folders/m4/4dsrk6zn74v3zzpk34nj11mh0000gn/T/sbt_e6a419e4/inner-class/)
[info] [info] Compiling 2 Scala sources to /private/var/folders/m4/4dsrk6zn74v3zzpk34nj11mh0000gn/T/sbt_e6a419e4/inner-class/target/scala-0.28/classes ...
[info] [info] Done compiling.
[info] [info] running B 
[info] 1
[info] [success] Total time: 3 s, completed Sep 2, 2020 2:32:47 PM
[info] [info] Compiling 1 Scala source to /private/var/folders/m4/4dsrk6zn74v3zzpk34nj11mh0000gn/T/sbt_e6a419e4/inner-class/target/scala-0.28/classes ...
[info] [info] Done compiling.
[info] [success] Total time: 0 s, completed Sep 2, 2020 2:32:47 PM
[info] [info] Compiling 1 Scala source to /private/var/folders/m4/4dsrk6zn74v3zzpk34nj11mh0000gn/T/sbt_e6a419e4/inner-class/target/scala-0.28/classes ...
[info] [info] Done compiling.
[info] [info] running B 
[info] [error] (run-main-1) java.lang.NoSuchMethodError: A$InnerClass.foo()I
[info] [error] java.lang.NoSuchMethodError: A$InnerClass.foo()I
[info] [error] 	at B$.main(B.scala:4)
[info] [error] 	at B.main(B.scala)
[info] [error] 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[info] [error] 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[info] [error] 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[info] [error] 	at java.lang.reflect.Method.invoke(Method.java:498)
[info] [error] stack trace is suppressed; run 'last Compile / bgRun' for the full output
[info] [error] Nonzero exit code: 1
[info] [error] (Compile / run) Nonzero exit code: 1
[info] [error] Total time: 0 s, completed Sep 2, 2020 2:32:47 PM

@smarter smarter force-pushed the fix-incremental-inner branch from bb2dee8 to 1d29924 Compare September 2, 2020 16:17
@smarter
Copy link
Member Author

smarter commented Sep 2, 2020

Oops, I broke it before pushing it, should be good now.

@smarter
Copy link
Member Author

smarter commented Sep 25, 2020

Ping for review.

@smarter smarter added this to the 3.0.0-M1 milestone Sep 28, 2020
@bishabosha
Copy link
Member

bishabosha commented Oct 15, 2020

when i run sbt-dotty/scripted, both source-dependencies/java-static and sbt-dotty/tasty-inspector-example-project fail

Copy link
Member

@bishabosha bishabosha left a comment

Choose a reason for hiding this comment

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

seems like static members of java classes are computed with a $ suffix on the binary class name

@smarter smarter force-pushed the fix-incremental-inner branch from 1d29924 to fe1781a Compare October 15, 2020 14:10
@smarter smarter marked this pull request as draft October 15, 2020 14:12
@bishabosha
Copy link
Member

seems a lot of PRs have hanging workflows

@smarter
Copy link
Member Author

smarter commented Oct 15, 2020

There's only one Windows CI runner currently, so windows jobs spend a long time in the queue.

@smarter smarter force-pushed the fix-incremental-inner branch from 0252e4b to fe1781a Compare October 15, 2020 15:24
@smarter smarter marked this pull request as ready for review October 15, 2020 15:24
@smarter
Copy link
Member Author

smarter commented Oct 15, 2020

There's only one Windows CI runner currently, so windows jobs spend a long time in the queue.

... or maybe the windows CI runner is not running currently? looking at https://github.com/lampepfl/dotty/actions I can't find any job where it's running right now /cc @liufengyun

@liufengyun
Copy link
Contributor

There's only one Windows CI runner currently, so windows jobs spend a long time in the queue.

... or maybe the windows CI runner is not running currently? looking at https://github.com/lampepfl/dotty/actions I can't find any job where it's running right now /cc @liufengyun

I'll try to add a new runner on the same machine and see if we get some speedup. Otherwise, we can either (1) run Windows CI only on master merged; or (2) add more physical machines.

@smarter smarter requested a review from bishabosha October 16, 2020 11:18
To communicate to sbt a dependency between a class currently being
compiled and some other class on the classpath, we use the
`binaryDependency` callback which requires passing the "binary class
name": for a class A in a package pkg, this is just "pkg.A", but for an
inner class B in A, that would be "pkg.A$B", in other words the last
component of the binary class name is the name of the corresponding
classfile.

Before this commit, we computed this name by extracting it from the path
of the `associatedFile` but it turns out that `associatedFile` for an
inner Scala class returns the classfile of the corresponding top-level
class! This happens because the compiler never actually reads inner
Scala classfiles since all the information is present in the top-level
.class and .tasty files. This means that under separate compilation a
dependency to an inner class was not recorded which could lead to
undercompilation (see added tests).

This commit fixes this by computing binary class names manually: they're
just made of the fullName of the enclosing package, followed by ".",
followed by the flatName of the current class. This is similar to what
is done in the Scala 2 compiler bridge in Zinc.
@smarter smarter force-pushed the fix-incremental-inner branch from fe1781a to 0e350d5 Compare October 16, 2020 13:38
@smarter smarter requested a review from bishabosha October 16, 2020 16:18
Copy link
Member

@bishabosha bishabosha left a comment

Choose a reason for hiding this comment

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

looks good to me

@smarter smarter merged commit c2abb49 into scala:master Oct 20, 2020
@smarter smarter deleted the fix-incremental-inner branch October 20, 2020 15:51
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.

3 participants