Skip to content

Commit bdf2b54

Browse files
committed
Adapt transparent value checking to new restrictions
1 parent 812d22d commit bdf2b54

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,7 @@ object messages {
17481748
val kind = "Syntax"
17491749
val msg = hl"no explicit ${"return"} allowed from transparent $owner"
17501750
val explanation =
1751-
hl"""Methods marked with ${"inline"} modifier may not use ${"return"} statements.
1751+
hl"""Methods marked with ${"transparent"} modifier may not use ${"return"} statements.
17521752
|Instead, you should rely on the last expression's value being
17531753
|returned from a method.
17541754
|"""
@@ -2057,7 +2057,7 @@ object messages {
20572057
case class ParamsNoTransparent(owner: Symbol)(implicit ctx: Context)
20582058
extends Message(ParamsNoTransparentID) {
20592059
val kind = "Syntax"
2060-
val msg = hl"""${"transparent"} modifier cannot be used for a ${owner.showKind} parameter"""
2060+
val msg = hl"""${"transparent"} modifier cannot be used for a parameter of a non-transparent method"""
20612061
val explanation = ""
20622062
}
20632063

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,10 @@ object Checking {
369369
if (!ok && !sym.is(Synthetic))
370370
fail(i"modifier `$flag` is not allowed for this definition")
371371

372-
if (sym.is(Transparent) && ((sym.is(ParamAccessor) && sym.owner.isClass) || sym.is(TermParam) && sym.owner.isClassConstructor))
372+
if (sym.is(Transparent) &&
373+
( sym.is(ParamAccessor) && sym.owner.isClass
374+
|| sym.is(TermParam) && !sym.owner.isTransparentMethod
375+
))
373376
fail(ParamsNoTransparent(sym.owner))
374377

375378
if (sym.is(ImplicitCommon)) {
@@ -669,14 +672,11 @@ trait Checking {
669672
def checkTransparentConformant(tree: Tree, isFinal: Boolean, what: => String)(implicit ctx: Context): Unit = {
670673
// final vals can be marked transparent even if they're not pure, see Typer#patchFinalVals
671674
val purityLevel = if (isFinal) Idempotent else Pure
672-
tree.tpe match {
673-
case tp: TermRef if tp.symbol.is(TransparentParam) => // ok
674-
case tp => tp.widenTermRefExpr match {
675-
case tp: ConstantType if exprPurity(tree) >= purityLevel => // ok
676-
case _ =>
677-
val allow = ctx.erasedTypes || ctx.owner.ownersIterator.exists(_.isTransparentMethod)
678-
if (!allow) ctx.error(em"$what must be a constant expression", tree.pos)
679-
}
675+
tree.tpe.widenTermRefExpr match {
676+
case tp: ConstantType if exprPurity(tree) >= purityLevel => // ok
677+
case _ =>
678+
val allow = ctx.erasedTypes || ctx.owner.ownersIterator.exists(_.isTransparentMethod)
679+
if (!allow) ctx.error(em"$what must be a constant expression", tree.pos)
680680
}
681681
}
682682

tests/neg/inlinevals.scala

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
object Test {
22

3-
def power(x: Double, transparent n: Int): Double = ???
3+
def power0(x: Double, transparent n: Int): Double = ??? // error
4+
5+
transparent def power(x: Double, transparent n: Int): Double = ??? // ok
46

57
transparent val N = 10
68
def X = 20
@@ -19,12 +21,15 @@ object Test {
1921

2022
transparent val xs = List(1, 2, 3) // error: must be a constant expression
2123

22-
def f(transparent xs: List[Int]) = xs
24+
transparent def foo(x: Int) = {
25+
26+
def f(transparent xs: List[Int]) = xs // error
2327

24-
f(List(1, 2, 3)) // error: must be a constant expression
28+
transparent val y = { println("hi"); 1 } // ok
29+
transparent val z = x // ok
2530

26-
def byname(transparent f: => String): Int = ??? // ok
31+
}
2732

28-
byname("hello" ++ " world")
33+
transparent def byname(transparent f: => String): Int = ??? // ok
2934

3035
}

tests/pos/i3597.scala

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)