From dde4a39a3d4e40e609dccf8adef944ac6a299625 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Sajn=C3=B3g?= <118359987+przemekiterators@users.noreply.github.com> Date: Tue, 1 Jul 2025 00:12:09 +0200 Subject: [PATCH] bugfix: Fix adjust type when already exists (#23455) Fixes: https://github.com/scalameta/metals/issues/6018 [Cherry-picked 3435ddbe39137ef9affe39638305bea0f98ebd2d] --- .../dotty/tools/pc/InferredTypeProvider.scala | 6 +- .../tests/edit/InsertInferredTypeSuite.scala | 64 +++++++++++++++++++ 2 files changed, 66 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 5cb85177927b..0ab1d264828e 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/InferredTypeProvider.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/InferredTypeProvider.scala @@ -183,8 +183,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( @@ -224,8 +223,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 a5d940612d66..e9ae9df9893c 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/edit/InsertInferredTypeSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/edit/InsertInferredTypeSuite.scala @@ -1035,6 +1035,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