Fix the tagged-union pattern-matching recommendation

The union chapter recommends matching a tag and its corresponding
union field together in a single pattern when implementing a C-style
tagged union.  This suggests the idiom is safe.

But it's not.  We don't guarantee the order that subpatterns are
checked -- this could only be safe if we could be sure the tag is
checked first.  And, in fact, if an or-pattern is used when matching
the tag, then `rustc` defers the check, causing actual UB (see
rust-lang/rust#158387).

Let's fix that.  We can remove the `items.union.pattern.subpattern`
rule entirely -- it's trivially true that a pattern can appear
as a subpattern.  In its place, let's add a warning admonition
that highlights what isn't guaranteed and describes what people
should do instead.
1 file changed
tree: db59158ec5d38c8479395839bfd8de511d46cdae
  1. .cargo/
  2. .github/
  3. dev-guide/
  4. docs/
  5. src/
  6. theme/
  7. tools/
  8. .gitattributes
  9. .gitignore
  10. book.toml
  11. Cargo.lock
  12. Cargo.toml
  13. CONTRIBUTING.md
  14. LICENSE-APACHE
  15. LICENSE-MIT
  16. README.md
  17. reference.md
  18. rust-toolchain.toml
  19. rustfmt.toml
  20. triagebot.toml
README.md

The Rust Language Reference

This document is the primary reference for the Rust programming language.

Contributor docs

See the Reference Developer Guide for information on contributing to the Reference.