Skip to content

Commit 47025d4

Browse files
Merge pull request #9318 from dotty-staging/fix-#9309
Fix #9309: Don't set TypeErrors on trees that are desugared later
2 parents 78ba7d5 + d1ba12c commit 47025d4

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

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

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2463,21 +2463,26 @@ class Typer extends Namer
24632463
case _ => typedUnadapted(desugar(tree), pt, locked)
24642464
}
24652465

2466-
val ifpt = defn.asContextFunctionType(pt)
2467-
val result =
2468-
if ifpt.exists
2469-
&& xtree.isTerm
2470-
&& !untpd.isContextualClosure(xtree)
2471-
&& !ctx.mode.is(Mode.Pattern)
2472-
&& !ctx.isAfterTyper
2473-
&& !ctx.isInlineContext
2474-
then
2475-
makeContextualFunction(xtree, ifpt)
2476-
else xtree match
2477-
case xtree: untpd.NameTree => typedNamed(xtree, pt)
2478-
case xtree => typedUnnamed(xtree)
2479-
2480-
simplify(result, pt, locked)
2466+
try
2467+
val ifpt = defn.asContextFunctionType(pt)
2468+
val result =
2469+
if ifpt.exists
2470+
&& xtree.isTerm
2471+
&& !untpd.isContextualClosure(xtree)
2472+
&& !ctx.mode.is(Mode.Pattern)
2473+
&& !ctx.isAfterTyper
2474+
&& !ctx.isInlineContext
2475+
then
2476+
makeContextualFunction(xtree, ifpt)
2477+
else xtree match
2478+
case xtree: untpd.NameTree => typedNamed(xtree, pt)
2479+
case xtree => typedUnnamed(xtree)
2480+
2481+
simplify(result, pt, locked)
2482+
catch case ex: TypeError => errorTree(xtree, ex, xtree.sourcePos.focus)
2483+
// use focussed sourcePos since tree might be a large definition
2484+
// and a large error span would hide all errors in interior.
2485+
// TODO: Not clear that hiding is what we want, actually
24812486
}
24822487
}
24832488

@@ -2534,22 +2539,13 @@ class Typer extends Namer
25342539
trace(i"typing $tree, pt = $pt", typr, show = true) {
25352540
record(s"typed $getClass")
25362541
record("typed total")
2537-
if (ctx.phase.isTyper)
2542+
if ctx.phase.isTyper then
25382543
assertPositioned(tree)
2539-
if (tree.source != ctx.source && tree.source.exists)
2544+
if tree.source != ctx.source && tree.source.exists then
25402545
typed(tree, pt, locked)(using ctx.withSource(tree.source))
2541-
else
2542-
try
2543-
if ctx.run.isCancelled then tree.withType(WildcardType)
2544-
else adapt(typedUnadapted(tree, pt, locked), pt, locked)
2545-
catch {
2546-
case ex: TypeError =>
2547-
errorTree(tree, ex, tree.sourcePos.focus)
2548-
// This uses tree.span.focus instead of the default tree.span, because:
2549-
// - since tree can be a top-level definition, tree.span can point to the whole definition
2550-
// - that would in turn hide all other type errors inside tree.
2551-
// TODO: might be even better to store positions inside TypeErrors.
2552-
}
2546+
else if ctx.run.isCancelled then
2547+
tree.withType(WildcardType)
2548+
else adapt(typedUnadapted(tree, pt, locked), pt, locked)
25532549
}
25542550

25552551
def typed(tree: untpd.Tree, pt: Type = WildcardType)(using Context): Tree =
@@ -2875,12 +2871,13 @@ class Typer extends Namer
28752871
* If all this fails, error
28762872
* Parameters as for `typedUnadapted`.
28772873
*/
2878-
def adapt(tree: Tree, pt: Type, locked: TypeVars, tryGadtHealing: Boolean = true)(using Context): Tree = {
2879-
trace(i"adapting $tree to $pt ${if (tryGadtHealing) "" else "(tryGadtHealing=false)" }\n", typr, show = true) {
2880-
record("adapt")
2881-
adapt1(tree, pt, locked, tryGadtHealing)
2882-
}
2883-
}
2874+
def adapt(tree: Tree, pt: Type, locked: TypeVars, tryGadtHealing: Boolean = true)(using Context): Tree =
2875+
try
2876+
trace(i"adapting $tree to $pt ${if (tryGadtHealing) "" else "(tryGadtHealing=false)" }\n", typr, show = true) {
2877+
record("adapt")
2878+
adapt1(tree, pt, locked, tryGadtHealing)
2879+
}
2880+
catch case ex: TypeError => errorTree(tree, ex, tree.sourcePos.focus)
28842881

28852882
final def adapt(tree: Tree, pt: Type)(using Context): Tree =
28862883
adapt(tree, pt, ctx.typerState.ownedVars)

tests/neg/i9309.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
case class Foo(foo: Int) {
2+
inline def foo = (foo) // error
3+
}

0 commit comments

Comments
 (0)