Skip to content

Commit 5cc3e37

Browse files
authored
Merge pull request #480 from scala/backport-lts-3.3-23455
Backport "bugfix: Fix adjust type when already exists" to 3.3 LTS
2 parents 0267968 + dde4a39 commit 5cc3e37

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

presentation-compiler/src/main/dotty/tools/pc/InferredTypeProvider.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,7 @@ final class InferredTypeProvider(
183183
typeNameEdit ::: imports
184184

185185
rhs match
186-
case t: Tree[?]
187-
if t.typeOpt.isErroneous && retryType && !tpt.sourcePos.span.isZeroExtent =>
186+
case t: Tree[?] if !tpt.sourcePos.span.isZeroExtent =>
188187
inferredTypeEdits(
189188
Some(
190189
AdjustTypeOpts(
@@ -224,8 +223,7 @@ final class InferredTypeProvider(
224223
while i >= 0 && sourceText(i) != ':' do i -= 1
225224
i
226225
rhs match
227-
case t: Tree[?]
228-
if t.typeOpt.isErroneous && retryType && !tpt.sourcePos.span.isZeroExtent =>
226+
case t: Tree[?] if !tpt.sourcePos.span.isZeroExtent =>
229227
inferredTypeEdits(
230228
Some(
231229
AdjustTypeOpts(

presentation-compiler/test/dotty/tools/pc/tests/edit/InsertInferredTypeSuite.scala

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,70 @@ class InsertInferredTypeSuite extends BaseCodeActionSuite:
10351035
|""".stripMargin
10361036
)
10371037

1038+
@Test def `Adjust type for val` =
1039+
checkEdit(
1040+
"""|object A{
1041+
| val <<alpha>>:String = 123
1042+
|}""".stripMargin,
1043+
1044+
"""|object A{
1045+
| val alpha: Int = 123
1046+
|}""".stripMargin,
1047+
)
1048+
1049+
@Test def `Adjust type for val2` =
1050+
checkEdit(
1051+
"""|object A{
1052+
| val <<alpha>>:Int = 123
1053+
|}""".stripMargin,
1054+
"""|object A{
1055+
| val alpha: Int = 123
1056+
|}""".stripMargin,
1057+
)
1058+
1059+
@Test def `Adjust type for val3` =
1060+
checkEdit(
1061+
"""|object A{
1062+
| val <<alpha>>: Int = 123
1063+
|}""".stripMargin,
1064+
"""|object A{
1065+
| val alpha: Int = 123
1066+
|}""".stripMargin,
1067+
)
1068+
1069+
@Test def `Adjust type for def` =
1070+
checkEdit(
1071+
"""|object A{
1072+
| def <<alpha>>:String = 123
1073+
|}""".stripMargin,
1074+
1075+
"""|object A{
1076+
| def alpha: Int = 123
1077+
|}""".stripMargin,
1078+
)
1079+
1080+
@Test def `Adjust type for def2` =
1081+
checkEdit(
1082+
"""|object A{
1083+
| def <<alpha>>:Int = 123
1084+
|}""".stripMargin,
1085+
"""|object A{
1086+
| def alpha: Int = 123
1087+
|}""".stripMargin,
1088+
)
1089+
1090+
1091+
@Test def `Adjust type for def3` =
1092+
checkEdit(
1093+
"""|object A{
1094+
| def <<alpha>>: Int = 123
1095+
|}""".stripMargin,
1096+
"""|object A{
1097+
| def alpha: Int = 123
1098+
|}""".stripMargin,
1099+
)
1100+
1101+
10381102
def checkEdit(
10391103
original: String,
10401104
expected: String

0 commit comments

Comments
 (0)