/src/test/run-pass/block-expr-precedence.rs

http://github.com/jruderman/rust · Rust · 56 lines · 8 code · 5 blank · 43 comment · 9 complexity · f2d0c6bd3b5cb58d69c6e50bb71ed7a3 MD5 · raw file

  1. // This test has some extra semis in it that the pretty-printer won't
  2. // reproduce so we don't want to automatically reformat it
  3. // no-reformat
  4. /*
  5. *
  6. * When you write a block-expression thing followed by
  7. * a lone unary operator, you can get a surprising parse:
  8. *
  9. * if (...) { ... }
  10. * -num;
  11. *
  12. * for example, or:
  13. *
  14. * if (...) { ... }
  15. * *box;
  16. *
  17. * These will parse as subtraction and multiplication binops.
  18. * To get them to parse "the way you want" you need to brace
  19. * the leading unops:
  20. * if (...) { ... }
  21. * {-num};
  22. *
  23. * or alternatively, semi-separate them:
  24. *
  25. * if (...) { ... };
  26. * -num;
  27. *
  28. * This seems a little wonky, but the alternative is to lower
  29. * precedence of such block-like exprs to the point where
  30. * you have to parenthesize them to get them to occur in the
  31. * RHS of a binop. For example, you'd have to write:
  32. *
  33. * 12 + (if (foo) { 13 } else { 14 });
  34. *
  35. * rather than:
  36. *
  37. * 12 + if (foo) { 13 } else { 14 };
  38. *
  39. * Since we want to maintain the ability to write the latter,
  40. * we leave the parens-burden on the trailing unop case.
  41. *
  42. */
  43. fn main() {
  44. let num = 12;
  45. assert if (true) { 12 } else { 12 } - num == 0;
  46. assert 12 - if (true) { 12 } else { 12 } == 0;
  47. if (true) { 12; } {-num};
  48. if (true) { 12; }; {-num};
  49. if (true) { 12; };;; -num;
  50. }