Skip to content

package object misbehaves in the presence of classfiles #4695

Closed
scala/scala
#9661
@scabug

Description

@scabug

Multiple issues here. (And that's before I get to the question of whether one's companion object can be a package object -- although I assume the answer is "no", I find nothing unambiguous in the spec.)

package foo

class Bar { }
package object Bar { }
  1. Non-idempotence of source compilation
  2. Classes not even on the classpath fail the compilation
// first time it compiles, second time it doesn't.
% scalac3 p.scala 
% scalac3 p.scala 
p.scala:1: error: package foo contains object and package with same name: Bar
one of them needs to be removed from classpath
package foo
        ^
one error found

// it will not compile as long as those classfiles are visible from the
// current directory, even though "." is explicitly unreferenced.
% scalac3 -d /tmp -cp /asldkfjlasdfj p.scala 
p.scala:1: error: package foo contains object and package with same name: Bar
one of them needs to be removed from classpath
package foo
        ^
one error found

// delete them, it compiles.
% rm -rf foo
% scalac3 -d /tmp -cp /asldkfjlasdfj p.scala 
%

Edit: Another, possibly more serious, related issue follows. Example from mark harrah.

// demo/A.scala containing:
package demo
trait A

// demo/package.scala
package object demo extends demo.A

#!/usr/bin/env bash
#

rm -rf ./build
mkdir build
scalac -d build -cp build demo/A.scala demo/package.scala
rm -f build/demo/A.class
scalac -d build -cp build demo/A.scala demo/package.scala

It fails even when given all the source files it has just compiled:

error: error while loading demo, class file needed by package is missing.
reference type A of package demo refers to nonexisting symbol.
one error found

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions