Skip to content

[Backport] Avoid crash by relaxing TyperState assertion #13296

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

Merged
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
10 changes: 9 additions & 1 deletion compiler/src/dotty/tools/dotc/core/TyperState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,15 @@ class TyperState() {
reporter.flush()
setCommittable(false)
val targetState = ctx.typerState
assert(!targetState.isCommitted, s"Attempt to commit $this into already committed $targetState")

// Committing into an already committed TyperState usually doesn't make
// sense since it means the constraints we're committing won't be propagated
// further, but it can happen if the targetState gets captured in a reported
// Message, because forcing that Message might involve creating and
// committing new TyperStates into the captured one after its been committed.
assert(!targetState.isCommitted || targetState.reporter.hasErrors || targetState.reporter.hasWarnings,
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 Down
24 changes: 24 additions & 0 deletions tests/neg/i13101.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
trait Vehicle
trait Car extends Vehicle

trait Encoder[A]
object Encoder {
implicit val encodeVehicle: Encoder[Vehicle] = ???
implicit val encodeCar: Encoder[Car] = ???
}

trait Route
trait Printer
trait Marshaller[-A] // must be contravariant

object Test {
implicit def marshaller[A: Encoder](implicit p: Printer = ???): Marshaller[A] = ???
// the `Printer` implicit arg seems to be necessary, either with default value, or no implicit in scope

def foo[A](v: A)(implicit m: Marshaller[A]): Route = ???

val route: Route = identity {
val f: (Car => Route) => Route = ??? // ok if s/Car/Vehicle/
f(vehicle => foo(vehicle)) // error: ambiguous implicit
}
}