You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Handle symbol.defTree properly for nested definitions in bindings
Nested definitions in inlining bindings will get new symbols in
`changeOwner`. The field `Symbol.defTree` is never set for those symbols.
The problem is exhibited in the following test
tests/pos/i10542.scala
The fix in 0f034aa works by accident:
```
@@ -115,7 +115,9 @@ class ReTyper extends Typer with ReChecking {
}
override def typedUnadapted(tree: untpd.Tree, pt: Type, locked: TypeVars)(using Context): Tree =
try super.typedUnadapted(tree, pt, locked)
try super.typedUnadapted(tree, pt, locked) match
case member: MemberDef => member.setDefTree
case tree => tree
```
The reason is the following:
- If we enable `-Ycheck:all`, it will sync the tree definition for
all symbols during TreeChecker, thus no crash in initialization
checker.
- if we disable `-Ycheck:all`, the `defTree` for `<init>` is not set,
initialization checker ignores the method, thus no crash.
However, if we change `InlineTyper` instead of `Retyper`:
```
+ override def typedUnadapted(tree: untpd.Tree, pt: Type, locked: TypeVars)(using Context): Tree =
+ super.typedUnadapted(tree, pt, locked) match
+ case member: MemberDef => member.setDefTree
+ case tree => tree
```
There will be some mysterious behavior in compiling `tests/pos/i10542.scala`:
- Without `-Ycheck:all`, it succeeds.
- With `-Ycheck:all`, the initialization check crashes due to missing
`defTree` for the class `$anon`.
The reason for the mysterious behavior is the following:
- Without `-Ycheck:all`, there is no defTree for `<init>`,
initialization check is skipped, thus no crash.
- With `-Ycheck:all`, the method `Typer.typedDefDef` will set
`Symbol.defTree` for `<init>`, called from the TreeChecker, which
extends `Typer` indirectly via `ReTyper`. However, in
`Typer.typedClassDef`, we never set `defTree` for class symbols,
thus the `defTree` for the class symbol `$anon` is empty, causing an
exception in initialization check.
Co-authored-by: Nicolas Stucki <[email protected]>
0 commit comments