Skip to content

Find implicitNotFoundMessage on types #11823

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

Merged
merged 1 commit into from
Mar 22, 2021
Merged

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Mar 19, 2021

Fixes #11797

@@ -0,0 +1,6 @@
object Foo:
@annotation.implicitNotFound("Oops")
type Bar
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to also add a test with an opaque type? Are opaque types just represented as a TypeRef like transparent types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes that's the same

@adamgfraser
Copy link
Contributor

Does this work when the type is just a type alias like this?

import scala.annotation.implicitNotFound

@implicitNotFound("${A} is not a subtype of ${B}")
type IsSubtypeOf[-A, +B] = A <:< B

object Example {
  implicitly[String IsSubtypeOf Int]
}

@odersky
Copy link
Contributor Author

odersky commented Mar 21, 2021

Does this work when the type is just a type alias like this?

No, since the compiler is free to dealias the type before attempting an implicit search. It has to be an abstract type or opaque type.

@adamgfraser
Copy link
Contributor

Would it be possible to support that? That functionality exists on Scala 2 and can be helpful for library authors to be able to alias existing types and provide more specific error messages associated with an implicit not being found rather than having to create new data types to wrap the existing ones.

@julienrf
Copy link
Contributor

It has to be an abstract type or opaque type.

How is this situation handled?

trait Foo:
  @implicitNotFound("Oops")
  type Bar

trait Foo2 extends Foo:
  type Bar = Unit

object Foo3 extends Foo2:
  summon[Bar]

Although Bar is initially defined as an abstract type member in Foo, it becomes a transparent type alias in Foo2.

@odersky
Copy link
Contributor Author

odersky commented Mar 21, 2021

@julienrf Looks like this falls back to the generic case as well.

@odersky
Copy link
Contributor Author

odersky commented Mar 21, 2021

I had a look: it does not look easy to improve on this. It seems the type is dealiased way before we try to compose an error message.

@adamgfraser
Copy link
Contributor

@odersky Thanks for looking!

@odersky odersky closed this Mar 22, 2021
@odersky odersky reopened this Mar 22, 2021
@odersky odersky merged commit 457b207 into scala:master Mar 22, 2021
@odersky odersky deleted the fix-11797 branch March 22, 2021 09:42
@Kordyjan Kordyjan added this to the 3.0.0 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@implicitNotFound is ignored when the queried type is a type member (not a trait or a class)
5 participants