Skip to content

Commit 70388bc

Browse files
committed
Better positions for infix term operations.
Preserving the position of infix operators is useful for IDEs' type-at-point. We also preserve the position of the untyped lhs of right-associative operators, this is useful both for IDEs and for error messages, before: 4 |val x: List[Int] = "foo" :: List(1) | ^ | found: String($1$) | required: Int | After: scala> val x: List[Int] = "foo" :: List(1) -- [E007] Type Mismatch Error: <console> --------------------------------------- 4 |val x: List[Int] = "foo" :: List(1) | ^^^^^ | found: String($1$) | required: Int | Note: It would be even nicer if we displayed "String" instead of "String($1$)" since $1$ is synthetic, this commit does not address this.
1 parent abb40d7 commit 70388bc

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,12 +750,14 @@ object desugar {
750750
case Tuple(args) => args mapConserve assignToNamedArg
751751
case _ => right :: Nil
752752
}
753-
Apply(Select(left, op.name), args)
753+
val selectPos = Position(left.pos.start, op.pos.end, op.pos.start)
754+
Apply(Select(left, op.name).withPos(selectPos), args)
754755
} else {
755756
val x = ctx.freshName().toTermName
757+
val selectPos = Position(op.pos.start, right.pos.end, op.pos.start)
756758
new InfixOpBlock(
757759
ValDef(x, TypeTree(), left).withMods(synthetic),
758-
Apply(Select(right, op.name), Ident(x)))
760+
Apply(Select(right, op.name).withPos(selectPos), Ident(x).withPos(left.pos)))
759761
}
760762
}
761763

tests/repl/errmsgs.check

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,11 @@ scala> class Foo() { def bar: Int = 1 }; val foo = new Foo(); foo.barr
7878
4 |class Foo() { def bar: Int = 1 }; val foo = new Foo(); foo.barr
7979
| ^^^^^^^^
8080
| value `barr` is not a member of Foo(foo) - did you mean `foo.bar`?
81+
scala> val x: List[Int] = "foo" :: List(1)
82+
-- [E007] Type Mismatch Error: <console> ---------------------------------------
83+
4 |val x: List[Int] = "foo" :: List(1)
84+
| ^^^^^
85+
| found: String($1$)
86+
| required: Int
87+
|
8188
scala> :quit

0 commit comments

Comments
 (0)