-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #9970: Reconcile how Scala 3 and 2 decide whether a trait has $init$. #10530
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import scala.quoted._ | ||
import scala.tasty.inspector._ | ||
|
||
/* Test that the constructor of a trait has the StableRealizable trait if and | ||
* only if the trait has NoInits. This is used by the TASTy reader in Scala 2 | ||
* to determine whether the constructor should be kept or discarded, and | ||
* consequently whether calls to $init$ should be emitted for the trait or not. | ||
*/ | ||
|
||
// Definitions to be inspected | ||
|
||
trait I9970IOApp { | ||
protected val foo = 23 | ||
|
||
def run(args: List[String]): Int | ||
|
||
final def main(args: Array[String]): Unit = { | ||
sys.exit(run(args.toList)) | ||
} | ||
} | ||
|
||
object I9970IOApp { | ||
trait Simple extends I9970IOApp { | ||
def run: Unit | ||
|
||
final def run(args: List[String]): Int = { | ||
run | ||
0 | ||
} | ||
} | ||
} | ||
|
||
// TASTy Inspector test boilerplate | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
// Artefact of the current test infrastructure | ||
// TODO improve infrastructure to avoid needing this code on each test | ||
val classpath = dotty.tools.dotc.util.ClasspathFromClassloader(this.getClass.getClassLoader).split(java.io.File.pathSeparator).find(_.contains("runWithCompiler")).get | ||
val allTastyFiles = dotty.tools.io.Path(classpath).walkFilter(_.extension == "tasty").map(_.toString).toList | ||
val tastyFiles = allTastyFiles.filter(_.contains("I9970")) | ||
|
||
new TestInspector().inspectTastyFiles(tastyFiles) | ||
} | ||
} | ||
|
||
// Inspector that performs the actual tests | ||
|
||
class TestInspector() extends TastyInspector: | ||
|
||
private var foundIOApp: Boolean = false | ||
private var foundSimple: Boolean = false | ||
|
||
protected def processCompilationUnit(using Quotes)(root: quotes.reflect.Tree): Unit = | ||
foundIOApp = false | ||
foundSimple = false | ||
inspectClass(root) | ||
// Sanity check to make sure that our pattern matches are not simply glossing over the things we want to test | ||
assert(foundIOApp, "the inspector did not encounter IOApp") | ||
assert(foundSimple, "the inspect did not encounter IOApp.Simple") | ||
|
||
private def inspectClass(using Quotes)(tree: quotes.reflect.Tree): Unit = | ||
import quotes.reflect._ | ||
tree match | ||
case t: PackageClause => | ||
t.stats.foreach(inspectClass(_)) | ||
|
||
case t: ClassDef if t.name.endsWith("$") => | ||
t.body.foreach(inspectClass(_)) | ||
|
||
case t: ClassDef => | ||
t.name match | ||
case "I9970IOApp" => | ||
foundIOApp = true | ||
// Cannot test the following because NoInits is not part of the quotes API | ||
//assert(!t.symbol.flags.is(Flags.NoInits)) | ||
assert(!t.constructor.symbol.flags.is(Flags.StableRealizable)) | ||
|
||
case "Simple" => | ||
foundSimple = true | ||
// Cannot test the following because NoInits is not part of the quotes API | ||
//assert(t.symbol.flags.is(Flags.NoInits)) | ||
assert(t.constructor.symbol.flags.is(Flags.StableRealizable)) | ||
|
||
case _ => | ||
assert(false, s"unexpected ClassDef '${t.name}'") | ||
|
||
case _ => | ||
end inspectClass |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.