Skip to content

Add logic to look for overload groups when emitting warnings #885

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

Conversation

patshaughnessy
Copy link
Contributor

Add logic to look for overload groups when emitting warnings and suggestions about ambiguous symbols. If exactly one of the ambiguous symbols is an overload group, then suggest the author simply remove the incorrect disambiguation instead of selecting a different disambiguation hash.

rdar://126382989

Summary

Today when an author links to a page using an invalid disambiguation hash, DocC emits a warning similar to this listing all of the valid disambiguation hashes:

warning: '7w26t' isn't a disambiguation for 'init(content:)' at '/MyFramework/SomeStruct'
 --> SomeStruct.md:7:19-7:25
5 | ### Some Interesting Topic
6 |
7 + - ``init(content:)-7w26t``
  |                   ├─suggestion: Replace '7w26t' with '25rvu' for'something'
  |                   ├─suggestion: Replace '7w26t' with '4tm5u' for'something-else'
  |                   ╰─suggestion: Replace '7w26t' with '5di5z' for'something-even-different'
8 | - ``init(content:)-8mfe``

However, we expect that most of the time authors need to specify disambiguation hashes when they are linking directly to one member of a set of overloaded symbols. (Symbols that have the same parameters but different types).

Therefore, this PR changes the diagnostic logic for ambiguous symbols to emit a warning similar to this instead:

warning: '7w26t' isn't a disambiguation for 'init(content:)' at '/MyFramework/SomeStruct'
 --> SomeStruct.md:7:19-7:25
5 | ### Some Interesting Topic
6 |
7 + - ``init(content:)-7w26t``
  |                   ╰─suggestion: Remove '-7w26t' to use overload group 'something'
8 | - ``init(content:)-8mfe``

Now DocC will encourage writers to use simpler paths when possible, allowing readers to navigate to overload group pages.

We might consider adding logic in a follow-on PR to emit warnings for valid links that use disambiguation hashes unnecessarily.

Dependencies

None

Testing

Steps:

  1. Compile documentation that contains a broken link due to an incorrect disambiguation hash. Use the --enable-experimental-overloaded-symbol-presentation command line argument to enable the experimental overloads feature flag.
  2. Check that the target page is one symbol is a set of overloaded symbols
  3. Test that DocC emits a warning similar to the example above.
  4. Repeat the test when the broken link selects ambiguous symbols that are not all overloads in the same group. DocC should emit the original warning.

Checklist

Make sure you check off the following items. If they cannot be completed, provide a reason.

  • [x ] Added tests
  • [x ] Ran the ./bin/test script and it succeeded
  • [ ] Updated documentation if necessary

suggestions about ambiguous symbols. If exactly one of the ambiguous
symbols is an overload group, then suggest the author simply remove
the incorrect disambiguation.
@patshaughnessy
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@d-ronnqvist d-ronnqvist left a comment

Choose a reason for hiding this comment

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

This shouldn't be special behavior for overload groups. It should be a general behavior whenever there is only one candidate who isn't disfavored in collisions (and therefore doesn't need disambiguation in its link).

Copy link
Contributor

@QuietMisdreavus QuietMisdreavus left a comment

Choose a reason for hiding this comment

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

Thanks so much for this! There are a few tweaks i spotted that i'd like to see, but overall i'm super glad to see this change.

@patshaughnessy patshaughnessy force-pushed the r126382989-warnings-ambiguous-symbols-overload-groups branch from 84db713 to 2f756bf Compare April 16, 2024 20:41
about selecting the preferred symbol without any disambiguation hash,
followed by a list of all the remaining symbols to select from with
their hashes.
@patshaughnessy patshaughnessy force-pushed the r126382989-warnings-ambiguous-symbols-overload-groups branch from 9520ba7 to d610548 Compare April 16, 2024 21:01
@patshaughnessy
Copy link
Contributor Author

@swift-ci please test

@patshaughnessy
Copy link
Contributor Author

I've refactored this to display all the ambiguous links like before, but showing the preferred ("favored") symbol first. Example:

'3xblz' isn't a disambiguation for 'init(param:)' at '/MyFramework/SomeClass'.
Remove '-3xblz' to select preferred symbol 'init(param:)'
Replace '3xblz' with '6swzo' for 'init(param:)'
Replace '3xblz' with '3bg7x' for etc...

@d-ronnqvist
Copy link
Contributor

d-ronnqvist commented Apr 17, 2024

I've refactored this to display all the ambiguous links like before, but showing the preferred ("favored") symbol first. Example:

'3xblz' isn't a disambiguation for 'init(param:)' at '/MyFramework/SomeClass'.
Remove '-3xblz' to select preferred symbol 'init(param:)'
Replace '3xblz' with '6swzo' for 'init(param:)'
Replace '3xblz' with '3bg7x' for etc...

Did we stop displaying the full declaration or is that something you simplified in this example? Without the full declaration there's no way to know which disambiguation refers to which symbol.

@patshaughnessy
Copy link
Contributor Author

Did we stop displaying the full declaration or is that something you simplified in this example?
No. I simplified the example.

- PathHierarchy.makePartialResultError checks for a preferred symbol,
normally an overload group, when throwing processing unknownDisambiguation
errors.

- Simplify makeTopicReferenceResolutionErrorInfo and limit it to formatting the
warning message.
@patshaughnessy
Copy link
Contributor Author

@swift-ci please test

Comment on lines 372 to 381
let candidates = disambiguationTree.disambiguatedValues()
let favoredSuffix = favoredSuffix(from: candidates)
let suffixes = candidates.map { suffix(for: $0.disambiguation) }
let candidatesAndSuffixes = zip(candidates, suffixes).map { (candidate, suffix) in
if suffix == favoredSuffix {
return (node: candidate.value, disambiguation: "")
} else {
return (node: candidate.value, disambiguation: suffix)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be useful for this logic to happen in disambiguatedValues(). That way, for example, the URL for the overloads page wouldn't need a hash.

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 investigated this a bit and I think it will lead to cleaner code and simpler URLs. However, I'd suggest we make this additional change in a separate PR in the near future. Changing the URLs will require updating a large number of unit test expectations, and also will lead to moving pages around in production, leading to more testing, creating the necessary redirects and generally more risk.

For now, I think updating the warning is a good first step.

Replacement(range: replacementRange, replacement: "-" + disambiguation)
])
// Suggest removing the disambiguation hash entirely
if disambiguation.isEmpty {
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally this would happen automatically, without the special check here.

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 found that we need to keep this check. It is possible to remove this check entirely, but then we would have to add the - prefix to all the unknownDisambiguation warnings throughout all the unit tests.

Example:

Replace '-abc123' with '-method' for\n'func `init`()'"

I think this reads fine for users, but I wouldn't want to make this change across all the unit test expectations unless you like this idea,

@patshaughnessy
Copy link
Contributor Author

@swift-ci please test

- Further simplify logic for displaying unknown disambiguation warnings
@patshaughnessy
Copy link
Contributor Author

@swift-ci please test

@patshaughnessy
Copy link
Contributor Author

@d-ronnqvist I simplified PathHierarchy+Error.swift even more, and updated some unit test expectations to account for the extra hyphen in the warning message. I think the code looks very clean now, and also the warnings read even better or the same as before.

Copy link
Contributor

@QuietMisdreavus QuietMisdreavus left a comment

Choose a reason for hiding this comment

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

This looks wonderful, great work!

@patshaughnessy
Copy link
Contributor Author

@swift-ci please test

@patshaughnessy patshaughnessy merged commit 1357a2d into swiftlang:main Apr 26, 2024
@patshaughnessy patshaughnessy deleted the r126382989-warnings-ambiguous-symbols-overload-groups branch April 26, 2024 18:42
patshaughnessy added a commit to patshaughnessy/swift-docc that referenced this pull request Apr 29, 2024
…ng#885)

* Add logic to look for overload groups when emitting warnings and
suggestions about ambiguous symbols. If exactly one of the ambiguous
symbols is an overload group, then suggest the author simply remove
the incorrect disambiguation.

* Refactor logic for ambiguous symbol warnings to first emit a message
about selecting the preferred symbol without any disambiguation hash,
followed by a list of all the remaining symbols to select from with
their hashes.

* PR Feedback:

- PathHierarchy.makePartialResultError checks for a preferred symbol,
normally an overload group, when throwing processing unknownDisambiguation
errors.

- Simplify makeTopicReferenceResolutionErrorInfo and limit it to formatting the
warning message.

* PR Feedback: Simplify PathHierarchy+Error.swift and
PathHierarchy.makePartialResultError further.

* PR Feedback:
- Further simplify logic for displaying unknown disambiguation warnings

(cherry picked from commit 1357a2d)
emilyychenn pushed a commit to emilyychenn/swift-docc that referenced this pull request Apr 30, 2024
…ng#885)

* Add logic to look for overload groups when emitting warnings and
suggestions about ambiguous symbols. If exactly one of the ambiguous
symbols is an overload group, then suggest the author simply remove
the incorrect disambiguation.

* Refactor logic for ambiguous symbol warnings to first emit a message
about selecting the preferred symbol without any disambiguation hash,
followed by a list of all the remaining symbols to select from with
their hashes.

* PR Feedback:

- PathHierarchy.makePartialResultError checks for a preferred symbol,
normally an overload group, when throwing processing unknownDisambiguation
errors.

- Simplify makeTopicReferenceResolutionErrorInfo and limit it to formatting the
warning message.

* PR Feedback: Simplify PathHierarchy+Error.swift and
PathHierarchy.makePartialResultError further.

* PR Feedback:
- Further simplify logic for displaying unknown disambiguation warnings
patshaughnessy added a commit that referenced this pull request May 2, 2024
…906)

* Add logic to look for overload groups when emitting warnings and
suggestions about ambiguous symbols. If exactly one of the ambiguous
symbols is an overload group, then suggest the author simply remove
the incorrect disambiguation.

* Refactor logic for ambiguous symbol warnings to first emit a message
about selecting the preferred symbol without any disambiguation hash,
followed by a list of all the remaining symbols to select from with
their hashes.

* PR Feedback:

- PathHierarchy.makePartialResultError checks for a preferred symbol,
normally an overload group, when throwing processing unknownDisambiguation
errors.

- Simplify makeTopicReferenceResolutionErrorInfo and limit it to formatting the
warning message.

* PR Feedback: Simplify PathHierarchy+Error.swift and
PathHierarchy.makePartialResultError further.

* PR Feedback:
- Further simplify logic for displaying unknown disambiguation warnings

(cherry picked from commit 1357a2d)
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.

3 participants