Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
odersky committed Nov 21, 2024
1 parent 8a1d0ab commit 8c583b8
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 15 deletions.
5 changes: 2 additions & 3 deletions compiler/src/dotty/tools/dotc/cc/CaptureOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,9 @@ extension (tp: Type)
tp

/** The first element of this path type */
final def pathRoot(using Context): Type = tp.dealiasKeepAnnots match
final def pathRoot(using Context): Type = tp.dealias match
case tp1: NamedType if tp1.symbol.owner.isClass => tp1.prefix.pathRoot
case ReachCapability(tp1) => tp1.pathRoot
case _ => tp
case tp1 => tp1

/** If this part starts with `C.this`, the class `C`.
* Otherwise, if it starts with a reference `r`, `r`'s owner.
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/cc/CaptureSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ object CaptureSet:
seen += t.symbol
val upper = t.info.bounds.hi
if includeTypevars && upper.isExactlyAny then CaptureSet.universal
else this(cs, t.info.bounds.hi)
else this(cs, upper)
case t @ FunctionOrMethod(args, res @ Existential(_, _))
if args.forall(_.isAlwaysPure) =>
this(cs, Existential.toCap(res))
Expand Down
16 changes: 7 additions & 9 deletions compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,12 @@ class CheckCaptures extends Recheck, SymTransformer:
// A captured reference with the symbol `sym` is visible from the environment
// if `sym` is not defined inside the owner of the environment.
inline def isVisibleFromEnv(sym: Symbol, env: Env) =
if env.kind == EnvKind.NestedInOwner then
!sym.isProperlyContainedIn(env.owner)
else
!sym.isContainedIn(env.owner)
sym.exists && {
if env.kind == EnvKind.NestedInOwner then
!sym.isProperlyContainedIn(env.owner)
else
!sym.isContainedIn(env.owner)
}

/** If captureRef `c` refers to a parameter that is not @use declared, report an error.
* Exception under deferredReaches: If use comes from a nested closure, accept it.
Expand Down Expand Up @@ -450,11 +452,7 @@ class CheckCaptures extends Recheck, SymTransformer:
// Only captured references that are visible from the environment
// should be included.
val included = cs.filter: c =>
val isVisible = c.pathRoot match
case ref: NamedType => isVisibleFromEnv(ref.symbol.owner, env)
case ref: ThisType => isVisibleFromEnv(ref.cls, env)
case ref => false

val isVisible = isVisibleFromEnv(c.pathOwner, env)
if !isVisible then
if ccConfig.deferredReaches
then avoidLocalCapability(c, env, lastEnv)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import config.SourceVersion.*

import dotty.tools.dotc.util.SourcePosition
import dotty.tools.dotc.ast.untpd.{MemberDef, Modifiers, PackageDef, RefTree, Template, TypeDef, ValOrDefDef}
import cc.{CaptureSet, CapturingType, toCaptureSet, IllegalCaptureRef, isRetains, ReachCapability, MaybeCapability, Existential}
import cc.*
import dotty.tools.dotc.parsing.JavaParsers

class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
Expand Down
3 changes: 2 additions & 1 deletion tests/neg-custom-args/captures/bad-uses-2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ class Test:
@use def F = ??? // error
@use val x = ??? // error
@use type T // error
def foo(@use c: Test): Unit = ??? // OK
def foo[@use T](@use c: T): Unit = ??? // OK

0 comments on commit 8c583b8

Please sign in to comment.