diff --git a/presentation-compiler/src/main/dotty/tools/pc/InferredTypeProvider.scala b/presentation-compiler/src/main/dotty/tools/pc/InferredTypeProvider.scala index d019368c7ed6..af5a0e409d1a 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/InferredTypeProvider.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/InferredTypeProvider.scala @@ -182,8 +182,7 @@ final class InferredTypeProvider( typeNameEdit ::: imports rhs match - case t: Tree[?] - if t.typeOpt.isErroneous && retryType && !tpt.sourcePos.span.isZeroExtent => + case t: Tree[?] if !tpt.sourcePos.span.isZeroExtent => inferredTypeEdits( Some( AdjustTypeOpts( @@ -223,8 +222,7 @@ final class InferredTypeProvider( while i >= 0 && sourceText(i) != ':' do i -= 1 i rhs match - case t: Tree[?] - if t.typeOpt.isErroneous && retryType && !tpt.sourcePos.span.isZeroExtent => + case t: Tree[?] if !tpt.sourcePos.span.isZeroExtent => inferredTypeEdits( Some( AdjustTypeOpts( diff --git a/presentation-compiler/test/dotty/tools/pc/tests/edit/InsertInferredTypeSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/edit/InsertInferredTypeSuite.scala index fccdb13db0e8..e9060738db54 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/edit/InsertInferredTypeSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/edit/InsertInferredTypeSuite.scala @@ -1055,6 +1055,70 @@ class InsertInferredTypeSuite extends BaseCodeActionSuite: |""".stripMargin ) + @Test def `Adjust type for val` = + checkEdit( + """|object A{ + | val <>:String = 123 + |}""".stripMargin, + + """|object A{ + | val alpha: Int = 123 + |}""".stripMargin, + ) + + @Test def `Adjust type for val2` = + checkEdit( + """|object A{ + | val <>:Int = 123 + |}""".stripMargin, + """|object A{ + | val alpha: Int = 123 + |}""".stripMargin, + ) + + @Test def `Adjust type for val3` = + checkEdit( + """|object A{ + | val <>: Int = 123 + |}""".stripMargin, + """|object A{ + | val alpha: Int = 123 + |}""".stripMargin, + ) + + @Test def `Adjust type for def` = + checkEdit( + """|object A{ + | def <>:String = 123 + |}""".stripMargin, + + """|object A{ + | def alpha: Int = 123 + |}""".stripMargin, + ) + + @Test def `Adjust type for def2` = + checkEdit( + """|object A{ + | def <>:Int = 123 + |}""".stripMargin, + """|object A{ + | def alpha: Int = 123 + |}""".stripMargin, + ) + + + @Test def `Adjust type for def3` = + checkEdit( + """|object A{ + | def <>: Int = 123 + |}""".stripMargin, + """|object A{ + | def alpha: Int = 123 + |}""".stripMargin, + ) + + def checkEdit( original: String, expected: String