Skip to content

[Parser] Improving diagnostics for access-level modifiers #68894

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

Merged

Conversation

Visckmart
Copy link
Contributor

@Visckmart Visckmart commented Oct 1, 2023

Problem

When declaring a method with multiple access-level modifiers (a programming mistake), the current diagnostic is not clear nor accurate.

As an example, when we compile the following block of code:

class Foo {
  public private func exampleMethod() { }
}

We get the following diagnostics:

error: duplicate modifier
  public private func exampleMethod() { }
         ^~~~~~~
note: modifier already specified here
  public private func exampleMethod() { }
  ^~~~~~

It can be argued that both modifiers are from the same "type" (access-level modifiers), but the example shows a situation where they aren't a duplicate of each other as the error message says.

Solution

The solution implemented on this pull request is a new diagnostic: an error message that describes the problem more clearly and a note that refers to the previous modifier by name.

error: multiple access-level modifiers specified
  public private func exampleMethod() { }
         ^~~~
note: 'public' previously specified here
  public private func exampleMethod() { }
  ^~~~~~~

Resolves #60914.

@AnthonyLatsis
Copy link
Collaborator

@ahoppen Are you OK with accepting this sort of improvement into the legacy parser? I would guess swift-syntax is not really expected to diagnose this stuff anyway.

@ahoppen
Copy link
Member

ahoppen commented Oct 7, 2023

Are you OK with accepting this sort of improvement into the legacy parser? I would guess swift-syntax is not really expected to diagnose this stuff anyway.

Yes, swift-syntax allows duplicate access level modifiers because it just models them as a list. ASTGen will need to diagnose these sorts of problems.

And I think this change is a good improvement. It would be nice to add a test case for it, though.

@Visckmart Visckmart force-pushed the multiple_modifiers_diagnostics branch from 1511afc to f1e3c82 Compare October 12, 2023 02:59
@Visckmart
Copy link
Contributor Author

Cool! I updated the diagnostics' messages to reflect the latest from the discussion with @AnthonyLatsis and updated the existing tests to reflect the new diagnostics as @ahoppen requested.

@Visckmart Visckmart marked this pull request as ready for review October 12, 2023 03:11
@Visckmart Visckmart force-pushed the multiple_modifiers_diagnostics branch from af6c0e2 to 3c9d6ca Compare October 16, 2023 22:51
Copy link
Collaborator

@AnthonyLatsis AnthonyLatsis left a comment

Choose a reason for hiding this comment

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

Awesome! Just one optional stylistic suggestion.

@AnthonyLatsis
Copy link
Collaborator

@swift-ci please smoke test macOS

@Visckmart Visckmart force-pushed the multiple_modifiers_diagnostics branch from 3c9d6ca to 481504d Compare October 17, 2023 15:00
@AnthonyLatsis
Copy link
Collaborator

@rintaro ping

@rintaro
Copy link
Member

rintaro commented Oct 24, 2023

@swift-ci Please smoke test

@rintaro
Copy link
Member

rintaro commented Oct 24, 2023

The change looks good to me. But I feel these all attribute diagnostics including others (e.g. "concurrency only") should be in Sema (TypeCheckAttr.cpp) not in Parse or ASTGen. For example, @differentiable only diagnose duplication if the parameter is semantically the same. WDYT @ahoppen ?

It's not a blocker for merging this PR though.

@ahoppen
Copy link
Member

ahoppen commented Oct 25, 2023

I agree with @rintaro. Implementing these checks in the type checker will make them applicable to both the old parser and the new parser.

@ahoppen
Copy link
Member

ahoppen commented Oct 25, 2023

@swift-ci Please test Windows

@Visckmart Visckmart force-pushed the multiple_modifiers_diagnostics branch from 481504d to 86e1a1e Compare October 26, 2023 20:22
@Visckmart
Copy link
Contributor Author

I just noticed I had mistakenly kept an extra argument on the note diagnostic. It's fixed now. I also ran the code formatter and cleaned up the commit message to make it more succinct.

Sorry for making these changes after the review comments and the tests have been run. @AnthonyLatsis would you mind triggering the tests once more?

Improve the diagnostics for situations where multiple access-level modifiers are used on the same declaration. Keep the original duplicate error message if the access levels are the same.
@Visckmart Visckmart force-pushed the multiple_modifiers_diagnostics branch from 86e1a1e to e18bfab Compare October 26, 2023 22:02
@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Oct 26, 2023

@rintaro Do we want anyone else to take a look at this?

@AnthonyLatsis
Copy link
Collaborator

@swift-ci please smoke test

Copy link
Member

@hborla hborla left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution! Exploring moving some of these attribute diagnostics out of the parser and into TypeCheckAttr.cpp would be a good next task if you're interested in continuing contributions in this area 🙂

@AnthonyLatsis AnthonyLatsis merged commit 8fe5561 into swiftlang:main Oct 27, 2023
@Visckmart
Copy link
Contributor Author

I’m definitely interested! I’ll take a look at that once I have some time.

I’d like to quickly thank @AnthonyLatsis for helping me a lot with this the process of making a contribution and @rintaro, @ahoppen and @hborla for the review and approval of my first pull request. ☺️

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.

Improve error message "Duplicate modifier"
5 participants