Skip to content

Commit 04df2e4

Browse files
committed
SI-7915 Corrected range positions created during default args expansion
The tree created during expansion of default arguments contained trees with the wrong type of positions. Let's discuss this with an example. Below is the tree generated for the `foo` method in the test class included in this commit. Before this commit: ``` [54:94]def foo(): [58]Unit = <70:90>{ [70:79]<artifact> val qual$1: [70]Bar = [70:79][70:79][70:79]new [74:77]Bar(); [80]<artifact> val x$1: [80]Int = [80]qual$1.bar$default$1; <70:90><70:83>qual$1.bar([80]x$1) } ``` Now: ``` [54:99]def foo(): [58]Unit = <70:95>{ <70:84><artifact> val qual$1: [70]Bar = [70:84][70:84][70:84]new [74:77]Bar(); [85]<artifact> val x$1: [85]Int = [85]qual$1.bar$default$1; <70:95>[84:88]qual$1.bar([85]x$1) } ``` Here are the list of changes: * The synthetic `qual$1` has a transparent position, instead of a range position. * The new Select tree (i.e., `qual$1.bar`) should always have a range position, because `selected` (i.e., the called method) is always visible in the source (in fact, this is the whole point of the fix, we need a range position or hyperlinking request from the Scala IDE won't work). * The Block that contains the expanded default arguments is forced to have a transparent position, as it never exist in the original source. The tricky part of the fix is the position assigned to the new Select tree, which needs to respect the range position's invariants. In the specific case, we ought to make sure that range positions don't overlap. Therefore, the position assigned to the new Select tree is computed by intersecting the original Select position (i.e., `baseFun`'s position) and the original qualifier's position (i.e., `qual`'s position). If you take a closer look at the range positions assigned in the tree after this commit, you'll notice that the range position of the `qual$1`'s rhs (i.e., [70:84]), and `qual$1.bar` (i.e., [84:88]) might seem to overlap, because the former ends where the latter begins. However, this not the case because of the range position's invariant 2, which states: > Invariant 2: in a range position, start <= point < end Hence, the above two positions aren't overlapping as far as the compiler is concerned. One additional aspect (that may look like a detail) is that we make sure to never generate a position such that its start is after its end. This is why we take the position with the smallest end point. Furthermore, calling `withStart` would turn any position in a range position, which isn't desiderable in general (and, even worse, this can lead to generation of invalid positions - bad offsets - if the computation is performed on offset positions). Hence, the position's computation is only performed when both `baseFun` and `qual` positions are range positions. Indeed, I expect this to be always the case if the compiler is started with -Yrangepos. (cherry picked from commit 3009a52)
1 parent b9f6860 commit 04df2e4

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ trait NamesDefaults { self: Analyzer =>
164164

165165
// never used for constructor calls, they always have a stable qualifier
166166
def blockWithQualifier(qual: Tree, selected: Name) = {
167-
val sym = blockTyper.context.owner.newValue(unit.freshTermName("qual$"), qual.pos) setInfo uncheckedBounds(qual.tpe)
167+
val sym = blockTyper.context.owner.newValue(unit.freshTermName("qual$"), qual.pos) setInfo uncheckedBounds(qual.tpe) setPos (qual.pos.makeTransparent)
168168
blockTyper.context.scope enter sym
169169
val vd = atPos(sym.pos)(ValDef(sym, qual) setType NoType)
170170
// it stays in Vegas: SI-5720, SI-5727
@@ -175,13 +175,16 @@ trait NamesDefaults { self: Analyzer =>
175175
// setSymbol below is important because the 'selected' function might be overloaded. by
176176
// assigning the correct method symbol, typedSelect will just assign the type. the reason
177177
// to still call 'typed' is to correctly infer singleton types, SI-5259.
178-
val f = blockTyper.typedOperator(Select(newQual, selected).setSymbol(baseFun1.symbol))
178+
val selectPos =
179+
if(qual.pos.isRange && baseFun.pos.isRange) qual.pos.union(baseFun.pos).withStart(Math.min(qual.pos.end, baseFun.pos.end))
180+
else baseFun.pos
181+
val f = blockTyper.typedOperator(Select(newQual, selected).setSymbol(baseFun1.symbol).setPos(selectPos))
179182
if (funTargs.isEmpty) f
180183
else TypeApply(f, funTargs).setType(baseFun.tpe)
181184
}
182185

183186
val b = Block(List(vd), baseFunTransformed)
184-
.setType(baseFunTransformed.tpe).setPos(baseFun.pos)
187+
.setType(baseFunTransformed.tpe).setPos(baseFun.pos.makeTransparent)
185188
context.namedApplyBlockInfo =
186189
Some((b, NamedApplyInfo(Some(newQual), defaultTargs, Nil, blockTyper)))
187190
b

test/files/presentation/t7915.check

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
reload: Foo.scala
2+
3+
askHyperlinkPos for `Bar` at (7,11) Foo.scala
4+
================================================================================
5+
[response] found askHyperlinkPos for `Bar` at (1,7) Foo.scala
6+
================================================================================
7+
8+
askHyperlinkPos for `bar` at (7,22) Foo.scala
9+
================================================================================
10+
[response] found askHyperlinkPos for `bar` at (2,7) Foo.scala
11+
================================================================================
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import scala.tools.nsc.interactive.tests.InteractiveTest
2+
3+
object Test extends InteractiveTest {
4+
override def runDefaultTests() {
5+
sourceFiles foreach (src => askLoadedTyped(src).get)
6+
super.runDefaultTests()
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Bar {
2+
def bar(b: Int = 2) {}
3+
}
4+
5+
class Foo {
6+
def foo() {
7+
new Bar/*#*/().bar/*#*/()
8+
}
9+
}

0 commit comments

Comments
 (0)