Skip to content

Fixes #3683 REPL: Incorrect pretty-printing of singleton types #4132

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

Closed
wants to merge 6 commits into from

Conversation

eklavya
Copy link
Contributor

@eklavya eklavya commented Mar 17, 2018

No description provided.

Copy link
Member

@dottybot dottybot left a comment

Choose a reason for hiding this comment

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

Hello, and thank you for opening this PR! 🎉

All contributors have signed the CLA, thank you! ❤️

Commit Messages

We want to keep history, but for that to actually be useful we have
some rules on how to format our commit messages (relevant xkcd).

Please stick to these guidelines for commit messages:

  1. Separate subject from body with a blank line
  2. When fixing an issue, start your commit message with Fix #<ISSUE-NBR>:
  3. Limit the subject line to 72 characters
  4. Capitalize the subject line
  5. Do not end the subject line with a period
  6. Use the imperative mood in the subject line ("Add" instead of "Added")
  7. Wrap the body at 80 characters
  8. Use the body to explain what and why vs. how

adapted from https://chris.beams.io/posts/git-commit

Have an awesome day! ☀️

@eklavya
Copy link
Contributor Author

eklavya commented Mar 17, 2018

I will fix the tests if this fix is ok.

Copy link
Contributor

@Blaisorblade Blaisorblade left a comment

Choose a reason for hiding this comment

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

I think one must keep the current output in examples like

scala> final val a = "foo"
val a: String("foo") = "foo"

If there aren't tests enforcing this behavior, this is a good time to add them, but I hope those are the failing tests.

@allanrenucci
Copy link
Contributor

Closing. Will be fixed by #4537. Thanks @eklavya!

@allanrenucci
Copy link
Contributor

My mistake #4537 is unrelated. @eklavya do you plan to keep working on this?

@eklavya
Copy link
Contributor Author

eklavya commented May 29, 2018

@allanrenucci I am not able to differentiate between x: a.type being singleton and final val x being singleton.

@allanrenucci allanrenucci reopened this May 29, 2018
@Blaisorblade
Copy link
Contributor

TL;DR
Talking with @allanrenucci we agreed that for constants like "foo" or 1 (in code, ConstantType) we want to use the constant as a singleton type (no .type added, so that they're legal syntax):

scala> val a: "foo" = "foo"
val a: "foo" = "foo"
scala> val b: "foo" = a
val b: "foo" = "foo"

but for other singleton types (SingletonType) that refer to terms we need to follow the request in #3683:

scala> val c: a.type = a 
val c: a.type = "foo"

ConstantType extends SingletonType, so this requires adding a new branch.

(Also wondering what happens on the other subclasses of SingletonType, but I hope adding .type works there too).

Details

@allanrenucci convinced me to reconsider. We can replace String("a") with "a", since those are SIP-23 literal types, which are all primitives, hence it's reasonable to assume users can find their types from the literals. Also, writing "a" means we output legal syntax. However, "a".type is not valid syntax, so we shouldn't add .type there.

I am not able to differentiate between x: a.type being singleton and final val x being singleton.

Sorry for being unclear, you don't need to find whether final was used; I just wrote final val a = "foo" as a shortcut for writing val a: "foo" = "foo".

@eklavya
Copy link
Contributor Author

eklavya commented May 30, 2018

Adding ConstantType case breaks defs :(

In the test "methodDoesNotTakeMorePrameters" -
expected:<[((a: Int): Unit)(Scope.foo)]> but was:<[Scope.foo.type]>

@allanrenucci
Copy link
Contributor

@eklavya Can you push your current solution to see all the test cases that break

@eklavya
Copy link
Contributor Author

eklavya commented May 30, 2018

@allanrenucci pushed, I am messing around with the types of SingletonType to figure out a way to make it work without changing existing behaviour on defs, which I can't seem to find.

@allanrenucci
Copy link
Contributor

@eklavya I pushed a commit that fixes the error message test failures. For the other test failures you should update the tests

Copy link
Contributor

@allanrenucci allanrenucci left a comment

Choose a reason for hiding this comment

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

I believe the changes should go into RefinedPrinter instead

@@ -141,6 +141,10 @@ class PlainPrinter(_ctx: Context) extends Printer {
ParamRefNameString(tp) ~ ".type"
case tp: TypeParamRef =>
ParamRefNameString(tp) ~ lambdaHash(tp.binder)
case tp: ConstantType =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be:

case ConstantType(value) =>
 toText(value)

case tp: ConstantType =>
toTextRef(tp)
case tp: TermRef =>
toTextPrefix(tp.prefix) ~ selectionString(tp) ~ ".type"
Copy link
Contributor

Choose a reason for hiding this comment

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

Why special case TermRef?

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 is for a.type singleton types.

scala> final val a = "foo"
val a: "foo" = foo

scala> val x: a.type = a
val x: a.type = foo

a.type is made by special casing TermRef. I am sorry if that's nonsensical 😛

Copy link
Contributor

Choose a reason for hiding this comment

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

That makes some sense, but there are other cases above for tp: TermRef which already produce toTextRef(tp) ~ ".type" (which is equivalent to the code here), do they not apply?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had missed that. No, those cases (in PlainPrinter) are not applied because of the guards.

@OlivierBlanvillain
Copy link
Contributor

@eklavya The PR is getting really old... If you plan to work again on it please reopen or make a new one, I will close it for now.

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.

5 participants