Commit 8ab5f00
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 <nicolas.stucki@gmail.com>1 parent ce30a1c commit 8ab5f00
File tree
2 files changed
+13
-6
lines changed- compiler/src/dotty/tools/dotc/typer
2 files changed
+13
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
464 | 464 | | |
465 | 465 | | |
466 | 466 | | |
467 | | - | |
| 467 | + | |
468 | 468 | | |
469 | 469 | | |
470 | 470 | | |
| |||
521 | 521 | | |
522 | 522 | | |
523 | 523 | | |
524 | | - | |
| 524 | + | |
525 | 525 | | |
526 | 526 | | |
527 | 527 | | |
| |||
797 | 797 | | |
798 | 798 | | |
799 | 799 | | |
800 | | - | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
801 | 806 | | |
802 | 807 | | |
803 | 808 | | |
| |||
1382 | 1387 | | |
1383 | 1388 | | |
1384 | 1389 | | |
| 1390 | + | |
| 1391 | + | |
| 1392 | + | |
| 1393 | + | |
1385 | 1394 | | |
1386 | 1395 | | |
1387 | 1396 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
118 | | - | |
119 | | - | |
120 | | - | |
| 118 | + | |
121 | 119 | | |
122 | 120 | | |
123 | 121 | | |
| |||
0 commit comments