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

Parser error type identifier #4878

Closed
wants to merge 7 commits into from

Conversation

jbondc
Copy link
Contributor

@jbondc jbondc commented Sep 20, 2015

This improves parsing an invalid type identifier #4067

let d: string | new () => void | number; // Invalid type. To avoid ambiguity, add parentheses: '(new () => void | number)'
let g: x.void; // Identifier expected, reserved word 'void' not allowed here.

Was tricky, added a ParserContextFlags.NoError flag to disable errors on parsing, then check for missing node(s) to improve the error(s).

node.parameterName = <Identifier>typeName;
node.type = parseType();
return finishNode(node);
let pnode = <TypePredicateNode>createNode(SyntaxKind.TypePredicate, typeName.pos);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

predicateNode

@DanielRosenwasser
Copy link
Member

@jbondc I don't think this is the correct approach for the original bug. As @CyrusNajmabadi pointed out, I think we'd be better off parsing the type and erroring afterwards.

Additionally, I think we should be reporting the specific errors suggested.

@jbondc
Copy link
Contributor Author

jbondc commented Sep 21, 2015

Used this technique to avoid lookAhead() in isStartOfFunctionType() but haven't done any benchmarks. Don't mind any other approach. Surprised you prefer the more verbose errors though.

@jbondc
Copy link
Contributor Author

jbondc commented Sep 24, 2015

Going to reference #3003 to this, having the ability to improve error messages seems valuable too.

@jbondc
Copy link
Contributor Author

jbondc commented Oct 8, 2015

@DanielRosenwasser is performance the primary concern? Don't mind revisiting the approach.

@jbondc
Copy link
Contributor Author

jbondc commented Oct 20, 2015

Another case 😢
image

@jbondc
Copy link
Contributor Author

jbondc commented Oct 20, 2015

Would be nice to have a more generalized solution to parser error recovery, #5173, #4260

Do think the approach I took was a step in the right direction:
https://github.com/Microsoft/TypeScript/search?q=helpful+error&type=Issues&utf8=%E2%9C%93

@CyrusNajmabadi
Copy link
Contributor

It seems like there should be a much easier fix here. Namely, after parsing out the | just allow parsing any sort of type. Then, later on, when we're type checking, see that you have a "construct signature type" in an innapropriate location (as a "union type element") and give a nice clear error that that's not allowed because of ambiguity, and you should wrap the type with parens.

I've very hesitant about having any sort of parser flags. Esp. around errors.

@jbondc
Copy link
Contributor Author

jbondc commented Oct 23, 2015

@CyrusNajmabadi I'll try something else.

Is there any reason a missing node doesn't have a flag?
https://github.com/Microsoft/TypeScript/pull/4878/files#diff-ead24f0f0f59c0ea9c1c511052e8884bR1027

That seems useful. It could be a single check here:
https://github.com/Microsoft/TypeScript/blob/master/src/compiler/utilities.ts#L186

    export function nodeIsMissing(node: Node) {
        if (!node) {
            return true;
        }

        return node.flags & NodeFlags.Missing;
    }

@CyrusNajmabadi
Copy link
Contributor

Yes. It was very intentional. Such a flag would be redundant with existing information already on the node. Redundant information in a tree is very concerning to me as it now means you have to ensure that they must stay in sync 100% of the time. If you screw that up then you get inconsistencies that make everything difficult.

Such a concern is not hypothetical either. Both the original TS parser and the Roslyn parsers suffered from this problem. They had redundant forms of information that weren't always kept in sync properly. And people used different mechanisms to grok out that information later in the pipeline, leading to all sorts of subtle and annoying bugs. I'd far prefer to have one uniform way to encode if a node is missing, and one way to check for it (through a function which everyone calls).

@jbondc jbondc force-pushed the parser-error-type-identifier branch from ba90e05 to 1868d43 Compare October 26, 2015 15:53
@jbondc
Copy link
Contributor Author

jbondc commented Oct 26, 2015

@CyrusNajmabadi Refactored using a contextualError, in the long run patch could:
a) speedup parser by avoiding lookAhead/speculationHelper:
https://github.com/jbondc/TypeScript/blob/72337f05db6ce954b07f79fa3ea48ad91c80caf5/src/compiler/parser.ts#L869
b) enable extension points into error handling
jbondc@30ea460#diff-ead24f0f0f59c0ea9c1c511052e8884bR938

Let me know what you think

!!! error TS1005: '}' expected.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a regression.

@CyrusNajmabadi
Copy link
Contributor

@jbondc I'm not a fan of this approach. As mentioned earlier, I don't believe the flag I appropriate. It's now confusing to users if they should check that flag or use the function to test if a node is missing.

Second, this adds a lot of complexity into the parser. A much simpler solution would be to just make parsing more lenient, and then check for invalid precedence later on during our normal syntactic checks.

@mhegazy
Copy link
Contributor

mhegazy commented Dec 7, 2015

As @CyrusNajmabadi explained, this is the correct approach. there has not been much activity here either, so closing. please resubmit with the outlined approach.

@mhegazy mhegazy closed this Dec 7, 2015
@jbondc
Copy link
Contributor Author

jbondc commented Dec 7, 2015

k bye

@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
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants