Skip to content

[Concurrency] Emit a better note when an isolated stored property initializer cannot be used. #75705

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
merged 6 commits into from
Aug 6, 2024

Conversation

hborla
Copy link
Member

@hborla hborla commented Aug 6, 2024

When a stored property initializer requires a specific isolation, an init with mismatching isolation cannot use that initial value. Today, the compiler emits misleading notes in that situation. For example:

@MainActor func f() -> Int { 0 }

class C {
  @MainActor var x = f()

  init() {}
}

Currently, the compiler emits the following diagnostics:

error: return from initializer without initializing all stored properties
2 | 
3 | class C {
4 |   @MainActor var x = f()
  |                  `- note: 'self.x' not initialized
5 | 
6 |   init() {}
  |           `- error: return from initializer without initializing all stored properties
7 | }
8 | 

The attached note is misleading, because x sure looks initialized at the declaration! The real problem is that the initializer is @MainActor-isolated, so it cannot be used in a nonisolated initializer. With this change, the attached note includes the isolation of the default value and the enclosing init that has mismatching isolation:

error: return from initializer without initializing all stored properties
2 | 
3 | class C {
4 |   @MainActor var x = f()
  |                  `- note: main actor-isolated default value of 'self.x' cannot be used in a nonisolated initalizer
5 | 
6 |   init() {}
  |           `- error: return from initializer without initializing all stored properties
7 | }
8 | 

Resolves: #74557

(Note that this PR includes the commits from #75687. Only the last commit is new)

hborla added 6 commits August 5, 2024 08:23
wording.

Splitting up the diagnostic into separate diagnostics based on the reference
kind is easier for me to read. The wording of the error message now puts
the problem -- crossing an isolation boundary -- at the center of the message,
and attempts to clarify how the value crosses an isolation boundary. E.g. for
the witness diagnostics, the value crosses an isolation boundary when calling
the witness through the protocol requirement in generic code.

This change does not add any additional information to the diagnostics, but it'd
be valuable to show both the source and destination isolation.
…tializer

cannot be used in an init with mismatching isolation.
@hborla
Copy link
Member Author

hborla commented Aug 6, 2024

@swift-ci please smoke test

@hborla hborla merged commit 0ccbc7b into swiftlang:main Aug 6, 2024
3 checks passed
@hborla hborla deleted the isolated-property-init-note branch August 6, 2024 13:54
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.

Bad diagnostic when constructor can't initialize isolated property
1 participant