Skip to content

Nobody expects to import toString #22594

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
som-snytt opened this issue Feb 12, 2025 · 2 comments
Open

Nobody expects to import toString #22594

som-snytt opened this issue Feb 12, 2025 · 2 comments
Labels
area:import Issues tied to imports. area:repl area:reporting Error reporting including formatting, implicit suggestions, etc itype:bug

Comments

@som-snytt
Copy link
Contributor

Compiler version

3.6.3

Minimized code

scala> def f = { import scala.util.Properties.*; toString }
-- [E049] Reference Error: -----------------------------------------------------
1 |def f = { import scala.util.Properties.*; toString }
  |                                          ^^^^^^^^
  |               Reference to toString is ambiguous.
  |               It is both defined in object rs$line$1
  |               and imported subsequently by import scala.util.Properties._
  |
  | longer explanation available when compiling with `-explain`
1 error found

Output

[error] 594 |      println(s"ARG $toString")
[error]     |                     ^^^^^^^^
[error]     |           Reference to toString is ambiguous.
[error]     |           It is both defined in class Test
[error]     |           and imported subsequently by import scala.util.Properties._

Expectation

When I'm really frazzled and trying to debug, don't mess with me, just print the string.

The example is from vulpix, where the local import was 100 lines earlier in the source.

This came up in Scala 2 implementation of the ambiguity for inherited vs definitions in scope. That was at scala/scala#10220 (comment) where an exception for toString was deemed too special.

Scala 2 disallows importing universal members from root imports. There is a ticket for package object members referenced by top level defs.

So there is precedent for placing limits on these behaviors which sacrifice convenience and intuition to regularity.

In a template nesting level, an import is shadowed by an inherited member, but a local import wins.

A possible limit would be to always disallow import of universal members by wildcard import (including root imports).

(Whether to detune the ambiguity for inherited vs scope is a separate issue, but worth considering.)

@som-snytt som-snytt added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Feb 12, 2025
@Gedochao
Copy link
Contributor

Note: this currently causes a warning when done outside of the REPL.

def f = { import scala.util.Properties.*; toString }
scala-cli compile repro.scala --server=false
-- [E181] Potential Issue Warning: /Users/pchabelski/IdeaProjects/scala-cli-tests-2/untitled/compiler-repro/repro.scala:1:42 
1 |def f = { import scala.util.Properties.*; toString }
  |                                          ^^^^^^^^
  |                         Suspicious top-level unqualified call to toString
  |
  | longer explanation available when compiling with `-explain`
1 warning found

@Gedochao Gedochao added area:import Issues tied to imports. and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Feb 13, 2025
@som-snytt
Copy link
Contributor Author

That's funny. The top-level warning is supposed to warn that you're using toString of a package object, which isn't the case here.

I don't know why it doesn't error in a top-level def.

class C:
  def f =
    import java.util.Properties.*
    toString // error typer

def g =
  import java.util.Properties.*
  toString // warn refchecks

@Gedochao Gedochao added area:repl area:reporting Error reporting including formatting, implicit suggestions, etc labels Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:import Issues tied to imports. area:repl area:reporting Error reporting including formatting, implicit suggestions, etc itype:bug
Projects
None yet
Development

No branches or pull requests

2 participants