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

interfaces: Prevent circular and non-closed requirements. #12073

Merged
merged 3 commits into from
Dec 9, 2021

Conversation

sofiafaro-da
Copy link
Contributor

Updates the haskell side to be more strict about requirements:

  • requirements must be transitively closed, so if A requires B, and B requires C,
    then A requires C.
  • no circular requirements allowed

The logic for circular requirements is a bit duplicated to get a better
error message.

(Also refactored the typechecker a little bit to use whenJust instead of pattern matching where appropriate.)

Part of #11978

changelog_begin
changelog_end

Pull Request Checklist

  • Read and understand the contribution guidelines
  • Include appropriate tests
  • Set a descriptive title and thorough description
  • Add a reference to the issue this PR will solve, if appropriate
  • Include changelog additions in one or more commit message bodies between the CHANGELOG_BEGIN and CHANGELOG_END tags
  • Normal production system change, include purpose of change in description
  • If you mean to change the status of a component, please make sure you keep the Component Status page up to date.

NOTE: CI is not automatically run on non-members pull-requests for security
reasons. The reviewer will have to comment with /AzurePipelines run to
trigger the build.

Updates the haskell side to be more strict about requirements:
- requirements must be transitively closed, so if A requires B, and B requires C,
  then A requires C.
- no circular requirements allowed

The logic for circular requirements is a bit duplicated to get a better
error message.

Part of #11978

changelog_begin
changelog_end
Copy link
Contributor

@cocreature cocreature left a comment

Choose a reason for hiding this comment

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

nice, thank you!

compiler/daml-lf-tools/src/DA/Daml/LF/TypeChecker/Check.hs Outdated Show resolved Hide resolved
Co-authored-by: Moritz Kiefer <moritz.kiefer@purelyfunctional.org>
Comment on lines 846 to 847
whenJust (listToMaybe (S.toList missing)) $ \missingIfaceId ->
throwWithContext (ENotClosedInterfaceRequires (intName iface) requiredIfaceId missingIfaceId)
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps it would be better to show all missing required interfaces in one go? I think this should do the trick:

Suggested change
whenJust (listToMaybe (S.toList missing)) $ \missingIfaceId ->
throwWithContext (ENotClosedInterfaceRequires (intName iface) requiredIfaceId missingIfaceId)
whenJust (nonEmpty (S.toList missing)) $ \missingIfaceIds ->
throwWithContext (ENotClosedInterfaceRequires (intName iface) requiredIfaceId missingIfaceIds)

(together with the corresponding change for ENotClosedInterfaceRequires)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good idea, changed the error to take a list!

Copy link
Contributor

Choose a reason for hiding this comment

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

nice, thank you!

@akrmn akrmn self-requested a review December 9, 2021 12:43
Copy link
Contributor

@akrmn akrmn left a comment

Choose a reason for hiding this comment

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

nothing else to add, thank you!

forM_ (intRequires iface) $ \requiredIfaceId -> do
requiredIface <- inWorld (lookupInterface requiredIfaceId)
when (tcon `S.member` intRequires requiredIface) $
throwWithContext (ECircularInterfaceRequires (intName iface) (Just requiredIfaceId))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not a big issue, but we actually does not need this case.
If you require the requires is transitively closed, you just need to check that an interface does not require itself (done at line 840).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, that's why I said it's only done to get a better error message.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Fair enough.
It is true that error messages in the compiler are more important than the one in the interpreter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. I think it's better for the check to be simpler in the interpreter. The compiler is more user-facing.

@sofiafaro-da sofiafaro-da merged commit cbeab4b into main Dec 9, 2021
@sofiafaro-da sofiafaro-da deleted the interface-requires-errors branch December 9, 2021 14:27
realvictorprm pushed a commit that referenced this pull request Dec 10, 2021
* interfaces: Prevent circular and non-closed reqs

Updates the haskell side to be more strict about requirements:
- requirements must be transitively closed, so if A requires B, and B requires C,
  then A requires C.
- no circular requirements allowed

The logic for circular requirements is a bit duplicated to get a better
error message.

Part of #11978

changelog_begin
changelog_end

* Update compiler/daml-lf-tools/src/DA/Daml/LF/TypeChecker/Check.hs

Co-authored-by: Moritz Kiefer <moritz.kiefer@purelyfunctional.org>

* take a list in NotClosed error

Co-authored-by: Moritz Kiefer <moritz.kiefer@purelyfunctional.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants