Skip to content

Conversation

artemcm
Copy link
Contributor

@artemcm artemcm commented Sep 30, 2025

  • Add simple check for returning of values of array and dictionary type
    This check will run on each type-checked primary input of the current compilation and emit a warning diagnostic for all discovered occurrences of this code pattern when the performance hints feature is enabled.

  • Add @performanceOverride attribute to silence individual performance hint diagnostics
    This attribute will accept two parameters: a diagnostic group identifier first, and a string literal 'explanation' for the override reason second

Copy link

@aviralg aviralg left a comment

Choose a reason for hiding this comment

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

FunctionReturnType is too generic when we are only checking for arrays and dictionaries. We will implement other kind of return type checks such as existential any that will come under a separate category. We should rename FunctionReturnType to something like FunctionReturnsHeapAllocatedData. This will change the name of the check, corresponding diagnostic override, and the name of the documentation file.

@artemcm artemcm force-pushed the SPA-Init branch 2 times, most recently from 7796cb7 to 89cd4ba Compare October 1, 2025 15:39
@artemcm
Copy link
Contributor Author

artemcm commented Oct 1, 2025

@swift-ci test

@artemcm
Copy link
Contributor Author

artemcm commented Oct 1, 2025

@swift-ci test

Copy link

@aviralg aviralg left a comment

Choose a reason for hiding this comment

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

LGTM!

GROUP(UnknownWarningGroup, "unknown-warning-group")
GROUP(CompilationCaching, "compilation-caching")
GROUP(WeakMutability, "weak-mutability")
GROUP(PerfHintReturnTypeImplicitCopy, "perf-hint-return-type-implicit-copy")
Copy link
Member

Choose a reason for hiding this comment

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

This is... very specific. Can we perhaps drop the "PerfHint"/"perf-hint-" prefix here and put them within a "PerformanceHints" group?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The goal here was to allow each check/hint to be enabled/disabled individually. And the idea is the each hint will actually have a small handful of diagnostic varieties, functions and closures being separate diagnostic entires in this PR being an example.

Copy link
Member

Choose a reason for hiding this comment

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

That's fine. You can have an overarching "PerformanceHints" group, and then subgroups for the smaller groups of related diagnostics.

@DougGregor
Copy link
Member

To be blunt, I don't really like the technical approach here, for a couple of reasons:

  • We already have a mechanism for opt-in warnings (DefaultIgnore flag + diagnostic groups), and this should be based on that rather than adding an experimental feature (this isn't a language feature).
  • Adding a global -Wsuppress <group name> is a very big hammer and something that we need to socialize and discuss. It also feels like the wrong level of granularity for suppressing these issues: the warnings should be opt-in for a module (you get that with my suggestion from the previous bullet), so we don't also need some way to make them opt-out again at a module level.
  • Using a performance-specific attribute (@performanceOverride) for warning suppression also feels like the wrong granularity, albeit in a different manner: it can be reasonable to suppress warnings within a region of code, but the performance hints aren't special in this way. Whether this should be an attribute or a file-level thing, I don't know, but it should apply to all warning groups.

@artemcm artemcm force-pushed the SPA-Init branch 3 times, most recently from d053a90 to 6cb0646 Compare October 1, 2025 19:56
@artemcm
Copy link
Contributor Author

artemcm commented Oct 1, 2025

To be blunt, I don't really like the technical approach here, for a couple of reasons:

  • We already have a mechanism for opt-in warnings (DefaultIgnore flag + diagnostic groups), and this should be based on that rather than adding an experimental feature (this isn't a language feature).
  • Adding a global -Wsuppress <group name> is a very big hammer and something that we need to socialize and discuss. It also feels like the wrong level of granularity for suppressing these issues: the warnings should be opt-in for a module (you get that with my suggestion from the previous bullet), so we don't also need some way to make them opt-out again at a module level.
  • Using a performance-specific attribute (@performanceOverride) for warning suppression also feels like the wrong granularity, albeit in a different manner: it can be reasonable to suppress warnings within a region of code, but the performance hints aren't special in this way. Whether this should be an attribute or a file-level thing, I don't know, but it should apply to all warning groups.

@DougGregor thanks for the feedback! I have switched the implementation over to use DefaultIgnore instead of having a suppression flag mechanism, and removed the attribute from this PR so that we can focus on landing the diagnostic logic and discuss any kind of in-source diagnostic suppression mechanism separately.

@artemcm artemcm force-pushed the SPA-Init branch 2 times, most recently from 7050c7b to d08816a Compare October 1, 2025 21:01
@artemcm
Copy link
Contributor Author

artemcm commented Oct 1, 2025

@swift-ci test

Copy link
Member

@DougGregor DougGregor 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 great, thank you!

GROUP(UnknownWarningGroup, "unknown-warning-group")
GROUP(CompilationCaching, "compilation-caching")
GROUP(WeakMutability, "weak-mutability")
GROUP(PerfHintReturnTypeImplicitCopy, "perf-hint-return-type-implicit-copy")
Copy link
Member

Choose a reason for hiding this comment

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

That's fine. You can have an overarching "PerformanceHints" group, and then subgroups for the smaller groups of related diagnostics.

Comment on lines +32 to +33
return !ctx.Diags.isIgnoredDiagnostic(diag::perf_hint_closure_returns_array.ID) ||
!ctx.Diags.isIgnoredDiagnostic(diag::perf_hint_function_returns_array.ID);
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, this is the best we have right now. At some point (not for this PR!) we should introduce a group-level query for this, which probably means a different underlying data structure.

@artemcm
Copy link
Contributor Author

artemcm commented Oct 2, 2025

@swift-ci smoke test

@artemcm artemcm enabled auto-merge October 2, 2025 16:28
… and dictionary type

This check will run on each type-checked primary input of the current compilation and emit a warning diagnostic for all discovered occurences of this code pattern when the performance hint diagnostic is enabled
@artemcm
Copy link
Contributor Author

artemcm commented Oct 2, 2025

@swift-ci smoke test

@artemcm artemcm merged commit 96ee032 into swiftlang:main Oct 2, 2025
3 checks passed
@artemcm artemcm deleted the SPA-Init branch October 3, 2025 15:56
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