Skip to content

Commit 06b4395

Browse files
committed
Infer result type for vals, like we do for defs
The lack of result type inference caused pos/t6780 to fail in the new field encoding for traits, as there is no separate accessor, and method synthesis computes the type signature based on the ValDef tree. This caused a cyclic error in implicit search, because now the implicit val's result type was not inferred from the super member, and inferring it from the RHS would cause implicit search to consider the member in question, so that a cycle is detected and type checking fails... Regardless of the new encoding, we should consistently infer result types for `def`s and `val`s. Removed test/files/run/t4287inferredMethodTypes.scala and test/files/presentation/t4287c, since they were relying on inferring argument types from "overridden" constructors in a test for range positions of default arguments. Constructors don't override, so that was a mis-feature of -Yinfer-argument-types. Had to slightly refactor test/files/presentation/doc, as it was relying on scalac inferring a big intersection type to approximate the anonymous class that's instantiated for `override lazy val analyzer`. Now that we infer `Global` as the expected type based on the overridden val, we make `getComment` private in navigating between good old Skylla and Charybdis. I'm not sure why we need this restriction for anonymous classes though; only structural calls are restricted in the way that we're trying to avoid. The old behavior is maintained nder -Xsource:2.11. TODO: - test/files/neg/val_infer.scala - test/files/neg/val_sig_infer_match.scala - test/files/neg/val_sig_infer_struct.scala
1 parent 13ab959 commit 06b4395

16 files changed

+224
-221
lines changed

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

Lines changed: 171 additions & 140 deletions
Large diffs are not rendered by default.

test/files/neg/val_infer.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
val_infer.scala:3: error: type mismatch;
2+
found : String("")
3+
required: Int
4+
trait Sub extends Base { val foo = "" }
5+
^
6+
one error found

test/files/neg/val_infer.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Test {
2+
trait Base { def foo: Int }
3+
trait Sub extends Base { val foo = "" }
4+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class A
2+
3+
class B extends A {
4+
def y: Int = 0
5+
}
6+
7+
class B1 extends B
8+
class B2 extends B
9+
10+
class C {
11+
def f: A = null
12+
}
13+
14+
class D extends C {
15+
def s = ""
16+
override final val f = s match {
17+
case "" => new B1
18+
case _ => new B2
19+
}
20+
21+
def m = f.y // doesn't compile anymore
22+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class C {
2+
def f: Object = this
3+
}
4+
5+
class D extends C {
6+
override val f = new Object { def foo = 1 }
7+
def bar = f.foo
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
abstract class A { def foo(a: Int): A }
2+
class B extends A {
3+
implicit def spackle(x: Int): A = new B
4+
def foo(a) = a
5+
}

test/files/pos/val_infer.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Test {
2+
implicit def s2i(s: String): Int = s.length
3+
trait Base { def foo: Int }
4+
trait Sub extends Base { val foo = "" }
5+
}

test/files/presentation/doc/doc.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ object Test extends InteractiveTest {
3737
prepre + docComment(nTags) + prepost + post
3838
}
3939

40-
override lazy val compiler = {
40+
override lazy val compiler: Global { def getComment(sym: Symbol, source: SourceFile, fragments: List[(Symbol,SourceFile)]): Option[Comment] } = {
4141
prepareSettings(settings)
4242
new Global(settings, compilerReporter) with MemberLookupBase with CommentFactoryBase with doc.ScaladocGlobalTrait {
4343
outer =>

test/files/presentation/t4287c.check

Lines changed: 0 additions & 11 deletions
This file was deleted.

test/files/presentation/t4287c/Test.scala

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/files/presentation/t4287c/src/Foo.scala

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/files/run/showdecl.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ initialized y: lazy val y: Int
88
uninitialized z: def z: <?>
99
initialized z: def z: Int
1010
uninitialized t: def t: <?>
11-
initialized t: def t[T <: Int](x: D)(y: x.W): Int
11+
initialized t: def t[T <: <?>](x: D)(y: x.W): Int
1212
uninitialized W: type W = String
1313
initialized W: type W = String
1414
uninitialized C: class C extends

test/files/run/showdecl/Macros_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object Macros {
99
import c.universe._
1010
def test(sym: Symbol): Unit = {
1111
println(s"uninitialized ${sym.name}: ${showDecl(sym)}")
12-
sym.info
12+
sym.info // NOTE: not fullyInitializeSymbol, so some parts may still be LazyTypes
1313
println(s"initialized ${sym.name}: ${showDecl(sym)}")
1414
}
1515

test/files/run/t4287inferredMethodTypes.check

Lines changed: 0 additions & 30 deletions
This file was deleted.

test/files/run/t4287inferredMethodTypes.scala

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)