Skip to content

Commit

Permalink
Tighten rules against escaping local references
Browse files Browse the repository at this point in the history
Fixes the problem in effect-swaps.scala
  • Loading branch information
odersky committed Jun 13, 2024
1 parent 27a3f80 commit 5571b5c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,9 @@ class CheckCaptures extends Recheck, SymTransformer:
/** If actual derives from caps.Capability, yet is not a capturing type itself,
* make its capture set explicit.
*/
private def makeCaptureSetExplicit(actual: Type)(using Context): Type = actual match
private def makeCaptureSetExplicit(actual: Type)(using Context): Type =
if false then actual
else actual match
case CapturingType(_, _) => actual
case _ if actual.derivesFromCapability =>
val cap: CaptureRef = actual match
Expand Down Expand Up @@ -1284,7 +1286,7 @@ class CheckCaptures extends Recheck, SymTransformer:
case ref: TermParamRef
if !allowed.contains(ref) && !seen.contains(ref) =>
seen += ref
if ref.underlying.isRef(defn.Caps_Capability) then
if ref.isMaxCapability then
report.error(i"escaping local reference $ref", tree.srcPos)
else
val widened = ref.captureSetOfInfo
Expand Down
6 changes: 2 additions & 4 deletions compiler/src/dotty/tools/dotc/cc/Setup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -575,10 +575,8 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
!refs.isEmpty
case tp: (TypeRef | AppliedType) =>
val sym = tp.typeSymbol
if sym.isClass then
!sym.isPureClass
else
sym != defn.Caps_Capability && instanceCanBeImpure(tp.superType)
if sym.isClass then !sym.isPureClass
else instanceCanBeImpure(tp.superType)
case tp: (RefinedOrRecType | MatchType) =>
instanceCanBeImpure(tp.underlying)
case tp: AndType =>
Expand Down
4 changes: 4 additions & 0 deletions tests/neg-custom-args/captures/effect-swaps.check
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@
73 | fr.await.ok
|
| longer explanation available when compiling with `-explain`
-- Error: tests/neg-custom-args/captures/effect-swaps.scala:66:15 ------------------------------------------------------
66 | Result.make: // error
| ^^^^^^^^^^^
| escaping local reference contextual$9.type
2 changes: 1 addition & 1 deletion tests/neg-custom-args/captures/effect-swaps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test[T, E](using Async) =
fr.await.ok

def fail4[T, E](fr: Future[Result[T, E]]^) =
Result.make: //lbl ?=> // should be error, escaping label from Result but infers Result[Any, Any]
Result.make: // error
Future: fut ?=>
fr.await.ok

Expand Down

0 comments on commit 5571b5c

Please sign in to comment.