HoriSun / closure-compiler

Automatically exported from code.google.com/p/closure-compiler
0 stars 0 forks source link

Closure compiler incorrectly infers the type of 'this' in Function.prototype.bind. #1298

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

1. Compile the following code:
  var f = function() {
    /** @type {null} var x = this;
    return x + 1;
  }.bind(2);

What is the expected output? What do you see instead?
This should produce an error:
  WARNING - initializing variable
  found: number
  required: null
However, instead the code passes, indicating that the type of 'this' within the 
function is not being bound properly.  Note that using goog.bind instead of 
Function.prototype.bind does, in fact, produce this warning.

Original issue reported on code.google.com by s...@google.com on 8 Apr 2014 at 10:32

GoogleCodeExporter commented 9 years ago
I think the fix for this would probably be to update the type annotations on 
the externs for Function.prototype.bind, in es5.js, so that the compiler can 
figure everything out, the way it does for goog.bind.

Original comment by tbreisac...@google.com on 8 Apr 2014 at 10:37

GoogleCodeExporter commented 9 years ago
I don't think this works.  With goog.bind you can write

  /** @param {function(this: T, ...)} @param {T} @template T */
  goog.bind = ....

But here, you'd have to write something like

  /** @this {function(this: T, ...)} @param {T} @template T */
  Function.prototype.bind = ...

And the compiler just doesn't understand how to infer the "this:" backwards 
into the "@this" function in the same way it can for goog.bind.  So I think 
this actually needs a patch to the compiler to understand this type of 
inference.

(It's particularly confusing to think about - since the point isn't getting the 
right "this" in the returned function, but reverse-inferring the "this" in the 
input function).

Original comment by s...@google.com on 15 Apr 2014 at 7:20

GoogleCodeExporter commented 9 years ago
Issue tracking has been migrated to github. Please make any further comments on 
this issue through https://github.com/google/closure-compiler/issues

Original comment by blic...@google.com on 1 May 2014 at 6:31

GoogleCodeExporter commented 9 years ago

Original comment by blic...@google.com on 1 May 2014 at 6:34