Skip to content

Fix #20146: attach the original name if there is an import selection for an indent #20163

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 4 commits into from
May 6, 2024
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
46 changes: 26 additions & 20 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
ctx

override def prepareForSelect(tree: tpd.Select)(using Context): Context =
unusedDataApply(_.registerUsed(tree.symbol, Some(tree.name)))
val name = tree.removeAttachment(OriginalName).orElse(Some(tree.name))
unusedDataApply(_.registerUsed(tree.symbol, name))

override def prepareForBlock(tree: tpd.Block)(using Context): Context =
pushInBlockTemplatePackageDef(tree)
Expand Down Expand Up @@ -327,6 +328,8 @@ object CheckUnused:
*/
private val _key = Property.StickyKey[UnusedData]

val OriginalName = Property.StickyKey[Name]

class PostTyper extends CheckUnused(PhaseMode.Aggregate, "PostTyper", _key)

class PostInlining extends CheckUnused(PhaseMode.Report, "PostInlining", _key)
Expand Down Expand Up @@ -677,36 +680,39 @@ object CheckUnused:
}

/** Given an import and accessibility, return selector that matches import<->symbol */
private def isInImport(imp: tpd.Import, isAccessible: Boolean, symName: Option[Name], isDerived: Boolean)(using Context): Option[ImportSelector] =
private def isInImport(imp: tpd.Import, isAccessible: Boolean, altName: Option[Name], isDerived: Boolean)(using Context): Option[ImportSelector] =
val tpd.Import(qual, sels) = imp
val dealiasedSym = dealias(sym)
val simpleSelections = qual.tpe.member(sym.name).alternatives
val typeSelections = sels.flatMap(n => qual.tpe.member(n.name.toTypeName).alternatives)
val termSelections = sels.flatMap(n => qual.tpe.member(n.name.toTermName).alternatives)
val sameTermPath = qual.isTerm && sym.exists && sym.owner.isType && qual.tpe.typeSymbol == sym.owner.asType
val selectionsToDealias = typeSelections ::: termSelections
val renamedSelection = if sameTermPath then sels.find(sel => sel.imported.name == sym.name) else None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no equivalent to this line in the new code. Why did it disappear?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamedSelection is removed. As its name suggests, it tries to find renamed selections. However, I think this logic is redundant, because it just compare the names from sels with sym.name and ensure the path is valid, which will be done in selector later and has been ensured in typer.

val qualHasSymbol = simpleSelections.map(_.symbol).contains(sym) || (simpleSelections ::: selectionsToDealias).map(_.symbol).map(dealias).contains(dealiasedSym) || renamedSelection.isDefined
def selector = sels.find(sel => (sel.name.toTermName == sym.name || sel.name.toTypeName == sym.name) && symName.map(n => n.toTermName == sel.rename).getOrElse(true))
def dealiasedSelector = if(isDerived) sels.flatMap(sel => selectionsToDealias.map(m => (sel, m.symbol))).collect {
case (sel, sym) if dealias(sym) == dealiasedSym => sel
}.headOption else None
val qualTpe = qual.tpe
val dealiasedSym = sym.dealias
val simpleSelections = qualTpe.member(sym.name).alternatives
val selectionsToDealias = sels.flatMap(sel =>
qualTpe.member(sel.name.toTypeName).alternatives
::: qualTpe.member(sel.name.toTermName).alternatives)
def qualHasSymbol = simpleSelections.map(_.symbol).contains(sym) || (simpleSelections ::: selectionsToDealias).map(_.symbol).map(_.dealias).contains(dealiasedSym)
def selector = sels.find(sel => (sel.name.toTermName == sym.name || sel.name.toTypeName == sym.name) && altName.map(n => n.toTermName == sel.rename).getOrElse(true))
def dealiasedSelector =
if isDerived then
sels.flatMap(sel => selectionsToDealias.map(m => (sel, m.symbol))).collect {
case (sel, sym) if sym.dealias == dealiasedSym => sel
}.headOption
else None
def givenSelector = if sym.is(Given) || sym.is(Implicit)
then sels.filter(sel => sel.isGiven && !sel.bound.isEmpty).find(sel => sel.boundTpe =:= sym.info)
else None
def wildcard = sels.find(sel => sel.isWildcard && ((sym.is(Given) == sel.isGiven && sel.bound.isEmpty) || sym.is(Implicit)))
if qualHasSymbol && (!isAccessible || sym.isRenamedSymbol(symName)) && sym.exists then
selector.orElse(dealiasedSelector).orElse(givenSelector).orElse(wildcard).orElse(renamedSelection) // selector with name or wildcard (or given)
if sym.exists && qualHasSymbol && (!isAccessible || sym.isRenamedSymbol(altName)) then
selector.orElse(dealiasedSelector).orElse(givenSelector).orElse(wildcard) // selector with name or wildcard (or given)
else
None

private def isRenamedSymbol(symNameInScope: Option[Name])(using Context) =
sym.name != nme.NO_NAME && symNameInScope.exists(_.toSimpleName != sym.name.toSimpleName)

private def dealias(symbol: Symbol)(using Context): Symbol =
if(symbol.isType && symbol.asType.denot.isAliasType) then
symbol.asType.typeRef.dealias.typeSymbol
else symbol
private def dealias(using Context): Symbol =
if sym.isType && sym.asType.denot.isAliasType then
sym.asType.typeRef.dealias.typeSymbol
else sym

/** Annotated with @unused */
private def isUnusedAnnot(using Context): Boolean =
sym.annotations.exists(a => a.symbol == ctx.definitions.UnusedAnnot)
Expand Down
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import NullOpsDecorator.*
import cc.{CheckCaptures, isRetainsLike}
import config.Config
import config.MigrationVersion
import transform.CheckUnused.OriginalName

import scala.annotation.constructorOnly

Expand Down Expand Up @@ -662,7 +663,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
val selection = untpd.cpy.Select(tree)(qualifier, name)
typed(selection, pt)
else if rawType.exists then
setType(ensureAccessible(rawType, superAccess = false, tree.srcPos))
val ref = setType(ensureAccessible(rawType, superAccess = false, tree.srcPos))
if ref.symbol.name != name then
ref.withAttachment(OriginalName, name)
else ref
else if name == nme._scope then
// gross hack to support current xml literals.
// awaiting a better implicits based solution for library-supported xml
Expand Down
7 changes: 7 additions & 0 deletions tests/warn/i20146.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//> using options -Wunused:imports

def test(list: List[Int]): Int =
import list.{head => first}
import list.{length => len} // warn
import list.{addString => add} // warn
first + list.length
Loading