Skip to content
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

Internal type error in extraction when passing anonymous trait instance to method expecting trait #1553

Open
vkuncak opened this issue Aug 11, 2024 · 2 comments

Comments

@vkuncak
Copy link
Collaborator

vkuncak commented Aug 11, 2024

This code:

object WrapperTest:
  trait Spec {
  }

  case class Wrapper(underlying: Spec) extends Spec {
  }

  def test =
    def w: Spec = Wrapper(new Wrapper(w) { })

end WrapperTest

gives the error message below. It seems fine to reject such wrapper as it causes non-termination etc., but we seem to reject by way of an internal error and possibly by accident, so we should at least report a meaningful error to the user.

[  Info  ] Finished compiling                                       
[  Info  ] Preprocessing the symbols...                             
[ Error  ] WrapperTest.scala:9:5: Type error: @final
[ Error  ] def w: Spec = Wrapper({
[ Error  ]   case class $anon extends Wrapper {
[ Error  ]     
[ Error  ]   
[ Error  ]     
[ Error  ]   }
[ Error  ]   $anon()
[ Error  ] })
[ Error  ] (), expected Unit,
[ Error  ] found <untyped>
[ Error  ] 
[ Error  ] Typing explanation:
[ Error  ] @final
[ Error  ] def w: Spec = Wrapper({
[ Error  ]   case class $anon extends Wrapper {
[ Error  ]     
[ Error  ]   
[ Error  ]     
[ Error  ]   }
[ Error  ]   $anon()
[ Error  ] })
[ Error  ] () is of type <untyped>
[ Error  ]   Wrapper({
[ Error  ]     case class $anon extends Wrapper {
[ Error  ]       
[ Error  ]     
[ Error  ]       
[ Error  ]     }
[ Error  ]     $anon()
[ Error  ]   }) is of type <untyped>
[ Error  ]     case class $anon extends Wrapper {
[ Error  ]       
[ Error  ]     
[ Error  ]       
[ Error  ]     }
[ Error  ]     $anon() is of type $anon
[ Error  ]       $anon() is of type $anon
[ Error  ]   () is of type Unit
               def w: Spec = Wrapper(new Wrapper(w) { })
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[ Fatal  ] Well-formedness check failed after extraction
[ Error  ] Stainless terminated with an error.
@vkuncak
Copy link
Collaborator Author

vkuncak commented Aug 11, 2024

Same error message if I define test as

  def test: Unit = {
    def w: Spec = Wrapper(new Wrapper(w) { })

    ()
  }

@vkuncak
Copy link
Collaborator Author

vkuncak commented Aug 11, 2024

An explicit asInstanceOf test works and this passes entirely (there are no methods to cause infinite recursion):

object WrapperTest:
  trait Spec { }
  case class Wrapper(underlying: Spec) extends Spec { }
  def test: Unit = {
    def w: Wrapper = Wrapper((new Wrapper(w) { }).asInstanceOf[Wrapper])
    ()
  }
end WrapperTest

So it looks like there's some limitation in how our type checker or extractor uses expected type in the presence of subtyping with anonymous classes

@vkuncak vkuncak changed the title Internal type error when wrapping interface around an inner class Internal type error in extraction when passing anonymous class instance to method expecting trait Aug 11, 2024
@vkuncak vkuncak changed the title Internal type error in extraction when passing anonymous class instance to method expecting trait Internal type error in extraction when passing anonymous trait instance to method expecting trait Aug 11, 2024
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

1 participant