Skip to content

Unnecessary type widening in type constructors #16987

Open
@chuwy

Description

@chuwy

Details

In Iron, a library for refined types, terms of F[StringWithConstraint] unnecessarily widen to F[String]. Hopefully @Iltotore can provide more details on how Iron constraints are defined, otherwise below code should be self-explanatory.

Original ticket: Iltotore/iron#105

Compiler version

3.2.0 and 3.2.2

Minimized code

With scala-cli:

$ scala-cli --dependency io.github.iltotore::iron:2.0.0 --dependency org.typelevel::cats-core:2.9.0 
import cats.implicits.*
import cats.Applicative
import _root_.io.github.iltotore.iron.*
import _root_.io.github.iltotore.iron.constraint.string.*

type Name = String :| Alphanumeric

final case class Person(name: Name)

def test[F[_]: Applicative](str: String): F[Person] =
  Applicative[F]
    .pure(str.refine[Alphanumeric])
    .map(name => Person(name))

It works if we make F contravariant or if we provide an exact Name type parameter for map

Output

16 |    .map(name => Person(name))
   |                        ^^^^
   |Cannot refine non full inlined input at compile-time.
   |To test a constraint at runtime, use the `refined` extension method.
   |
   |Note: Due to a Scala limitation, already-refined types cannot be tested at compile-time (unless proven by an `Implication`).
   |
   |Inlined input: name
   |----------------------------------------------------------------------------
   |Inline stack trace
   |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   |This location contains code that was inlined from rs$line$3:16
    ----------------------------------------------------------------------------
1 error found

Expectation

The code should compile as is.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions