/src/test/ui/parser/issues/issue-35813-postfix-after-cast.rs

https://gitlab.com/rust-lang/rust · Rust · 171 lines · 105 code · 25 blank · 41 comment · 19 complexity · cfb4a011b5b6caddecf0f0c91495ea78 MD5 · raw file

  1. // edition:2018
  2. #![crate_type = "lib"]
  3. #![feature(type_ascription)]
  4. use std::future::Future;
  5. use std::pin::Pin;
  6. // This tests the parser for "x as Y[z]". It errors, but we want to give useful
  7. // errors and parse such that further code gives useful errors.
  8. pub fn index_after_as_cast() {
  9. vec![1, 2, 3] as Vec<i32>[0];
  10. //~^ ERROR: casts cannot be followed by indexing
  11. vec![1, 2, 3]: Vec<i32>[0];
  12. //~^ ERROR: casts cannot be followed by indexing
  13. }
  14. pub fn index_after_cast_to_index() {
  15. (&[0]) as &[i32][0];
  16. //~^ ERROR: casts cannot be followed by indexing
  17. (&[0i32]): &[i32; 1][0];
  18. //~^ ERROR: casts cannot be followed by indexing
  19. }
  20. pub fn cast_after_cast() {
  21. if 5u64 as i32 as u16 == 0u16 {
  22. }
  23. if 5u64: u64: u64 == 0u64 {
  24. }
  25. let _ = 5u64: u64: u64 as u8 as i8 == 9i8;
  26. let _ = 0i32: i32: i32;
  27. let _ = 0 as i32: i32;
  28. let _ = 0i32: i32 as i32;
  29. let _ = 0 as i32 as i32;
  30. let _ = 0i32: i32: i32 as u32 as i32;
  31. }
  32. pub fn cast_cast_method_call() {
  33. let _ = 0i32: i32: i32.count_ones();
  34. //~^ ERROR: casts cannot be followed by a method call
  35. let _ = 0 as i32: i32.count_ones();
  36. //~^ ERROR: casts cannot be followed by a method call
  37. let _ = 0i32: i32 as i32.count_ones();
  38. //~^ ERROR: casts cannot be followed by a method call
  39. let _ = 0 as i32 as i32.count_ones();
  40. //~^ ERROR: casts cannot be followed by a method call
  41. let _ = 0i32: i32: i32 as u32 as i32.count_ones();
  42. //~^ ERROR: casts cannot be followed by a method call
  43. let _ = 0i32: i32.count_ones(): u32;
  44. //~^ ERROR: casts cannot be followed by a method call
  45. let _ = 0 as i32.count_ones(): u32;
  46. //~^ ERROR: casts cannot be followed by a method call
  47. let _ = 0i32: i32.count_ones() as u32;
  48. //~^ ERROR: casts cannot be followed by a method call
  49. let _ = 0 as i32.count_ones() as u32;
  50. //~^ ERROR: casts cannot be followed by a method call
  51. let _ = 0i32: i32: i32.count_ones() as u32 as i32;
  52. //~^ ERROR: casts cannot be followed by a method call
  53. }
  54. pub fn multiline_error() {
  55. let _ = 0
  56. as i32
  57. .count_ones();
  58. //~^^^ ERROR: casts cannot be followed by a method call
  59. }
  60. // this tests that the precedence for `!x as Y.Z` is still what we expect
  61. pub fn precedence() {
  62. let x: i32 = &vec![1, 2, 3] as &Vec<i32>[0];
  63. //~^ ERROR: casts cannot be followed by indexing
  64. }
  65. pub fn method_calls() {
  66. 0 as i32.max(0);
  67. //~^ ERROR: casts cannot be followed by a method call
  68. 0: i32.max(0);
  69. //~^ ERROR: casts cannot be followed by a method call
  70. }
  71. pub fn complex() {
  72. let _ = format!(
  73. "{} and {}",
  74. if true { 33 } else { 44 } as i32.max(0),
  75. //~^ ERROR: casts cannot be followed by a method call
  76. if true { 33 } else { 44 }: i32.max(0)
  77. //~^ ERROR: casts cannot be followed by a method call
  78. );
  79. }
  80. pub fn in_condition() {
  81. if 5u64 as i32.max(0) == 0 {
  82. //~^ ERROR: casts cannot be followed by a method call
  83. }
  84. if 5u64: u64.max(0) == 0 {
  85. //~^ ERROR: casts cannot be followed by a method call
  86. }
  87. }
  88. pub fn inside_block() {
  89. let _ = if true {
  90. 5u64 as u32.max(0) == 0
  91. //~^ ERROR: casts cannot be followed by a method call
  92. } else { false };
  93. let _ = if true {
  94. 5u64: u64.max(0) == 0
  95. //~^ ERROR: casts cannot be followed by a method call
  96. } else { false };
  97. }
  98. static bar: &[i32] = &(&[1,2,3] as &[i32][0..1]);
  99. //~^ ERROR: casts cannot be followed by indexing
  100. static bar2: &[i32] = &(&[1i32,2,3]: &[i32; 3][0..1]);
  101. //~^ ERROR: casts cannot be followed by indexing
  102. pub fn cast_then_try() -> Result<u64,u64> {
  103. Err(0u64) as Result<u64,u64>?;
  104. //~^ ERROR: casts cannot be followed by `?`
  105. Err(0u64): Result<u64,u64>?;
  106. //~^ ERROR: casts cannot be followed by `?`
  107. Ok(1)
  108. }
  109. pub fn cast_then_call() {
  110. type F = fn(u8);
  111. // type ascription won't actually do [unique drop fn type] -> fn(u8) casts.
  112. let drop_ptr = drop as fn(u8);
  113. drop as F();
  114. //~^ ERROR: parenthesized type parameters may only be used with a `Fn` trait [E0214]
  115. drop_ptr: F();
  116. //~^ ERROR: parenthesized type parameters may only be used with a `Fn` trait [E0214]
  117. }
  118. pub fn cast_to_fn_should_work() {
  119. let drop_ptr = drop as fn(u8);
  120. drop as fn(u8);
  121. drop_ptr: fn(u8);
  122. }
  123. pub fn parens_after_cast_error() {
  124. let drop_ptr = drop as fn(u8);
  125. drop as fn(u8)(0);
  126. //~^ ERROR: casts cannot be followed by a function call
  127. drop_ptr: fn(u8)(0);
  128. //~^ ERROR: casts cannot be followed by a function call
  129. }
  130. pub async fn cast_then_await() {
  131. Box::pin(noop()) as Pin<Box<dyn Future<Output = ()>>>.await;
  132. //~^ ERROR: casts cannot be followed by `.await`
  133. Box::pin(noop()): Pin<Box<_>>.await;
  134. //~^ ERROR: casts cannot be followed by `.await`
  135. }
  136. pub async fn noop() {}
  137. #[derive(Default)]
  138. pub struct Foo {
  139. pub bar: u32,
  140. }
  141. pub fn struct_field() {
  142. Foo::default() as Foo.bar;
  143. //~^ ERROR: cannot be followed by a field access
  144. Foo::default(): Foo.bar;
  145. //~^ ERROR: cannot be followed by a field access
  146. }