Skip to content

Commit 13942b0

Browse files
authored
Merge pull request #12837 from dotty-staging/backport-skip-already-committed
[backport] Fix TyperState assertion failures
2 parents 945ef3f + b3ac78f commit 13942b0

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

compiler/src/dotty/tools/dotc/core/Contexts.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,10 @@ object Contexts {
526526
final def withOwner(owner: Symbol): Context =
527527
if (owner ne this.owner) fresh.setOwner(owner) else this
528528

529+
final def withUncommittedTyperState: Context =
530+
val ts = typerState.uncommittedAncestor
531+
if ts ne typerState then fresh.setTyperState(ts) else this
532+
529533
final def withProperty[T](key: Key[T], value: Option[T]): Context =
530534
if (property(key) == value) this
531535
else value match {

compiler/src/dotty/tools/dotc/core/TyperState.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,12 @@ class TyperState() {
138138
*/
139139
def commit()(using Context): Unit = {
140140
Stats.record("typerState.commit")
141-
assert(isCommittable)
141+
assert(isCommittable, s"$this is not committable")
142+
assert(!isCommitted, s"$this is already committed")
143+
reporter.flush()
142144
setCommittable(false)
143145
val targetState = ctx.typerState
146+
assert(!targetState.isCommitted, s"Attempt to commit $this into already committed $targetState")
144147
if constraint ne targetState.constraint then
145148
Stats.record("typerState.commit.new constraint")
146149
constr.println(i"committing $this to $targetState, fromConstr = $constraint, toConstr = ${targetState.constraint}")
@@ -150,7 +153,6 @@ class TyperState() {
150153
else
151154
targetState.mergeConstraintWith(this)
152155
targetState.gc()
153-
reporter.flush()
154156
isCommitted = true
155157
}
156158

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,8 @@ object ProtoTypes {
388388
if state.typedArgs.size == args.length then state.typedArgs
389389
else
390390
val passedTyperState = ctx.typerState
391-
inContext(protoCtx) {
392-
val protoTyperState = protoCtx.typerState
391+
inContext(protoCtx.withUncommittedTyperState) {
392+
val protoTyperState = ctx.typerState
393393
val oldConstraint = protoTyperState.constraint
394394
val args1 = args.mapWithIndexConserve((arg, idx) =>
395395
cacheTypedArg(arg, arg => typer.typed(norm(arg, idx)), force = false))

tests/neg/i12736a.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object Test {
2+
def apply[S](r: Any): Any = r
3+
4+
def test =
5+
(x: Int) => Test(doesntexist, x) // error
6+
}

tests/neg/i12736b.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object Test {
2+
def apply[S](r: Any)(using DoesntExist): Any = r // error
3+
4+
def test(o: Option[Any]) =
5+
o.map(x => Test(doesntExist, x)) // error
6+
}

0 commit comments

Comments
 (0)