This is bug in current swift versions as compiler is not able to recognize the global actor context for a defer
block, the discussion for this is going on swift forum and a PR with fix also available that should resolve this issue in future swift versions. For now, explicitly global actor context need to be provided for code to compile:
defer { Task { @MainActor in dismiss(animated: true) } }