Skip to content

Commit 7dfb0cf

Browse files
committed
create fallback virtualfile and warn
1 parent 95ed582 commit 7dfb0cf

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

sbt-bridge/src/dotty/tools/xsbt/CompilerBridgeDriver.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.io.IOException;
2323
import java.util.Comparator;
24+
import java.util.Collections;
2425
import java.util.HashMap;
2526
import java.util.Map;
2627
import java.util.Arrays;
@@ -62,6 +63,25 @@ private static VirtualFile asVirtualFileOrNull(SourceFile sourceFile) {
6263
}
6364
}
6465

66+
private static void reportMissingFile(DelegatingReporter reporter, dotty.tools.dotc.interfaces.SourceFile sourceFile) {
67+
String underline = String.join("", Collections.nCopies(sourceFile.path().length(), "^"));
68+
String message =
69+
sourceFile.path() + ": Missing virtual file\n" +
70+
underline + "\n" +
71+
" Falling back to placeholder for the given source file (of class " + sourceFile.getClass().getName() + ")\n" +
72+
" This is likely a bug in incremental compilation for the Scala 3 compiler. Please report it to the Scala 3 maintainers.";
73+
reporter.reportBasicWarning(message);
74+
}
75+
76+
private static VirtualFile fallbackVirtualFile(DelegatingReporter reporter,
77+
dotty.tools.dotc.interfaces.SourceFile sourceFile,
78+
Map<String, VirtualFile> placeholders) {
79+
return placeholders.computeIfAbsent(sourceFile.path(), path -> {
80+
reportMissingFile(reporter, sourceFile);
81+
return new PlaceholderVirtualFile(sourceFile);
82+
});
83+
}
84+
6585
synchronized public void run(VirtualFile[] sources, AnalysisCallback callback, Logger log, Reporter delegate) {
6686
VirtualFile[] sortedSources = new VirtualFile[sources.length];
6787
System.arraycopy(sources, 0, sortedSources, 0, sources.length);
@@ -86,17 +106,19 @@ synchronized public void run(VirtualFile[] sources, AnalysisCallback callback, L
86106
}
87107
});
88108

109+
HashMap<String, VirtualFile> placeholders = new HashMap<>();
110+
89111
IncrementalCallback incCallback = new IncrementalCallback(callback, sourceFile -> {
90112
if (sourceFile instanceof SourceFile) {
91113
SourceFile sf = (SourceFile) sourceFile;
92114
VirtualFile vf = asVirtualFileOrNull(sf);
93115
if (vf != null) {
94116
return vf;
95117
} else {
96-
throw new IllegalStateException("Unknown source file: " + sourceFile.path());
118+
return fallbackVirtualFile(reporter, sourceFile, placeholders);
97119
}
98120
} else {
99-
throw new IllegalStateException("Unknown source file: " + sourceFile.path());
121+
return fallbackVirtualFile(reporter, sourceFile, placeholders);
100122
}
101123
});
102124

sbt-bridge/src/dotty/tools/xsbt/DelegatingReporter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import xsbti.Position;
2020
import xsbti.Severity;
2121

22+
import java.util.Collections;
2223
import java.util.function.*;
2324

2425
final public class DelegatingReporter extends AbstractReporter {
@@ -60,6 +61,14 @@ public void doReport(Diagnostic dia, Context ctx) {
6061
delegate.log(new Problem(position, messageBuilder.toString(), severity, rendered.toString(), diagnosticCode, actions, lookup));
6162
}
6263

64+
public void reportBasicWarning(String message) {
65+
Position position = PositionBridge.noPosition;
66+
Severity severity = Severity.Warn;
67+
String diagnosticCode = "-1"; // no error code
68+
List<CodeAction> actions = Collections.emptyList();
69+
delegate.log(new Problem(position, message, severity, message, diagnosticCode, actions, lookup));
70+
}
71+
6372
private static Severity severityOf(int level) {
6473
Severity severity;
6574
switch (level) {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package dotty.tools.xsbt;
2+
3+
import dotty.tools.dotc.interfaces.SourceFile;
4+
import java.io.InputStream;
5+
import java.nio.charset.StandardCharsets;
6+
7+
public final class PlaceholderVirtualFile extends xsbti.BasicVirtualFileRef implements xsbti.VirtualFile {
8+
9+
private final SourceFile sourceFile;
10+
11+
public PlaceholderVirtualFile(SourceFile sourceFile) {
12+
super(sourceFile.path());
13+
this.sourceFile = sourceFile;
14+
}
15+
16+
private static byte[] toBytes(char[] chars) {
17+
return new String(chars).getBytes(StandardCharsets.UTF_8);
18+
}
19+
20+
public InputStream input() {
21+
return new java.io.ByteArrayInputStream(toBytes(sourceFile.content()));
22+
}
23+
24+
public long contentHash() {
25+
int murmurHash3 = scala.util.hashing.MurmurHash3$.MODULE$.bytesHash(toBytes(sourceFile.content()));
26+
return scala.util.hashing.package$.MODULE$.byteswap64((long) murmurHash3);
27+
}
28+
29+
}

sbt-test/tasty-compat/add-overload/build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ lazy val `a-changes` = project.in(file("a-changes"))
1616

1717
lazy val c = project.in(file("."))
1818
.settings(
19-
scalacOptions ++= Seq("-from-tasty", "-Ycheck:readTasty"),
19+
scalacOptions ++= Seq("-from-tasty", "-Ycheck:readTasty", "-Xfatal-warnings"),
2020
Compile / sources := Seq(new java.io.File("c-input/B.tasty")),
2121
Compile / unmanagedClasspath += (ThisBuild / baseDirectory).value / "c-input",
2222
Compile / classDirectory := (ThisBuild / baseDirectory).value / "c-output"

0 commit comments

Comments
 (0)