@@ -3326,6 +3326,28 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
33263326 // calls to the default getters. Example:
33273327 // foo[Int](a)() ==> foo[Int](a)(b = foo$qual.foo$default$2[Int](a))
33283328 checkNotMacro()
3329+
3330+ // SI-8111 transformNamedApplication eagerly shuffles around the application to preserve
3331+ // evaluation order. During this process, it calls `changeOwner` on symbols that
3332+ // are transplanted underneath synthetic temporary vals.
3333+ //
3334+ // Here, we keep track of the symbols owned by `context.owner` to enable us to
3335+ // rollback, so that we don't end up with "orphaned" symbols.
3336+ //
3337+ // TODO: Find a better way!
3338+ //
3339+ // Note that duplicating trees would not be enough to fix this problem, we would also need to
3340+ // clone local symbols in the duplicated tree to truly isolate things (in the spirit of BodyDuplicator),
3341+ // or, better yet, disentangle the logic in `transformNamedApplication` so that we could
3342+ // determine whether names/defaults is viable *before* transforming trees.
3343+ def ownerOf (sym : Symbol ) = if (sym == null || sym == NoSymbol ) NoSymbol else sym.owner
3344+ val symsOwnedByContextOwner = tree.collect {
3345+ case t @ (_ : DefTree | _ : Function ) if ownerOf(t.symbol) == context.owner => t.symbol
3346+ }
3347+ def rollbackNamesDefaultsOwnerChanges () {
3348+ symsOwnedByContextOwner foreach (_.owner = context.owner)
3349+ }
3350+
33293351 val fun1 = transformNamedApplication(Typer .this , mode, pt)(fun, x => x)
33303352 if (fun1.isErroneous) duplErrTree
33313353 else {
@@ -3354,6 +3376,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
33543376 if (! (context.diagnostic contains note)) context.diagnostic = note :: context.diagnostic
33553377 doTypedApply(tree, if (blockIsEmpty) fun else fun1, allArgs, mode, pt)
33563378 } else {
3379+ rollbackNamesDefaultsOwnerChanges()
33573380 tryTupleApply orElse duplErrorTree(NotEnoughArgsError (tree, fun, missing))
33583381 }
33593382 }
0 commit comments