Skip to content

Add Printer to reflect Constant.show #10755

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
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
10 changes: 9 additions & 1 deletion compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2092,7 +2092,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
given ConstantMethods: ConstantMethods with
extension (self: Constant)
def value: Any = self.value
def show: String = Extractors.showConstant(using QuotesImpl.this)(self)
def show(using printer: Printer[Constant]): String = printer.show(self)
end extension
end ConstantMethods

Expand Down Expand Up @@ -2774,6 +2774,14 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
def show(tpe: TypeRepr): String =
Extractors.showType(using QuotesImpl.this)(tpe)

lazy val ConstantCode: Printer[Constant] = new Printer[Constant]:
def show(const: Constant): String =
const.show(using ctx.fresh.setSetting(ctx.settings.color, "never"))

lazy val ConstantStructure: Printer[Constant] = new Printer[Constant]:
def show(const: Constant): String =
Extractors.showConstant(using QuotesImpl.this)(const)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there usage for ConstantStructure? If ConstantCode is the only printer, maybe change toString and/or show method is enough for practical usage.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ConstantStructure is mainly used for debugging.

We do not have access to the Context in the to toString which we need to handle the ClassOf case. In general, we tell users that the toString is unreliable and that they should use show, this should not be an exception or it may lead to confusion.


end Printer
end reflect

Expand Down
11 changes: 10 additions & 1 deletion library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2758,7 +2758,8 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
def value: Any

/** Shows the constant as a String */
def show: String
def show(using Printer[Constant]): String

end extension
}

Expand Down Expand Up @@ -4113,6 +4114,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
/** Default pinter for `TypeRepr` used when calling `tpe.show` */
given TypeReprPrinter: Printer[TypeRepr] = Printer.TypeReprCode

/** Default pinter for `Constant` used when calling `const.show` */
given ConstantPrinter: Printer[Constant] = Printer.ConstantCode

/** Module object of `type Printer`.
* Contains custom printers such as `TreeCode`, `TreeAnsiCode`, `TreeCases`, `TypeReprCode`, ..., `SymbolFullName` and `FlagsCombination`.
*/
Expand Down Expand Up @@ -4152,6 +4156,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
*/
def TypeReprStructure: Printer[TypeRepr]

/** Prints the constant in source code. */
def ConstantCode: Printer[Constant]

/** Prints a pattern like representation of the `Constant`. */
def ConstantStructure: Printer[Constant]
}

}
Expand Down
11 changes: 2 additions & 9 deletions scala3doc/src/dotty/dokka/tasty/BasicSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,15 @@ trait BasicSupport:
val params = annotTerm match
case Apply(target, appliedWith) => {
appliedWith.flatMap {
case Literal(constant) => Some(Annotation.PrimitiveParameter(None, constant.value match {
case s: String => "\"" + s"$s" + "\""
case other => other.toString()
}))
case NamedArg(name, Literal(constant)) => Some(Annotation.PrimitiveParameter(Some(name), constant.value match
case s: String => "\"" + s"$s" + "\""
case other => other.toString()
))
case Literal(constant) => Some(Annotation.PrimitiveParameter(None, constant.show))
case NamedArg(name, Literal(constant)) => Some(Annotation.PrimitiveParameter(Some(name), constant.show))
case x @ Select(qual, name) => None
case other => Some(Annotation.UnresolvedParameter(None, other.show))
}
}

Annotation(dri, params)


extension (sym: Symbol)
def documentation = sym.docstring match
case Some(docstring) =>
Expand Down
5 changes: 1 addition & 4 deletions scala3doc/src/dotty/dokka/tasty/TypesSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ trait TypesSupport:
case AndType(left, right) => inner(left) ++ texts(" & ") ++ inner(right)
case ByNameType(tpe) => text("=> ") :: inner(tpe)
case ConstantType(constant) =>
texts(constant.value match
case c: Char => s"'$c'"
case other => other.toString
)
texts(constant.show)
case ThisType(tpe) => inner(tpe)
case AnnotatedType(AppliedType(_, Seq(tpe)), annotation) if isRepeated(annotation) =>
inner(tpe) :+ text("*")
Expand Down