Skip to content

Commit 140a2ef

Browse files
committed
Refactoring: Move selection error addendum to ErrorReporting
1 parent ea2fe4b commit 140a2ef

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,41 @@ object ErrorReporting {
144144
def rewriteNotice: String =
145145
if Feature.migrateTo3 then "\nThis patch can be inserted automatically under -rewrite."
146146
else ""
147+
148+
def selectErrorAddendum
149+
(tree: untpd.RefTree, qual1: Tree, qualType: Type, suggestImports: Type => String)
150+
(using Context): String =
151+
val attempts: List[Tree] = qual1.getAttachment(Typer.HiddenSearchFailure) match
152+
case Some(failures) =>
153+
for failure <- failures
154+
if !failure.reason.isInstanceOf[Implicits.NoMatchingImplicits]
155+
yield failure.tree
156+
case _ => Nil
157+
if qualType.derivesFrom(defn.DynamicClass) then
158+
"\npossible cause: maybe a wrong Dynamic method signature?"
159+
else if attempts.nonEmpty then
160+
val extMethods =
161+
if attempts.length > 1 then "Extension methods were"
162+
else "An extension method was"
163+
val attemptStrings = attempts.map(_.showIndented(4))
164+
i""".
165+
|$extMethods tried, but could not be fully constructed:
166+
|
167+
| $attemptStrings%\nor\n %"""
168+
else if tree.hasAttachment(desugar.MultiLineInfix) then
169+
i""".
170+
|Note that `${tree.name}` is treated as an infix operator in Scala 3.
171+
|If you do not want that, insert a `;` or empty line in front
172+
|or drop any spaces behind the operator."""
173+
else if qualType.isBottomType then
174+
""
175+
else
176+
val add = suggestImports(
177+
ViewProto(qualType.widen,
178+
SelectionProto(tree.name, WildcardType, NoViewsAllowed, privateOK = false)))
179+
if add.isEmpty then ""
180+
else ", but could be made available as an extension method." ++ add
181+
end selectErrorAddendum
147182
}
148183

149184
def dependentStr =

compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -150,38 +150,7 @@ trait TypeAssigner {
150150
errorType(ex"$qualType does not have a constructor", tree.sourcePos)
151151
else {
152152
val kind = if (name.isTypeName) "type" else "value"
153-
def addendum =
154-
val attempts: List[Tree] = qual1.getAttachment(Typer.HiddenSearchFailure) match
155-
case Some(failures) =>
156-
for failure <- failures
157-
if !failure.reason.isInstanceOf[Implicits.NoMatchingImplicits]
158-
yield failure.tree
159-
case _ => Nil
160-
if qualType.derivesFrom(defn.DynamicClass) then
161-
"\npossible cause: maybe a wrong Dynamic method signature?"
162-
else if attempts.nonEmpty then
163-
val extMethods =
164-
if attempts.length > 1 then "Extension methods were"
165-
else "An extension method was"
166-
val attemptStrings = attempts.map(_.showIndented(4))
167-
i""".
168-
|$extMethods tried, but could not be fully constructed:
169-
|
170-
| $attemptStrings%\nor\n %"""
171-
else if tree.hasAttachment(desugar.MultiLineInfix) then
172-
i""".
173-
|Note that `$name` is treated as an infix operator in Scala 3.
174-
|If you do not want that, insert a `;` or empty line in front
175-
|or drop any spaces behind the operator."""
176-
else if qualType.isBottomType then
177-
""
178-
else
179-
val add = importSuggestionAddendum(
180-
ViewProto(qualType.widen,
181-
SelectionProto(name, WildcardType, NoViewsAllowed, privateOK = false)))
182-
if add.isEmpty then ""
183-
else ", but could be made available as an extension method." ++ add
184-
end addendum
153+
def addendum = err.selectErrorAddendum(tree, qual1, qualType, importSuggestionAddendum)
185154
errorType(NotAMember(qualType, name, kind, addendum), tree.sourcePos)
186155
}
187156
}

0 commit comments

Comments
 (0)