Skip to content

Commit 50e4e75

Browse files
committed
simplified precise default argument support
1 parent 9236c6c commit 50e4e75

File tree

2 files changed

+16
-35
lines changed

2 files changed

+16
-35
lines changed

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -304,33 +304,14 @@ object desugar {
304304
vparam => toDefParam(vparam, keepAnnotations = true, keepDefault = false)
305305
}
306306

307-
// gets arguments should be considered precise
308-
val paramPrecises: List[Boolean] =
309-
// indication for the type parameters preciseness
310-
val preciseMap : Map[TypeName, Boolean] =
311-
meth.leadingTypeParams.map(t => (t.name, t.mods.annotations.exists(isPreciseAnnot))).toMap
312-
// mapping type parameters preciseness onto term parameter preciseness
313-
meth.termParamss.view.flatten.map(p => p.tpt).map {
314-
case Ident(n) => preciseMap.getOrElse(n.asTypeName, false)
315-
case _ => false
316-
}.toList
317-
318307
def defaultGetters(paramss: List[ParamClause], n: Int): List[DefDef] = paramss match
319308
case ValDefs(vparam :: vparams) :: paramss1 =>
320309
def defaultGetter: DefDef =
321-
val (rhs, tpt) =
322-
// if the parameter is precise, then we add explicit return type for the
323-
// definition to keep the precise type after precise typing.
324-
if paramPrecises(n) then
325-
val rhsTyped = withMode(Mode.Precise){ctx.typer.typedExpr(vparam.rhs)}
326-
(TypedSplice(rhsTyped), TypeTree(rhsTyped.tpe))
327-
// otherwise, the desugaring is unchanged from the status quo
328-
else (vparam.rhs, TypeTree())
329310
DefDef(
330311
name = DefaultGetterName(meth.name, n),
331312
paramss = getterParamss(n),
332-
tpt = tpt,
333-
rhs = rhs
313+
tpt = TypeTree(),
314+
rhs = vparam.rhs
334315
)
335316
.withMods(Modifiers(
336317
meth.mods.flags & (AccessFlags | Synthetic) | (vparam.mods.flags & Inline),

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,19 +1855,18 @@ class Namer { typer: Typer =>
18551855
case _ =>
18561856
approxTp
18571857

1858-
var rhsCtx = ctx.fresh.addMode(Mode.InferringReturnType)
1859-
if sym.isInlineMethod then rhsCtx = rhsCtx.addMode(Mode.InlineableBody)
1860-
if sym.is(ExtensionMethod) then rhsCtx = rhsCtx.addMode(Mode.InExtensionMethod)
1861-
val typeParams = paramss.collect { case TypeSymbols(tparams) => tparams }.flatten
1862-
if (typeParams.nonEmpty) {
1863-
// we'll be typing an expression from a polymorphic definition's body,
1864-
// so we must allow constraining its type parameters
1865-
// compare with typedDefDef, see tests/pos/gadt-inference.scala
1866-
rhsCtx.setFreshGADTBounds
1867-
rhsCtx.gadt.addToConstraint(typeParams)
1868-
}
1869-
1870-
def typedAheadRhs(pt: Type) =
1858+
def typedAheadRhs(pt: Type)(using Context) =
1859+
var rhsCtx = ctx.fresh.addMode(Mode.InferringReturnType)
1860+
if sym.isInlineMethod then rhsCtx = rhsCtx.addMode(Mode.InlineableBody)
1861+
if sym.is(ExtensionMethod) then rhsCtx = rhsCtx.addMode(Mode.InExtensionMethod)
1862+
val typeParams = paramss.collect { case TypeSymbols(tparams) => tparams }.flatten
1863+
if (typeParams.nonEmpty) {
1864+
// we'll be typing an expression from a polymorphic definition's body,
1865+
// so we must allow constraining its type parameters
1866+
// compare with typedDefDef, see tests/pos/gadt-inference.scala
1867+
rhsCtx.setFreshGADTBounds
1868+
rhsCtx.gadt.addToConstraint(typeParams)
1869+
}
18711870
PrepareInlineable.dropInlineIfError(sym,
18721871
typedAheadExpr(mdef.rhs, pt)(using rhsCtx))
18731872

@@ -1877,7 +1876,8 @@ class Namer { typer: Typer =>
18771876
// parameters like in `def mkList[T](value: T = 1): List[T]`.
18781877
val defaultTp = defaultParamType
18791878
val pt = inherited.orElse(expectedDefaultArgType).orElse(fallbackProto).widenExpr
1880-
val tp = typedAheadRhs(pt).tpe
1879+
val preciseMode = if (defaultTp.isPrecise) Mode.Precise else Mode.None
1880+
val tp = withMode(preciseMode){ typedAheadRhs(pt) }.tpe
18811881
if (defaultTp eq pt) && (tp frozen_<:< defaultTp) then
18821882
// When possible, widen to the default getter parameter type to permit a
18831883
// larger choice of overrides (see `default-getter.scala`).

0 commit comments

Comments
 (0)