Skip to content
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

bugfix: Remove PrettyType which might often fail in the compiler #5763

Merged
merged 1 commit into from
Oct 19, 2023
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
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))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This makes sure that sym is not dealiased ince the error type is not an alias, but we still have all the correct type 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(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replacing PrettyType with SingleType wa enough except for CompletionWorkspaceSuite.type-import where the type would alwyas be dealiased when printing. So I changed the symbol to always be error in shortened type.

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
Loading