Skip to content
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

fix:better error msg for cyclic error for constructors #17131

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

doofin
Copy link

@doofin doofin commented Mar 18, 2023

This provides better error msg for #17076 , doesn't solve the cyclic error yet
image

@dwijnand
Copy link
Member

@doofin can we have a test case for this, at least?

@doofin
Copy link
Author

doofin commented Apr 28, 2023

@doofin can we have a test case for this, at least?

ok,I'll try to look at tests and find a suitable place to add them

@doofin
Copy link
Author

doofin commented May 1, 2023

@dwijnand added one test case

@ckipp01 ckipp01 requested a review from dwijnand May 9, 2023 17:26
@ckipp01 ckipp01 assigned dwijnand and unassigned doofin May 9, 2023
@doofin
Copy link
Author

doofin commented Oct 25, 2023

Hi @dwijnand @ckipp01 got some time to check this ? Just saw the new scala blog post "Give your feedback to improve error reporting" and it reminds me of this, thanks!

@@ -140,8 +140,11 @@ class CyclicReference private (val denot: SymDenotation)(using Context) extends
// cycleSym.flags would try completing denot and would fail, but here we can use flagsUNSAFE to detect flags
// set by the parser.
val unsafeFlags = cycleSym.flagsUNSAFE
val isMethod = unsafeFlags.is(Method)
val isMethod = unsafeFlags.is(Method) // sometimes,isMethod and isConstructor can both be true!
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment is not necessary.

Copy link
Author

Choose a reason for hiding this comment

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

Is it expected that a symbol can both be isMethod and isConstructor ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes. It is a method because it is represented with a def.

Copy link
Author

@doofin doofin Oct 25, 2023

Choose a reason for hiding this comment

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

if I remember correctly, isMethod is true(which shouldn't be) for case class as in #17076 so I add this warning, maybe I should indicate in a different way? (or maybe open a different issue for isMethod)

Copy link
Contributor

Choose a reason for hiding this comment

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

If we print in PostTyper

        case tree: DefDef =>
          println("---")
          println("DefDef: " + tree.show)
          println("flags: " + tree.symbol.flagsString)
          println("isConstructor: " + tree.symbol.isConstructor)

we can see that this class

class F(a: Int):
  def this() = this(0)
  def f = 1

has the the Method flag and isConstructor to true for both the primary and secondary constructors.

---
DefDef: def <init>(a: Int): Unit
flags: <method> <stable> <touched>
isConstructor: true
---
DefDef: def <init>(): Unit =
  {
    this(0)
    ()
  }
flags: <method> <touched> <no-default-params>
isConstructor: true
---
DefDef: def f: Int = 1
flags: <method> <touched>
isConstructor: false

Copy link
Contributor

Choose a reason for hiding this comment

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

What might happen is that the Method was not in flagsUNSAFE because it would be computed and added to flags while typing. But the failure may have happened before that.

Copy link
Author

Choose a reason for hiding this comment

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

thanks i got it, removed the comment

Copy link
Contributor

@nicolasstucki nicolasstucki left a comment

Choose a reason for hiding this comment

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

The error message of the failing code is

-- Error: t/Test.scala:7:30 ----------------------------------------------------
7 |  case class Foo[T <: Int](x: Any)
  |                              ^^^
  |                  Something's wrong: missing original symbol for type tree
-- [E044] Cyclic Error: t/Test.scala:7:2 ---------------------------------------
7 |  case class Foo[T <: Int](x: Any)
  |  ^
  |  Overloaded or recursive constructor Foo needs return type
  |
  |   Run with -explain-cyclic for more details.
  |
  | longer explanation available when compiling with `-explain`

We should fix the Something's wrong: missing original symbol for type tree. Patching the second error is not going to help. It might even hide useful information such as the info of -explain-cyclic that tells us how this happened.

@doofin
Copy link
Author

doofin commented Feb 10, 2024

The error message of the failing code is

-- Error: t/Test.scala:7:30 ----------------------------------------------------
7 |  case class Foo[T <: Int](x: Any)
  |                              ^^^
  |                  Something's wrong: missing original symbol for type tree
-- [E044] Cyclic Error: t/Test.scala:7:2 ---------------------------------------
7 |  case class Foo[T <: Int](x: Any)
  |  ^
  |  Overloaded or recursive constructor Foo needs return type
  |
  |   Run with -explain-cyclic for more details.
  |
  | longer explanation available when compiling with `-explain`

We should fix the Something's wrong: missing original symbol for type tree. Patching the second error is not going to help. It might even hide useful information such as the info of -explain-cyclic that tells us how this happened.

@nicolasstucki could you point out which file/LOC that I should change?

@nicolasstucki
Copy link
Contributor

@nicolasstucki could you point out which file/LOC that I should change?

Not sure where the bug that ended up with Something's wrong: missing original symbol for type tree originates. This is caught in

https://github.com/lampepfl/dotty/blob/6efcdbaa3df8a6b1e48dfcb208efe682bf5382e6/compiler/src/dotty/tools/dotc/typer/Typer.scala#L2159-L2168

But it does not seem like something simple to figure out.

The message was added in 9c834fc. Which comes from a println in a4516ea.

The issue might be located in tree.ensureCompletions.

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.

3 participants