Skip to content

Commit

Permalink
Fix condition in prefixIsElidable to prevent compiler crash (#18924)
Browse files Browse the repository at this point in the history
Fix #18901 

The check in `prefixIsElidable` was defined as follows:
```scala
tp.symbol.isParamOrAccessor && !pre.cls.is(Trait) && ctx.owner.enclosingClass == pre.cls
```

I assume that `!pre.cls.is(Trait)` condition was introduced to
accommodate for `Mixin` phase getting rid of `ParamAccessor` defined in
traits. However, the prefix does not indicate where the symbol is really
defined - it only represents the prefix from the perspective of the
current template, so it could be inherited. When it's inherited from a
trait, the prefix would be the current class, but the member still is
defined in the trait, and `Mixin` would get rid of the `ParamAccessor`
flag. Therefore, I changed this condition to the following:

```scala
tp.symbol.isParamOrAccesso && !pre.cls.is(Trait) && !tp.symbol.owner.is(Trait) && ctx.owner.enclosingClass == pre.cls
```
  • Loading branch information
sjrd authored Nov 17, 2023
2 parents 50498d8 + 01a37ec commit 772be76
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/tpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
case pre: ThisType =>
tp.isType ||
pre.cls.isStaticOwner ||
tp.symbol.isParamOrAccessor && !pre.cls.is(Trait) && ctx.owner.enclosingClass == pre.cls
tp.symbol.isParamOrAccessor && !pre.cls.is(Trait) && !tp.symbol.owner.is(Trait) && ctx.owner.enclosingClass == pre.cls
// was ctx.owner.enclosingClass.derivesFrom(pre.cls) which was not tight enough
// and was spuriously triggered in case inner class would inherit from outer one
// eg anonymous TypeMap inside TypeMap.andThen
Expand Down
5 changes: 5 additions & 0 deletions tests/pos/i18091.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
trait B(val y: Int)

class C extends B(20) {
def foo(): Unit = println(y)
}

0 comments on commit 772be76

Please sign in to comment.