Closed
Description
CFE uses late final
variable with an initializer to achieve lazy evaluation and caching of temporary values. The initializer itself is represented by a closure (in accordance to late final
representation which is currently used). This closure captures variables from the surrounding scope which can lead to potential over capturing if there are other closures in the same scope. Consider for example this code:
void Function() f(List<int> list) {
return switch (list) {
[final item] => () { // (*)
use(item);
},
[] => () {
},
};
}
The first closure will end up holding to list
due to the late final
field introduced by pattern desugaring.
It is in some sense duplicate of #36983, but we could consider if we want to change desugaring of late variables with initializers (e.g. simply duplicate the body of the initializer?)