@@ -300,6 +300,10 @@ extends NotFoundMsg(MissingIdentID) {
300
300
class TypeMismatch (val found : Type , expected : Type , val inTree : Option [untpd.Tree ], addenda : => String * )(using Context )
301
301
extends TypeMismatchMsg (found, expected)(TypeMismatchID ):
302
302
303
+ private var shouldSuggestNN = false
304
+ // Ensures that shouldSuggestNN will always be correctly computed before `actions` is called
305
+ msg
306
+
303
307
def msg (using Context ) =
304
308
// replace constrained TypeParamRefs and their typevars by their bounds where possible
305
309
// and the bounds are not f-bounds.
@@ -344,6 +348,7 @@ class TypeMismatch(val found: Type, expected: Type, val inTree: Option[untpd.Tre
344
348
val (found2, expected2) =
345
349
if (found1 frozen_<:< expected1) || reported.fbounded then (found, expected)
346
350
else (found1, expected1)
351
+ if found2 frozen_<:< OrNull (expected) then shouldSuggestNN = true
347
352
val (foundStr, expectedStr) = Formatting .typeDiff(found2.normalized, expected2.normalized)
348
353
i """ |Found: $foundStr
349
354
|Required: $expectedStr${reported.notes}"""
@@ -360,6 +365,28 @@ class TypeMismatch(val found: Type, expected: Type, val inTree: Option[untpd.Tre
360
365
val treeStr = inTree.map(x => s " \n Tree: \n\n ${x.show}\n " ).getOrElse(" " )
361
366
treeStr + " \n " + super .explain
362
367
368
+ override def actions (using Context ) =
369
+ if shouldSuggestNN then
370
+ inTree match {
371
+ case Some (tree) if tree != null =>
372
+ val content = tree.source.content().slice(tree.srcPos.startPos.start, tree.srcPos.endPos.end).mkString
373
+ val replacement = tree match
374
+ case a @ Apply (fun, args) => " (" + content + " ).nn"
375
+ case _ => content
376
+ List (
377
+ CodeAction (title = """ Add .nn""" ,
378
+ description = None ,
379
+ patches = List (
380
+ ActionPatch (tree.srcPos.sourcePos, replacement)
381
+ )
382
+ )
383
+ )
384
+ case _ =>
385
+ List ()
386
+ }
387
+ else
388
+ List ()
389
+
363
390
end TypeMismatch
364
391
365
392
class NotAMember (site : Type , val name : Name , selected : String , proto : Type , addendum : => String = " " )(using Context )
0 commit comments