Skip to content

Commit 73533af

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

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
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: 9 additions & 4 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,7 +394,13 @@ 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:
399+
if name.is(ContextFunctionParamName) then
400+
name match
401+
case ContextFunctionParamName(name, _) => name
402+
case name => name
403+
else name
399404

400405
protected def ParamRefNameString(param: ParamRef): String =
401406
ParamRefNameString(param.binder.paramNames(param.paramNum))

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/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)