scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

Problem with structural typing #967

Closed scabug closed 7 years ago

scabug commented 16 years ago

While trying to simulate SML:

scala> type Nat = { type T; val z: T; def s(arg: T): T }
defined type alias Nat

scala> val nat : Nat = new { type T = Int; val z = 0; def s(arg:Int) = arg + 1 }
nat: Nat = $$anon$$1@150ed68

scala> nat.z
res0: nat.T = 0

scala> nat.s(nat.z)
java.lang.NoSuchMethodException: $$anon$$1.s(java.lang.Object)
        at java.lang.Class.getMethod(Class.java:1581)
        at .reflMethod$$Method1(<console>:7)
        at .<init>(<console>:7)
        at .<clinit>(<console>)
        at RequestResult$$.<init>(<console>:3)
        at RequestResult$$.<clinit>(<console>)
        at RequestResult$$result(<console>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflec...
scabug commented 16 years ago

Imported From: https://issues.scala-lang.org/browse/SI-967?orig=1 Reporter: Geoffrey Alan Washburn (washburn)

scabug commented 15 years ago

@odersky said: Milestone next_bugfix deleted

scabug commented 15 years ago

@dubochet said: I now understand why this is a problem. Unfortunately, without runtime types (or manifests) this bug cannot be fixed in another way than to have the compiler reject that code.

More precisely --- take a deep breath at that point ---, the compiler must reject method calls on structural types that contain methods in which a parameter is defined as a type member of the structural type.

I'll implement that test when I have time.

scabug commented 15 years ago

@dubochet said: Fixed in r19442.

scabug commented 11 years ago

@milessabin said: I'm going to raise this from the dead, because a very minor variation on Geoffrey's original example fails in exactly the same way. This isn't a regression in 2.10 ... I don't think the problem was ever properly fixed.

scala> import scala.language.reflectiveCalls
import scala.language.reflectiveCalls

scala> type Nat = { type T; val z: T; def s()(arg: T): T }
defined type alias Nat

scala> val nat : Nat = new { type T = Int; val z = 0; def s()(arg:Int) = arg + 1 }
nat: Nat = $anon$1@4a2ead0a

scala> nat.z
res0: nat.T = 0

scala> nat.s()(nat.z)
java.lang.NoSuchMethodException: $anon$1.s(java.lang.Object)
        at java.lang.Class.getMethod(Class.java:1622)
        at .reflMethod$Method1(<console>:11)
        at .<init>(<console>:11)
        at .<clinit>(<console>:11)
        at .<init>(<console>:7)
        at .<clinit>(<console>)
        at $print(<console>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:731)
        at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:980)
        at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:570)
        at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:601)
        at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:565)
        at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:745)
        at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:790)
        at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:702)
        at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:566)
        at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:573)
        at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:576)
        at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:867)
        at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
        at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
        at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
        at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822)
        at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:83)
        at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96)
        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:105)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
scabug commented 11 years ago

@paulp said: See also #5378, which I never got back to somehow. There are probably a whole bunch of variations on this available given the ad hoc manner in which it is recognized.

scabug commented 11 years ago

@JamesIry said: 2.10.2 is about to be cut. Kicking down the road and un-assigning to foster work stealing.

scabug commented 7 years ago

Olivier Blanvillain (OlivierBlanvillain) said: I think this can be closed, scalac now properly rejects the Nat type in Miles comments with an error: Parameter type in structural refinement may not refer to a type member of that refinement.