Cherry-pick RAC 3.0's -flatten: into master

An alternative to #1000, since `-flatten:` on 3.0 appeared to work great. This PR copies that implementation, stripping out all the policy stuff and fixing up patterns that don't exist yet.

Resolves #988 (based on the unit tests, anyways).

@corristo @Coneko