Skip to content

Backport "Use result of lambda type of implicit in CheckUnused" to 3.7.2 #23545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2720,7 +2720,7 @@ object SymDenotations {
|| owner.isRefinementClass
|| owner.is(Scala2x)
|| owner.unforcedDecls.contains(denot.name, denot.symbol)
|| (denot.is(Synthetic) && denot.is(ModuleClass) && stillValidInOwner(denot.companionClass))
|| (denot.is(Synthetic) && denot.is(ModuleClass) && denot.companionClass.exists && stillValidInOwner(denot.companionClass))
|| denot.isSelfSym
|| denot.isLocalDummy)
catch case ex: StaleSymbol => false
Expand Down
9 changes: 6 additions & 3 deletions compiler/src/dotty/tools/dotc/interactive/Completion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,15 @@ object Completion:
* 8. symbol is not a constructor proxy module when in type completion mode
* 9. have same term/type kind as name prefix given so far
*/
def isValidCompletionSymbol(sym: Symbol, completionMode: Mode, isNew: Boolean)(using Context): Boolean =

def isValidCompletionSymbol(sym: Symbol, completionMode: Mode, isNew: Boolean)(using Context): Boolean = try
lazy val isEnum = sym.is(Enum) ||
(sym.companionClass.exists && sym.companionClass.is(Enum))

sym.exists &&
!sym.isAbsent(canForce = false) &&
!sym.isPrimaryConstructor &&
sym.sourceSymbol.exists &&
// running sourceSymbol on ExportedTerm will force a lot of computation from collectSubTrees
(sym.is(ExportedTerm) || sym.sourceSymbol.exists) &&
(!sym.is(Package) || sym.is(ModuleClass)) &&
!sym.isAllOf(Mutable | Accessor) &&
!sym.isPackageObject &&
Expand All @@ -343,6 +343,9 @@ object Completion:
(completionMode.is(Mode.Term) && (sym.isTerm || sym.is(ModuleClass))
|| (completionMode.is(Mode.Type) && (sym.isType || sym.isStableMember)))
)
catch
case NonFatal(ex) =>
false
end isValidCompletionSymbol

given ScopeOrdering(using Context): Ordering[Seq[SingleDenotation]] with
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ object CheckUnused:
def isCanEqual: Boolean =
sym.isOneOf(GivenOrImplicit) && sym.info.finalResultType.baseClasses.exists(_.derivesFrom(defn.CanEqualClass))
def isMarkerTrait: Boolean =
sym.info.hiBound.allMembers.forall: d =>
sym.info.hiBound.resultType.allMembers.forall: d =>
val m = d.symbol
!m.isTerm || m.isSelfSym || m.is(Method) && (m.owner == defn.AnyClass || m.owner == defn.ObjectClass)
def isEffectivelyPrivate: Boolean =
Expand Down
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/TailRec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,10 @@ class TailRec extends MiniPhase {
case prefix: This if prefix.symbol == enclosingClass =>
// Avoid assigning `this = this`
assignParamPairs
case prefix if prefix.symbol.is(Module) && prefix.symbol.moduleClass == enclosingClass =>
case prefix
if prefix.symbol.is(Module)
&& prefix.symbol.moduleClass == enclosingClass
&& isPurePath(prefix) =>
// Avoid assigning `this = MyObject`
assignParamPairs
case _ =>
Expand Down
4 changes: 0 additions & 4 deletions tests/neg/i19351.check

This file was deleted.

5 changes: 0 additions & 5 deletions tests/neg/i19351/A.scala

This file was deleted.

4 changes: 0 additions & 4 deletions tests/neg/i19351a.check

This file was deleted.

6 changes: 6 additions & 0 deletions tests/pos-macros/i13821-a/Macro.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
trait Conversions:
given conv(using DFBits.Candidate): Conversion[Int, DFVal] = ???

import scala.quoted.*
transparent inline def f: Short = ${ getWidthMacro }
private def getWidthMacro(using Quotes): Expr[Short] = '{ ??? }
12 changes: 12 additions & 0 deletions tests/pos-macros/i13821-a/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type DFBits = Long
object DFBits:
def a: Unit = f // forces suspension of this compilation unit in typer
def b: DFVal = 2 // uses implicit conversion `DFVal.conv`

trait Candidate
object Candidate:
given candidate: Candidate = ??? // completed in run 3 but created in run 2
end DFBits

trait DFVal
object DFVal extends Conversions
7 changes: 7 additions & 0 deletions tests/pos-macros/i13821-b/Macro.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import outer._
trait Conversions:
given conv(using DFBits.Candidate): Conversion[Int, DFVal] = ???

import scala.quoted.*
transparent inline def f: Short = ${ getWidthMacro }
private def getWidthMacro(using Quotes): Expr[Short] = '{ ??? }
14 changes: 14 additions & 0 deletions tests/pos-macros/i13821-b/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type outer = Int
object outer:
type DFBits = Long
object DFBits:
def a: Unit = f // forces suspension of this compilation unit in typer
def b: DFVal = 2 // uses implicit conversion `DFVal.conv`

trait Candidate
object Candidate:
given candidate: Candidate = ??? // completed in run 3 but created in run 2
end DFBits

trait DFVal
object DFVal extends Conversions
6 changes: 6 additions & 0 deletions tests/pos-macros/i19351/A.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// object A:
val x: Int = 1
inline def myMacro(): x.type = ${myMacroExpr}
def test = myMacro()

@main def main() = ()
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import scala.quoted.*
//import A.*
// import A.*
def myMacroExpr(using Quotes): Expr[x.type] = '{???}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type Bool = [R] => (R, R) => R
val True: Bool = [R] => (t: R, _: R) => t
val False: Bool = [R] => (_: R, f: R) => f

inline def not(b: Bool): Bool = ${notMacro('b)} // error
inline def not(b: Bool): Bool = ${notMacro('b)}
inline def show(b: Bool): String = ${showMacro('b)}
//inline def not(b: Bool): Bool = ${foldMacro('b, 'False, 'True)}
//inline def show(b: Bool): String = ${foldMacro('b, '{"TRUE"}, '{"FALSE"})}
Expand Down
22 changes: 22 additions & 0 deletions tests/run/i23444.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

import annotation.*

class Path(action: () => Unit, parent: Option[Path]):
object O:
@tailrec
def apply(): Unit =
action()

parent match
case Some(p) =>
p.O.apply()
case None =>

@main def Test: Unit =
var counter = 0
val fun = () => {
counter += 1
if counter > 2 then throw AssertionError("bad loop")
}
val path = Path(fun, Some(Path(fun, None)))
path.O()
7 changes: 6 additions & 1 deletion tests/warn/i15503f.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//> using options -Wunused:implicits
//> using options -Wunused:implicits

/* This goes around the "trivial method" detection */
val default_int = 1
Expand Down Expand Up @@ -67,6 +67,8 @@ package givens:
trait Y:
def doY: String

trait Z

given X:
def doX = 7

Expand All @@ -84,6 +86,9 @@ package givens:

given namely: (x: X) => Y: // warn protected param to given class
def doY = "8"

def f(using => X) = println() // warn
def g(using => Z) = println() // nowarn marker trait
end givens

object i22895:
Expand Down
15 changes: 15 additions & 0 deletions tests/warn/i23494.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//> using options -Wunused:implicits

import scala.deriving.Mirror

abstract class EnumerationValues[A]:
type Out

object EnumerationValues:
type Aux[A, B] = EnumerationValues[A] { type Out = B }

def apply[A, B](): EnumerationValues.Aux[A, B] = new EnumerationValues[A]:
override type Out = B

given sum[A, B <: Tuple](using mirror: Mirror.SumOf[A] { type MirroredElemTypes = B }): EnumerationValues.Aux[A, A] =
EnumerationValues[A, A]()
Loading