Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TyperState assertion failures #12827

Merged
merged 2 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,10 @@ object Contexts {
final def withOwner(owner: Symbol): Context =
if (owner ne this.owner) fresh.setOwner(owner) else this

final def withUncommittedTyperState: Context =
val ts = typerState.uncommittedAncestor
if ts ne typerState then fresh.setTyperState(ts) else this

final def withProperty[T](key: Key[T], value: Option[T]): Context =
if (property(key) == value) this
else value match {
Expand Down
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/core/TyperState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,12 @@ class TyperState() {
*/
def commit()(using Context): Unit = {
Stats.record("typerState.commit")
assert(isCommittable)
assert(isCommittable, s"$this is not committable")
assert(!isCommitted, s"$this is already committed")
reporter.flush()
setCommittable(false)
val targetState = ctx.typerState
assert(!targetState.isCommitted, s"Attempt to commit $this into already committed $targetState")
if constraint ne targetState.constraint then
Stats.record("typerState.commit.new constraint")
constr.println(i"committing $this to $targetState, fromConstr = $constraint, toConstr = ${targetState.constraint}")
Expand All @@ -150,7 +153,6 @@ class TyperState() {
else
targetState.mergeConstraintWith(this)
targetState.gc()
reporter.flush()
isCommitted = true
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,8 @@ object ProtoTypes {
if state.typedArgs.size == args.length then state.typedArgs
else
val passedTyperState = ctx.typerState
inContext(protoCtx) {
val protoTyperState = protoCtx.typerState
inContext(protoCtx.withUncommittedTyperState) {
val protoTyperState = ctx.typerState
val oldConstraint = protoTyperState.constraint
val args1 = args.mapWithIndexConserve((arg, idx) =>
cacheTypedArg(arg, arg => typer.typed(norm(arg, idx)), force = false))
Expand Down
6 changes: 6 additions & 0 deletions tests/neg/i12736a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object Test {
def apply[S](r: Any): Any = r

def test =
(x: Int) => Test(doesntexist, x) // error
}
6 changes: 6 additions & 0 deletions tests/neg/i12736b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object Test {
def apply[S](r: Any)(using DoesntExist): Any = r // error

def test(o: Option[Any]) =
o.map(x => Test(doesntExist, x)) // error
}