Skip to content

Commit ab61fed

Browse files
committed
Ensure trait var accessor type is widened
If we don't widen, we'll fail to find the setter when typing `x = 42`, because `x` is constant-folded to `0`, as its type is `=> Int(0)`. After widening, `x` is type checked to `x` and its symbol is the getter in the trait, which can then be rewritten to the setter. Regression spotted and test case by szeiger.
1 parent 3304bc3 commit ab61fed

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/compiler/scala/tools/nsc/typechecker/Namers.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,8 @@ trait Namers extends MethodSynthesis {
952952
!tpe.typeSymbolDirect.isModuleClass // Infer Foo.type instead of "object Foo"
953953
&& (tpe.widen <:< pt) // Don't widen our way out of conforming to pt
954954
&& ( sym.isVariable
955-
|| sym.isMethod && !sym.hasAccessorFlag
955+
|| sym.hasFlag(ACCESSOR) && !sym.hasFlag(STABLE)
956+
|| sym.isMethod && !sym.hasFlag(ACCESSOR)
956957
|| isHidden(tpe)
957958
)
958959
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// check that the var below is assigned Int, and that we can assign to it
2+
trait C { protected final var x = 0; x = 42 }

0 commit comments

Comments
 (0)