Skip to content

Commit 6c2147d

Browse files
committed
Only suggest .nn on value types
1 parent f952dd2 commit 6c2147d

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
@@ -300,9 +300,10 @@ extends NotFoundMsg(MissingIdentID) {
300300
class TypeMismatch(val found: Type, expected: Type, val inTree: Option[untpd.Tree], addenda: => String*)(using Context)
301301
extends TypeMismatchMsg(found, expected)(TypeMismatchID):
302302

303-
private var shouldSuggestNN = false
304-
// Ensures that shouldSuggestNN will always be correctly computed before `actions` is called
305-
msg
303+
private val shouldSuggestNN =
304+
if expected.isValueType then
305+
found frozen_<:< OrNull(expected)
306+
else false
306307

307308
def msg(using Context) =
308309
// replace constrained TypeParamRefs and their typevars by their bounds where possible
@@ -348,7 +349,6 @@ class TypeMismatch(val found: Type, expected: Type, val inTree: Option[untpd.Tre
348349
val (found2, expected2) =
349350
if (found1 frozen_<:< expected1) || reported.fbounded then (found, expected)
350351
else (found1, expected1)
351-
if found2 frozen_<:< OrNull(expected) then shouldSuggestNN = true
352352
val (foundStr, expectedStr) = Formatting.typeDiff(found2.normalized, expected2.normalized)
353353
i"""|Found: $foundStr
354354
|Required: $expectedStr${reported.notes}"""
@@ -372,7 +372,7 @@ class TypeMismatch(val found: Type, expected: Type, val inTree: Option[untpd.Tre
372372
val content = tree.source.content().slice(tree.srcPos.startPos.start, tree.srcPos.endPos.end).mkString
373373
val replacement = tree match
374374
case a @ Apply(fun, args) => "(" + content + ").nn"
375-
case _ => content
375+
case _ => content + ".nn"
376376
List(
377377
CodeAction(title = """Add .nn""",
378378
description = None,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class CodeActionTest extends DottyTest:
189189
title = "Add .nn",
190190
expected =
191191
"""val s: String|Null = ???
192-
| val t: String = (s).nn""".stripMargin,
192+
| val t: String = s.nn""".stripMargin,
193193
ctxx = ctxx
194194
)
195195

0 commit comments

Comments
 (0)