Skip to content

Commit cfd231a

Browse files
authored
Rollup merge of rust-lang#99895 - compiler-errors:type-ascription-aint-cast, r=davidtwco
don't call type ascription "cast" Noticed in rust-lang#99885
2 parents eb378d2 + 7cdd937 commit cfd231a

File tree

9 files changed

+80
-79
lines changed

9 files changed

+80
-79
lines changed

compiler/rustc_parse/src/parser/expr.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -827,11 +827,12 @@ impl<'a> Parser<'a> {
827827
cast_expr: P<Expr>,
828828
) -> PResult<'a, P<Expr>> {
829829
let span = cast_expr.span;
830-
let maybe_ascription_span = if let ExprKind::Type(ascripted_expr, _) = &cast_expr.kind {
831-
Some(ascripted_expr.span.shrink_to_hi().with_hi(span.hi()))
832-
} else {
833-
None
834-
};
830+
let (cast_kind, maybe_ascription_span) =
831+
if let ExprKind::Type(ascripted_expr, _) = &cast_expr.kind {
832+
("type ascription", Some(ascripted_expr.span.shrink_to_hi().with_hi(span.hi())))
833+
} else {
834+
("cast", None)
835+
};
835836

836837
// Save the memory location of expr before parsing any following postfix operators.
837838
// This will be compared with the memory location of the output expression.
@@ -844,7 +845,7 @@ impl<'a> Parser<'a> {
844845
// If the resulting expression is not a cast, or has a different memory location, it is an illegal postfix operator.
845846
if !matches!(with_postfix.kind, ExprKind::Cast(_, _) | ExprKind::Type(_, _)) || changed {
846847
let msg = format!(
847-
"casts cannot be followed by {}",
848+
"{cast_kind} cannot be followed by {}",
848849
match with_postfix.kind {
849850
ExprKind::Index(_, _) => "indexing",
850851
ExprKind::Try(_) => "`?`",

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

+33-33
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ use std::pin::Pin;
88
// errors and parse such that further code gives useful errors.
99
pub fn index_after_as_cast() {
1010
vec![1, 2, 3] as Vec<i32>[0];
11-
//~^ ERROR: casts cannot be followed by indexing
11+
//~^ ERROR: cast cannot be followed by indexing
1212
vec![1, 2, 3]: Vec<i32>[0];
13-
//~^ ERROR: casts cannot be followed by indexing
13+
//~^ ERROR: type ascription cannot be followed by indexing
1414
}
1515

1616
pub fn index_after_cast_to_index() {
1717
(&[0]) as &[i32][0];
18-
//~^ ERROR: casts cannot be followed by indexing
18+
//~^ ERROR: cast cannot be followed by indexing
1919
(&[0i32]): &[i32; 1][0];
20-
//~^ ERROR: casts cannot be followed by indexing
20+
//~^ ERROR: type ascription cannot be followed by indexing
2121
}
2222

2323
pub fn cast_after_cast() {
@@ -37,89 +37,89 @@ pub fn cast_after_cast() {
3737

3838
pub fn cast_cast_method_call() {
3939
let _ = 0i32: i32: i32.count_ones();
40-
//~^ ERROR: casts cannot be followed by a method call
40+
//~^ ERROR: type ascription cannot be followed by a method call
4141
let _ = 0 as i32: i32.count_ones();
42-
//~^ ERROR: casts cannot be followed by a method call
42+
//~^ ERROR: type ascription cannot be followed by a method call
4343
let _ = 0i32: i32 as i32.count_ones();
44-
//~^ ERROR: casts cannot be followed by a method call
44+
//~^ ERROR: cast cannot be followed by a method call
4545
let _ = 0 as i32 as i32.count_ones();
46-
//~^ ERROR: casts cannot be followed by a method call
46+
//~^ ERROR: cast cannot be followed by a method call
4747
let _ = 0i32: i32: i32 as u32 as i32.count_ones();
48-
//~^ ERROR: casts cannot be followed by a method call
48+
//~^ ERROR: cast cannot be followed by a method call
4949
let _ = 0i32: i32.count_ones(): u32;
50-
//~^ ERROR: casts cannot be followed by a method call
50+
//~^ ERROR: type ascription cannot be followed by a method call
5151
let _ = 0 as i32.count_ones(): u32;
52-
//~^ ERROR: casts cannot be followed by a method call
52+
//~^ ERROR: cast cannot be followed by a method call
5353
let _ = 0i32: i32.count_ones() as u32;
54-
//~^ ERROR: casts cannot be followed by a method call
54+
//~^ ERROR: type ascription cannot be followed by a method call
5555
let _ = 0 as i32.count_ones() as u32;
56-
//~^ ERROR: casts cannot be followed by a method call
56+
//~^ ERROR: cast cannot be followed by a method call
5757
let _ = 0i32: i32: i32.count_ones() as u32 as i32;
58-
//~^ ERROR: casts cannot be followed by a method call
58+
//~^ ERROR: type ascription cannot be followed by a method call
5959
}
6060

6161
pub fn multiline_error() {
6262
let _ = 0
6363
as i32
6464
.count_ones();
65-
//~^^^ ERROR: casts cannot be followed by a method call
65+
//~^^^ ERROR: cast cannot be followed by a method call
6666
}
6767

6868
// this tests that the precedence for `!x as Y.Z` is still what we expect
6969
pub fn precedence() {
7070
let x: i32 = &vec![1, 2, 3] as &Vec<i32>[0];
71-
//~^ ERROR: casts cannot be followed by indexing
71+
//~^ ERROR: cast cannot be followed by indexing
7272
}
7373

7474
pub fn method_calls() {
7575
0 as i32.max(0);
76-
//~^ ERROR: casts cannot be followed by a method call
76+
//~^ ERROR: cast cannot be followed by a method call
7777
0: i32.max(0);
78-
//~^ ERROR: casts cannot be followed by a method call
78+
//~^ ERROR: type ascription cannot be followed by a method call
7979
}
8080

8181
pub fn complex() {
8282
let _ = format!(
8383
"{} and {}",
8484
if true { 33 } else { 44 } as i32.max(0),
85-
//~^ ERROR: casts cannot be followed by a method call
85+
//~^ ERROR: cast cannot be followed by a method call
8686
if true { 33 } else { 44 }: i32.max(0)
87-
//~^ ERROR: casts cannot be followed by a method call
87+
//~^ ERROR: type ascription cannot be followed by a method call
8888
);
8989
}
9090

9191
pub fn in_condition() {
9292
if 5u64 as i32.max(0) == 0 {
93-
//~^ ERROR: casts cannot be followed by a method call
93+
//~^ ERROR: cast cannot be followed by a method call
9494
}
9595
if 5u64: u64.max(0) == 0 {
96-
//~^ ERROR: casts cannot be followed by a method call
96+
//~^ ERROR: type ascription cannot be followed by a method call
9797
}
9898
}
9999

100100
pub fn inside_block() {
101101
let _ = if true {
102102
5u64 as u32.max(0) == 0
103-
//~^ ERROR: casts cannot be followed by a method call
103+
//~^ ERROR: cast cannot be followed by a method call
104104
} else { false };
105105
let _ = if true {
106106
5u64: u64.max(0) == 0
107-
//~^ ERROR: casts cannot be followed by a method call
107+
//~^ ERROR: type ascription cannot be followed by a method call
108108
} else { false };
109109
}
110110

111111
static bar: &[i32] = &(&[1,2,3] as &[i32][0..1]);
112-
//~^ ERROR: casts cannot be followed by indexing
112+
//~^ ERROR: cast cannot be followed by indexing
113113

114114
static bar2: &[i32] = &(&[1i32,2,3]: &[i32; 3][0..1]);
115-
//~^ ERROR: casts cannot be followed by indexing
115+
//~^ ERROR: type ascription cannot be followed by indexing
116116

117117

118118
pub fn cast_then_try() -> Result<u64,u64> {
119119
Err(0u64) as Result<u64,u64>?;
120-
//~^ ERROR: casts cannot be followed by `?`
120+
//~^ ERROR: cast cannot be followed by `?`
121121
Err(0u64): Result<u64,u64>?;
122-
//~^ ERROR: casts cannot be followed by `?`
122+
//~^ ERROR: type ascription cannot be followed by `?`
123123
Ok(1)
124124
}
125125

@@ -143,17 +143,17 @@ pub fn cast_to_fn_should_work() {
143143
pub fn parens_after_cast_error() {
144144
let drop_ptr = drop as fn(u8);
145145
drop as fn(u8)(0);
146-
//~^ ERROR: casts cannot be followed by a function call
146+
//~^ ERROR: cast cannot be followed by a function call
147147
drop_ptr: fn(u8)(0);
148-
//~^ ERROR: casts cannot be followed by a function call
148+
//~^ ERROR: type ascription cannot be followed by a function call
149149
}
150150

151151
pub async fn cast_then_await() {
152152
Box::pin(noop()) as Pin<Box<dyn Future<Output = ()>>>.await;
153-
//~^ ERROR: casts cannot be followed by `.await`
153+
//~^ ERROR: cast cannot be followed by `.await`
154154

155155
Box::pin(noop()): Pin<Box<_>>.await;
156-
//~^ ERROR: casts cannot be followed by `.await`
156+
//~^ ERROR: type ascription cannot be followed by `.await`
157157
}
158158

159159
pub async fn noop() {}
@@ -167,5 +167,5 @@ pub fn struct_field() {
167167
Foo::default() as Foo.bar;
168168
//~^ ERROR: cannot be followed by a field access
169169
Foo::default(): Foo.bar;
170-
//~^ ERROR: cannot be followed by a field access
170+
//~^ ERROR: type ascription cannot be followed by a field access
171171
}

0 commit comments

Comments
 (0)