Skip to content

local variable name in function parameter shadows package name #11044

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
scalavision opened this issue Jan 9, 2021 · 5 comments
Closed

local variable name in function parameter shadows package name #11044

scalavision opened this issue Jan 9, 2021 · 5 comments

Comments

@scalavision
Copy link

Minimized code

package interrupt

sealed trait InterruptType
case class Stop() extends InterruptType

final class Interrupt {

  def stop(interrupt: String) =
    println(interrupt)
    interrupt.Stop()
}

Output

[error] -- [E008] Not Found Error: /home/yoda/Projects/tmp/scala3-example-project/src/main/scala/minimal/SmallTest.scala:10:14 
[error] 10 |    interrupt.Stop()
[error]    |    ^^^^^^^^^^^^^^
[error]    |    value Stop is not a member of String - did you mean interrupt.strip?

Expectation

should compile (it is compiling in scala 2.13)

@som-snytt
Copy link
Contributor

som-snytt commented Jan 9, 2021

If you supply the braces required for "old-school" lexical scoping, scala 2 demonstrates the same correct shadowing.

➜  snips cat i11044.scala
package interrupt

sealed trait InterruptType
case class Stop() extends InterruptType

final class Interrupt {

  def stop(interrupt: String) = {
    println(interrupt)
    interrupt.Stop()
  }
}
➜  snips scalac i11044.scala
i11044.scala:10: error: value Stop is not a member of String
    interrupt.Stop()
              ^
1 error
➜  snips scalac -version
Scala compiler version 2.13.4 -- Copyright 2002-2020, LAMP/EPFL and Lightbend, Inc.
➜  snips

I thought shadowing an inherited definition was an error now? but I get the same error message.

trait T {
  def interrupt = 42
}

final class Interrupt extends T {

  def stop(interrupt: String) = {
    interrupt.Stop()
  }
}

I must be missing something. I was going to test whether bindings introduced by package clauses also conflict with local defs.

Edit: it was inherited shadowing local def, where new T {} doesn't make it obvious that interrupt is in play.

@scalavision
Copy link
Author

yeah.. I forgot adding the curly braces in my example, sorry. I encountered this when trying to update zio lib to support scala3-M3, here:

https://github.com/zio/zio/pull/4516/files#diff-55db93d1616fb3c94a9699b1194a5778e50bc20c9390fe5ed3a5671cafa330c1

This works at least with scala 2.13, and probably also with M2, so it might be a regression somewhere. Thanks for looking into this.

@griggt
Copy link
Contributor

griggt commented Jan 10, 2021

The Scala Language Specification, section 9.4 says:

If a package name is shadowed, it's possible to refer to its fully-qualified name by prefixing it with the special predefined name _root_, which refers to the outermost root package that contains all top-level packages.

which is how we handled that particular update to ZIO for 3.0.0-M3 in the dotty community build: dotty-staging/zio@c08d2b0 (the change itself is needed due to the new creator applications scheme in #10784)

The _root_ construct was already used elsewhere in ZIO, e.g.: https://github.com/zio/zio/blob/4501f16ac57385ec54a38164b145a55c25726379/core/shared/src/main/scala/zio/ZIO.scala#L4326

@scalavision
Copy link
Author

thank you @griggt, I guess this one can be closed then, or did you discover anything else @som-snytt ? Sorry for the inconvenience!

@b-studios
Copy link
Contributor

Thanks @griggt, I am closing this issue for the time being.

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