Skip to content

Commit dca0248

Browse files
authored
Merge branch 'main' into merge-26985-27004
2 parents 1ffaa02 + 07fb189 commit dca0248

46 files changed

Lines changed: 308 additions & 117 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 86 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,13 @@ object desugar {
213213
&& (!mods.is(Private) || ctx.owner.is(Trait) || ctx.owner.isPackageObject)
214214
}
215215

216-
/** var x: Int = expr
216+
/** Normalize the member name and generate a setter where needed.
217+
* ```
218+
* var x: Int = expr
217219
* ==>
218220
* def x: Int = expr
219221
* def x_=($1: <TypeTree()>): Unit = ()
220-
*
221-
* Generate setter where needed
222+
* ```
222223
*/
223224
def valDef(vdef0: ValDef)(using Context): Tree =
224225
val vdef @ ValDef(_, tpt, rhs) = vdef0
@@ -1274,14 +1275,13 @@ object desugar {
12741275
case mdef: DefDef => defDef(extMethod(mdef, ext.paramss))
12751276
case _ => EmptyTree // we ignore all the other trees. Error was reported during parsing.
12761277
}
1277-
/** Transforms
1278-
*
1278+
/** Type pattern variables get an annotation `@patternType`.
1279+
* ```
12791280
* <mods> type t >: Low <: Hi
1280-
* to
1281-
*
1282-
* @patternType <mods> type $T >: Low <: Hi
1283-
*
1284-
* if the type has a pattern variable name
1281+
* ==>
1282+
* @patternType <mods> type t >: Low <: Hi
1283+
* ```
1284+
* if the type `t` has a pattern variable name.
12851285
*/
12861286
def quotedPatternTypeDef(tree: TypeDef)(using Context): TypeDef = {
12871287
assert(ctx.mode.isQuotedPattern)
@@ -1350,8 +1350,12 @@ object desugar {
13501350
case _ => body
13511351
cpy.PolyFunction(tree)(tree.targs, stripped(tree.body)).asInstanceOf[PolyFunction]
13521352

1353-
/** Desugar [T_1, ..., T_M] => (P_1, ..., P_N) => R
1354-
* Into scala.PolyFunction { def apply[T_1, ..., T_M](x$1: P_1, ..., x$N: P_N): R }
1353+
/** Desugar a `PolyFunction` to a type with the corresponding `apply` member.
1354+
* ```
1355+
* [T_1, ..., T_M] => (P_1, ..., P_N) => R
1356+
* ==>
1357+
* scala.PolyFunction { def apply[T_1, ..., T_M](x$1: P_1, ..., x$N: P_N): R }
1358+
* ```
13551359
*/
13561360
def makePolyFunctionType(tree: PolyFunction)(using Context): RefinedTypeTree = (tree: @unchecked) match
13571361
case PolyFunction(tparams: List[untpd.TypeDef] @unchecked, fun @ untpd.Function(formals, res)) =>
@@ -1789,8 +1793,12 @@ object desugar {
17891793
return apply
17901794
}
17911795

1792-
/** Translate throws type `A throws E1 | ... | En` to
1793-
* $throws[... $throws[A, E1] ... , En].
1796+
/** Translate throws type to `runtime.$throws`.
1797+
* ```
1798+
* A throws E1 | ... | En
1799+
* ==>
1800+
* $throws[... $throws[A, E1] ... , En].
1801+
* ```
17941802
*/
17951803
def throws(tpt: Tree, op: Ident, excepts: Tree)(using Context): AppliedTypeTree = excepts match
17961804
case Parens(excepts1) =>
@@ -1970,10 +1978,12 @@ object desugar {
19701978
}
19711979

19721980
/** Make closure corresponding to function.
1981+
* ```
19731982
* [tparams] => params => body
19741983
* ==>
19751984
* def $anonfun[tparams](params) = body
19761985
* Closure($anonfun)
1986+
* ```
19771987
*/
19781988
def makeClosure(tparams: List[TypeDef], vparams: List[ValDef], body: Tree, tpt: Tree | Null = null, span: Span)(using Context): Block =
19791989
val paramss: List[ParamClause] =
@@ -1985,39 +1995,49 @@ object desugar {
19851995
.withMods(synthetic | Artifact),
19861996
Closure(Nil, Ident(nme.ANON_FUN), EmptyTree).withSpan(span))
19871997

1988-
/** If `nparams` == 1, expand partial function
1998+
/** Expand a pattern matching anonymous function to a function with match body.
19891999
*
2000+
* If `nparams` == 1, expand partial function
2001+
* ```
19902002
* { cases }
19912003
* ==>
19922004
* x$1 => (x$1 @unchecked?) match { cases }
2005+
* ```
19932006
*
19942007
* If `nparams` != 1, expand instead to
1995-
*
2008+
* ```
19962009
* (x$1, ..., x$n) => (x$0, ..., x${n-1} @unchecked?) match { cases }
2010+
* ```
19972011
*/
19982012
def makeCaseLambda(cases: List[CaseDef], checkMode: MatchCheck, nparams: Int = 1)(using Context): Function = {
19992013
val params = (1 to nparams).toList.map(makeSyntheticParameter(_))
20002014
val selector = makeTuple(params.map(p => Ident(p.name)))
20012015
Function(params, Match(makeSelector(selector, checkMode), cases))
20022016
}
20032017

2004-
/** Map n-ary function `(x1: T1, ..., xn: Tn) => body` where n != 1 to unary function as follows:
2018+
/** Map an n-ary function to a unary function.
20052019
*
2020+
* Map n-ary function `(x1: T1, ..., xn: Tn) => body` where n != 1 to unary function as follows:
2021+
*
2022+
* ```
20062023
* (x$1: (T1, ..., Tn)) => {
20072024
* val x1: T1 = x$1._1
20082025
* ...
20092026
* val xn: Tn = x$1._n
20102027
* body
20112028
* }
2029+
* ```
20122030
*
20132031
* or if `isGenericTuple`
20142032
*
2015-
* (x$1: (T1, ... Tn) => {
2033+
* ```
2034+
* (x$1: (T1, ... Tn)) => {
20162035
* val x1: T1 = x$1.apply(0)
20172036
* ...
20182037
* val xn: Tn = x$1.apply(n-1)
20192038
* body
20202039
* }
2040+
* ```
20212041
*
20222042
* If some of the Ti's are absent, omit the : (T1, ..., Tn) type ascription
20232043
* in the selector.
@@ -2102,49 +2122,66 @@ object desugar {
21022122
*
21032123
* 1. if betterFors is enabled:
21042124
*
2125+
* ```
21052126
* for () do E ==> E
2127+
* ```
21062128
* or
2129+
* ```
21072130
* for () yield E ==> E
2131+
* ```
21082132
*
21092133
* (Where empty for-comprehensions are excluded by the parser)
21102134
*
21112135
* 2.
21122136
*
2137+
* ```
21132138
* for (P <- G) do E ==> G.foreach (P => E)
2139+
* ```
21142140
*
21152141
* Here and in the following (P => E) is interpreted as the function (P => E)
21162142
* if P is a variable pattern and as the partial function { case P => E } otherwise.
21172143
*
21182144
* 3.
21192145
*
2146+
* ```
21202147
* for (P <- G) yield E ==> G.map (P => E)
2148+
* ```
21212149
*
21222150
* 4.
21232151
*
2152+
* ```
21242153
* for (P_1 <- G_1; P_2 <- G_2; ...) ...
21252154
* ==>
21262155
* G_1.flatMap (P_1 => for (P_2 <- G_2; ...) ...)
2156+
* ```
21272157
*
21282158
* 5.
21292159
*
2160+
* ```
21302161
* for (P <- G; if E; ...) ...
21312162
* ==>
21322163
* for (P <- G.withFilter (P => E); ...) ...
2164+
* ```
21332165
*
21342166
* 6. For any N, if betterFors is enabled:
21352167
*
2168+
* ```
21362169
* for (P <- G; P_1 = E_1; ... P_N = E_N; P1 <- G1; ...) ...
21372170
* ==>
21382171
* G.flatMap (P => for (P_1 = E_1; ... P_N = E_N; ...))
2172+
* ```
21392173
*
21402174
* 7. For any N, if betterFors is enabled:
21412175
*
2176+
* ```
21422177
* for (P <- G; P_1 = E_1; ... P_N = E_N) ...
21432178
* ==>
21442179
* G.map (P => for (P_1 = E_1; ... P_N = E_N) ...)
2180+
* ```
21452181
*
21462182
* 8. For any N:
21472183
*
2184+
* ```
21482185
* for (P <- G; P_1 = E_1; ... P_N = E_N; ...)
21492186
* ==>
21502187
* for (TupleN(P, P_1, ... P_N) <-
@@ -2154,18 +2191,21 @@ object desugar {
21542191
* val x_N @ P_N = E_N
21552192
* TupleN(x, x_1, ..., x_N)
21562193
* }; if E; ...)
2194+
* ```
21572195
*
21582196
* If any of the P_i are variable patterns, the corresponding `x_i @ P_i` is not generated
21592197
* and the variable constituting P_i is used instead of x_i
21602198
*
21612199
* 9. For any N, if betterFors is enabled:
21622200
*
2201+
* ```
21632202
* for (P_1 = E_1; ... P_N = E_N; ...)
21642203
* ==>
21652204
* {
21662205
* val x_N @ P_N = E_N
21672206
* for (...)
21682207
* }
2208+
* ```
21692209
*
21702210
* @param mapName The name to be used for maps (either map or foreach)
21712211
* @param flatMapName The name to be used for flatMaps (either flatMap or foreach)
@@ -2231,30 +2271,40 @@ object desugar {
22312271
(Bind(name, pat), Ident(name))
22322272

22332273
/** Make a pattern filter:
2274+
* ```
22342275
* rhs.withFilter { case pat => true case _ => false }
2276+
* ```
22352277
*
22362278
* On handling irrefutable patterns:
22372279
* The idea is to wait until the pattern matcher sees a call
22382280
*
2281+
* ```
22392282
* xs withFilter { cases }
2283+
* ```
22402284
*
22412285
* where cases can be proven to be refutable i.e. cases would be
22422286
* equivalent to { case _ => true }
22432287
*
22442288
* In that case, compile to
22452289
*
2290+
* ```
22462291
* xs withFilter alwaysTrue
2292+
* ```
22472293
*
22482294
* where `alwaysTrue` is a predefined function value:
22492295
*
2296+
* ```
22502297
* val alwaysTrue: Any => Boolean = true
2298+
* ```
22512299
*
22522300
* In the libraries operations can take advantage of alwaysTrue to shortcircuit the
22532301
* withFilter call.
22542302
*
2303+
* ```
22552304
* def withFilter(f: Elem => Boolean) =
22562305
* if (f eq alwaysTrue) this // or rather identity filter monadic applied to this
22572306
* else real withFilter
2307+
* ```
22582308
*/
22592309
def makePatFilter(rhs: Tree, pat: Tree): Tree = {
22602310
val cases = List(
@@ -2265,7 +2315,7 @@ object desugar {
22652315

22662316
/** Is pattern `pat` irrefutable when matched against `rhs`?
22672317
* We only can do a simple syntactic check here; a more refined check
2268-
* is done later in the pattern matcher (see discussion in @makePatFilter).
2318+
* is done later in the pattern matcher (see discussion in [makePatFilter]).
22692319
*/
22702320
def isIrrefutable(pat: Tree, rhs: Tree): Boolean = {
22712321
def matchesTuple(pats: List[Tree], rhs: Tree): Boolean = rhs match {
@@ -2446,15 +2496,20 @@ object desugar {
24462496
}
24472497

24482498
/** Turn a function value `handlerFun` into a catch case for a try.
2499+
*
24492500
* If `handlerFun` is a partial function, translate to
24502501
*
2502+
* ```
24512503
* case ex =>
24522504
* val ev$1 = handlerFun
24532505
* if ev$1.isDefinedAt(ex) then ev$1.apply(ex) else throw ex
2506+
* ```
24542507
*
24552508
* Otherwise translate to
24562509
*
2510+
* ```
24572511
* case ex => handlerFun.apply(ex)
2512+
* ```
24582513
*/
24592514
def makeTryCase(handlerFun: tpd.Tree)(using Context): CaseDef =
24602515
val handler = TypedSplice(handlerFun)
@@ -2476,25 +2531,33 @@ object desugar {
24762531
/** Create a class definition with the same info as the refined type given by `parent`
24772532
* and `refinements`.
24782533
*
2534+
* ```
24792535
* parent { refinements }
24802536
* ==>
24812537
* trait <refinement> extends core { this: self => refinements }
2538+
* ```
24822539
*
24832540
* Here, `core` is the (possibly parameterized) class part of `parent`.
24842541
* If `parent` is the same as `core`, self is empty. Otherwise `self` is `parent`.
24852542
*
24862543
* Example: Given
24872544
*
2545+
* ```
24882546
* class C
24892547
* type T1 = C { type T <: A }
2548+
* ```
24902549
*
24912550
* the refined type
24922551
*
2552+
* ```
24932553
* T1 { type T <: B }
2554+
* ```
24942555
*
24952556
* is expanded to
24962557
*
2558+
* ```
24972559
* trait <refinement> extends C { this: T1 => type T <: A }
2560+
* ```
24982561
*
24992562
* The result of this method is used for validity checking, is thrown away afterwards.
25002563
* @param parent The type of `parent`
@@ -2533,9 +2596,13 @@ object desugar {
25332596

25342597
/** Ensure the given function tree use only ValDefs for parameters.
25352598
* For example,
2599+
* ```
25362600
* FunctionWithMods(List(TypeTree(A), TypeTree(B)), body, mods, erasedParams)
2601+
* ```
25372602
* gets converted to
2603+
* ```
25382604
* FunctionWithMods(List(ValDef(x$1, A), ValDef(x$2, B)), body, mods, erasedParams)
2605+
* ```
25392606
*/
25402607
def makeFunctionWithValDefs(tree: Function)(using Context): Function = {
25412608
val Function(args, result) = tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,16 @@ trait TreeInfo[T <: Untyped] { self: Trees.Instance[T] =>
6767
/** The method part of an application node, possibly enclosed in a block
6868
* with only valdefs as statements. the reason for also considering blocks
6969
* is that named arguments can transform a call into a block, e.g.
70+
* ```
7071
* <init>(b = foo, a = bar)
72+
* ```
7173
* is transformed to
74+
* ```
7275
* { val x$1 = foo
7376
* val x$2 = bar
7477
* <init>(x$2, x$1)
7578
* }
79+
* ```
7680
*/
7781
def methPart(tree: Tree): Tree = stripApply(tree) match {
7882
case TypeApply(fn, _) => methPart(fn)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,15 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
106106

107107
/** A function def
108108
*
109+
* ```
109110
* vparams => expr
111+
* ```
110112
*
111113
* gets expanded to
112114
*
115+
* ```
113116
* { def $anonfun(vparams) = expr; Closure($anonfun) }
117+
* ```
114118
*
115119
* where the closure's type is the target type of the expression (FunctionN, unless
116120
* otherwise specified).

compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,9 @@ class CheckCaptures extends Recheck, SymTransformer:
10941094
.showing(i"rechecked closure $tree / $pt = $result", capt)
10951095

10961096
/** Recheck a lambda of the form
1097+
* ```
10971098
* { def $anonfun(...) = ...; closure($anonfun, ...)}
1099+
* ```
10981100
*/
10991101
override def recheckClosureBlock(mdef: DefDef, expr: Closure, pt: Type)(using Context): Type =
11001102
val anonfun = mdef.symbol

compiler/src/dotty/tools/dotc/config/Settings.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ object Settings:
370370
* It contains all necessary information to deprecate given option.
371371
* Scala Settings are considered deprecated when this object is present at their creation site.
372372
*
373-
* @param msg deprecation message that will be displayed in following format: s"Option $name is deprecated: $msg"
373+
* @param msg deprecation message that will be displayed in following format: s"Option \$name is deprecated: \$msg"
374374
* @param replacedBy option that is substituting current option
375375
*/
376376
case class Deprecation(

0 commit comments

Comments
 (0)