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 #18246: correctly compute capture sets in TypeComparer.glb #18254

Merged
merged 3 commits into from
Jul 23, 2023
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
9 changes: 4 additions & 5 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2610,12 +2610,11 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
case tp1: TypeVar if tp1.isInstantiated =>
tp1.underlying & tp2
case CapturingType(parent1, refs1) =>
if subCaptures(tp2.captureSet, refs1, frozen = true).isOK
val refs2 = tp2.captureSet
if subCaptures(refs2, refs1, frozen = true).isOK
&& tp1.isBoxedCapturing == tp2.isBoxedCapturing
then
parent1 & tp2
else
tp1.derivedCapturingType(parent1 & tp2, refs1)
then (parent1 & tp2).capturing(refs2)
else tp1.derivedCapturingType(parent1 & tp2, refs1)
case tp1: AnnotatedType if !tp1.isRefining =>
tp1.underlying & tp2
case _ =>
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1940,7 +1940,7 @@ object Types {
* the two capture sets are combined.
*/
def capturing(cs: CaptureSet)(using Context): Type =
if cs.isConst && cs.subCaptures(captureSet, frozen = true).isOK then this
if cs.isAlwaysEmpty || cs.isConst && cs.subCaptures(captureSet, frozen = true).isOK then this
else this match
case CapturingType(parent, cs1) => parent.capturing(cs1 ++ cs)
case _ => CapturingType(this, cs)
Expand Down
7 changes: 7 additions & 0 deletions tests/neg-custom-args/captures/cc-glb.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/cc-glb.scala:7:19 ----------------------------------------
7 | val x2: Foo[T] = x1 // error
| ^^
| Found: (x1 : (Foo[T]^) & (Foo[Any]^{io}))
| Required: Foo[T]
|
| longer explanation available when compiling with `-explain`
8 changes: 8 additions & 0 deletions tests/neg-custom-args/captures/cc-glb.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import language.experimental.captureChecking
trait Cap
trait Foo[+T]

def magic[T](io: Cap^, x: Foo[T]^{io}): Foo[T]^{} =
val x1: Foo[T]^{cap} & Foo[Any]^{io} = x
val x2: Foo[T] = x1 // error
x2 // boom, an impure value becomes pure