Skip to content

Extension method not choosing available alternative in infix syntax #26793

Description

@WojciechMazur

Based on OpenCB failure in input-output-hk/scrypto - activated by 6d8bc4a

Original usage was operating on Int -> RichInt, this reproducer changes that to trait Number to ensure stdlib does not affect it

Compiler version

3.10.0-RC1-bin-20260813-afdb1e2-NIGHTLY

All Scala versions, tested with 3.3.0

Minimized code

trait Number
object ImplictSyntax:
    implicit def toRichInt(self: Number): RichInt = ???
    class RichInt(self: Number):
        def to(end: Int): Range = ???
        def to(end: Int, step: Int): Range = ???

object ExtensionSyntax:
    extension (self: Number) 
        def to(end: Int): Range = ???
        def to(end: Int, step: Int): Range = ???

def Test(max: Number) = 
    locally:
        import ImplictSyntax.*
        val _ = max to(0, -1) // warn, infix syntax
        val _ = max.to(0, -1)
    locally:
        import ExtensionSyntax.*
        val _  = max to(0, -1) // error type mismatch
        val _  = max.to(0, -1)

Output

 scala compile test.scala -S 3.nightly --server=false
-- [E007] Type Mismatch Error: /Users/wmazur/projects/scala/community-build3/test.scala:21:23 
21 |        val _  = max to(0, -1) // error
   |                       ^^^^^^^
   |                       Found:    (Int, Int)
   |                       Required: Int
   |
   | longer explanation available when compiling with `-explain`
-- Warning: /Users/wmazur/projects/scala/community-build3/test.scala:17:20 -----
17 |        val _ = max to(0, -1)
   |                    ^^
   |Alphanumeric method to is not declared infix; it should not be used as infix operator.
   |Instead, use method syntax .to(...) or backticked identifier `to`.
   |The latter can be rewritten automatically under -rewrite -source 3.4-migration.
there was 1 feature warning; re-run with -feature for details
2 warnings found
1 error found

Expectation

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions