Skip to content

Commit

Permalink
bugfix: Remove PrettyType which might often fail if handled via the c…
Browse files Browse the repository at this point in the history
…ompiler

Previously, we would use PrettyType to get any string output we wanted, however that broke if the type w read by the compiler. Now, we use standard compiler types to achive the same.
  • Loading branch information
tgodzik committed Oct 19, 2023
1 parent 2dc3125 commit da305ba
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
55 changes: 31 additions & 24 deletions mtags/src/main/scala-2/scala/meta/internal/pc/MetalsGlobal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -252,20 +252,6 @@ class MetalsGlobal(
}
}

/**
* A `Type` with custom pretty-printing representation, not used for typechecking.
*
* NOTE(olafur) Creating a new `Type` subclass is a hack, a better long-term solution would be
* to implement a custom pretty-printer for types so that we don't have to rely on `Type.toString`.
*/
class PrettyType(
override val prefixString: String,
override val safeToString: String
) extends Type {
def this(string: String) =
this(string + ".", string)
}

private def backtickify(sym: Symbol) =
if (
Identifier.needsBacktick(sym.name.decoded)
Expand Down Expand Up @@ -294,11 +280,19 @@ class MetalsGlobal(
isVisited += key
val result = tpe match {
case TypeRef(pre, sym, args) =>
def backtickifiedSymbol = backtickify(sym)
def shortSymbol = {
/* If it's an alias type we want to prevent dealiasing it
AnyRef should stay to be dropped if neded later on since it's
not an important class.
*/
if (sym.isAliasType && sym != definitions.AnyRefClass)
backtickify(sym.newErrorSymbol(sym.name).updateInfo(sym.info))
else backtickify(sym)
}
if (history.isSymbolInScope(sym, pre)) {
TypeRef(
NoPrefix,
backtickifiedSymbol,
shortSymbol,
args.map(arg => loop(arg, None))
)
} else {
Expand All @@ -324,8 +318,11 @@ class MetalsGlobal(
history.config.get(ownerSymbol) match {
case Some(rename) if canRename(rename, ownerSymbol) =>
TypeRef(
new PrettyType(rename.toString),
backtickifiedSymbol,
SingleType(
NoPrefix,
sym.newErrorSymbol(rename)
),
shortSymbol,
args.map(arg => loop(arg, None))
)
case _ =>
Expand All @@ -352,20 +349,20 @@ class MetalsGlobal(
if (history.nameResolvesToSymbol(sym.name, sym)) {
TypeRef(
NoPrefix,
backtickifiedSymbol,
shortSymbol,
args.map(arg => loop(arg, None))
)
} else {
TypeRef(
ThisType(pre.typeSymbol),
backtickifiedSymbol,
shortSymbol,
args.map(arg => loop(arg, None))
)
}
} else {
TypeRef(
loop(pre, Some(ShortName(sym))),
backtickifiedSymbol,
shortSymbol,
args.map(arg => loop(arg, None))
)
}
Expand Down Expand Up @@ -424,7 +421,10 @@ class MetalsGlobal(
}
else renamedOwnerIndex
if (prefix < 0) {
new PrettyType(history.fullname(sym))
SingleType(
NoPrefix,
sym.newErrorSymbol(TypeName(history.fullname(sym)))
)
} else {
val names = owners
.take(prefix + 1)
Expand All @@ -440,7 +440,10 @@ class MetalsGlobal(
val ref = names.tail.foldLeft(names.head: m.Term.Ref) {
case (qual, name) => m.Term.Select(qual, name)
}
new PrettyType(ref.syntax)
SingleType(
NoPrefix,
sym.newErrorSymbol(TypeName(ref.syntax))
)
}
}
case ConstantType(Constant(sym: TermSymbol))
Expand All @@ -464,7 +467,11 @@ class MetalsGlobal(
// [x] => F[x] is not printable in the code, we need to use just `F`
case TypeRef(_, sym, args)
if typeParams == args.map(_.typeSymbol) =>
new PrettyType(sym.name.toString())
TypeRef(
NoPrefix,
sym.newErrorSymbol(sym.name),
Nil
)
case otherType =>
PolyType(typeParams, otherType)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ trait Signatures { compiler: MetalsGlobal =>
case LookupSucceeded(qual, symbol) =>
symbol.isKindaTheSameAs(sym) && {
prefix == NoPrefix ||
prefix.isInstanceOf[PrettyType] ||
qual.tpe.computeMemberType(symbol) <:<
prefix.computeMemberType(sym)
}
Expand Down

0 comments on commit da305ba

Please sign in to comment.