@@ -3269,6 +3269,28 @@ trait Typers extends Modes with Adaptations with Tags {
32693269 // calls to the default getters. Example:
32703270 // foo[Int](a)() ==> foo[Int](a)(b = foo$qual.foo$default$2[Int](a))
32713271 checkNotMacro()
3272+
3273+ // SI-8111 transformNamedApplication eagerly shuffles around the application to preserve
3274+ // evaluation order. During this process, it calls `changeOwner` on symbols that
3275+ // are transplanted underneath synthetic temporary vals.
3276+ //
3277+ // Here, we keep track of the symbols owned by `context.owner` to enable us to
3278+ // rollback, so that we don't end up with "orphaned" symbols.
3279+ //
3280+ // TODO: Find a better way!
3281+ //
3282+ // Note that duplicating trees would not be enough to fix this problem, we would also need to
3283+ // clone local symbols in the duplicated tree to truly isolate things (in the spirit of BodyDuplicator),
3284+ // or, better yet, disentangle the logic in `transformNamedApplication` so that we could
3285+ // determine whether names/defaults is viable *before* transforming trees.
3286+ def ownerOf (sym : Symbol ) = if (sym == null || sym == NoSymbol ) NoSymbol else sym.owner
3287+ val symsOwnedByContextOwner = tree.collect {
3288+ case t @ (_ : DefTree | _ : Function ) if ownerOf(t.symbol) == context.owner => t.symbol
3289+ }
3290+ def rollbackNamesDefaultsOwnerChanges () {
3291+ symsOwnedByContextOwner foreach (_.owner = context.owner)
3292+ }
3293+
32723294 val fun1 = transformNamedApplication(Typer .this , mode, pt)(fun, x => x)
32733295 if (fun1.isErroneous) duplErrTree
32743296 else {
@@ -3297,6 +3319,7 @@ trait Typers extends Modes with Adaptations with Tags {
32973319 if (! (context.diagnostic contains note)) context.diagnostic = note :: context.diagnostic
32983320 doTypedApply(tree, if (blockIsEmpty) fun else fun1, allArgs, mode, pt)
32993321 } else {
3322+ rollbackNamesDefaultsOwnerChanges()
33003323 tryTupleApply getOrElse duplErrorTree(NotEnoughArgsError (tree, fun, missing))
33013324 }
33023325 }
0 commit comments