Skip to content

Commit 88e83f9

Browse files
authored
Merge branch 'scala:main' into get-tree-rhs
2 parents 89fd7c1 + 417d542 commit 88e83f9

File tree

18 files changed

+223
-23
lines changed

18 files changed

+223
-23
lines changed

compiler/src/dotty/tools/dotc/interactive/Completion.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ object Completion:
663663
*/
664664
private def implicitConversionTargets(qual: tpd.Tree)(using Context): Set[SearchSuccess] = {
665665
val typer = ctx.typer
666-
val conversions = new typer.ImplicitSearch(defn.AnyType, qual, pos.span).allImplicits
666+
val conversions = new typer.ImplicitSearch(defn.AnyType, qual, pos.span, Set.empty).allImplicits
667667

668668
interactiv.println(i"implicit conversion targets considered: ${conversions.toList}%, %")
669669
conversions

compiler/src/dotty/tools/dotc/transform/Dependencies.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ abstract class Dependencies(root: ast.tpd.Tree, @constructorOnly rootContext: Co
137137
if !enclosure.exists then throw NoPath()
138138
if enclosure == sym.enclosure then NoSymbol
139139
else
140+
/** is sym a constructor or a term that is nested in a constructor? */
140141
def nestedInConstructor(sym: Symbol): Boolean =
141142
sym.isConstructor
142143
|| sym.isTerm && nestedInConstructor(sym.enclosure)
@@ -237,6 +238,10 @@ abstract class Dependencies(root: ast.tpd.Tree, @constructorOnly rootContext: Co
237238
captureImplicitThis(tree.tpe)
238239
case tree: Select =>
239240
if isExpr(sym) && isLocal(sym) then markCalled(sym, enclosure)
241+
case tree: New =>
242+
val constr = tree.tpe.typeSymbol.primaryConstructor
243+
if constr.exists then
244+
symSet(called, enclosure) += constr
240245
case tree: This =>
241246
narrowTo(tree.symbol.asClass)
242247
case tree: MemberDef if isExpr(sym) && sym.owner.isTerm =>
@@ -291,7 +296,6 @@ abstract class Dependencies(root: ast.tpd.Tree, @constructorOnly rootContext: Co
291296
val calleeOwner = normalizedCallee.owner
292297
if calleeOwner.isTerm then narrowLogicOwner(caller, logicOwner(normalizedCallee))
293298
else
294-
assert(calleeOwner.is(Trait))
295299
// methods nested inside local trait methods cannot be lifted out
296300
// beyond the trait. Note that we can also call a trait method through
297301
// a qualifier; in that case no restriction to lifted owner arises.

compiler/src/dotty/tools/dotc/transform/LambdaLift.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,7 @@ object LambdaLift:
129129

130130
private def proxy(sym: Symbol)(using Context): Symbol = {
131131
def liftedEnclosure(sym: Symbol) =
132-
if sym.is(Method)
133-
then deps.logicalOwner.getOrElse(sym, sym.enclosure)
134-
else sym.enclosure
132+
deps.logicalOwner.getOrElse(sym, sym.enclosure)
135133
def searchIn(enclosure: Symbol): Symbol = {
136134
if (!enclosure.exists) {
137135
def enclosures(encl: Symbol): List[Symbol] =

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -928,8 +928,8 @@ trait Implicits:
928928
/** Find an implicit argument for parameter `formal`.
929929
* Return a failure as a SearchFailureType in the type of the returned tree.
930930
*/
931-
def inferImplicitArg(formal: Type, span: Span)(using Context): Tree =
932-
inferImplicit(formal, EmptyTree, span) match
931+
def inferImplicitArg(formal: Type, span: Span, ignored: Set[Symbol] = Set.empty)(using Context): Tree =
932+
inferImplicit(formal, EmptyTree, span, ignored) match
933933
case SearchSuccess(arg, _, _, _) => arg
934934
case fail @ SearchFailure(failed) =>
935935
if fail.isAmbiguous then failed
@@ -1082,7 +1082,7 @@ trait Implicits:
10821082
* it should be applied, EmptyTree otherwise.
10831083
* @param span The position where errors should be reported.
10841084
*/
1085-
def inferImplicit(pt: Type, argument: Tree, span: Span)(using Context): SearchResult = ctx.profiler.onImplicitSearch(pt):
1085+
def inferImplicit(pt: Type, argument: Tree, span: Span, ignored: Set[Symbol] = Set.empty)(using Context): SearchResult = ctx.profiler.onImplicitSearch(pt):
10861086
trace(s"search implicit ${pt.show}, arg = ${argument.show}: ${argument.tpe.show}", implicits, show = true) {
10871087
record("inferImplicit")
10881088
assert(ctx.phase.allowsImplicitSearch,
@@ -1110,7 +1110,7 @@ trait Implicits:
11101110
else i"conversion from ${argument.tpe} to $pt"
11111111

11121112
CyclicReference.trace(i"searching for an implicit $searchStr"):
1113-
try ImplicitSearch(pt, argument, span)(using searchCtx).bestImplicit
1113+
try ImplicitSearch(pt, argument, span, ignored)(using searchCtx).bestImplicit
11141114
catch case ce: CyclicReference =>
11151115
ce.inImplicitSearch = true
11161116
throw ce
@@ -1130,9 +1130,9 @@ trait Implicits:
11301130
result
11311131
case result: SearchFailure if result.isAmbiguous =>
11321132
val deepPt = pt.deepenProto
1133-
if (deepPt ne pt) inferImplicit(deepPt, argument, span)
1133+
if (deepPt ne pt) inferImplicit(deepPt, argument, span, ignored)
11341134
else if (migrateTo3 && !ctx.mode.is(Mode.OldImplicitResolution))
1135-
withMode(Mode.OldImplicitResolution)(inferImplicit(pt, argument, span)) match {
1135+
withMode(Mode.OldImplicitResolution)(inferImplicit(pt, argument, span, ignored)) match {
11361136
case altResult: SearchSuccess =>
11371137
report.migrationWarning(
11381138
result.reason.msg
@@ -1243,7 +1243,7 @@ trait Implicits:
12431243
}
12441244

12451245
/** An implicit search; parameters as in `inferImplicit` */
1246-
class ImplicitSearch(protected val pt: Type, protected val argument: Tree, span: Span)(using Context):
1246+
class ImplicitSearch(protected val pt: Type, protected val argument: Tree, span: Span, ignored: Set[Symbol])(using Context):
12471247
assert(argument.isEmpty || argument.tpe.isValueType || argument.tpe.isInstanceOf[ExprType],
12481248
em"found: $argument: ${argument.tpe}, expected: $pt")
12491249

@@ -1670,14 +1670,17 @@ trait Implicits:
16701670
SearchFailure(TooUnspecific(pt), span)
16711671
else
16721672
val contextual = ctxImplicits != null
1673-
val preEligible = // the eligible candidates, ignoring positions
1673+
var preEligible = // the eligible candidates, ignoring positions
16741674
if ctxImplicits != null then
16751675
if ctx.gadt.isNarrowing then
16761676
withoutMode(Mode.ImplicitsEnabled) {
16771677
ctxImplicits.uncachedEligible(wildProto)
16781678
}
16791679
else ctxImplicits.eligible(wildProto)
16801680
else implicitScope(wildProto).eligible
1681+
if !ignored.isEmpty then
1682+
preEligible =
1683+
preEligible.filter(candidate => !ignored.contains(candidate.implicitRef.underlyingRef.symbol))
16811684

16821685
/** Does candidate `cand` come too late for it to be considered as an
16831686
* eligible candidate? This is the case if `cand` appears in the same

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2544,6 +2544,14 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
25442544
// See tests/pos-macros/exprSummonWithTypeVar with -Xcheck-macros.
25452545
dotc.typer.Inferencing.fullyDefinedType(implicitTree.tpe, "", implicitTree)
25462546
implicitTree
2547+
def searchIgnoring(tpe: TypeRepr)(ignored: Symbol*): ImplicitSearchResult =
2548+
import tpd.TreeOps
2549+
val implicitTree = ctx.typer.inferImplicitArg(tpe, Position.ofMacroExpansion.span, ignored.toSet)
2550+
// Make sure that we do not have any uninstantiated type variables.
2551+
// See tests/pos-macros/i16636.
2552+
// See tests/pos-macros/exprSummonWithTypeVar with -Xcheck-macros.
2553+
dotc.typer.Inferencing.fullyDefinedType(implicitTree.tpe, "", implicitTree)
2554+
implicitTree
25472555
end Implicits
25482556

25492557
type ImplicitSearchResult = Tree

docs/_docs/reference/dropped-features/this-qualifier.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,15 @@ This can cause problems if a program tries to access the missing private field v
2929
// [C] needed if `field` is to be accessed through reflection
3030
val retained = field * field
3131
```
32+
33+
Class parameters are normally inferred object-private,
34+
so that members introduced by explicitly declaring them `val` or `var` are exempt from the rule described here.
35+
36+
In particular, the following field is not excluded from variance checking:
37+
```scala
38+
class C[-T](private val t: T) // error
39+
```
40+
And in contrast to the private field shown above, this field is not eliminated:
41+
```scala
42+
class C(private val c: Int)
43+
```

library/src/scala/quoted/Expr.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,23 @@ object Expr {
280280
}
281281
}
282282

283+
/** Find a given instance of type `T` in the current scope,
284+
* while excluding certain symbols from the initial implicit search.
285+
* Return `Some` containing the expression of the implicit or
286+
* `None` if implicit resolution failed.
287+
*
288+
* @tparam T type of the implicit parameter
289+
* @param ignored Symbols ignored during the initial implicit search
290+
*
291+
* @note if the found given requires additional search for other given instances,
292+
* this additional search will NOT exclude the symbols from the `ignored` list.
293+
*/
294+
def summonIgnoring[T](using Type[T])(using quotes: Quotes)(ignored: quotes.reflect.Symbol*): Option[Expr[T]] = {
295+
import quotes.reflect._
296+
Implicits.searchIgnoring(TypeRepr.of[T])(ignored*) match {
297+
case iss: ImplicitSearchSuccess => Some(iss.tree.asExpr.asInstanceOf[Expr[T]])
298+
case isf: ImplicitSearchFailure => None
299+
}
300+
}
301+
283302
}

library/src/scala/quoted/Quotes.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3708,6 +3708,18 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
37083708
* @param tpe type of the implicit parameter
37093709
*/
37103710
def search(tpe: TypeRepr): ImplicitSearchResult
3711+
3712+
/** Find a given instance of type `T` in the current scope provided by the current enclosing splice,
3713+
* while excluding certain symbols from the initial implicit search.
3714+
* Return an `ImplicitSearchResult`.
3715+
*
3716+
* @param tpe type of the implicit parameter
3717+
* @param ignored Symbols ignored during the initial implicit search
3718+
*
3719+
* @note if an found given requires additional search for other given instances,
3720+
* this additional search will NOT exclude the symbols from the `ignored` list.
3721+
*/
3722+
def searchIgnoring(tpe: TypeRepr)(ignored: Symbol*): ImplicitSearchResult
37113723
}
37123724

37133725
/** Result of a given instance search */

project/MiMaFilters.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ object MiMaFilters {
9595
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#MethodTypeModule.apply"),
9696
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#MethodTypeMethods.methodTypeKind"),
9797
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#MethodTypeMethods.isContextual"),
98+
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#ImplicitsModule.searchIgnoring"),
9899
// Change `experimental` annotation to a final class
99100
ProblemFilters.exclude[FinalClassProblem]("scala.annotation.experimental"),
100101
),

tests/neg/i22620.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
import scala.collection.mutable.ArrayBuffer
3+
4+
class PrivateTest[-M](private val v: ArrayBuffer[M]) // error

0 commit comments

Comments
 (0)