Skip to content

Execution branches not evaluated for super/this calls #38512

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
ricmoo opened this issue May 12, 2020 · 1 comment · Fixed by #38612
Closed

Execution branches not evaluated for super/this calls #38512

ricmoo opened this issue May 12, 2020 · 1 comment · Fixed by #38612
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@ricmoo
Copy link

ricmoo commented May 12, 2020

Search Terms:
super, this and variations of "'super' must be called before accessing 'this' in the constructor of a derived class."

Code

export class Foo {
  constructor(value: number) {
  }
}

export class BarCorrectlyFails extends Foo {
    constructor(something: boolean) {
      if (!something) {
        const value = this.bar();  // <--- this used before super; correctly detected
        super(value);
      } else {
        super(1337);
      }
    }
    bar(): number { return 4; }
}

export class BarIncorrectlyWorks extends Foo {
    constructor(something: boolean) {
      if (something) {
        super(1337);
      } else {
        const value = this.bar();  // <--- this used before super; not detected
        super(value);
      }
    }
    bar(): number { return 4; }
}

Expected behavior:

Both of the above Bar* classes should fail to compile. The branches seem to be ignored when determining if this and super are called in-order, so the above super call in another branch seems to allow compilation to continue.

Actual behavior:

The BarIncorrectlyWorks produces ESM code which fails at runtime, when the super occurs before the this.

@ahejlsberg
Copy link
Member

The issue here is our fairly naive check that first occurrence of a super call in lexical order occurs before every this member access. That check is really only correct when there are no branches in the code.

@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label May 12, 2020
@ahejlsberg ahejlsberg added this to the TypeScript 4.0 milestone May 12, 2020
@ahejlsberg ahejlsberg self-assigned this May 13, 2020
@ahejlsberg ahejlsberg added the Fix Available A PR has been opened for this issue label May 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants