Skip to content

Extension method overload not picked up when parameter is conditional #20335

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

Closed
ghostdogpr opened this issue May 5, 2024 · 2 comments · Fixed by #23212
Closed

Extension method overload not picked up when parameter is conditional #20335

ghostdogpr opened this issue May 5, 2024 · 2 comments · Fixed by #23212
Labels
area:implicits related to implicits itype:bug

Comments

@ghostdogpr
Copy link
Contributor

ghostdogpr commented May 5, 2024

Compiler version

3.4.1

Minimized code

import java.time.OffsetDateTime
import scala.concurrent.duration.*

val dateTime = OffsetDateTime.now()

implicit class DateTimeOps(val dateTime: OffsetDateTime) extends AnyVal {
  def plus(amount: FiniteDuration): OffsetDateTime =
    dateTime
}

dateTime plus Duration.Zero // ok
dateTime plus (if (true) Duration.Zero else Duration.Zero) // not ok

Scastie: https://scastie.scala-lang.org/YNoWvmUYTHSWmgpHzWwj9A

Output

Found:    (scala.concurrent.duration.Duration.Zero :
  scala.concurrent.duration.FiniteDuration)
Required: java.time.temporal.TemporalAmount

Expectation

The same code compiles well with Scala 2: https://scastie.scala-lang.org/lfVKMgn4RRWpelXheh5rjQ
I tried using extension but the result is the same.
Note that this happens when there is already a method of the same name defined on the class (plus in my example exists in OffsetDateTime with different parameters).

@ghostdogpr ghostdogpr added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels May 5, 2024
@ghostdogpr ghostdogpr changed the title Extension method not picked up when parameter is conditional Extension method overload not picked up when parameter is conditional May 5, 2024
@nox213
Copy link
Contributor

nox213 commented May 8, 2024

If I modify second case like this, it compiles.

dateTime plus ((if (true) Duration.Zero else Duration.Zero): FiniteDuration)

Mabye the issue is related with type inference.

@som-snytt
Copy link
Contributor

also OffsetDateTime#plus is overloaded. But I wasn't able to minimize quickly. Maybe there are implicits on Duration involved.

@Gedochao Gedochao added area:infer area:implicits related to implicits and removed stat:needs triage Every issue needs to have an "area" and "itype" label area:infer labels May 9, 2024
Alex1005a added a commit to Alex1005a/scala3 that referenced this issue May 21, 2025
noti0na1 added a commit that referenced this issue May 22, 2025
…3212)

Fixes #20335
The problem seems to be that the compiler does not try to look for
extensions and implicit conversions in case of an error, which is
generally correct, for performance reasons. But if the argument type
does not match the one the function expects, it is correct to try to
find an extension with the appropriate type.
tgodzik pushed a commit to scala/scala3-lts that referenced this issue May 26, 2025
tgodzik pushed a commit to scala/scala3-lts that referenced this issue May 26, 2025
EnzeXing pushed a commit to EnzeXing/dotty that referenced this issue May 27, 2025
EnzeXing pushed a commit to EnzeXing/dotty that referenced this issue May 27, 2025
tgodzik added a commit to scala/scala3-lts that referenced this issue May 27, 2025
Backport "Fix scala#20335: Try extensions for arguments with type mismatch error" to 3.3 LTS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:implicits related to implicits itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants