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

Disallow covariant caps in the lower bound of type members #19624

Merged
merged 4 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ object CheckCaptures:
if !seen.contains(t) then
seen += t
traverseChildren(t)

// Check the lower bound of path dependent types.
// See issue #19330.
val isTypeParam = t.prefix eq NoPrefix
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isTypeParam is misleading as a name. It could also be a local type definition, that also will lead to a NoPrefix. I'd turn it around and define:

val isMember = t.prefix ne NoPrefix

t.info match
case TypeBounds(lo, hi) if !isTypeParam => traverse(lo)
case _ =>
case AnnotatedType(_, ann) if ann.symbol == defn.UncheckedCapturesAnnot =>
()
case t =>
Expand Down
14 changes: 14 additions & 0 deletions tests/neg-custom-args/captures/i19330-alt.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import language.experimental.captureChecking

trait Logger
def usingLogger[T](op: Logger^ => T): T = ???

def foo[T >: () => Logger^](): T =
val leaked = usingLogger[T]: l => // ok
val t: () => Logger^ = () => l
t: T
leaked

def test(): Unit =
val bad: () => Logger^ = foo[() => Logger^] // error
val leaked: Logger^ = bad() // leaked scoped capability!
13 changes: 13 additions & 0 deletions tests/neg-custom-args/captures/i19330-alt2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import language.experimental.captureChecking

trait Logger
def usingLogger[T](op: Logger^ => T): T = ???

trait Foo:
type T >: () => Logger^

def foo: this.T =
val leaked = usingLogger[T]: l => // error
val t: () => Logger^ = () => l
t: T
leaked
21 changes: 21 additions & 0 deletions tests/neg-custom-args/captures/i19330.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import language.experimental.captureChecking

trait Logger
def usingLogger[T](op: Logger^ => T): T = ???

trait Foo:
type T >: () => Logger^

class Bar extends Foo:
type T = () => Logger^

def foo(x: Foo): x.T =
val leaked = usingLogger[x.T]: l => // error
val t: () => Logger^ = () => l
t: x.T
leaked

def test(): Unit =
val bar = new Bar
val bad: bar.T = foo(bar)
val leaked: Logger^ = bad() // leaked scoped capability!
Loading