Skip to content

Commit e3de91c

Browse files
committed
address comments
1 parent 246dfc4 commit e3de91c

13 files changed

+105
-109
lines changed

compiler/src/dotty/tools/dotc/core/Contexts.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ object Contexts {
172172
val local = incCallback
173173
if local != null then op(local)
174174

175-
def incrementalEnabled: Boolean =
175+
def runZincPhases: Boolean =
176+
def forceRun = settings.YdumpSbtInc.value || settings.YforceSbtPhases.value
176177
val local = incCallback
177-
if local != null then local.enabled
178-
else false
178+
local != null && local.enabled || forceRun
179179

180180
/** The current plain printer */
181181
def printerFn: Context => Printer = store(printerFnLoc)

compiler/src/dotty/tools/dotc/sbt/ExtractAPI.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ class ExtractAPI extends Phase {
4949
override def description: String = ExtractAPI.description
5050

5151
override def isRunnable(using Context): Boolean = {
52-
def forceRun = ctx.settings.YdumpSbtInc.value || ctx.settings.YforceSbtPhases.value
53-
super.isRunnable && (ctx.incrementalEnabled || forceRun)
52+
super.isRunnable && ctx.runZincPhases
5453
}
5554

5655
// Check no needed. Does not transform trees

compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala

+3-11
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ class ExtractDependencies extends Phase {
5656
override def description: String = ExtractDependencies.description
5757

5858
override def isRunnable(using Context): Boolean = {
59-
def forceRun = ctx.settings.YdumpSbtInc.value || ctx.settings.YforceSbtPhases.value
60-
super.isRunnable && (ctx.incrementalEnabled || forceRun)
59+
super.isRunnable && ctx.runZincPhases
6160
}
6261

6362
// Check no needed. Does not transform trees
@@ -123,15 +122,8 @@ class ExtractDependencies extends Phase {
123122
case Some(zip) if zip.jpath != null =>
124123
binaryDependency(zip.jpath, binaryClassName)
125124
case _ =>
126-
case pf: PlainFile => // The dependency comes from a class file
127-
// FIXME: pf.file is null for classfiles coming from the modulepath
128-
// (handled by JrtClassPath) because they cannot be represented as
129-
// java.io.File, since the `binaryDependency` callback must take a
130-
// java.io.File, this means that we cannot record dependencies coming
131-
// from the modulepath. For now this isn't a big deal since we only
132-
// support having the standard Java library on the modulepath.
133-
if pf.jpath != null then
134-
binaryDependency(pf.jpath, binaryClassName)
125+
case pf: PlainFile => // The dependency comes from a class file, Zinc handles JRT filesystem
126+
binaryDependency(pf.jpath, binaryClassName)
135127
case _ =>
136128
internalError(s"Ignoring dependency $depFile of unknown class ${depFile.getClass}}", dep.from.srcPos)
137129
}

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

-15
This file was deleted.

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

+43-10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import dotty.tools.dotc.core.Contexts;
1313
import dotty.tools.dotc.util.SourceFile;
1414
import dotty.tools.io.AbstractFile;
15+
import dotty.tools.io.PlainFile;
16+
import dotty.tools.io.Path;
17+
import dotty.tools.io.Streamable;
1518
import scala.collection.mutable.ListBuffer;
1619
import scala.jdk.javaapi.CollectionConverters;
1720
import scala.io.Codec;
@@ -20,6 +23,8 @@
2023
import xsbti.compile.Output;
2124

2225
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.io.OutputStream;
2328
import java.util.Comparator;
2429
import java.util.Collections;
2530
import java.util.HashMap;
@@ -60,19 +65,20 @@ private static VirtualFile asVirtualFile(SourceFile sourceFile, DelegatingReport
6065
return lookup.computeIfAbsent(sourceFile.file(), path -> {
6166
reportMissingFile(reporter, sourceFile);
6267
if (sourceFile.file().jpath() != null)
63-
return new BasicPathBasedFile(sourceFile);
68+
return new FallbackPathBasedFile(sourceFile);
6469
else
65-
return new PlaceholderVirtualFile(sourceFile);
70+
return new FallbackVirtualFile(sourceFile);
6671
});
6772
}
6873

6974
private static void reportMissingFile(DelegatingReporter reporter, SourceFile sourceFile) {
7075
String underline = String.join("", Collections.nCopies(sourceFile.path().length(), "^"));
7176
String message =
72-
sourceFile.path() + ": Missing virtual file\n" +
77+
sourceFile.path() + ": Missing Zinc virtual file\n" +
7378
underline + "\n" +
7479
" Falling back to placeholder for the given source file (of class " + sourceFile.getClass().getName() + ")\n" +
75-
" This is likely a bug in incremental compilation for the Scala 3 compiler. Please report it to the Scala 3 maintainers.";
80+
" This is likely a bug in incremental compilation for the Scala 3 compiler.\n" +
81+
" Please report it to the Scala 3 maintainers at https://github.com/lampepfl/dotty/issues.";
7682
reporter.reportBasicWarning(message);
7783
}
7884

@@ -91,9 +97,19 @@ synchronized public void run(VirtualFile[] sources, AnalysisCallback callback, L
9197
lookup.put(abstractFile, source);
9298
}
9399

94-
DelegatingReporter reporter = new DelegatingReporter(delegate, (self, sourceFile) ->
95-
asVirtualFile(sourceFile, self, lookup).id()
96-
);
100+
DelegatingReporter reporter = new DelegatingReporter(delegate, sourceFile -> {
101+
// TODO: possible situation here where we use -from-tasty and TASTy source files but
102+
// the reporter log is associated to a Scala source file?
103+
104+
// Zinc will use the output of this function to possibly lookup a mapped virtual file,
105+
// e.g. convert `${ROOT}/Foo.scala` to `/path/to/Foo.scala` if it exists in the lookup map.
106+
VirtualFile vf = lookup.get(sourceFile.file());
107+
if (vf != null)
108+
return vf.id();
109+
else
110+
// follow Zinc, which uses the path of the source file as a fallback.
111+
return sourceFile.path();
112+
});
97113

98114
IncrementalCallback incCallback = new IncrementalCallback(callback, sourceFile ->
99115
asVirtualFile(sourceFile, reporter, lookup)
@@ -137,11 +153,28 @@ synchronized public void run(VirtualFile[] sources, AnalysisCallback callback, L
137153
}
138154

139155
private static AbstractFile asDottyFile(VirtualFile virtualFile) {
140-
if (virtualFile instanceof PathBasedFile)
141-
return new ZincPlainFile((PathBasedFile) virtualFile);
156+
if (virtualFile instanceof PathBasedFile) {
157+
java.nio.file.Path path = ((PathBasedFile) virtualFile).toPath();
158+
return new PlainFile(new Path(path));
159+
}
142160

143161
try {
144-
return new ZincVirtualFile(virtualFile);
162+
return new dotty.tools.io.VirtualFile(virtualFile.name(), virtualFile.id()) {
163+
{
164+
// fill in the content
165+
try (OutputStream output = output()) {
166+
try (InputStream input = virtualFile.input()) {
167+
Streamable.Bytes bytes = new Streamable.Bytes() {
168+
@Override
169+
public InputStream inputStream() {
170+
return input;
171+
}
172+
};
173+
output.write(bytes.toByteArray());
174+
}
175+
}
176+
}
177+
};
145178
} catch (IOException e) {
146179
throw new IllegalArgumentException("invalid file " + virtualFile.name(), e);
147180
}

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

+11-8
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@
2424

2525
final public class DelegatingReporter extends AbstractReporter {
2626
private xsbti.Reporter delegate;
27-
private final BiFunction<DelegatingReporter, SourceFile, String> baseLookup;
28-
private final Function<SourceFile, String> lookup;
2927

30-
public DelegatingReporter(xsbti.Reporter delegate, BiFunction<DelegatingReporter, SourceFile, String> baseLookup) {
28+
// A function that can lookup the `id` of the VirtualFile
29+
// associated with a SourceFile. If there is not an associated virtual file,
30+
// then it is the path of the SourceFile as a String.
31+
private final Function<SourceFile, String> lookupVirtualFileId;
32+
33+
public DelegatingReporter(xsbti.Reporter delegate, Function<SourceFile, String> lookupVirtualFileId) {
3134
super();
3235
this.delegate = delegate;
33-
this.baseLookup = baseLookup;
34-
this.lookup = sourceFile -> baseLookup.apply(this, sourceFile);
36+
this.lookupVirtualFileId = lookupVirtualFileId;
3537
}
3638

3739
public void dropDelegate() {
@@ -60,15 +62,16 @@ public void doReport(Diagnostic dia, Context ctx) {
6062
messageBuilder.append(System.lineSeparator()).append(explanation(message, ctx));
6163
}
6264

63-
delegate.log(new Problem(position, messageBuilder.toString(), severity, rendered.toString(), diagnosticCode, actions, lookup));
65+
delegate.log(new Problem(position, messageBuilder.toString(), severity, rendered.toString(), diagnosticCode, actions,
66+
lookupVirtualFileId));
6467
}
6568

6669
public void reportBasicWarning(String message) {
6770
Position position = PositionBridge.noPosition;
6871
Severity severity = Severity.Warn;
6972
String diagnosticCode = "-1"; // no error code
7073
List<CodeAction> actions = Collections.emptyList();
71-
delegate.log(new Problem(position, message, severity, message, diagnosticCode, actions, lookup));
74+
delegate.log(new Problem(position, message, severity, message, diagnosticCode, actions, lookupVirtualFileId));
7275
}
7376

7477
private static Severity severityOf(int level) {
@@ -85,7 +88,7 @@ private static Severity severityOf(int level) {
8588

8689
private Position positionOf(SourcePosition pos) {
8790
if (pos.exists()) {
88-
return new PositionBridge(pos, lookup.apply(pos.source()));
91+
return new PositionBridge(pos, lookupVirtualFileId.apply(pos.source()));
8992
} else {
9093
return PositionBridge.noPosition;
9194
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package dotty.tools.xsbt;
2+
3+
import dotty.tools.dotc.util.SourceFile;
4+
5+
/**A basic implementation of PathBasedFile that is only used when
6+
* the real virtual file can not be found.
7+
*
8+
* See FallbackVirtualFile for more details.
9+
*/
10+
public class FallbackPathBasedFile extends FallbackVirtualFile implements xsbti.PathBasedFile {
11+
12+
public FallbackPathBasedFile(SourceFile sourceFile) {
13+
super(sourceFile);
14+
}
15+
16+
public java.nio.file.Path toPath() {
17+
return sourceFile.file().jpath();
18+
}
19+
20+
}

sbt-bridge/src/dotty/tools/xsbt/PlaceholderVirtualFile.java renamed to sbt-bridge/src/dotty/tools/xsbt/FallbackVirtualFile.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@
44
import java.io.InputStream;
55
import java.nio.charset.StandardCharsets;
66

7-
public class PlaceholderVirtualFile extends xsbti.BasicVirtualFileRef implements xsbti.VirtualFile {
7+
/**A basic implementation of VirtualFile that is only used when
8+
* the real virtual file can not be found.
9+
*
10+
* This has a very basic implementation of contentHash that is almost certainly colliding more than the implementation
11+
* in Zinc. It does not matter anyway as Zinc will recompile the associated source file, because it did not recieve the
12+
* same virtual file back.
13+
*/
14+
public class FallbackVirtualFile extends xsbti.BasicVirtualFileRef implements xsbti.VirtualFile {
815

916
protected final SourceFile sourceFile;
1017

11-
public PlaceholderVirtualFile(SourceFile sourceFile) {
18+
public FallbackVirtualFile(SourceFile sourceFile) {
1219
super(sourceFile.path());
1320
this.sourceFile = sourceFile;
1421
}
@@ -23,7 +30,7 @@ public InputStream input() {
2330

2431
public long contentHash() {
2532
int murmurHash3 = scala.util.hashing.MurmurHash3$.MODULE$.bytesHash(toBytes(sourceFile.content()));
26-
return scala.util.hashing.package$.MODULE$.byteswap64((long) murmurHash3);
33+
return (long) murmurHash3;
2734
}
2835

2936
}

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

+12-8
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,22 @@ final public class Problem implements xsbti.Problem {
2626
private final Optional<String> _rendered;
2727
private final String _diagnosticCode;
2828
private final List<CodeAction> _actions;
29-
private final Function<SourceFile, String> _lookup;
29+
30+
// A function that can lookup the `id` of the VirtualFile
31+
// associated with a SourceFile. If there is not an associated virtual file,
32+
// then it is the path of the SourceFile as a String.
33+
private final Function<SourceFile, String> _lookupVirtualFileId;
3034

3135
public Problem(Position position, String message, Severity severity, String rendered, String diagnosticCode, List<CodeAction> actions,
32-
Function<SourceFile, String> lookup) {
36+
Function<SourceFile, String> lookupVirtualFileId) {
3337
super();
3438
this._position = position;
3539
this._message = message;
3640
this._severity = severity;
3741
this._rendered = Optional.of(rendered);
3842
this._diagnosticCode = diagnosticCode;
3943
this._actions = actions;
40-
this._lookup = lookup;
44+
this._lookupVirtualFileId = lookupVirtualFileId;
4145
}
4246

4347
public String category() {
@@ -86,23 +90,23 @@ public List<xsbti.Action> actions() {
8690
// never getting called.
8791
return _actions
8892
.stream()
89-
.map(action -> new Action(action.title(), OptionConverters.toJava(action.description()), toWorkspaceEdit(CollectionConverters.asJava(action.patches()), _lookup)))
93+
.map(action -> new Action(action.title(), OptionConverters.toJava(action.description()), toWorkspaceEdit(CollectionConverters.asJava(action.patches()), _lookupVirtualFileId)))
9094
.collect(toList());
9195
}
9296
}
9397

94-
private static WorkspaceEdit toWorkspaceEdit(List<ActionPatch> patches, Function<SourceFile, String> lookup) {
98+
private static WorkspaceEdit toWorkspaceEdit(List<ActionPatch> patches, Function<SourceFile, String> lookupVirtualFileId) {
9599
return new WorkspaceEdit(
96100
patches
97101
.stream()
98-
.map(patch -> new TextEdit(positionOf(patch.srcPos(), lookup), patch.replacement()))
102+
.map(patch -> new TextEdit(positionOf(patch.srcPos(), lookupVirtualFileId), patch.replacement()))
99103
.collect(toList())
100104
);
101105
}
102106

103-
private static Position positionOf(SourcePosition pos, Function<SourceFile, String> lookup) {
107+
private static Position positionOf(SourcePosition pos, Function<SourceFile, String> lookupVirtualFileId) {
104108
if (pos.exists()){
105-
return new PositionBridge(pos, lookup.apply(pos.source()));
109+
return new PositionBridge(pos, lookupVirtualFileId.apply(pos.source()));
106110
} else {
107111
return PositionBridge.noPosition;
108112
}

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

-14
This file was deleted.

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

-33
This file was deleted.

sbt-bridge/src/xsbt/CachedCompilerImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ synchronized public void run(File[] sources, DependencyChanges changes, Analysis
6767

6868
Context ctx = new ContextBase().initialCtx().fresh()
6969
.setIncCallback(incCallback)
70-
.setReporter(new DelegatingReporter(delegate, (self, source) -> source.file().absolutePath()));
70+
.setReporter(new DelegatingReporter(delegate, source -> source.file().absolutePath()));
7171

7272
dotty.tools.dotc.reporting.Reporter reporter = Main.process(commandArguments(sources), ctx);
7373
if (reporter.hasErrors()) {

sbt-bridge/src/xsbt/DottydocRunner.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void run() {
5353
args = retained.toArray(new String[retained.size()]);
5454

5555
Context ctx = new ContextBase().initialCtx().fresh()
56-
.setReporter(new DelegatingReporter(delegate, (self, source) -> source.file().absolutePath()));
56+
.setReporter(new DelegatingReporter(delegate, source -> source.file().absolutePath()));
5757

5858
try {
5959
Class<?> dottydocMainClass = Class.forName("dotty.tools.dottydoc.Main");

0 commit comments

Comments
 (0)