Skip to content

[Diagnostics] A self argument implicitly passed as an inout parameter is diagnosed as used before initialized #78441

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

stzn
Copy link
Contributor

@stzn stzn commented Jan 5, 2025

Resolves #55825

When using the self argument that is implicitly passed as an inout parameter without initialization, the compiler generates a "passed by reference before being initialized" diagnostic message.

var test : [String: String]
test["1"] = "1" // variable 'test' passed by reference before being initialized

This error message may be confusing since the "passed by reference" is not explicitly visible in the code.

In this specific case, just "used before being initialized" would be a more understandable diagnostic message.

var test : [String: String]
test["1"] = "1" // variable 'test' used before being initialized

For other uses of inout parameters, the current diagnostic message should remain unchanged, as the additional information about "passed by reference" provides helpful context for developers.

func takes_inout(_ a: inout Int) {}
var a1 : Int
takes_inout(&a1)    //  variable 'a1' passed by reference before being initialized

@stzn stzn force-pushed the improve-use-before-initialized-diagnostic branch from 97bba15 to bd2936e Compare January 5, 2025 10:51
@stzn stzn marked this pull request as ready for review January 5, 2025 19:41
@stzn stzn requested a review from eeckstein as a code owner January 5, 2025 19:41
@stzn stzn changed the title [DI] A self argument implicitly passed as an inout parameter is diagnosed as used before initialized [Diagnostics] A self argument implicitly passed as an inout parameter is diagnosed as used before initialized Jan 5, 2025
@stzn stzn force-pushed the improve-use-before-initialized-diagnostic branch from bd2936e to d429a90 Compare January 5, 2025 20:05
@AnthonyLatsis AnthonyLatsis self-assigned this Jan 7, 2025
@AnthonyLatsis
Copy link
Collaborator

@swift-ci please smoke test macOS

@omochi
Copy link
Contributor

omochi commented Jan 11, 2025

@swift-ci please smoke test Linux platform
@swift-ci please smoke test Windows platform

@omochi
Copy link
Contributor

omochi commented Jan 11, 2025

@swift-ci please smoke test Linux platform

Excuse me, it seems that only one CI command could be written at a time.

@omochi
Copy link
Contributor

omochi commented Jan 11, 2025

@swift-ci Please smoke test Linux platform

Copy link
Contributor

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

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

lgtm, thanks!

@eeckstein eeckstein merged commit 999669c into swiftlang:main Jan 12, 2025
3 checks passed
@stzn stzn deleted the improve-use-before-initialized-diagnostic branch January 13, 2025 00:48
@AnthonyLatsis AnthonyLatsis removed their assignment Feb 19, 2025
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.

[SR-13385] Improved diagnostics when using instance methods before initialization
4 participants