Skip to content

Warn when a user defined error message references undefined names #4008

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
Jasper-M opened this issue Feb 16, 2018 · 7 comments · Fixed by #9430
Closed

Warn when a user defined error message references undefined names #4008

Jasper-M opened this issue Feb 16, 2018 · 7 comments · Fixed by #9430
Labels
area:annotations area:reporting Error reporting including formatting, implicit suggestions, etc help wanted itype:enhancement prio:low

Comments

@Jasper-M
Copy link
Contributor

Currently dotty doesn't warn when you reference an unknown type parameter, but silently accepts it. It might be friendlier towards the user to warn that he probably made a typo.

scala> @annotation.implicitNotFound("Bar[${B}] not found") trait Bar[A] 
// defined trait Bar
scala> implicitly[Bar[String]] 
1 |implicitly[Bar[String]]
  |                       ^
  |                       Bar[${B}] not found
@Blaisorblade Blaisorblade added itype:bug itype:enhancement prio:low area:annotations area:reporting Error reporting including formatting, implicit suggestions, etc exp:novice labels Feb 16, 2018
@Blaisorblade
Copy link
Contributor

Agreed that would be better, and @smarter already agreed in #4007 (comment).

I tried triaging and labeling but I'm new here, might have to revisit.

@YourPsychiatrist
Copy link

YourPsychiatrist commented Jul 19, 2020

I have pretty much figured out how to implement this. Something that still puzzles me though: The argument to the annotation is not interpolated (with the s string context for example);

So what I am asking myself is: Is there a string interpolation subsystem somewhere in the compiler that I can use to extract terms or would I have to implement that myself since the interpolation actually uses the compiler state context and not the program context?

Once that question is cleared, i'd gladly take care of the issue!

@Blaisorblade
Copy link
Contributor

Blaisorblade commented Jul 20, 2020

Others know better, but AFAIK: There is an interpolation subsystem in Dotty for Dotty's use, but the arguments of implicitNotFound are parsed using different code; so I don't think I understand the question well.

  1. The compiler does have all sorts of interpolators for producing error messages — many are used in:
    https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/reporting/messages.scala.
    And some (including ex) were introduced in 8d4d9a3.
  2. Here's how implicitNotFound arguments are parsed, AFAICS:
    https://github.com/lampepfl/dotty/blob/aa64078d9defe1df6856158a16c1f236792231f2/compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala#L131-L142.

However, that code is run when the message is actually used, so the error must be added elsewhere.

@YourPsychiatrist
Copy link

YourPsychiatrist commented Jul 20, 2020

Yes this seems like the use-site error reporting. For the declaration of the user-defined error string, I am looking through the literal in the RefChecks phase. But I don't want to reimplement this special kind of interpolation. Is there a spec for how such user defined messages can be structured? Is it equal to what the s context provides?

@smarter
Copy link
Member

smarter commented Jul 20, 2020

Is there a spec for how such user defined messages can be structured?

The documentation of the annotation has some details: https://www.scala-lang.org/api/2.13.3/scala/annotation/implicitNotFound.html

@YourPsychiatrist
Copy link

Ah! Thank you kindly, then I should be all set.

@YourPsychiatrist
Copy link

Just for the reference, I want to also add the def case to this issue since its in the same domain:

class K[A] {
  def m[B](implicit c: C[List[A], B]) = 0
  def n[B](implicit @implicitNotFound("Specific message for C of list of ${A} and ${B}") c: C[List[A], B]) = 1
}

For the sake of completeness, I feel like this issue should also cover this case (more generally, for all DefDefs, including constructors)

YourPsychiatrist pushed a commit to YourPsychiatrist/dotty that referenced this issue Jul 24, 2020
YourPsychiatrist pushed a commit to YourPsychiatrist/dotty that referenced this issue Jul 25, 2020
Both, annotated function arguments and annotated classes / traits,
are now checked for invalid type variable references and warn at
declaration site.
YourPsychiatrist pushed a commit to YourPsychiatrist/dotty that referenced this issue Jul 25, 2020
Both, annotated function arguments and annotated classes / traits,
are now checked for invalid type variable references and warn at
declaration site.
YourPsychiatrist pushed a commit to YourPsychiatrist/dotty that referenced this issue Jul 25, 2020
Both, annotated function arguments and annotated classes / traits,
are now checked for invalid type variable references and warn at
declaration site.
YourPsychiatrist pushed a commit to YourPsychiatrist/dotty that referenced this issue Aug 7, 2020
Both, annotated function arguments and annotated classes / traits,
are now checked for invalid type variable references and warn at
declaration site.
YourPsychiatrist pushed a commit to YourPsychiatrist/dotty that referenced this issue Aug 7, 2020
Both, annotated function arguments and annotated classes / traits,
are now checked for invalid type variable references and warn at
declaration site.
YourPsychiatrist pushed a commit to YourPsychiatrist/dotty that referenced this issue Aug 7, 2020
Both, annotated function arguments and annotated classes / traits,
are now checked for invalid type variable references and warn at
declaration site.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:annotations area:reporting Error reporting including formatting, implicit suggestions, etc help wanted itype:enhancement prio:low
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants