Skip to content

Commit 9658555

Browse files
committed
Rule out nonsensical bounds for capture variables
1 parent e57fc00 commit 9658555

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

+9-2
Original file line numberDiff line numberDiff line change
@@ -3053,8 +3053,15 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
30533053
if sym.isOpaqueAlias then
30543054
checkFullyAppliedType(rhs1, "Opaque type alias must be fully applied, but ")
30553055
checkNoContextFunctionType(rhs1)
3056-
if Feature.ccEnabled && rhs1.tpe.derivesFrom(defn.Caps_CapSet) then
3057-
rhs1.putAttachment(CaptureVar, ())
3056+
if Feature.ccEnabled then
3057+
val isCap = tdef.hasAttachment(CaptureVar)
3058+
rhs1 match
3059+
case TypeBoundsTree(lo, hi, _) =>
3060+
if !isCap && (lo.tpe.derivesFrom(defn.Caps_CapSet) ^ hi.tpe.derivesFrom(defn.Caps_CapSet)) then
3061+
report.error(em"Illegal type bounds: >: $lo <: $hi. Capture-set bounds cannot be mixed with type bounds of other kinds", tdef.srcPos)
3062+
if isCap && !(lo.tpe.derivesFrom(defn.Caps_CapSet) && hi.tpe.derivesFrom(defn.Caps_CapSet)) then
3063+
report.error(em"Illegal type bounds: >: $lo <: $hi. $name^ can only have capture sets as bounds", tdef.srcPos)
3064+
case _ =>
30583065
assignType(cpy.TypeDef(tdef)(name, rhs1), sym)
30593066
}
30603067

tests/neg-custom-args/captures/capset-members5.scala

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ def test =
2424
type M >: {x, y, O.z} <: {C}
2525
type N >: {x} <: {x}
2626
type O >: {O.z} <: {O.z}
27-
type P >: {B,D} // error
27+
type P >: {B,D} // error
28+
type Q >: {E} <: Int // error
29+
type R^ >: {E} <: Int // error
30+
type S^ >: Nothing <: Int // error
31+
type T >: Nothing <: Int

0 commit comments

Comments
 (0)