diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 43b4861397664..c0f661f7dbbf9 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -827,11 +827,12 @@ impl<'a> Parser<'a> { cast_expr: P, ) -> PResult<'a, P> { let span = cast_expr.span; - let maybe_ascription_span = if let ExprKind::Type(ascripted_expr, _) = &cast_expr.kind { - Some(ascripted_expr.span.shrink_to_hi().with_hi(span.hi())) - } else { - None - }; + let (cast_kind, maybe_ascription_span) = + if let ExprKind::Type(ascripted_expr, _) = &cast_expr.kind { + ("type ascription", Some(ascripted_expr.span.shrink_to_hi().with_hi(span.hi()))) + } else { + ("cast", None) + }; // Save the memory location of expr before parsing any following postfix operators. // This will be compared with the memory location of the output expression. @@ -844,7 +845,7 @@ impl<'a> Parser<'a> { // If the resulting expression is not a cast, or has a different memory location, it is an illegal postfix operator. if !matches!(with_postfix.kind, ExprKind::Cast(_, _) | ExprKind::Type(_, _)) || changed { let msg = format!( - "casts cannot be followed by {}", + "{cast_kind} cannot be followed by {}", match with_postfix.kind { ExprKind::Index(_, _) => "indexing", ExprKind::Try(_) => "`?`", diff --git a/src/test/ui/parser/issues/issue-35813-postfix-after-cast.rs b/src/test/ui/parser/issues/issue-35813-postfix-after-cast.rs index 23f245a51681b..7bd4b3a165c3d 100644 --- a/src/test/ui/parser/issues/issue-35813-postfix-after-cast.rs +++ b/src/test/ui/parser/issues/issue-35813-postfix-after-cast.rs @@ -8,16 +8,16 @@ use std::pin::Pin; // errors and parse such that further code gives useful errors. pub fn index_after_as_cast() { vec![1, 2, 3] as Vec[0]; - //~^ ERROR: casts cannot be followed by indexing + //~^ ERROR: cast cannot be followed by indexing vec![1, 2, 3]: Vec[0]; - //~^ ERROR: casts cannot be followed by indexing + //~^ ERROR: type ascription cannot be followed by indexing } pub fn index_after_cast_to_index() { (&[0]) as &[i32][0]; - //~^ ERROR: casts cannot be followed by indexing + //~^ ERROR: cast cannot be followed by indexing (&[0i32]): &[i32; 1][0]; - //~^ ERROR: casts cannot be followed by indexing + //~^ ERROR: type ascription cannot be followed by indexing } pub fn cast_after_cast() { @@ -37,89 +37,89 @@ pub fn cast_after_cast() { pub fn cast_cast_method_call() { let _ = 0i32: i32: i32.count_ones(); - //~^ ERROR: casts cannot be followed by a method call + //~^ ERROR: type ascription cannot be followed by a method call let _ = 0 as i32: i32.count_ones(); - //~^ ERROR: casts cannot be followed by a method call + //~^ ERROR: type ascription cannot be followed by a method call let _ = 0i32: i32 as i32.count_ones(); - //~^ ERROR: casts cannot be followed by a method call + //~^ ERROR: cast cannot be followed by a method call let _ = 0 as i32 as i32.count_ones(); - //~^ ERROR: casts cannot be followed by a method call + //~^ ERROR: cast cannot be followed by a method call let _ = 0i32: i32: i32 as u32 as i32.count_ones(); - //~^ ERROR: casts cannot be followed by a method call + //~^ ERROR: cast cannot be followed by a method call let _ = 0i32: i32.count_ones(): u32; - //~^ ERROR: casts cannot be followed by a method call + //~^ ERROR: type ascription cannot be followed by a method call let _ = 0 as i32.count_ones(): u32; - //~^ ERROR: casts cannot be followed by a method call + //~^ ERROR: cast cannot be followed by a method call let _ = 0i32: i32.count_ones() as u32; - //~^ ERROR: casts cannot be followed by a method call + //~^ ERROR: type ascription cannot be followed by a method call let _ = 0 as i32.count_ones() as u32; - //~^ ERROR: casts cannot be followed by a method call + //~^ ERROR: cast cannot be followed by a method call let _ = 0i32: i32: i32.count_ones() as u32 as i32; - //~^ ERROR: casts cannot be followed by a method call + //~^ ERROR: type ascription cannot be followed by a method call } pub fn multiline_error() { let _ = 0 as i32 .count_ones(); - //~^^^ ERROR: casts cannot be followed by a method call + //~^^^ ERROR: cast cannot be followed by a method call } // this tests that the precedence for `!x as Y.Z` is still what we expect pub fn precedence() { let x: i32 = &vec![1, 2, 3] as &Vec[0]; - //~^ ERROR: casts cannot be followed by indexing + //~^ ERROR: cast cannot be followed by indexing } pub fn method_calls() { 0 as i32.max(0); - //~^ ERROR: casts cannot be followed by a method call + //~^ ERROR: cast cannot be followed by a method call 0: i32.max(0); - //~^ ERROR: casts cannot be followed by a method call + //~^ ERROR: type ascription cannot be followed by a method call } pub fn complex() { let _ = format!( "{} and {}", if true { 33 } else { 44 } as i32.max(0), - //~^ ERROR: casts cannot be followed by a method call + //~^ ERROR: cast cannot be followed by a method call if true { 33 } else { 44 }: i32.max(0) - //~^ ERROR: casts cannot be followed by a method call + //~^ ERROR: type ascription cannot be followed by a method call ); } pub fn in_condition() { if 5u64 as i32.max(0) == 0 { - //~^ ERROR: casts cannot be followed by a method call + //~^ ERROR: cast cannot be followed by a method call } if 5u64: u64.max(0) == 0 { - //~^ ERROR: casts cannot be followed by a method call + //~^ ERROR: type ascription cannot be followed by a method call } } pub fn inside_block() { let _ = if true { 5u64 as u32.max(0) == 0 - //~^ ERROR: casts cannot be followed by a method call + //~^ ERROR: cast cannot be followed by a method call } else { false }; let _ = if true { 5u64: u64.max(0) == 0 - //~^ ERROR: casts cannot be followed by a method call + //~^ ERROR: type ascription cannot be followed by a method call } else { false }; } static bar: &[i32] = &(&[1,2,3] as &[i32][0..1]); -//~^ ERROR: casts cannot be followed by indexing +//~^ ERROR: cast cannot be followed by indexing static bar2: &[i32] = &(&[1i32,2,3]: &[i32; 3][0..1]); -//~^ ERROR: casts cannot be followed by indexing +//~^ ERROR: type ascription cannot be followed by indexing pub fn cast_then_try() -> Result { Err(0u64) as Result?; - //~^ ERROR: casts cannot be followed by `?` + //~^ ERROR: cast cannot be followed by `?` Err(0u64): Result?; - //~^ ERROR: casts cannot be followed by `?` + //~^ ERROR: type ascription cannot be followed by `?` Ok(1) } @@ -143,17 +143,17 @@ pub fn cast_to_fn_should_work() { pub fn parens_after_cast_error() { let drop_ptr = drop as fn(u8); drop as fn(u8)(0); - //~^ ERROR: casts cannot be followed by a function call + //~^ ERROR: cast cannot be followed by a function call drop_ptr: fn(u8)(0); - //~^ ERROR: casts cannot be followed by a function call + //~^ ERROR: type ascription cannot be followed by a function call } pub async fn cast_then_await() { Box::pin(noop()) as Pin>>.await; - //~^ ERROR: casts cannot be followed by `.await` + //~^ ERROR: cast cannot be followed by `.await` Box::pin(noop()): Pin>.await; - //~^ ERROR: casts cannot be followed by `.await` + //~^ ERROR: type ascription cannot be followed by `.await` } pub async fn noop() {} @@ -167,5 +167,5 @@ pub fn struct_field() { Foo::default() as Foo.bar; //~^ ERROR: cannot be followed by a field access Foo::default(): Foo.bar; - //~^ ERROR: cannot be followed by a field access + //~^ ERROR: type ascription cannot be followed by a field access } diff --git a/src/test/ui/parser/issues/issue-35813-postfix-after-cast.stderr b/src/test/ui/parser/issues/issue-35813-postfix-after-cast.stderr index 4cf273d8be50c..0c328bde285aa 100644 --- a/src/test/ui/parser/issues/issue-35813-postfix-after-cast.stderr +++ b/src/test/ui/parser/issues/issue-35813-postfix-after-cast.stderr @@ -1,4 +1,4 @@ -error: casts cannot be followed by indexing +error: cast cannot be followed by indexing --> $DIR/issue-35813-postfix-after-cast.rs:10:5 | LL | vec![1, 2, 3] as Vec[0]; @@ -9,7 +9,7 @@ help: try surrounding the expression in parentheses LL | (vec![1, 2, 3] as Vec)[0]; | + + -error: casts cannot be followed by indexing +error: type ascription cannot be followed by indexing --> $DIR/issue-35813-postfix-after-cast.rs:12:5 | LL | vec![1, 2, 3]: Vec[0]; @@ -25,7 +25,7 @@ LL - vec![1, 2, 3]: Vec[0]; LL + vec![1, 2, 3][0]; | -error: casts cannot be followed by indexing +error: cast cannot be followed by indexing --> $DIR/issue-35813-postfix-after-cast.rs:17:5 | LL | (&[0]) as &[i32][0]; @@ -36,7 +36,7 @@ help: try surrounding the expression in parentheses LL | ((&[0]) as &[i32])[0]; | + + -error: casts cannot be followed by indexing +error: type ascription cannot be followed by indexing --> $DIR/issue-35813-postfix-after-cast.rs:19:5 | LL | (&[0i32]): &[i32; 1][0]; @@ -52,7 +52,7 @@ LL - (&[0i32]): &[i32; 1][0]; LL + (&[0i32])[0]; | -error: casts cannot be followed by a method call +error: type ascription cannot be followed by a method call --> $DIR/issue-35813-postfix-after-cast.rs:39:13 | LL | let _ = 0i32: i32: i32.count_ones(); @@ -68,7 +68,7 @@ LL - let _ = 0i32: i32: i32.count_ones(); LL + let _ = 0i32: i32.count_ones(); | -error: casts cannot be followed by a method call +error: type ascription cannot be followed by a method call --> $DIR/issue-35813-postfix-after-cast.rs:41:13 | LL | let _ = 0 as i32: i32.count_ones(); @@ -84,7 +84,7 @@ LL - let _ = 0 as i32: i32.count_ones(); LL + let _ = 0 as i32.count_ones(); | -error: casts cannot be followed by a method call +error: cast cannot be followed by a method call --> $DIR/issue-35813-postfix-after-cast.rs:43:13 | LL | let _ = 0i32: i32 as i32.count_ones(); @@ -95,7 +95,7 @@ help: try surrounding the expression in parentheses LL | let _ = (0i32: i32 as i32).count_ones(); | + + -error: casts cannot be followed by a method call +error: cast cannot be followed by a method call --> $DIR/issue-35813-postfix-after-cast.rs:45:13 | LL | let _ = 0 as i32 as i32.count_ones(); @@ -106,7 +106,7 @@ help: try surrounding the expression in parentheses LL | let _ = (0 as i32 as i32).count_ones(); | + + -error: casts cannot be followed by a method call +error: cast cannot be followed by a method call --> $DIR/issue-35813-postfix-after-cast.rs:47:13 | LL | let _ = 0i32: i32: i32 as u32 as i32.count_ones(); @@ -117,7 +117,7 @@ help: try surrounding the expression in parentheses LL | let _ = (0i32: i32: i32 as u32 as i32).count_ones(); | + + -error: casts cannot be followed by a method call +error: type ascription cannot be followed by a method call --> $DIR/issue-35813-postfix-after-cast.rs:49:13 | LL | let _ = 0i32: i32.count_ones(): u32; @@ -133,7 +133,7 @@ LL - let _ = 0i32: i32.count_ones(): u32; LL + let _ = 0i32.count_ones(): u32; | -error: casts cannot be followed by a method call +error: cast cannot be followed by a method call --> $DIR/issue-35813-postfix-after-cast.rs:51:13 | LL | let _ = 0 as i32.count_ones(): u32; @@ -144,7 +144,7 @@ help: try surrounding the expression in parentheses LL | let _ = (0 as i32).count_ones(): u32; | + + -error: casts cannot be followed by a method call +error: type ascription cannot be followed by a method call --> $DIR/issue-35813-postfix-after-cast.rs:53:13 | LL | let _ = 0i32: i32.count_ones() as u32; @@ -160,7 +160,7 @@ LL - let _ = 0i32: i32.count_ones() as u32; LL + let _ = 0i32.count_ones() as u32; | -error: casts cannot be followed by a method call +error: cast cannot be followed by a method call --> $DIR/issue-35813-postfix-after-cast.rs:55:13 | LL | let _ = 0 as i32.count_ones() as u32; @@ -171,7 +171,7 @@ help: try surrounding the expression in parentheses LL | let _ = (0 as i32).count_ones() as u32; | + + -error: casts cannot be followed by a method call +error: type ascription cannot be followed by a method call --> $DIR/issue-35813-postfix-after-cast.rs:57:13 | LL | let _ = 0i32: i32: i32.count_ones() as u32 as i32; @@ -187,7 +187,7 @@ LL - let _ = 0i32: i32: i32.count_ones() as u32 as i32; LL + let _ = 0i32: i32.count_ones() as u32 as i32; | -error: casts cannot be followed by a method call +error: cast cannot be followed by a method call --> $DIR/issue-35813-postfix-after-cast.rs:62:13 | LL | let _ = 0 @@ -201,7 +201,7 @@ LL ~ let _ = (0 LL ~ as i32) | -error: casts cannot be followed by indexing +error: cast cannot be followed by indexing --> $DIR/issue-35813-postfix-after-cast.rs:70:18 | LL | let x: i32 = &vec![1, 2, 3] as &Vec[0]; @@ -212,7 +212,7 @@ help: try surrounding the expression in parentheses LL | let x: i32 = (&vec![1, 2, 3] as &Vec)[0]; | + + -error: casts cannot be followed by a method call +error: cast cannot be followed by a method call --> $DIR/issue-35813-postfix-after-cast.rs:75:5 | LL | 0 as i32.max(0); @@ -223,7 +223,7 @@ help: try surrounding the expression in parentheses LL | (0 as i32).max(0); | + + -error: casts cannot be followed by a method call +error: type ascription cannot be followed by a method call --> $DIR/issue-35813-postfix-after-cast.rs:77:5 | LL | 0: i32.max(0); @@ -239,7 +239,7 @@ LL - 0: i32.max(0); LL + 0.max(0); | -error: casts cannot be followed by a method call +error: cast cannot be followed by a method call --> $DIR/issue-35813-postfix-after-cast.rs:92:8 | LL | if 5u64 as i32.max(0) == 0 { @@ -250,7 +250,7 @@ help: try surrounding the expression in parentheses LL | if (5u64 as i32).max(0) == 0 { | + + -error: casts cannot be followed by a method call +error: type ascription cannot be followed by a method call --> $DIR/issue-35813-postfix-after-cast.rs:95:8 | LL | if 5u64: u64.max(0) == 0 { @@ -266,7 +266,7 @@ LL - if 5u64: u64.max(0) == 0 { LL + if 5u64.max(0) == 0 { | -error: casts cannot be followed by a method call +error: cast cannot be followed by a method call --> $DIR/issue-35813-postfix-after-cast.rs:102:9 | LL | 5u64 as u32.max(0) == 0 @@ -277,7 +277,7 @@ help: try surrounding the expression in parentheses LL | (5u64 as u32).max(0) == 0 | + + -error: casts cannot be followed by a method call +error: type ascription cannot be followed by a method call --> $DIR/issue-35813-postfix-after-cast.rs:106:9 | LL | 5u64: u64.max(0) == 0 @@ -293,7 +293,7 @@ LL - 5u64: u64.max(0) == 0 LL + 5u64.max(0) == 0 | -error: casts cannot be followed by indexing +error: cast cannot be followed by indexing --> $DIR/issue-35813-postfix-after-cast.rs:111:24 | LL | static bar: &[i32] = &(&[1,2,3] as &[i32][0..1]); @@ -304,7 +304,7 @@ help: try surrounding the expression in parentheses LL | static bar: &[i32] = &((&[1,2,3] as &[i32])[0..1]); | + + -error: casts cannot be followed by indexing +error: type ascription cannot be followed by indexing --> $DIR/issue-35813-postfix-after-cast.rs:114:25 | LL | static bar2: &[i32] = &(&[1i32,2,3]: &[i32; 3][0..1]); @@ -320,7 +320,7 @@ LL - static bar2: &[i32] = &(&[1i32,2,3]: &[i32; 3][0..1]); LL + static bar2: &[i32] = &(&[1i32,2,3][0..1]); | -error: casts cannot be followed by `?` +error: cast cannot be followed by `?` --> $DIR/issue-35813-postfix-after-cast.rs:119:5 | LL | Err(0u64) as Result?; @@ -331,7 +331,7 @@ help: try surrounding the expression in parentheses LL | (Err(0u64) as Result)?; | + + -error: casts cannot be followed by `?` +error: type ascription cannot be followed by `?` --> $DIR/issue-35813-postfix-after-cast.rs:121:5 | LL | Err(0u64): Result?; @@ -347,7 +347,7 @@ LL - Err(0u64): Result?; LL + Err(0u64)?; | -error: casts cannot be followed by a function call +error: cast cannot be followed by a function call --> $DIR/issue-35813-postfix-after-cast.rs:145:5 | LL | drop as fn(u8)(0); @@ -358,7 +358,7 @@ help: try surrounding the expression in parentheses LL | (drop as fn(u8))(0); | + + -error: casts cannot be followed by a function call +error: type ascription cannot be followed by a function call --> $DIR/issue-35813-postfix-after-cast.rs:147:5 | LL | drop_ptr: fn(u8)(0); @@ -374,7 +374,7 @@ LL - drop_ptr: fn(u8)(0); LL + drop_ptr(0); | -error: casts cannot be followed by `.await` +error: cast cannot be followed by `.await` --> $DIR/issue-35813-postfix-after-cast.rs:152:5 | LL | Box::pin(noop()) as Pin>>.await; @@ -385,7 +385,7 @@ help: try surrounding the expression in parentheses LL | (Box::pin(noop()) as Pin>>).await; | + + -error: casts cannot be followed by `.await` +error: type ascription cannot be followed by `.await` --> $DIR/issue-35813-postfix-after-cast.rs:155:5 | LL | Box::pin(noop()): Pin>.await; @@ -401,7 +401,7 @@ LL - Box::pin(noop()): Pin>.await; LL + Box::pin(noop()).await; | -error: casts cannot be followed by a field access +error: cast cannot be followed by a field access --> $DIR/issue-35813-postfix-after-cast.rs:167:5 | LL | Foo::default() as Foo.bar; @@ -412,7 +412,7 @@ help: try surrounding the expression in parentheses LL | (Foo::default() as Foo).bar; | + + -error: casts cannot be followed by a field access +error: type ascription cannot be followed by a field access --> $DIR/issue-35813-postfix-after-cast.rs:169:5 | LL | Foo::default(): Foo.bar; @@ -428,7 +428,7 @@ LL - Foo::default(): Foo.bar; LL + Foo::default().bar; | -error: casts cannot be followed by a method call +error: cast cannot be followed by a method call --> $DIR/issue-35813-postfix-after-cast.rs:84:9 | LL | if true { 33 } else { 44 } as i32.max(0), @@ -439,7 +439,7 @@ help: try surrounding the expression in parentheses LL | (if true { 33 } else { 44 } as i32).max(0), | + + -error: casts cannot be followed by a method call +error: type ascription cannot be followed by a method call --> $DIR/issue-35813-postfix-after-cast.rs:86:9 | LL | if true { 33 } else { 44 }: i32.max(0) diff --git a/src/test/ui/type/ascription/issue-54516.fixed b/src/test/ui/type/ascription/issue-54516.fixed index 181637b97bb6a..f78268894daec 100644 --- a/src/test/ui/type/ascription/issue-54516.fixed +++ b/src/test/ui/type/ascription/issue-54516.fixed @@ -3,5 +3,5 @@ use std::collections::BTreeMap; fn main() { println!("{}", std::mem::size_of::>()); - //~^ ERROR casts cannot be followed by a function call + //~^ ERROR type ascription cannot be followed by a function call } diff --git a/src/test/ui/type/ascription/issue-54516.rs b/src/test/ui/type/ascription/issue-54516.rs index f09ddd487d895..1f34e6943bab2 100644 --- a/src/test/ui/type/ascription/issue-54516.rs +++ b/src/test/ui/type/ascription/issue-54516.rs @@ -3,5 +3,5 @@ use std::collections::BTreeMap; fn main() { println!("{}", std::mem:size_of::>()); - //~^ ERROR casts cannot be followed by a function call + //~^ ERROR type ascription cannot be followed by a function call } diff --git a/src/test/ui/type/ascription/issue-54516.stderr b/src/test/ui/type/ascription/issue-54516.stderr index 2c7ff6bdc4866..1ab9093e58445 100644 --- a/src/test/ui/type/ascription/issue-54516.stderr +++ b/src/test/ui/type/ascription/issue-54516.stderr @@ -1,4 +1,4 @@ -error: casts cannot be followed by a function call +error: type ascription cannot be followed by a function call --> $DIR/issue-54516.rs:5:20 | LL | println!("{}", std::mem:size_of::>()); diff --git a/src/test/ui/type/ascription/issue-60933.fixed b/src/test/ui/type/ascription/issue-60933.fixed index ac9f6a07031fc..3e8be3875b34e 100644 --- a/src/test/ui/type/ascription/issue-60933.fixed +++ b/src/test/ui/type/ascription/issue-60933.fixed @@ -1,5 +1,5 @@ // run-rustfix fn main() { let _: usize = std::mem::size_of::(); - //~^ ERROR casts cannot be followed by a function call + //~^ ERROR type ascription cannot be followed by a function call } diff --git a/src/test/ui/type/ascription/issue-60933.rs b/src/test/ui/type/ascription/issue-60933.rs index cb093735efa58..2a4ad7bdc4ee7 100644 --- a/src/test/ui/type/ascription/issue-60933.rs +++ b/src/test/ui/type/ascription/issue-60933.rs @@ -1,5 +1,5 @@ // run-rustfix fn main() { let _: usize = std::mem:size_of::(); - //~^ ERROR casts cannot be followed by a function call + //~^ ERROR type ascription cannot be followed by a function call } diff --git a/src/test/ui/type/ascription/issue-60933.stderr b/src/test/ui/type/ascription/issue-60933.stderr index 5c35de88e14d6..0b7f8edf62402 100644 --- a/src/test/ui/type/ascription/issue-60933.stderr +++ b/src/test/ui/type/ascription/issue-60933.stderr @@ -1,4 +1,4 @@ -error: casts cannot be followed by a function call +error: type ascription cannot be followed by a function call --> $DIR/issue-60933.rs:3:20 | LL | let _: usize = std::mem:size_of::();