Skip to content

Commit 01ac719

Browse files
committed
Can be different explanations for not finding implicit, be more precise
Some small fixes
1 parent 293e6c5 commit 01ac719

3 files changed

Lines changed: 15 additions & 18 deletions

File tree

core/shared/src/main/scala-3.x/monocle/internal/focus/ErrorHandling.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ private[focus] trait ErrorHandling {
2121
case FocusError.UnexpectedCodeStructure(code) => s"Unexpected code structure: $code"
2222
case FocusError.CouldntFindFieldType(fromType, fieldName) => s"Couldn't find type for $fromType.$fieldName"
2323
case FocusError.InvalidDowncast(fromType, toType) => s"Type '$fromType' could not be cast to '$toType'"
24-
case FocusError.ImplicitNotFound(implicitType) =>
25-
s"Could not find implicit for '$implicitType'. Note: implicits with default values are not supported."
24+
case FocusError.ImplicitNotFound(implicitType, explanation) =>
25+
s"Could not find (unique) implicit value for '$implicitType' due to $explanation. Note: implicits with default values are not supported."
2626
case FocusError.ExpansionFailed(reason) =>
2727
s"Case class with multiple parameter sets could not be expanded because of: $reason"
2828
}

core/shared/src/main/scala-3.x/monocle/internal/focus/FocusBase.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private[focus] trait FocusBase {
6161
case CouldntFindFieldType(fromType: String, fieldName: String)
6262
case ComposeMismatch(type1: String, type2: String)
6363
case InvalidDowncast(fromType: String, toType: String)
64-
case ImplicitNotFound(implicitType: String)
64+
case ImplicitNotFound(implicitType: String, explanation: String)
6565
case ExpansionFailed(reason: String)
6666

6767
def asResult: FocusResult[Nothing] = Left(this)

core/shared/src/main/scala-3.x/monocle/internal/focus/features/SelectParserBase.scala

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ private[focus] trait SelectParserBase extends ParserBase {
1616
val companionObject: Term = Ref(classSymbol.companionModule)
1717

1818
private val (typeParams, caseFieldParams :: otherParams) =
19-
classSymbol.primaryConstructor.paramSymss.span(_.head.isTypeParam)
19+
classSymbol.primaryConstructor.paramSymss.span(_.headOption.fold(false)(_.isTypeParam))
2020
val hasOnlyOneCaseField: Boolean = caseFieldParams.length == 1
2121
val hasOnlyOneParameterList: Boolean = otherParams.isEmpty
2222
private val nonCaseNonImplicitParameters: List[Symbol] =
@@ -32,7 +32,10 @@ private[focus] trait SelectParserBase extends ParserBase {
3232
case None => FocusError.NotACaseField(typeRepr.show, fieldName).asResult
3333
}
3434
def getCaseFieldType(caseFieldSymbol: Symbol): FocusResult[TypeRepr] =
35-
getFieldType(typeRepr, caseFieldSymbol)
35+
caseFieldSymbol match {
36+
case FieldType(possiblyTypeArg) => Right(swapWithSuppliedType(typeRepr, possiblyTypeArg))
37+
case _ => FocusError.CouldntFindFieldType(typeRepr.show, caseFieldSymbol.name).asResult
38+
}
3639
}
3740

3841
object CaseClassExtractor {
@@ -42,25 +45,19 @@ private[focus] trait SelectParserBase extends ParserBase {
4245
}
4346
}
4447

45-
def getSuppliedTypeArgs(fromType: TypeRepr): List[TypeRepr] =
48+
private def getSuppliedTypeArgs(fromType: TypeRepr): List[TypeRepr] =
4649
fromType match {
4750
case AppliedType(_, argTypeReprs) => argTypeReprs
4851
case _ => Nil
4952
}
5053

51-
def getFieldType(fromType: TypeRepr, caseFieldSymbol: Symbol): FocusResult[TypeRepr] =
52-
caseFieldSymbol match {
53-
case FieldType(possiblyTypeArg) => Right(swapWithSuppliedType(fromType, possiblyTypeArg))
54-
case _ => FocusError.CouldntFindFieldType(fromType.show, caseFieldSymbol.name).asResult
55-
}
56-
5754
private object FieldType {
5855
def unapply(fieldSymbol: Symbol): Option[TypeRepr] = fieldSymbol match {
5956
case sym if sym.isNoSymbol => None
6057
case sym =>
6158
sym.tree match {
6259
case ValDef(_, typeTree, _) => Some(typeTree.tpe)
63-
// Only needed for Tuples because `_1` is a DefDef while `_1 ` is a ValDef.
60+
// Only needed for Tuples because `_1` is a DefDef while `_1 ` is the corresponding ValDef.
6461
case DefDef(_, _, typeTree, _) => Some(typeTree.tpe)
6562
case _ => None
6663
}
@@ -100,13 +97,13 @@ private[focus] trait SelectParserBase extends ParserBase {
10097
case (Right(acc), ValDef(_, t, _)) =>
10198
def searchForImplicit(typeRepr: TypeRepr): FocusResult[Term] =
10299
Implicits.search(typeRepr) match {
103-
case success: ImplicitSearchSuccess => Right(success.tree)
104-
case _ => FocusError.ImplicitNotFound(typeRepr.show).asResult
100+
case success: ImplicitSearchSuccess =>
101+
Right(success.tree)
102+
case failure: ImplicitSearchFailure =>
103+
FocusError.ImplicitNotFound(typeRepr.show, failure.explanation).asResult
105104
}
105+
106106
searchForImplicit(t.tpe)
107-
.orElse(searchForImplicit(t.tpe.dealias))
108-
.orElse(searchForImplicit(t.tpe.widen))
109-
.orElse(searchForImplicit(t.tpe.widen.dealias))
110107
.map(acc :+ _)
111108

112109
case (Right(acc), other) =>

0 commit comments

Comments
 (0)