diff --git a/compiler/src/dotty/tools/dotc/transform/PostTyper.scala b/compiler/src/dotty/tools/dotc/transform/PostTyper.scala index 34bd406d69ba..3790691f05ec 100644 --- a/compiler/src/dotty/tools/dotc/transform/PostTyper.scala +++ b/compiler/src/dotty/tools/dotc/transform/PostTyper.scala @@ -335,7 +335,17 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase && ctx.compilationUnit.source.exists && sym != defn.SourceFileAnnot then - sym.addAnnotation(Annotation.makeSourceFile(ctx.compilationUnit.source.file.path)) + def sourcerootPath = + java.nio.file.Paths.get(ctx.settings.sourceroot.value) + .toAbsolutePath + .normalize + val file = ctx.compilationUnit.source.file + val jpath = file.jpath + val relativePath = + if jpath eq null then file.path // repl and other custom tests use abstract files with no path + else if jpath.isAbsolute then sourcerootPath.relativize(jpath.normalize).toString + else jpath.normalize.toString + sym.addAnnotation(Annotation.makeSourceFile(relativePath)) else (tree.rhs, sym.info) match case (rhs: LambdaTypeTree, bounds: TypeBounds) => VarianceChecker.checkLambda(rhs, bounds) diff --git a/project/scripts/cmdTests b/project/scripts/cmdTests index c3f3839b1c37..382c5a76de79 100755 --- a/project/scripts/cmdTests +++ b/project/scripts/cmdTests @@ -33,6 +33,11 @@ clear_out "$OUT" "$SBT" ";scalac -d $OUT/out.jar $SOURCE; scalac -decompile -color:never $OUT/out.jar" > "$tmp" grep -qe "def main(args: scala.Array\[scala.Predef.String\]): scala.Unit =" "$tmp" +echo "testing that paths SourceFile annotations are relativized" +clear_out "$OUT" +"$SBT" ";scalac -d $OUT/out.jar $(pwd)/$SOURCE; scalac -decompile -color:never $OUT/out.jar" > "$tmp" +grep -qe "SourceFile(\"$SOURCE\")" "$tmp" + echo "testing sbt scalac with suspension" clear_out "$OUT" "$SBT" "scala3-compiler-bootstrapped/scalac -d $OUT tests/pos-macros/macros-in-same-project-1/Bar.scala tests/pos-macros/macros-in-same-project-1/Foo.scala" > "$tmp"