Skip to content

Add better source positions to kind errors - #3495

Merged
natefaubion merged 8 commits into
purescript:masterfrom
natefaubion:better-kind-errors
Jan 4, 2019
Merged

Add better source positions to kind errors#3495
natefaubion merged 8 commits into
purescript:masterfrom
natefaubion:better-kind-errors

Conversation

@natefaubion

Copy link
Copy Markdown
Contributor

This implements https://www.youtube.com/watch?v=rdVqQUOvxSU for the kind checker. The kinds in kind unification errors now have exact positions for the terms in the source they are derived from. Since unification doesn't really have a canonical source for the error (could be either one), and we don't have a way of reporting multiple positions (I guess we could throw two errors?), it just has a bias when reporting the source position. This seems to be a huge improvement in all the testing I've done.

screen shot 2018-12-21 at 11 23 12 pm

let sa = case getAnnForKind k2' of
NullSourceAnn -> getAnnForKind k1'
ann -> ann
throwError . errorMessage' (fst sa) $ KindsDoNotUnify k1' k2'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I guess since PositionedError takes a NEL, I could put both in there.

@kritzcreek

Copy link
Copy Markdown
Member

Christmas is a little early this year 🎉 ❤️ 🎄

errorMessage'' sss err = MultipleErrors [ErrorMessage [PositionedError sss] err]

-- | Create an error from multiple (possibly empty) source spans, reversed sorted.
errorMessage''' :: [SourceSpan] -> SimpleErrorMessage -> MultipleErrors

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Keeping the tradition alive.

@nwolverson

Copy link
Copy Markdown
Contributor

and we don't have a way of reporting multiple positions (I guess we could throw two errors?)

I think a potential future improvement (to JSON error reporting in general) is to add an additional source position, the LSP has a concept of DiagnosticRelatedInformation intended for just this kind of "here's the thing that conflicted" case.

@kritzcreek kritzcreek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is great, I tested it out on a few work things and it already helped me so much.

-> m ([SourceKind], [SourceKind])
kindsOfAll moduleName syns tys = fmap tidyUp . withFreshSubstitution . captureSubstitution $ do
synVars <- replicateM (length syns) freshKind
synVars <- replicateM (length syns) freshKind'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we traverse these syns instead and use their spans to initialize the fresh kinds here? (Also goes for all the other instances of replicateM (length x) y in here)

Something like:

  synVars <- traverse (\(_, _, sourceTy) -> freshKind (getAnnForType sourceTy)) syns

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Potentially, but I think we need to change the signature of this function to take source positions directly. The SourceType here refers to the rhs constructors, however I think when kind-checking a group like this we would want the source position to point to the lhs name.

Comment thread src/Language/PureScript/Types.hs Outdated
quantify ty = foldr (\arg t -> ForAll ann arg t Nothing) ty $ freeTypeVariables ty
where
ann = annotationForType ty
ann = getAnnForType ty

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think this name is any more helpful than just inlining its definition.

forM_ ks $ \k -> unifyKinds k kindType
unless isData $
unifyKinds tyCon (foldr (FunKind nullSourceAnn) (head ks) kargs)
unless isData $ do

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Don't need that do, unless you're doing it for consistency with the line above?

Suggested change
unless isData $ do
unless isData $

@natefaubion

Copy link
Copy Markdown
Contributor Author

I've addressed the feedback.

@kritzcreek kritzcreek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Lovely

@natefaubion
natefaubion merged commit 1dc385e into purescript:master Jan 4, 2019
@natefaubion
natefaubion deleted the better-kind-errors branch January 4, 2019 16:18
@garyb garyb mentioned this pull request Jan 12, 2019
3 tasks
dariooddenino pushed a commit to dariooddenino/purescript that referenced this pull request Jan 18, 2019
* Add lenses for annotations

* Add better source positions to kind errors

* Throw possibly multiple source spans

* Extract to error utility

* Remove unused imports

* Revert unnecessary rethrowWithPosition

* Add positions to kindsForAll vars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants