flofriday / Moose

🐐 A new fun programming language
MIT License
6 stars 1 forks source link

Typechecker crashes with fatalError inside of Highlighter #42

Closed Jozott00 closed 1 year ago

Jozott00 commented 1 year ago

Example

a: [B] = [A(1), A(2), A(3)]
if a is [D] { t = 1 }

Note: The line number is important!

a: [B] = [A(1), A(2), A(3)]
if a is [D] { t = 1 }

The second does not crash with this error!

Description

This error occures inside of the Highlighter in line 14:

 out += String(repeating: " ", count: location.col - 1)

with the error description:

Swift/StringLegacy.swift:31: Fatal error: Negative count not allowed
2022-09-13 19:18:55.425438+0200 xctest[91287:15429268] Swift/StringLegacy.swift:31: Fatal error: Negative count not allowed
flofriday commented 1 year ago

Sure, the highlighter crashes but the lexer should never create a token that starts with col < 1.

I don't think we should even prevent the highlighter from crashing here, the assumption it makes is compleatly reasonable. The line starts at column 1 so subtracting one should be fine and never result in a negative number.

I will look into why the parser generates anything at col <= 0.

flofriday commented 1 year ago

So I printed the tokens and it seams that newlines are getting column = 0.

flofriday commented 1 year ago

Fixed, yeah newlines are tricky to handle in the lexer, so previous-me left it as an exercise for current-me (even with a comment describing the exact problem)