Skip to content

Commit a727545

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 c26d171 commit a727545

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
@@ -1857,7 +1857,13 @@ class Typer extends Namer
18571857
assertPositioned(tree)
18581858
try adapt(typedUnadapted(tree, pt, locked), pt, locked)
18591859
catch {
1860-
case ex: TypeError => errorTree(tree, ex.toMessage)
1860+
case ex: TypeError =>
1861+
// This is equivalent to errorTree(tree, ex.toMessage) but
1862+
// uses tree.pos.focus instead of tree.pos. We use this because:
1863+
// - since tree can be a top-level definition, tree.pos can point to the whole definition
1864+
// - that would in turn hide all other type errors inside tree.
1865+
// TODO: might be even better to store positions inside TypeErrors.
1866+
tree withType errorType(ex.toMessage, tree.pos.focus)
18611867
}
18621868
}
18631869

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)