Skip to content

Wrong source when mixing inline and transparent inline #16609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
odersky opened this issue Jan 2, 2023 · 3 comments
Open

Wrong source when mixing inline and transparent inline #16609

odersky opened this issue Jan 2, 2023 · 3 comments

Comments

@odersky
Copy link
Contributor

odersky commented Jan 2, 2023

Compiler version

3.2.2 RC2

Minimized example

Two files.

// boundary.scala
object boundary:
  transparent inline def apply[R](inline body: R): R =  // works if `transparent` is dropped
    body

2nd file:

// Test.scala
inline def f [T](fallback: => T) = fallback
def foo(x: Int): Int = boundary.apply {
  f(10)
}

Output

Under -Ycheck:all:

*** error while checking Test.scala after phase inlining ***
java.lang.AssertionError: assertion failed: wrong source set for 10 # -1 of class dotty.tools.dotc.ast.Trees$Literal, set to boundary.scala but context had Test.scala
 <permanent> while running Ycheck on Test.scala
exception occurred while compiling Test.scala, boundary.scala
java.lang.AssertionError: assertion failed: wrong source set for 10 # -1 of class dotty.tools.dotc.ast.Trees$Literal, set to boundary.scala but context had Test.scala
 <permanent> while compiling Test.scala, boundary.scala
Exception in thread "main" java.lang.AssertionError: assertion failed: wrong source set for 10 # -1 of class dotty.tools.dotc.ast.Trees$Literal, set to boundary.scala but context had Test.scala
 <permanent>
	at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
	at dotty.tools.dotc.transform.YCheckPositions$$anon$1.traverse(YCheckPositions.scala:36)
	at dotty.tools.dotc.transform.YCheckPositions$$anon$1.traverse(YCheckPositions.scala:45)
	at dotty.tools.dotc.ast.Trees$Instance$TreeTraverser.apply(Trees.scala:1655)
	at dotty.tools.dotc.ast.Trees$Instance$TreeTraverser.apply(Trees.scala:1655)
	at dotty.tools.dotc.ast.Trees$Instance$TreeAccumulator.foldOver(Trees.scala:1554)
	at dotty.tools.dotc.ast.Trees$Instance$TreeTraverser.traverseChildren(Trees.scala:1656)

Expectation

Should pass Ycheck

@odersky odersky added stat:needs triage Every issue needs to have an "area" and "itype" label area:inline and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jan 2, 2023
@julienrf
Copy link
Contributor

julienrf commented Jan 3, 2023

Might be related or solved by #15684

@nicolasstucki
Copy link
Contributor

Compiling this with -Xprint-inline shows that after inlining we have

   def foo(x: Int): Int =
      {{/* inlined from boundary */
        {{/* inlined from outside */
          {{/* inlined from f[Int](10) */{{/* inlined from outside */10}}:Int}}
          }}
      }}

This implies that the Inlined tree nodes are correct.

The issue must be that we copy the Literal tree for 10 with a context that has the wrong source set.

@nicolasstucki
Copy link
Contributor

Compiling with -Ydebug-tree-with-id -2 I got the id of the failing Literal. Lets say 716 as an example.

java.lang.AssertionError: assertion failed: wrong source set for 10 # 716 of class dotty.tools.dotc.ast.Trees$Literal, set to tests/pos/i16609/boundary.scala but context had tests/pos/i16609/Test.scala

Re-compiling with -Ydebug-tree-with-id 716 we get the following stack dump when that node is created.

Stack dump
Debug tree (id=716) creation 
Literal(Constant(10))
        at java.base/java.lang.Thread.dumpStack(Thread.java:1380)
        at dotty.tools.dotc.ast.Positioned.allocateId(Positioned.scala:38)
        at dotty.tools.dotc.ast.Positioned.<init>(Positioned.scala:40)
        at dotty.tools.dotc.ast.Trees$Tree.<init>(Trees.scala:50)
        at dotty.tools.dotc.ast.Trees$Literal.<init>(Trees.scala:533)
        at dotty.tools.dotc.ast.untpd$.Literal(untpd.scala:380)
        at dotty.tools.dotc.ast.tpd$.Literal(tpd.scala:58)
        at dotty.tools.dotc.ast.tpd$.singleton(tpd.scala:450)
        at dotty.tools.dotc.inlines.Inliner.$anonfun$6(Inliner.scala:586)
        at dotty.tools.dotc.ast.TreeTypeMap.transform(TreeTypeMap.scala:94)
        at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1418)
        at dotty.tools.dotc.ast.TreeTypeMap.transform(TreeTypeMap.scala:145)
        at dotty.tools.dotc.inlines.Inliner.inlined(Inliner.scala:614)
        at dotty.tools.dotc.inlines.Inlines$InlineCall.expand(Inlines.scala:444)
        at dotty.tools.dotc.inlines.Inlines$.inlineCall(Inlines.scala:151)
        at dotty.tools.dotc.transform.Inlining$InliningTreeMap.transform(Inlining.scala:84)
        at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1444)
        at dotty.tools.dotc.ast.TreeMapWithImplicits.transform(TreeMapWithImplicits.scala:73)
        at dotty.tools.dotc.transform.Inlining$InliningTreeMap.transform(Inlining.scala:90)
        at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1444)
        at dotty.tools.dotc.ast.TreeMapWithImplicits.transform(TreeMapWithImplicits.scala:73)
        at dotty.tools.dotc.transform.Inlining$InliningTreeMap.transform(Inlining.scala:90)
        at dotty.tools.dotc.ast.TreeMapWithImplicits.transform(TreeMapWithImplicits.scala:56)
        at dotty.tools.dotc.transform.Inlining$InliningTreeMap.transform(Inlining.scala:78)
        at dotty.tools.dotc.ast.tpd$TreeMapWithPreciseStatContexts.loop$2(tpd.scala:1225)
        at dotty.tools.dotc.ast.tpd$TreeMapWithPreciseStatContexts.transformStats(tpd.scala:1225)
        at dotty.tools.dotc.ast.tpd$TreeMapWithPreciseStatContexts.transformStats(tpd.scala:1227)
        at dotty.tools.dotc.ast.TreeMapWithImplicits.transform(TreeMapWithImplicits.scala:64)
        at dotty.tools.dotc.transform.Inlining$InliningTreeMap.transform(Inlining.scala:90)
        at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1478)
        at dotty.tools.dotc.ast.TreeMapWithImplicits.transform(TreeMapWithImplicits.scala:73)
        at dotty.tools.dotc.transform.Inlining$InliningTreeMap.transform(Inlining.scala:78)
        at dotty.tools.dotc.ast.tpd$TreeMapWithPreciseStatContexts.loop$2(tpd.scala:1225)
        at dotty.tools.dotc.ast.tpd$TreeMapWithPreciseStatContexts.transformStats(tpd.scala:1225)
        at dotty.tools.dotc.ast.tpd$TreeMapWithPreciseStatContexts.transformStats(tpd.scala:1227)
        at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1486)
        at dotty.tools.dotc.ast.TreeMapWithImplicits.transform(TreeMapWithImplicits.scala:73)
        at dotty.tools.dotc.transform.Inlining$InliningTreeMap.transform(Inlining.scala:90)
        at dotty.tools.dotc.transform.Inlining$$anon$2.transform(Inlining.scala:62)
        at dotty.tools.dotc.transform.MacroTransform.run(MacroTransform.scala:18)
        at dotty.tools.dotc.transform.Inlining.run(Inlining.scala:34)
        at dotty.tools.dotc.core.Phases$Phase.runOn$$anonfun$1(Phases.scala:324)
        at scala.collection.immutable.List.map(List.scala:246)
        at dotty.tools.dotc.core.Phases$Phase.runOn(Phases.scala:328)
        at dotty.tools.dotc.transform.Inlining.runOn(Inlining.scala:38)
        at dotty.tools.dotc.Run.runPhases$1$$anonfun$1(Run.scala:244)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
        at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1321)
        at dotty.tools.dotc.Run.runPhases$1(Run.scala:260)
        at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:268)
        at dotty.tools.dotc.Run.compileUnits$$anonfun$adapted$1(Run.scala:277)
        at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:68)
        at dotty.tools.dotc.Run.compileUnits(Run.scala:277)
        at dotty.tools.dotc.Run.compileSources(Run.scala:195)
        at dotty.tools.dotc.Run.compile(Run.scala:179)
        at dotty.tools.dotc.Driver.doCompile(Driver.scala:35)
        at dotty.tools.dotc.Driver.process(Driver.scala:195)
        at dotty.tools.dotc.Driver.process(Driver.scala:163)
        at dotty.tools.dotc.Driver.process(Driver.scala:175)
        at dotty.tools.dotc.Driver.main(Driver.scala:205)
        at dotty.tools.dotc.Main.main(Main.scala)

The following line seems to be the place where we need to fix this

        at dotty.tools.dotc.ast.tpd$.singleton(tpd.scala:450)
        at dotty.tools.dotc.inlines.Inliner.$anonfun$6(Inliner.scala:586)

We need to make sure that the correct context is set when we call singleton in the inliner. What is not clear yet is why the context does not have the correct source position.

https://github.com/lampepfl/dotty/blob/main/compiler/src/dotty/tools/dotc/inlines/Inliner.scala#L586

odersky added a commit to dotty-staging/dotty that referenced this issue Jan 4, 2023
Drop the `transparent` in order to curcumvent scala#16609
odersky added a commit to dotty-staging/dotty that referenced this issue Jan 4, 2023
Drop the `transparent` in order to curcumvent scala#16609
odersky added a commit to dotty-staging/dotty that referenced this issue Jan 4, 2023
Drop the `transparent` in order to curcumvent scala#16609
odersky added a commit to dotty-staging/dotty that referenced this issue Jan 4, 2023
Drop the `transparent` in order to curcumvent scala#16609
odersky added a commit to dotty-staging/dotty that referenced this issue Jan 11, 2023
Drop the `transparent` in order to curcumvent scala#16609
odersky added a commit to dotty-staging/dotty that referenced this issue Jan 12, 2023
Drop the `transparent` in order to curcumvent scala#16609
@Kordyjan Kordyjan added this to the Future versions milestone Jan 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants