Skip to content

Avoid typechecking val and def tpt-s twice #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/compiler/scala/tools/nsc/typechecker/Namers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,11 @@ trait Namers extends MethodSynthesis {

val resTpGiven =
if (tpt.isEmpty) WildcardType
else typer.typedType(tpt).tpe
else {
val tp = typer.typedType(tpt).tpe
ddef.tpt setType tp
tp
}


// ignore missing types unless we can look to overridden method to recover the missing information
Expand Down Expand Up @@ -1429,7 +1433,9 @@ trait Namers extends MethodSynthesis {
// trait T { def f: A }; class C extends T { implicit def b2a(t: B): A = ???; def f = new B }
val resTpComputedUnlessGiven =
if (tpt.isEmpty) assignTypeToTree(ddef, typer, resTpFromOverride)
else resTpGiven
else {
resTpGiven
}

// #2382: return type of default getters are always @uncheckedVariance
if (meth.hasDefault) resTpComputedUnlessGiven.withAnnotation(AnnotationInfo(uncheckedVarianceClass.tpe, List(), List()))
Expand Down Expand Up @@ -1715,7 +1721,11 @@ trait Namers extends MethodSynthesis {

tptFromRhsUnderPt
}
} else typer.typedType(tpt).tpe
} else {
val tp = typer.typedType(tpt).tpe
vdef.tpt setType tp
tp
}

// println(s"val: $result / ${vdef.tpt.tpe} / ")

Expand Down
10 changes: 5 additions & 5 deletions test/junit/scala/reflect/internal/PrintersTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class BasePrintTest {
|}""",
typedCode = sm"""
|{
| class foo[t](x: scala.Int);
| class foo[t](x: Int);
| {
| final class $$anon extends foo[scala.Predef.String](3) {
| ()
Expand Down Expand Up @@ -327,12 +327,12 @@ class BasePrintTest {
@Test def testFunc1 = assertResultCode(
code = "List(1, 2, 3).map((i: Int) => i - 1)")(
parsedCode = "List(1, 2, 3).map(((i: Int) => i.-(1)))",
typedCode = sm"scala.collection.immutable.List.apply[Int](1, 2, 3).map[Int, List[Int]](((i: scala.Int) => i.-(1)))(scala.collection.immutable.List.canBuildFrom[Int])")
typedCode = sm"scala.collection.immutable.List.apply[Int](1, 2, 3).map[Int, List[Int]](((i: Int) => i.-(1)))(scala.collection.immutable.List.canBuildFrom[Int])")

@Test def testFunc2 = assertResultCode(
code = "val sum: Seq[Int] => Int = _ reduceLeft (_+_)")(
parsedCode = "val sum: _root_.scala.Function1[Seq[Int], Int] = ((x$1) => x$1.reduceLeft(((x$2, x$3) => x$2.+(x$3))))",
typedCode = "val sum: _root_.scala.Function1[scala.`package`.Seq[scala.Int], scala.Int] = ((x$1: Seq[Int]) => x$1.reduceLeft[Int](((x$2: Int, x$3: Int) => x$2.+(x$3))))")
typedCode = "val sum: _root_.scala.Function1[Seq[Int], Int] = ((x$1: Seq[Int]) => x$1.reduceLeft[Int](((x$2: Int, x$3: Int) => x$2.+(x$3))))")

@Test def testFunc3 = assertResultCode(
code = "List(1, 2, 3) map (_ - 1)")(
Expand All @@ -342,7 +342,7 @@ class BasePrintTest {
@Test def testFunc4 = assertResultCode(
code = "val x: String => Int = ((str: String) => 1)")(
parsedCode = "val x: _root_.scala.Function1[String, Int] = ((str: String) => 1)",
typedCode = " val x: _root_.scala.Function1[_root_.scala.Predef.String, _root_.scala.Int] = ((str: _root_.scala.Predef.String) => 1)", printRoot = true)
typedCode = " val x: _root_.scala.Function1[String, Int] = ((str: String) => 1)", printRoot = true)

@Test def testAssign1 = assertPrintedCode("(f.v = 5).toString", checkTypedTree = false)

Expand Down Expand Up @@ -832,7 +832,7 @@ class ClassPrintTest {
typedCode = sm"""
|object Test {
| import java.io._;
| var file: java.io.PrintStream = null;
| var file: PrintStream = null;
| try {
| val out = new java.io.FileOutputStream("myfile.txt");
| Test.this.`file_=`(new java.io.PrintStream(out))
Expand Down