From e8aee47985a02fe384f5786f673e7cc5df2f30b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Sajn=C3=B3g?= Date: Mon, 30 Jun 2025 21:01:13 +0200 Subject: [PATCH 1/2] bugfix: Fix adjust type when already exists --- .../dotty/tools/pc/InferredTypeProvider.scala | 4 +- .../tests/edit/InsertInferredTypeSuite.scala | 64 +++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/presentation-compiler/src/main/dotty/tools/pc/InferredTypeProvider.scala b/presentation-compiler/src/main/dotty/tools/pc/InferredTypeProvider.scala index d019368c7ed6..e6a2d9eb46c6 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/InferredTypeProvider.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/InferredTypeProvider.scala @@ -183,7 +183,7 @@ final class InferredTypeProvider( rhs match case t: Tree[?] - if t.typeOpt.isErroneous && retryType && !tpt.sourcePos.span.isZeroExtent => + if !tpt.sourcePos.span.isZeroExtent => inferredTypeEdits( Some( AdjustTypeOpts( @@ -224,7 +224,7 @@ final class InferredTypeProvider( i rhs match case t: Tree[?] - if t.typeOpt.isErroneous && retryType && !tpt.sourcePos.span.isZeroExtent => + 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 From 0cd54fb92ac57e68516a43d3a7b0b69c4e3db840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Sajn=C3=B3g?= Date: Mon, 30 Jun 2025 21:39:10 +0200 Subject: [PATCH 2/2] bugfix: Fix adjust type when already exists vol2 --- .../src/main/dotty/tools/pc/InferredTypeProvider.scala | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/presentation-compiler/src/main/dotty/tools/pc/InferredTypeProvider.scala b/presentation-compiler/src/main/dotty/tools/pc/InferredTypeProvider.scala index e6a2d9eb46c6..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 !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 !tpt.sourcePos.span.isZeroExtent => + case t: Tree[?] if !tpt.sourcePos.span.isZeroExtent => inferredTypeEdits( Some( AdjustTypeOpts(