Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On function and method calls in patterns, link to the book #131312

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/rustc_parse/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,8 @@ parse_unexpected_expr_in_pat =
*[false] a pattern
}, found an expression

.label = arbitrary expressions are not allowed in patterns
.label = not a pattern
.note = arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>

parse_unexpected_expr_in_pat_const_sugg = consider extracting the expression into a `const`

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2608,6 +2608,7 @@ pub(crate) struct ExpectedCommaAfterPatternField {

#[derive(Diagnostic)]
#[diag(parse_unexpected_expr_in_pat)]
#[note]
pub(crate) struct UnexpectedExpressionInPattern {
/// The unexpected expr's span.
#[primary_span]
Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {

self.suggest_bare_struct_literal(&mut err);
self.suggest_changing_type_to_const_param(&mut err, res, source, span);
self.explain_functions_in_pattern(&mut err, res, source);

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

fn explain_functions_in_pattern(
&mut self,
err: &mut Diag<'_>,
res: Option<Res>,
source: PathSource<'_>,
) {
let PathSource::TupleStruct(_, _) = source else { return };
let Some(Res::Def(DefKind::Fn, _)) = res else { return };
err.primary_message("expected a pattern, found a function call");
err.note("function calls are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>");
}

fn suggest_changing_type_to_const_param(
&mut self,
err: &mut Diag<'_>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ error: expected a pattern range bound, found an expression
--> $DIR/range_pat_interactions1.rs:17:16
|
LL | 0..5+1 => errors_only.push(x),
| ^^^ arbitrary expressions are not allowed in patterns
| ^^^ not a pattern
|
= note: arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>
help: consider extracting the expression into a `const`
|
LL + const VAL: /* Type */ = 5+1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ error: expected a pattern range bound, found an expression
--> $DIR/range_pat_interactions2.rs:10:18
|
LL | 0..=(5+1) => errors_only.push(x),
| ^^^ arbitrary expressions are not allowed in patterns
| ^^^ not a pattern
|
= note: arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>
help: consider extracting the expression into a `const`
|
LL + const VAL: /* Type */ = 5+1;
Expand Down
4 changes: 3 additions & 1 deletion tests/ui/parser/bad-name.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ error: expected a pattern, found an expression
--> $DIR/bad-name.rs:2:7
|
LL | let x.y::<isize>.z foo;
| ^^^^^^^^^^^^^^ arbitrary expressions are not allowed in patterns
| ^^^^^^^^^^^^^^ not a pattern
|
= note: arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>

error: expected one of `(`, `.`, `::`, `:`, `;`, `=`, `?`, `|`, or an operator, found `foo`
--> $DIR/bad-name.rs:2:22
Expand Down
4 changes: 3 additions & 1 deletion tests/ui/parser/issues/issue-24197.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ error: expected a pattern, found an expression
--> $DIR/issue-24197.rs:2:9
|
LL | let buf[0] = 0;
| ^^^^^^ arbitrary expressions are not allowed in patterns
| ^^^^^^ not a pattern
|
= note: arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>

error: aborting due to 1 previous error

3 changes: 2 additions & 1 deletion tests/ui/parser/issues/issue-24375.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ error: expected a pattern, found an expression
--> $DIR/issue-24375.rs:6:9
|
LL | tmp[0] => {}
| ^^^^^^ arbitrary expressions are not allowed in patterns
| ^^^^^^ not a pattern
|
= note: arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>
help: consider moving the expression to a match arm guard
|
LL | val if val == tmp[0] => {}
Expand Down
4 changes: 3 additions & 1 deletion tests/ui/parser/pat-lt-bracket-5.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ error: expected a pattern, found an expression
--> $DIR/pat-lt-bracket-5.rs:2:9
|
LL | let v[0] = v[1];
| ^^^^ arbitrary expressions are not allowed in patterns
| ^^^^ not a pattern
|
= note: arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>

error[E0425]: cannot find value `v` in this scope
--> $DIR/pat-lt-bracket-5.rs:2:16
Expand Down
4 changes: 3 additions & 1 deletion tests/ui/parser/pat-lt-bracket-6.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ error: expected a pattern, found an expression
--> $DIR/pat-lt-bracket-6.rs:5:14
|
LL | let Test(&desc[..]) = x;
| ^^^^^^^^^ arbitrary expressions are not allowed in patterns
| ^^^^^^^^^ not a pattern
|
= note: arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>

error[E0308]: mismatched types
--> $DIR/pat-lt-bracket-6.rs:10:30
Expand Down
8 changes: 6 additions & 2 deletions tests/ui/parser/pat-ranges-3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ error: expected a pattern range bound, found an expression
--> $DIR/pat-ranges-3.rs:4:16
|
LL | let 10 ..= 10 + 3 = 12;
| ^^^^^^ arbitrary expressions are not allowed in patterns
| ^^^^^^ not a pattern
|
= note: arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>

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

error: aborting due to 2 previous errors

Loading
Loading