Skip to content

Commit dab9edb

Browse files
committed
Inferred Contextual params are less nameable
1 parent 32bfd5f commit dab9edb

File tree

7 files changed

+41
-15
lines changed

7 files changed

+41
-15
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,10 +1987,12 @@ object desugar {
19871987
.collect:
19881988
case vd: ValDef => vd
19891989

1990-
def makeContextualFunction(formals: List[Tree], paramNamesOrNil: List[TermName], body: Tree, erasedParams: List[Boolean])(using Context): Function =
1990+
def makeContextualFunction(formals: List[Tree], paramNamesOrNil: List[TermName], body: Tree, erasedParams: List[Boolean], augmenting: Boolean = false)(using Context): Function =
19911991
val paramNames =
1992-
if paramNamesOrNil.nonEmpty then paramNamesOrNil
1993-
else formals.map(_ => ContextFunctionParamName.fresh())
1992+
if paramNamesOrNil.nonEmpty then
1993+
if augmenting then paramNamesOrNil.map(ContextFunctionParamName.fresh(_))
1994+
else paramNamesOrNil
1995+
else List.fill(formals.length)(ContextFunctionParamName.fresh())
19941996
val params = for (tpt, pname) <- formals.zip(paramNames) yield
19951997
ValDef(pname, tpt, EmptyTree).withFlags(Given | Param)
19961998
FunctionWithMods(params, body, Modifiers(Given), erasedParams)

compiler/src/dotty/tools/dotc/core/NameKinds.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,12 @@ object NameKinds {
303303
/** The name of an inferred contextual function parameter:
304304
*
305305
* val x: A ?=> B = b
306+
* val f: (x: A) ?=> B = b
306307
*
307308
* becomes:
308309
*
309310
* val x: A ?=> B = (contextual$1: A) ?=> b
311+
* val f: (x: A) ?=> B = (xcontextual$1: A) ?=> b
310312
*/
311313
val ContextFunctionParamName: UniqueNameKind = new UniqueNameKind("contextual$")
312314

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package printing
44
import core.*
55
import Texts.*, Types.*, Flags.*, Names.*, Symbols.*, NameOps.*, Constants.*, Denotations.*
66
import StdNames.*
7+
import NameKinds.ContextFunctionParamName
78
import Contexts.*
89
import Scopes.Scope, Denotations.Denotation, Annotations.Annotation
910
import StdNames.nme
@@ -361,9 +362,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
361362
case tp: LazyRef =>
362363
def refTxt =
363364
try toTextGlobal(tp.ref)
364-
catch {
365-
case ex: Throwable => Str("...")
366-
}
365+
catch case _: Throwable => Str("...") // reconsider catching errors
367366
"LazyRef(" ~ refTxt ~ ")"
368367
case Range(lo, hi) =>
369368
toText(lo) ~ ".." ~ toText(hi)
@@ -395,10 +394,17 @@ class PlainPrinter(_ctx: Context) extends Printer {
395394
Text(lam.paramRefs.map(paramText), ", ")
396395
}
397396

398-
protected def ParamRefNameString(name: Name): String = nameString(name)
397+
protected def ParamRefNameString(name: Name): String =
398+
nameString(name)
399399

400400
protected def ParamRefNameString(param: ParamRef): String =
401-
ParamRefNameString(param.binder.paramNames(param.paramNum))
401+
val name = param.binder.paramNames(param.paramNum)
402+
ParamRefNameString:
403+
if name.is(ContextFunctionParamName) then
404+
name match
405+
case ContextFunctionParamName(name, _) if !name.isEmpty => name
406+
case name => name
407+
else name
402408

403409
/** The name of the symbol without a unique id. */
404410
protected def simpleNameString(sym: Symbol): String = nameString(sym.name)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3912,7 +3912,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
39123912
case _ => paramTypes.map(_ => false)
39133913
}
39143914

3915-
val ifun = desugar.makeContextualFunction(paramTypes, paramNamesOrNil, tree, erasedParams)
3915+
val ifun = desugar.makeContextualFunction(paramTypes, paramNamesOrNil, tree, erasedParams, augmenting = true)
39163916
typr.println(i"make contextual function $tree / $pt ---> $ifun")
39173917
typedFunctionValue(ifun, pt)
39183918
.tap:

tests/neg-custom-args/captures/filevar.check

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
|The leakage occurred when trying to match the following types:
66
|
77
|Found: (l: scala.caps.Capability^) ?->'s2 File^'s3 ->'s4 Unit
8-
|Required: (l: scala.caps.Capability^) ?-> (f: File^{l}) => Unit
8+
|Required: (l²: scala.caps.Capability^) ?-> (f: File^{l²}) => Unit
99
|
10-
|where: => refers to a root capability associated with the result type of (using l: scala.caps.Capability^): (f: File^{l}) => Unit
10+
|where: => refers to a root capability associated with the result type of (using l²: scala.caps.Capability^): (f: File^{l²}) => Unit
1111
| ^ refers to the universal root capability
12+
| l is a reference to a value parameter
13+
| l² is a reference to a value parameter
1214
16 | val o = Service()
1315
17 | o.file = f
1416
18 | o.log

tests/neg-custom-args/captures/i15923.check

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
|The leakage occurred when trying to match the following types:
66
|
77
|Found: (lcap: scala.caps.Capability^) ?->'s2 Cap^'s3 ->'s4 Id[Cap^'s5]^'s6
8-
|Required: (lcap: scala.caps.Capability^) ?-> Cap^{lcap} => Id[Cap^'s1]^'s7
8+
|Required: (lcap²: scala.caps.Capability^) ?-> Cap^{lcap²} => Id[Cap^'s1]^'s7
99
|
10-
|where: => refers to a root capability associated with the result type of (using lcap: scala.caps.Capability^): Cap^{lcap} => Id[Cap^'s1]^'s7
11-
| ^ refers to the universal root capability
12-
| cap is a root capability associated with the result type of (x$0: Cap^'s3): Id[Cap^'s5]^'s6
10+
|where: => refers to a root capability associated with the result type of (using lcap²: scala.caps.Capability^): Cap^{lcap²} => Id[Cap^'s1]^'s7
11+
| ^ refers to the universal root capability
12+
| cap is a root capability associated with the result type of (x$0: Cap^'s3): Id[Cap^'s5]^'s6
13+
| lcap is a reference to a value parameter
14+
| lcap² is a reference to a value parameter
1315
|
1416
| longer explanation available when compiling with `-explain`

tests/neg/i24375.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
trait Ord[X]:
2+
def compare(x: X, y: X): Int
3+
type T
4+
5+
trait Show[X]:
6+
def show(x: X): String
7+
8+
val f0: Show[String] ?=> String = summon[Show[String]].show("hello, world") * 2
9+
10+
val f1: (x: Show[String]) ?=> String = x.show("hello, world") * 2 // error
11+
12+
val f2 = (x: Show[String]) ?=> x.show("hello, world") * 2

0 commit comments

Comments
 (0)