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

Type checking const "this.*" incorrectly checks outside of scope where names overlap #4197

Open
hills opened this issue Oct 24, 2024 · 2 comments

Comments

@hills
Copy link

hills commented Oct 24, 2024

In this minimal test case the compiler seems to be (incorrectly) type checking against a variable declared outside the scope?

  • the @const is necessary
  • both functions must be declared with the same name
  • the name "zzz" can be anything
  • global scope is not special; it can be all wrapped in a function with the same result
$ java -jar closure-compiler-v20240317.jar -O ADVANCED  test.js
test.js:14:4: WARNING - [JSC_TYPE_MISMATCH] assignment to property zzz of MyObject
found   : function(boolean): undefined
required: function(number): undefined
  14|     this.zzz = zzz;
          ^^^^^^^^^^^^^^

0 error(s), 1 warning(s), 100.0% typed
/**
 * @param {!number} w
 */

function zzz(w) {     // this function should be irrelevant
}

/**
 * @constructor
 */

function MyObject() {
    /** @const */
    this.zzz = zzz;   // fails the type check

    /**
     * @param {boolean} v
     */

    function zzz(v) {
    }
}
@brad4d
Copy link
Contributor

brad4d commented Oct 25, 2024

Yep, that's a bug!

Thanks for reporting it.

@brad4d
Copy link
Contributor

brad4d commented Oct 25, 2024

If the inner function zzz() declaration gets moved above the this.zzz = zzz; assignment, the error no longer appears. It seems like the type checking is failing to take function hoisting into account.

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

No branches or pull requests

2 participants