Skip to content

Commit

Permalink
unused
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Sep 11, 2024
1 parent c84bad6 commit d33b67c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.softwaremill.macwire.internals.autowire

import scala.quoted.Quotes
import scala.quoted.Expr

class AutowireGraph[Q <: Quotes](using val q: Q):
import q.reflect.*
Expand All @@ -9,7 +10,8 @@ class AutowireGraph[Q <: Quotes](using val q: Q):
tpe: TypeRepr, // the type of the instance that will be created
symbol: Symbol, // the symbol of the val-def that will hold the instance
dependencies: List[Symbol], // incoming edges: the nodes for types that need to be created before this one
createInstance: Term // code to create an instance of `tpe`
createInstance: Term, // code to create an instance of `tpe`
raw: Option[Expr[Any]] // the raw dependency as provided by the user, if any
)

/** A fully resolved graph, i.e. for each node's dependency there's a node with that symbol. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ReportError[Q <: Quotes](using val q: Q):
finally path = path.init

def apply(msg: String): Nothing =
report.errorAndAbort(s"$msg\nWiring path: $showPath")
val suffix = if path.nonEmpty then s"\nWiring path: $showPath" else ""
report.errorAndAbort(s"$msg$suffix")

private def showPath: String = path.map(showTypeName).mkString(" -> ")
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def autowireImpl[T: Type](dependencies: Expr[Seq[Any]])(using q: Quotes): Expr[T
Symbol.noSymbol
)
val updatedGraph2 = updatedGraph.addNode(
Node(t, tSymbol, dependencySymbols, tProvider.create(dependencySymbols.map(Ref(_))))
Node(t, tSymbol, dependencySymbols, tProvider.create(dependencySymbols.map(Ref(_))), tProvider.raw)
)

(tSymbol, updatedGraph2)
Expand All @@ -89,14 +89,24 @@ def autowireImpl[T: Type](dependencies: Expr[Seq[Any]])(using q: Quotes): Expr[T
case '[Int] | '[Long] | '[Byte] | '[Short] | '[Char] | '[Boolean] | '[Double] | '[Float] | '[String] =>
reportError(s"Cannot use a primitive type or String in autowiring.")
case _ => // ok
end verifyNotPrimitive

def verifyEachDependencyUsed(nodes: Vector[Node]): Unit =
val usedDependencies = nodes.flatMap(_.raw).toSet
val unusedDependencies = rawDependencies.filterNot(usedDependencies.contains)
if unusedDependencies.nonEmpty then
reportError(
s"Unused dependencies: ${unusedDependencies.map(_.asTerm.show(using Printer.TreeShortCode)).mkString(", ")}."
)

//

val t = TypeRepr.of[T]
val (rootSymbol, fullGraph) = expandGraph(t, Graph(Vector.empty), Vector.empty)

// the graph is already sorted topologically: nodes are appended, and added only once all dependencies are present

// TODO: verify each dependency used
verifyEachDependencyUsed(fullGraph.nodes)

val code = Block(
fullGraph.nodes.map(node => ValDef(node.symbol, Some(node.createInstance))).toList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ case class D(c: C)

val a = A()
val b = B()
val c = C(a, b)
val d = D(c)
val d2 = autowire[D](a, b, c, d)
val d = autowire[D](a, b)

require (d2 eq d)
require (d.c.a eq a)
require (d.c.b eq b)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
case class A()
case class B(a: A)
case class C()

autowire[B](C())
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class AutowireCompileTests extends CompileTestsSupport:
"Wiring path: A"
),
"cyclicDependency" -> List("Cyclic dependencies detected.", "Wiring path: A -> B -> A"),
"primitives" -> List("Cannot use a primitive type or String in autowiring.", "Wiring path: A -> String")
"primitives" -> List("Cannot use a primitive type or String in autowiring.", "Wiring path: A -> String"),
"unusedDependency" -> List("Unused dependencies: C.apply().")
)
)

0 comments on commit d33b67c

Please sign in to comment.