Skip to content

Commit 7814a44

Browse files
committed
Typer: Refine position when catching top-level TypeError
Ensures stackoverflows aren't hidden by errors, without needing #4511. Also add relevant testcase from #4385 (comment); I confirmed this fails without the position fix.
1 parent 34c4107 commit 7814a44

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

+7-1
Original file line numberDiff line numberDiff line change
@@ -1862,7 +1862,13 @@ class Typer extends Namer
18621862
assertPositioned(tree)
18631863
try adapt(typedUnadapted(tree, pt, locked), pt, locked)
18641864
catch {
1865-
case ex: TypeError => errorTree(tree, ex.toMessage)
1865+
case ex: TypeError =>
1866+
// This is equivalent to errorTree(tree, ex.toMessage) but
1867+
// uses tree.pos.focus instead of tree.pos. We use this because:
1868+
// - since tree can be a top-level definition, tree.pos can point to the whole definition
1869+
// - that would in turn hide all other type errors inside tree.
1870+
// TODO: might be even better to store positions inside TypeErrors.
1871+
tree withType errorType(ex.toMessage, tree.pos.focus)
18661872
}
18671873
}
18681874

tests/neg/i4385.scala

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class i0 { // error: stack overflow.
2+
class i1 { type i2 }
3+
type i3 = i1.i2 // error
4+
type i4 = i0 { type i1 <: i4 } // this line triggers the overflow
5+
// (and reporting it here would be better).
6+
// This test ensure the above stack overflow is reported and not hidden by the earlier error.
7+
}

0 commit comments

Comments
 (0)