Skip to content

Assertion error with if-else-if inside class #5444

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
jeffreymorlan opened this issue Oct 29, 2015 · 5 comments
Closed

Assertion error with if-else-if inside class #5444

jeffreymorlan opened this issue Oct 29, 2015 · 5 comments
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this

Comments

@jeffreymorlan
Copy link
Contributor

Type-checking the following (broken) program:

class C {
    if (a) {}
    else if (0) {}
}

results in an assertion error:

Error: Debug Failure. False expression: 
    at Object.assert (/home/jeffrey/TypeScript-1.6.2/lib/tsc.js:724:23)
    at reportImplementationExpectedError (/home/jeffrey/TypeScript-1.6.2/lib/tsc.js:18530:38)
    at checkFunctionOrConstructorSymbol (/home/jeffrey/TypeScript-1.6.2/lib/tsc.js:18580:25)
    at checkFunctionLikeDeclaration (/home/jeffrey/TypeScript-1.6.2/lib/tsc.js:18943:21)
    at checkMethodDeclaration (/home/jeffrey/TypeScript-1.6.2/lib/tsc.js:18251:13)
    at checkSourceElement (/home/jeffrey/TypeScript-1.6.2/lib/tsc.js:20413:28)
    at Object.forEach (/home/jeffrey/TypeScript-1.6.2/lib/tsc.js:90:30)
    at checkClassDeclaration (/home/jeffrey/TypeScript-1.6.2/lib/tsc.js:19663:16)
    at checkSourceElement (/home/jeffrey/TypeScript-1.6.2/lib/tsc.js:20477:28)
    at Object.forEach (/home/jeffrey/TypeScript-1.6.2/lib/tsc.js:90:30)
@mhegazy mhegazy added Bug A bug in TypeScript Help Wanted You can do this labels Oct 29, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Oct 29, 2015

A PR would be appreciated.

@mhegazy mhegazy added this to the Community milestone Oct 29, 2015
@jeffreymorlan
Copy link
Contributor Author

I don't know what the right fix to this is, but here's my analysis:

  • The "if"s are getting parsed as MethodDeclarations. The first method node's .end is less than the second method node's .pos because of the intervening erroneous "else", which does not become a node.

  • checkFunctionOrConstructSymbol loops over declarations of a symbol. If declarations are not "consecutive", it calls reportImplementationExpectedError on the earlier one:

    else if (!isExportSymbolInsideModule && previousDeclaration && previousDeclaration.parent === node.parent && previousDeclaration.end !== node.pos) {
        reportImplementationExpectedError(previousDeclaration);
    }
  • reportImplementationExpectedError assumes that its node and its next sibling (found by ts.forEachChild) must be for different symbols (and thus differ in some way: kind, name, or static-ness). Because if they were for the same symbol, those would be "consecutive" declarations and not an error.

The problem is the two different definitions of what makes nodes "consecutive". Is it node.pos === prev.end, or being next in the ts.forEachChild iteration? cFOCS uses the former definition, while rIEE uses the latter. And with the current parser, they are not the same.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 7, 2015

@vladima any thoughts?

jeffreymorlan pushed a commit to jeffreymorlan/TypeScript that referenced this issue Nov 8, 2015
jeffreymorlan added a commit to jeffreymorlan/TypeScript that referenced this issue Nov 8, 2015
reportImplementationExpectedError: The next node in the tree is not
necessarily consecutive. This happens due to syntax errors, e.g.

    class C { foo(), foo(); }
@jeffreymorlan
Copy link
Contributor Author

I've realized it would be inefficient for checkFunctionOrConstructorSymbol to walk the tree since it has to check for consecutiveness on every function/method overload, whether error or not, and having to scan the list of children for each means checking a class becomes potentially O(n²). So it's reportImplementationExpectedError that needs to change. PR

@DanielRosenwasser DanielRosenwasser added the Fixed A PR has been merged for this issue label Nov 28, 2015
@DanielRosenwasser
Copy link
Member

Thanks @jeffreymorlan!

@mhegazy mhegazy modified the milestones: TypeScript 1.8, Community Nov 30, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

3 participants