We actually can’t just implement all Swift generics as if they were C++ templates and force eager specialization all the way down
Just to be clear, I'm only suggesting that we create specializations of functions that call C++ function templates-- not all Swift generics. I think this proposal essentially follows what you outlined in your comment here.
there are semantic limitations on what you can do with C++ templates that we don’t impose in Swift. For example, in Swift you can recurse in a generic function with a “larger” type argument, and as long as the recursion isn’t dynamically infinite it will succeed; in a C++ template, that kind of recursion would violate instantiation-depth restrictions (which exist specifically to allow that eager-specialization implementation).
Sure, in this case, we could just emit an error because there would be no way to determine what type to specialize the C++ function template with.
So I think some way to force eager specialization — with all the concomitant expectations and restrictions — is probably necessary on some level, even if you can figure out some way to avoid writing it in common cases.
I assume you're talking about a @_must_specialize
attribute (or equivalent). My thought process was, there's no behavioral change whether we require Swift functions that call C++ function templates to have that attribute or not. The only difference is that in one case they would be required to explicitly mark the function as "must specialize" and in the other case it would be assumed (because there's no alternative).
I think there's a good argument for the expressiveness and clarity of requiring @_must_specialize
.