Skip to content

Commit 6751630

Browse files
committed
clarify wording of match ergonomics diagnostics
1 parent 3014e79 commit 6751630

18 files changed

+467
-517
lines changed

compiler/rustc_hir_typeck/src/pat.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3145,11 +3145,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
31453145
// so, we may want to inspect the span's source callee or macro backtrace.
31463146
"occurs within macro expansion".to_owned()
31473147
} else {
3148-
let dbm_str = match def_br_mutbl {
3149-
Mutability::Not => "ref",
3150-
Mutability::Mut => "ref mut",
3151-
};
3152-
format!("{pat_kind} not allowed under `{dbm_str}` default binding mode")
3148+
format!("explicit {pat_kind} not allowed within elided reference pattern")
31533149
};
31543150
info.primary_labels.push((trimmed_span, primary_label));
31553151
}

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,7 @@ declare_lint! {
15991599
"detects patterns whose meaning will change in Rust 2024",
16001600
@future_incompatible = FutureIncompatibleInfo {
16011601
reason: FutureIncompatibilityReason::EditionSemanticsChange(Edition::Edition2024),
1602-
reference: "<https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>",
1602+
reference: "<https://doc.rust-lang.org/edition-guide/rust-2024/match-ergonomics.html>",
16031603
};
16041604
}
16051605

compiler/rustc_mir_build/messages.ftl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,13 @@ mir_build_pointer_pattern = function pointers and raw pointers not derived from
318318
319319
mir_build_privately_uninhabited = pattern `{$witness_1}` is currently uninhabited, but this variant contains private fields which may become inhabited in the future
320320
321-
mir_build_rust_2024_incompatible_pat = {$bad_modifiers ->
321+
mir_build_rust_2024_incompatible_pat = explicit {$bad_modifiers ->
322322
*[true] binding modifiers{$bad_ref_pats ->
323323
*[true] {" "}and reference patterns
324324
[false] {""}
325325
}
326326
[false] reference patterns
327-
} may only be written when the default binding mode is `move`{$is_hard_error ->
327+
} may not be written within elided reference patterns{$is_hard_error ->
328328
*[true] {""}
329329
[false] {" "}in Rust 2024
330330
}

compiler/rustc_mir_build/src/errors.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,7 @@ pub(crate) struct Rust2024IncompatiblePatSugg {
11131113
pub(crate) suggestion: Vec<(Span, String)>,
11141114
pub(crate) ref_pattern_count: usize,
11151115
pub(crate) binding_mode_count: usize,
1116+
pub(crate) bad_modifiers: bool,
11161117
/// Labels for where incompatibility-causing by-ref default binding modes were introduced.
11171118
pub(crate) default_mode_labels: FxIndexMap<Span, ty::Mutability>,
11181119
}
@@ -1124,12 +1125,19 @@ impl Subdiagnostic for Rust2024IncompatiblePatSugg {
11241125
for (span, def_br_mutbl) in self.default_mode_labels.into_iter().rev() {
11251126
// Don't point to a macro call site.
11261127
if !span.from_expansion() {
1127-
let note_msg = "matching on a reference type with a non-reference pattern changes the default binding mode";
1128-
let label_msg =
1129-
format!("this matches on type `{}_`", def_br_mutbl.ref_prefix_str());
1130-
let mut label = MultiSpan::from(span);
1131-
label.push_span_label(span, label_msg);
1132-
diag.span_note(label, note_msg);
1128+
let label_msg = format!(
1129+
"this matches on a reference type `{}_` without a reference pattern",
1130+
def_br_mutbl.ref_prefix_str()
1131+
);
1132+
if self.bad_modifiers {
1133+
// Only explain default binding modes if pointing to redundant/conflicting modifiers.
1134+
let note_msg = "matching on a reference type with a non-reference pattern makes variables within bind by reference";
1135+
let mut label = MultiSpan::from(span);
1136+
label.push_span_label(span, label_msg);
1137+
diag.span_note(label, note_msg);
1138+
} else {
1139+
diag.span_label(span, label_msg);
1140+
}
11331141
}
11341142
}
11351143

@@ -1150,7 +1158,7 @@ impl Subdiagnostic for Rust2024IncompatiblePatSugg {
11501158
} else {
11511159
String::new()
11521160
};
1153-
format!("make the implied reference pattern{plural_derefs}{and_modes} explicit")
1161+
format!("make the elided reference pattern{plural_derefs}{and_modes} explicit")
11541162
};
11551163
// FIXME(dianne): for peace of mind, don't risk emitting a 0-part suggestion (that panics!)
11561164
if !self.suggestion.is_empty() {

compiler/rustc_mir_build/src/thir/pattern/migration.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ impl<'a> PatMigration<'a> {
5454
suggestion: self.suggestion,
5555
ref_pattern_count: self.ref_pattern_count,
5656
binding_mode_count: self.binding_mode_count,
57+
bad_modifiers: self.info.bad_modifiers,
5758
default_mode_labels: self.default_mode_labels,
5859
};
5960
// If a relevant span is from at least edition 2024, this is a hard error.

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.classic2021.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,20 @@ LL | let [&bind_ref_mut!(x)] = &mut [0];
5959
| |
6060
| help: replace this `&` with `&mut`: `&mut`
6161

62-
error: binding modifiers may only be written when the default binding mode is `move`
62+
error: explicit binding modifiers may not be written within elided reference patterns
6363
--> $DIR/mixed-editions.rs:30:10
6464
|
6565
LL | let [bind_ref!(y)] = &[0];
6666
| ^^^^^^^^^^^^ occurs within macro expansion
6767
|
68-
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
69-
note: matching on a reference type with a non-reference pattern changes the default binding mode
68+
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/match-ergonomics.html>
69+
note: matching on a reference type with a non-reference pattern makes variables within bind by reference
7070
--> $DIR/mixed-editions.rs:30:9
7171
|
7272
LL | let [bind_ref!(y)] = &[0];
73-
| ^^^^^^^^^^^^^^ this matches on type `&_`
73+
| ^^^^^^^^^^^^^^ this matches on a reference type `&_` without a reference pattern
7474
= note: this error originates in the macro `bind_ref` (in Nightly builds, run with -Z macro-backtrace for more info)
75-
help: make the implied reference pattern explicit
75+
help: make the elided reference pattern explicit
7676
|
7777
LL | let &[bind_ref!(y)] = &[0];
7878
| +

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.classic2024.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ LL | let [&bind_ref_mut!(x)] = &mut [0];
5858
| |
5959
| help: replace this `&` with `&mut`: `&mut`
6060

61-
error: binding modifiers may only be written when the default binding mode is `move`
61+
error: explicit binding modifiers may not be written within elided reference patterns
6262
--> $DIR/mixed-editions.rs:26:21
6363
|
6464
LL | let match_ctor!(ref x) = &[0];
65-
| ^^^ binding modifier not allowed under `ref` default binding mode
65+
| ^^^ explicit binding modifier not allowed within elided reference pattern
6666
|
67-
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
68-
help: make the implied reference pattern explicit
67+
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/match-ergonomics.html>
68+
help: make the elided reference pattern explicit
6969
--> $DIR/auxiliary/mixed-editions-macros.rs:11:9
7070
|
7171
LL | &[$p]

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ fn assert_type_eq<T, U: Eq<T>>(_: T, _: U) {}
2424
/// only when the binding is from edition 2024.
2525
fn ref_binding_tests() {
2626
let match_ctor!(ref x) = &[0];
27-
//[classic2024,structural2024]~^ ERROR: binding modifiers may only be written when the default binding mode is `move`
27+
//[classic2024,structural2024]~^ ERROR: explicit binding modifiers may not be written within elided reference patterns
2828
#[cfg(any(classic2021, structural2021))] assert_type_eq(x, &0u32);
2929

3030
let [bind_ref!(y)] = &[0];
31-
//[classic2021,structural2021]~^ ERROR: binding modifiers may only be written when the default binding mode is `move`
31+
//[classic2021,structural2021]~^ ERROR: explicit binding modifiers may not be written within elided reference patterns
3232
#[cfg(any(classic2024, structural2024))] assert_type_eq(y, &0u32);
3333
}
3434

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.structural2021.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,20 @@ LL | let [&bind_ref_mut!(x)] = &mut [0];
3737
| |
3838
| help: replace this `&` with `&mut`: `&mut`
3939

40-
error: binding modifiers may only be written when the default binding mode is `move`
40+
error: explicit binding modifiers may not be written within elided reference patterns
4141
--> $DIR/mixed-editions.rs:30:10
4242
|
4343
LL | let [bind_ref!(y)] = &[0];
4444
| ^^^^^^^^^^^^ occurs within macro expansion
4545
|
46-
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
47-
note: matching on a reference type with a non-reference pattern changes the default binding mode
46+
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/match-ergonomics.html>
47+
note: matching on a reference type with a non-reference pattern makes variables within bind by reference
4848
--> $DIR/mixed-editions.rs:30:9
4949
|
5050
LL | let [bind_ref!(y)] = &[0];
51-
| ^^^^^^^^^^^^^^ this matches on type `&_`
51+
| ^^^^^^^^^^^^^^ this matches on a reference type `&_` without a reference pattern
5252
= note: this error originates in the macro `bind_ref` (in Nightly builds, run with -Z macro-backtrace for more info)
53-
help: make the implied reference pattern explicit
53+
help: make the elided reference pattern explicit
5454
|
5555
LL | let &[bind_ref!(y)] = &[0];
5656
| +

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.structural2024.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ LL | let [&bind_ref_mut!(x)] = &mut [0];
3636
| |
3737
| help: replace this `&` with `&mut`: `&mut`
3838

39-
error: binding modifiers may only be written when the default binding mode is `move`
39+
error: explicit binding modifiers may not be written within elided reference patterns
4040
--> $DIR/mixed-editions.rs:26:21
4141
|
4242
LL | let match_ctor!(ref x) = &[0];
43-
| ^^^ binding modifier not allowed under `ref` default binding mode
43+
| ^^^ explicit binding modifier not allowed within elided reference pattern
4444
|
45-
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
46-
help: make the implied reference pattern explicit
45+
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/match-ergonomics.html>
46+
help: make the elided reference pattern explicit
4747
--> $DIR/auxiliary/mixed-editions-macros.rs:11:9
4848
|
4949
LL | &[$p]

0 commit comments

Comments
 (0)