Skip to content

enums seem to be considered just like Unit when passed to as function parameters. #16115

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

Open
FrancisToth opened this issue Sep 27, 2022 · 2 comments

Comments

@FrancisToth
Copy link

Compiler version

3.1.3

Minimized code

case class Foo(data: Unit)

enum Bar:
  case Bar0

Foo(Bar.Bar0) // compiles
case object Qux0
Foo(Qux0) // does not compile as expected

Output

NA

Expectation

I may be wrong but I would expect the first snippet to throw a compilation error considering that Bar0 is not a Unit. I've asked this question on the Scala discord channel and got the following answer:

The warning on converting arbitrary values to Unit only occurs when the expression that produces the value is guaranteed to have no side-effects, and the way that scalac checks this is via checking whether the expression is

  1. a simple identifier Qux0 (not compound expressions like Bar.Bar0)
  2. not a method

If both of these conditions are met, scalac will issue a warning
You can reproduce the behavior with Bar0 by import Bar.*

I think it is totally reasonable to consider this a bug and open a ticket for it; imo paths that are made of values should throw this warning too...

@FrancisToth FrancisToth added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Sep 27, 2022
@bishabosha bishabosha removed the stat:needs triage Every issue needs to have an "area" and "itype" label label Sep 28, 2022
@bishabosha
Copy link
Member

I agree that enum values should probably be considered pure, changing this warning would be source incompatible change

@odersky
Copy link
Contributor

odersky commented Sep 29, 2022

Currently our "purity" check is very coarse. An object is considered pure if it does not have initialization code. That's why Qux0 is pure. On the other hand, the companion object of the enum Bar is not pure since it initializes some arrays.

So the purity warning is currently strictly opportunistic: If it is really easy to tell that something's pure, let's flag it. I think to make progress we should see whether we can get better effect checking in general. I'd prefer that route rather than adding a particular hack that overrides for enum's (and it's not so easy to do, since an enum companion could have side effects.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants