It took me a while to debug this (you always assume it was a bug in your code), but in the end, it boiled down to this (notice that the newline after the definition of f is relevant to triggering the behavior!):
I already reported that to Wolfram Support, so sharing here just for fun.