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

Improve error messages for leaking of this as constructor arguments #15363

Closed
liufengyun opened this issue Jun 2, 2022 · 0 comments · Fixed by #15364
Closed

Improve error messages for leaking of this as constructor arguments #15363

liufengyun opened this issue Jun 2, 2022 · 0 comments · Fixed by #15364

Comments

@liufengyun
Copy link
Contributor

Minimized example

Given the following program

class A:
  // should report one error here
  val b = new B(this) // error
  val m = 10
  val n = 20

class B(a: A):
  val x = a.m
  val y = a.n

Output

Compile with -Ysafe-init, the compiler generates the following warnings:

-- Warning: tests/init/neg/leak-constr.scala:8:12 ------------------------------
8 |  val x = a.m
  |          ^^^
  |Access field on a value with an unknown initialization status. Calling trace:
  |-> class A:	[ leak-constr.scala:1 ]
  |   ^
  |-> val b = new B(this) // error	[ leak-constr.scala:3 ]
  |           ^^^^^^^^^^^
  |-> class B(a: A):	[ leak-constr.scala:7 ]
  |   ^
  |-> val x = a.m	[ leak-constr.scala:8 ]
  |           ^^^
-- Warning: tests/init/neg/leak-constr.scala:9:12 ------------------------------
9 |  val y = a.n
  |          ^^^
  |Access field on a value with an unknown initialization status. Calling trace:
  |-> class A:	[ leak-constr.scala:1 ]
  |   ^
  |-> val b = new B(this) // error	[ leak-constr.scala:3 ]
  |           ^^^^^^^^^^^
  |-> class B(a: A):	[ leak-constr.scala:7 ]
  |   ^
  |-> val y = a.n	[ leak-constr.scala:9 ]
  |           ^^^
2 warnings found

Expectation

The warnings are legit, however, there are two problems:

  • The leaking cause too many errors to be useful
  • The error does not point to the leaking site and is a little difficult to understand

Ideally, we should only report one error at the leaking site if the leaking is unsafe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants