Skip to content

Commit f376a98

Browse files
authored
Don't override graph extension factories' default functions (#946)
1 parent d284535 commit f376a98

4 files changed

Lines changed: 53 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Changelog
44
**Unreleased**
55
--------------
66

7+
* **Fix**: Don't override graph extension factories' default functions.
8+
* **Fix**: Fix Kotlin internal error overriding Metro error when there's a missing factory for a Java `@Inject` class.
9+
710
0.6.0
811
-----
912

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// https://github.com/ZacSweers/metro/issues/940
2+
@DependencyGraph(AppScope::class)
3+
interface AppGraph
4+
5+
@GraphExtension
6+
interface ExtGraph {
7+
val value: Int
8+
9+
@GraphExtension.Factory
10+
@ContributesTo(AppScope::class)
11+
fun interface Factory : BaseFactory {
12+
fun create(@Provides value: Int): ExtGraph
13+
14+
override fun create(
15+
name: String,
16+
value: Int
17+
): ExtGraph {
18+
println(name)
19+
return create(value)
20+
}
21+
}
22+
}
23+
24+
interface BaseFactory {
25+
fun create(name: String, value: Int): ExtGraph
26+
}
27+
28+
fun box(): String {
29+
val graph = createGraph<AppGraph>()
30+
val extGraph = graph.create("Hello", 3)
31+
assertEquals(3, extGraph.value)
32+
return "OK"
33+
}

compiler-tests/src/test/java/dev/zacsweers/metro/compiler/BoxTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/src/main/kotlin/dev/zacsweers/metro/compiler/ir/DependencyGraphNodeCache.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ internal class DependencyGraphNodeCache(
302302

303303
if (isGraphExtensionFactory) {
304304
isGraphExtension = true
305-
break
305+
// Only continue because we may ignore this if it has a default body in a parent
306+
continue
306307
}
307308

308309
// Check if return type is a @GraphExtension itself (i.e. no factory)
@@ -315,7 +316,8 @@ internal class DependencyGraphNodeCache(
315316
)
316317
if (returnsExtensionOrExtensionFactory) {
317318
isGraphExtension = true
318-
break
319+
// Only continue because we may ignore this if it has a default body in a parent
320+
continue
319321
}
320322
}
321323
}
@@ -586,7 +588,7 @@ internal class DependencyGraphNodeCache(
586588
if (overlapErrors.isNotEmpty()) {
587589
val message = buildString {
588590
appendLine(
589-
"Graph extension '${dependencyGraphNode.sourceGraph.sourceGraphIfMetroGraph.kotlinFqName}' has overlapping scope annotations with ancestor graphs':",
591+
"Graph extension '${dependencyGraphNode.sourceGraph.sourceGraphIfMetroGraph.kotlinFqName}' has overlapping scope annotations with ancestor graphs':"
590592
)
591593
for (overlap in overlapErrors) {
592594
appendLine(overlap)
@@ -595,14 +597,13 @@ internal class DependencyGraphNodeCache(
595597

596598
// TODO in 2.2.20 use just diagnostic reporter
597599
if (graphDeclaration.origin === Origins.GeneratedGraphExtension) {
598-
messageCollector.report(CompilerMessageSeverity.ERROR, message, graphDeclaration.locationOrNull())
600+
messageCollector.report(
601+
CompilerMessageSeverity.ERROR,
602+
message,
603+
graphDeclaration.locationOrNull(),
604+
)
599605
} else {
600-
diagnosticReporter
601-
.at(graphDeclaration)
602-
.report(
603-
MetroIrErrors.METRO_ERROR,
604-
message,
605-
)
606+
diagnosticReporter.at(graphDeclaration).report(MetroIrErrors.METRO_ERROR, message)
606607
}
607608
exitProcessing()
608609
}

0 commit comments

Comments
 (0)