Skip to content

Commit 01b2404

Browse files
Fix for issue 91058
1 parent 3ba27e7 commit 01b2404

File tree

6 files changed

+38
-24
lines changed

6 files changed

+38
-24
lines changed

Diff for: compiler/rustc_infer/src/infer/error_reporting/mod.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -1695,11 +1695,23 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
16951695
}
16961696
_ => exp_found,
16971697
};
1698-
debug!("exp_found {:?} terr {:?}", exp_found, terr);
1698+
debug!("exp_found {:?} terr {:?} cause.code {:?}", exp_found, terr, cause.code);
16991699
if let Some(exp_found) = exp_found {
1700-
self.suggest_as_ref_where_appropriate(span, &exp_found, diag);
1701-
self.suggest_accessing_field_where_appropriate(cause, &exp_found, diag);
1702-
self.suggest_await_on_expect_found(cause, span, &exp_found, diag);
1700+
let should_suggest_fixes = if let ObligationCauseCode::Pattern { root_ty, .. } =
1701+
&cause.code
1702+
{
1703+
// Skip if the root_ty of the pattern is not the same as the expected_ty.
1704+
// If these types aren't equal then we've probably peeled off a layer of arrays.
1705+
same_type_modulo_infer(self.resolve_vars_if_possible(*root_ty), exp_found.expected)
1706+
} else {
1707+
true
1708+
};
1709+
1710+
if should_suggest_fixes {
1711+
self.suggest_as_ref_where_appropriate(span, &exp_found, diag);
1712+
self.suggest_accessing_field_where_appropriate(cause, &exp_found, diag);
1713+
self.suggest_await_on_expect_found(cause, span, &exp_found, diag);
1714+
}
17031715
}
17041716

17051717
// In some (most?) cases cause.body_id points to actual body, but in some cases

Diff for: src/test/ui/half-open-range-patterns/exclusive_range_pattern_syntax_collision.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ LL | [_, 99.., _] => {},
88
|
99
= note: expected struct `std::ops::Range<{integer}>`
1010
found type `{integer}`
11-
help: you might have meant to use field `start` whose type is `{integer}`
12-
|
13-
LL | match [5..4, 99..105, 43..44].start {
14-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1511

1612
error: aborting due to previous error
1713

Diff for: src/test/ui/half-open-range-patterns/exclusive_range_pattern_syntax_collision2.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ LL | [_, 99..] => {},
1414
|
1515
= note: expected struct `std::ops::Range<{integer}>`
1616
found type `{integer}`
17-
help: you might have meant to use field `start` whose type is `{integer}`
18-
|
19-
LL | match [5..4, 99..105, 43..44].start {
20-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2117

2218
error: aborting due to 2 previous errors
2319

Diff for: src/test/ui/half-open-range-patterns/exclusive_range_pattern_syntax_collision3.stderr

-12
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ LL | [..9, 99..100, _] => {},
88
|
99
= note: expected struct `std::ops::Range<{integer}>`
1010
found type `{integer}`
11-
help: you might have meant to use field `start` whose type is `{integer}`
12-
|
13-
LL | match [5..4, 99..105, 43..44].start {
14-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1511

1612
error[E0308]: mismatched types
1713
--> $DIR/exclusive_range_pattern_syntax_collision3.rs:6:15
@@ -25,10 +21,6 @@ LL | [..9, 99..100, _] => {},
2521
|
2622
= note: expected struct `std::ops::Range<{integer}>`
2723
found type `{integer}`
28-
help: you might have meant to use field `start` whose type is `{integer}`
29-
|
30-
LL | match [5..4, 99..105, 43..44].start {
31-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3224

3325
error[E0308]: mismatched types
3426
--> $DIR/exclusive_range_pattern_syntax_collision3.rs:6:19
@@ -42,10 +34,6 @@ LL | [..9, 99..100, _] => {},
4234
|
4335
= note: expected struct `std::ops::Range<{integer}>`
4436
found type `{integer}`
45-
help: you might have meant to use field `start` whose type is `{integer}`
46-
|
47-
LL | match [5..4, 99..105, 43..44].start {
48-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4937

5038
error: aborting due to 3 previous errors
5139

Diff for: src/test/ui/match/issue-91058.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct S(());
2+
3+
fn main() {
4+
let array = [S(())];
5+
6+
match array {
7+
[()] => {}
8+
//~^ ERROR mismatched types [E0308]
9+
_ => {}
10+
}
11+
}

Diff for: src/test/ui/match/issue-91058.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-91058.rs:7:10
3+
|
4+
LL | match array {
5+
| ----- this expression has type `[S; 1]`
6+
LL | [()] => {}
7+
| ^^ expected struct `S`, found `()`
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)