Skip to content

Commit 128e6c3

Browse files
HarrisL2tgodzik
authored andcommitted
Only suggest .nn on value types
[Cherry-picked 6c2147d]
1 parent d359e48 commit 128e6c3

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,10 @@ extends NotFoundMsg(MissingIdentID) {
295295
class TypeMismatch(val found: Type, expected: Type, val inTree: Option[untpd.Tree], addenda: => String*)(using Context)
296296
extends TypeMismatchMsg(found, expected)(TypeMismatchID):
297297

298-
private var shouldSuggestNN = false
299-
// Ensures that shouldSuggestNN will always be correctly computed before `actions` is called
300-
msg
298+
private val shouldSuggestNN =
299+
if expected.isValueType then
300+
found frozen_<:< OrNull(expected)
301+
else false
301302

302303
def msg(using Context) =
303304
// replace constrained TypeParamRefs and their typevars by their bounds where possible
@@ -343,7 +344,6 @@ class TypeMismatch(val found: Type, expected: Type, val inTree: Option[untpd.Tre
343344
val (found2, expected2) =
344345
if (found1 frozen_<:< expected1) || reported.fbounded then (found, expected)
345346
else (found1, expected1)
346-
if found2 frozen_<:< OrNull(expected) then shouldSuggestNN = true
347347
val (foundStr, expectedStr) = Formatting.typeDiff(found2.normalized, expected2.normalized)
348348
i"""|Found: $foundStr
349349
|Required: $expectedStr${reported.notes}"""
@@ -367,7 +367,7 @@ class TypeMismatch(val found: Type, expected: Type, val inTree: Option[untpd.Tre
367367
val content = tree.source.content().slice(tree.srcPos.startPos.start, tree.srcPos.endPos.end).mkString
368368
val replacement = tree match
369369
case a @ Apply(fun, args) => "(" + content + ").nn"
370-
case _ => content
370+
case _ => content + ".nn"
371371
List(
372372
CodeAction(title = """Add .nn""",
373373
description = None,

compiler/test/dotty/tools/dotc/reporting/CodeActionTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class CodeActionTest extends DottyTest:
175175
title = "Add .nn",
176176
expected =
177177
"""val s: String|Null = ???
178-
| val t: String = (s).nn""".stripMargin,
178+
| val t: String = s.nn""".stripMargin,
179179
ctxx = ctxx
180180
)
181181

0 commit comments

Comments
 (0)