Skip to content

Commit 7f5548f

Browse files
committed
On function and method calls in patterns, link to the book
``` error: expected a pattern, found an expression --> f889.rs:3:13 | 3 | let (x, y.drop()) = (1, 2); //~ ERROR | ^^^^^^^^ not a pattern | = note: arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html> error[E0532]: expected a pattern, found a function call --> f889.rs:2:13 | 2 | let (x, drop(y)) = (1, 2); //~ ERROR | ^^^^ not a tuple struct or tuple variant | = note: function calls are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html> ``` Fix rust-lang#97200.
1 parent 14f303b commit 7f5548f

19 files changed

+156
-64
lines changed

compiler/rustc_parse/messages.ftl

+2-1
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,8 @@ parse_unexpected_expr_in_pat =
812812
*[false] a pattern
813813
}, found an expression
814814
815-
.label = arbitrary expressions are not allowed in patterns
815+
.label = not a pattern
816+
.note = arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>
816817
817818
parse_unexpected_expr_in_pat_const_sugg = consider extracting the expression into a `const`
818819

compiler/rustc_parse/src/errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2608,6 +2608,7 @@ pub(crate) struct ExpectedCommaAfterPatternField {
26082608

26092609
#[derive(Diagnostic)]
26102610
#[diag(parse_unexpected_expr_in_pat)]
2611+
#[note]
26112612
pub(crate) struct UnexpectedExpressionInPattern {
26122613
/// The unexpected expr's span.
26132614
#[primary_span]

compiler/rustc_resolve/src/late/diagnostics.rs

+13
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
443443

444444
self.suggest_bare_struct_literal(&mut err);
445445
self.suggest_changing_type_to_const_param(&mut err, res, source, span);
446+
self.explain_functions_in_pattern(&mut err, res, source);
446447

447448
if self.suggest_pattern_match_with_let(&mut err, source, span) {
448449
// Fallback label.
@@ -1166,6 +1167,18 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
11661167
}
11671168
}
11681169

1170+
fn explain_functions_in_pattern(
1171+
&mut self,
1172+
err: &mut Diag<'_>,
1173+
res: Option<Res>,
1174+
source: PathSource<'_>,
1175+
) {
1176+
let PathSource::TupleStruct(_, _) = source else { return };
1177+
let Some(Res::Def(DefKind::Fn, _)) = res else { return };
1178+
err.primary_message("expected a pattern, found a function call");
1179+
err.note("function calls are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>");
1180+
}
1181+
11691182
fn suggest_changing_type_to_const_param(
11701183
&mut self,
11711184
err: &mut Diag<'_>,

tests/ui/half-open-range-patterns/range_pat_interactions1.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ error: expected a pattern range bound, found an expression
22
--> $DIR/range_pat_interactions1.rs:17:16
33
|
44
LL | 0..5+1 => errors_only.push(x),
5-
| ^^^ arbitrary expressions are not allowed in patterns
5+
| ^^^ not a pattern
66
|
7+
= note: arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>
78
help: consider extracting the expression into a `const`
89
|
910
LL + const VAL: /* Type */ = 5+1;

tests/ui/half-open-range-patterns/range_pat_interactions2.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ error: expected a pattern range bound, found an expression
1414
--> $DIR/range_pat_interactions2.rs:10:18
1515
|
1616
LL | 0..=(5+1) => errors_only.push(x),
17-
| ^^^ arbitrary expressions are not allowed in patterns
17+
| ^^^ not a pattern
1818
|
19+
= note: arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>
1920
help: consider extracting the expression into a `const`
2021
|
2122
LL + const VAL: /* Type */ = 5+1;

tests/ui/parser/bad-name.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ error: expected a pattern, found an expression
88
--> $DIR/bad-name.rs:2:7
99
|
1010
LL | let x.y::<isize>.z foo;
11-
| ^^^^^^^^^^^^^^ arbitrary expressions are not allowed in patterns
11+
| ^^^^^^^^^^^^^^ not a pattern
12+
|
13+
= note: arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>
1214

1315
error: expected one of `(`, `.`, `::`, `:`, `;`, `=`, `?`, `|`, or an operator, found `foo`
1416
--> $DIR/bad-name.rs:2:22

tests/ui/parser/issues/issue-24197.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error: expected a pattern, found an expression
22
--> $DIR/issue-24197.rs:2:9
33
|
44
LL | let buf[0] = 0;
5-
| ^^^^^^ arbitrary expressions are not allowed in patterns
5+
| ^^^^^^ not a pattern
6+
|
7+
= note: arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>
68

79
error: aborting due to 1 previous error
810

tests/ui/parser/issues/issue-24375.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ error: expected a pattern, found an expression
22
--> $DIR/issue-24375.rs:6:9
33
|
44
LL | tmp[0] => {}
5-
| ^^^^^^ arbitrary expressions are not allowed in patterns
5+
| ^^^^^^ not a pattern
66
|
7+
= note: arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>
78
help: consider moving the expression to a match arm guard
89
|
910
LL | val if val == tmp[0] => {}

tests/ui/parser/pat-lt-bracket-5.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error: expected a pattern, found an expression
22
--> $DIR/pat-lt-bracket-5.rs:2:9
33
|
44
LL | let v[0] = v[1];
5-
| ^^^^ arbitrary expressions are not allowed in patterns
5+
| ^^^^ not a pattern
6+
|
7+
= note: arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>
68

79
error[E0425]: cannot find value `v` in this scope
810
--> $DIR/pat-lt-bracket-5.rs:2:16

tests/ui/parser/pat-lt-bracket-6.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error: expected a pattern, found an expression
22
--> $DIR/pat-lt-bracket-6.rs:5:14
33
|
44
LL | let Test(&desc[..]) = x;
5-
| ^^^^^^^^^ arbitrary expressions are not allowed in patterns
5+
| ^^^^^^^^^ not a pattern
6+
|
7+
= note: arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>
68

79
error[E0308]: mismatched types
810
--> $DIR/pat-lt-bracket-6.rs:10:30

tests/ui/parser/pat-ranges-3.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ error: expected a pattern range bound, found an expression
22
--> $DIR/pat-ranges-3.rs:4:16
33
|
44
LL | let 10 ..= 10 + 3 = 12;
5-
| ^^^^^^ arbitrary expressions are not allowed in patterns
5+
| ^^^^^^ not a pattern
6+
|
7+
= note: arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>
68

79
error: expected a pattern range bound, found an expression
810
--> $DIR/pat-ranges-3.rs:7:9
911
|
1012
LL | let 10 - 3 ..= 10 = 8;
11-
| ^^^^^^ arbitrary expressions are not allowed in patterns
13+
| ^^^^^^ not a pattern
14+
|
15+
= note: arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>
1216

1317
error: aborting due to 2 previous errors
1418

0 commit comments

Comments
 (0)