Skip to content

Nobody expects to import toString #22594

Open
@som-snytt

Description

@som-snytt

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.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:importIssues tied to imports.area:replarea:reportingError reporting including formatting, implicit suggestions, etcitype:bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions