SE-0493: Support `async` calls in `defer` bodies

FWIW @Douglas_Gregor had objections when we discussed this back in 2020 when async/await was first introduced: SE-0296: async/await - #77 by Douglas_Gregor .

Banning await inside defer seems like an artificial restriction, so it seems easy to lift?

Perhaps, but I don't think it fits well with the goals of the proposal. It means having potential suspension points that are somewhat arbitrary, e.g., at every try and returnwhere there is such a defer , you would have an implicit potential suspension point.

What do you mean by “arbitrary”?

defer is conceptually just code that runs at the end of the function. I see why it has special behavior with regards to try: we want to avoid nested error throwing. I don’t see why it should have special behavior with regards to await.

It turns any try, throw, or return when there is an active async defer into an implicit “await”. That seems reason enough not to support it.

1 Like