Semver compatibility: `impl Trait`

When used in argument position, impl Trait is equivalent to a type param, with the main difference being the inability to manually specify the type using turbofish.

Is it then a semver-compatible change to go from impl Trait to an actual type parameter?

In general, I think the page in the cargo book that talks about semver compatibility could use a lot more details.

Might be that this is not part of Cargo's semver guide, because APITs not being specifiable via turbofish is considered a restriction that is supposed to be amended in the future. In the mean time, I'd go with the reference:

Changing a parameter from either one to the other can constitute a breaking change for the callers of a function, since this changes the number of generic arguments.

and consider it a breaking change to go from an APIT to a generic parameter.

1 Like

It also makes the function incompatible with precise capturing (-> impl use<..> ..).

Not if you have named non-lifetime generic parameters, as you'll break code that uses non-empty turbofish. Example.

There's a number of little details here, like

  • Empty turbofish is equivalent to not having a turbofish, and is always allowed
  • And APIT forbade turbofishing to begin with, so this wasn't a breaking change until you were allowed to turbofish with APIT
  • Default parameters are deprecated on functions, so that's not a blessed workaround

Incidentally, the fact that it can be a breaking change to change from APIT to a generic means that some crates can't migrate to edition 2024 without a breaking change.

The amendment so far is to allow turbofishing without a way to specify the APITs.[1] I think technically some non-turbofish door is still open, but perhaps not practically. The FCP went with option 2 listed here.


  1. That document is out of date enough that it claims you can't mix the two. â†Šī¸Ž

2 Likes