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

Make refined type printing more source compatible #16303

Merged
merged 1 commit into from
Nov 11, 2022

Conversation

jan-pieter
Copy link
Contributor

Fixes #12939
Supersedes #13698

@SethTisue SethTisue requested a review from tgodzik November 8, 2022 16:44
@SethTisue
Copy link
Member

on the previous PR, @tgodzik wondered if the formatting could be improved. I think it's out of scope for this PR. Maybe Dale's recent pretty-printing PR (not merged yet) will improve the situation

@SethTisue
Copy link
Member

SethTisue commented Nov 8, 2022

we only made two further small tweaks:

  • added a test that checks that the type printing also works as expected in the REPL when the refined type is written directly, rather than originating from an anonymous subclass
  • add a catchall case that prints something if we see something unexpected in the refinedInfo — hopefully this case will never be hit. as I read the Scala 2 spec, the only things that are syntactically legal in a refined type are val, def, and type; but I'm a little less certain that we've covered all the cases of how those might be represented as trees

if this passes review, we can squash the commits before merge

@SethTisue SethTisue marked this pull request as draft November 8, 2022 17:54
@SethTisue
Copy link
Member

marking as draft for now since there are some test failures. probably just need to update some expected outputs to match the new format?

@SethTisue SethTisue self-assigned this Nov 8, 2022
@jan-pieter
Copy link
Contributor Author

jan-pieter commented Nov 8, 2022

probably just need to update some expected outputs to match the new format?

Most of the failures were indeed due to some unexpected output. I've updated the expected output for those.

The catchall case was actually being hit by the tests for capture checking. I lack knowledge about the implementation details to explain that. For now I've updated this case to return an empty string which makes the tests pass. Some example output with the previous implementation:

-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/i15772.scala:20:49 ---------------------------------------
20 |    val boxed1 : (({*} C) => Unit) -> Unit = box1(c)  // error
   |                                             ^^^^^^^
   |Found:    {c} 
   |  ({*} 
   |    ({c} 
   |      C{
   |        AnnotatedType(TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),class C),CaptureAnnotation(BiMapped3802(Var3741{TermRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class scala)),module class caps$)),val *)}, elems = {TermRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class scala)),module class caps$)),val *)}),false)) 
   |          arg
   |        : {*} C
   |      }
   |    )
   |   -> Unit)
   | -> Unit
   |Required: (({*} C) => Unit) -> Unit
   |
   | longer explanation available when compiling with `-explain`

@@ -112,7 +112,13 @@ class PlainPrinter(_ctx: Context) extends Printer {

/** String representation of a refinement */
protected def toTextRefinement(rt: RefinedType): Closed =
(refinementNameString(rt) ~ toTextRHS(rt.refinedInfo)).close
val keyword = rt.refinedInfo match {
case _: TypeRef => "val "
Copy link
Member

@bishabosha bishabosha Nov 9, 2022

Choose a reason for hiding this comment

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

I suggest this case to be made below TypeBounds and turn it to TypeProxy, so it can work with e.g. val x: List[Int], which is AppliedType, type proxy should cover most cases - such as annotated types Foo { val x: String @unchecked }.

(refinementNameString(rt) ~ toTextRHS(rt.refinedInfo)).close
val keyword = rt.refinedInfo match {
case _: TypeRef => "val "
case _: ExprType | _: MethodType => "def "
Copy link
Member

Choose a reason for hiding this comment

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

there could also be PolyType, e.g. def bar: Foo { def foo[T <: Any](t: T): String } = ???

Copy link
Member

@bishabosha bishabosha Nov 9, 2022

Choose a reason for hiding this comment

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

for this case, Foo must be a class that already defines foo as a polymorphic method

@jan-pieter
Copy link
Contributor Author

Thanks for your comments and suggestions @bishabosha. I've updated the code & tests accordingly. This also solves the issue with AnnotatedTypes in the capture checking tests.

@jan-pieter jan-pieter marked this pull request as ready for review November 10, 2022 08:57
@jan-pieter
Copy link
Contributor Author

@SethTisue Shall I rebase on the main branch, squash the commits and force push this branch?

@dwijnand
Copy link
Member

Is it just me that's bothered that C1{val x: Int} isn't spaced out as C1 { val x: Int }?

@jan-pieter
Copy link
Contributor Author

No, I prefer the latter one as well. However we previously said it to be out of scope for this PR: #16303 (comment) . This PR only adds keywords. Perhaps #16277 did bring some improvements already? Otherwise I propose to improve that in a separate PR.

@SethTisue
Copy link
Member

Shall I rebase on the main branch, squash the commits and force push this branch?

Yes please!

@jan-pieter jan-pieter force-pushed the fix-12939 branch 2 times, most recently from 2f9cd62 to d8ae923 Compare November 11, 2022 09:32
@jan-pieter
Copy link
Contributor Author

@SethTisue It took a few tries, but now it's ready to be merged I think 😉

@dwijnand dwijnand merged commit cc4c279 into scala:main Nov 11, 2022
@SethTisue
Copy link
Member

Yay!

@jan-pieter jan-pieter deleted the fix-12939 branch November 14, 2022 10:20
@Kordyjan Kordyjan added this to the 3.3.0 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

make refined type printing more source compatible
5 participants