Skip to content

clarify wording of match ergonomics diagnostics (rust_2024_incompatible_pat lint and error) #144006

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 1 addition & 5 deletions compiler/rustc_hir_typeck/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3145,11 +3145,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// so, we may want to inspect the span's source callee or macro backtrace.
"occurs within macro expansion".to_owned()
} else {
let dbm_str = match def_br_mutbl {
Mutability::Not => "ref",
Mutability::Mut => "ref mut",
};
format!("{pat_kind} not allowed under `{dbm_str}` default binding mode")
format!("explicit {pat_kind} not allowed within elided reference pattern")
};
info.primary_labels.push((trimmed_span, primary_label));
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,7 @@ declare_lint! {
"detects patterns whose meaning will change in Rust 2024",
@future_incompatible = FutureIncompatibleInfo {
reason: FutureIncompatibilityReason::EditionSemanticsChange(Edition::Edition2024),
reference: "<https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>",
reference: "<https://doc.rust-lang.org/edition-guide/rust-2024/match-ergonomics.html>",
};
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_build/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,13 @@ mir_build_pointer_pattern = function pointers and raw pointers not derived from

mir_build_privately_uninhabited = pattern `{$witness_1}` is currently uninhabited, but this variant contains private fields which may become inhabited in the future

mir_build_rust_2024_incompatible_pat = {$bad_modifiers ->
mir_build_rust_2024_incompatible_pat = explicit {$bad_modifiers ->
*[true] binding modifiers{$bad_ref_pats ->
*[true] {" "}and reference patterns
[false] {""}
}
[false] reference patterns
} may only be written when the default binding mode is `move`{$is_hard_error ->
} may not be written within elided reference patterns{$is_hard_error ->
*[true] {""}
[false] {" "}in Rust 2024
}
Expand Down
22 changes: 15 additions & 7 deletions compiler/rustc_mir_build/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,7 @@ pub(crate) struct Rust2024IncompatiblePatSugg {
pub(crate) suggestion: Vec<(Span, String)>,
pub(crate) ref_pattern_count: usize,
pub(crate) binding_mode_count: usize,
pub(crate) bad_modifiers: bool,
/// Labels for where incompatibility-causing by-ref default binding modes were introduced.
pub(crate) default_mode_labels: FxIndexMap<Span, ty::Mutability>,
}
Expand All @@ -1124,12 +1125,19 @@ impl Subdiagnostic for Rust2024IncompatiblePatSugg {
for (span, def_br_mutbl) in self.default_mode_labels.into_iter().rev() {
// Don't point to a macro call site.
if !span.from_expansion() {
let note_msg = "matching on a reference type with a non-reference pattern changes the default binding mode";
let label_msg =
format!("this matches on type `{}_`", def_br_mutbl.ref_prefix_str());
let mut label = MultiSpan::from(span);
label.push_span_label(span, label_msg);
diag.span_note(label, note_msg);
let label_msg = format!(
"this matches on a reference type `{}_` without a reference pattern",
def_br_mutbl.ref_prefix_str()
);
if self.bad_modifiers {
// Only explain default binding modes if pointing to redundant/conflicting modifiers.
let note_msg = "matching on a reference type with a non-reference pattern makes variables within bind by reference";
let mut label = MultiSpan::from(span);
label.push_span_label(span, label_msg);
diag.span_note(label, note_msg);
} else {
diag.span_label(span, label_msg);
}
}
}

Expand All @@ -1150,7 +1158,7 @@ impl Subdiagnostic for Rust2024IncompatiblePatSugg {
} else {
String::new()
};
format!("make the implied reference pattern{plural_derefs}{and_modes} explicit")
format!("make the elided reference pattern{plural_derefs}{and_modes} explicit")
};
// FIXME(dianne): for peace of mind, don't risk emitting a 0-part suggestion (that panics!)
if !self.suggestion.is_empty() {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_mir_build/src/thir/pattern/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl<'a> PatMigration<'a> {
suggestion: self.suggestion,
ref_pattern_count: self.ref_pattern_count,
binding_mode_count: self.binding_mode_count,
bad_modifiers: self.info.bad_modifiers,
default_mode_labels: self.default_mode_labels,
};
// If a relevant span is from at least edition 2024, this is a hard error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,20 @@ LL | let [&bind_ref_mut!(x)] = &mut [0];
| |
| help: replace this `&` with `&mut`: `&mut`

error: binding modifiers may only be written when the default binding mode is `move`
error: explicit binding modifiers may not be written within elided reference patterns
--> $DIR/mixed-editions.rs:30:10
|
LL | let [bind_ref!(y)] = &[0];
| ^^^^^^^^^^^^ occurs within macro expansion
|
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
note: matching on a reference type with a non-reference pattern changes the default binding mode
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/match-ergonomics.html>
note: matching on a reference type with a non-reference pattern makes variables within bind by reference
--> $DIR/mixed-editions.rs:30:9
|
LL | let [bind_ref!(y)] = &[0];
| ^^^^^^^^^^^^^^ this matches on type `&_`
| ^^^^^^^^^^^^^^ this matches on a reference type `&_` without a reference pattern
= note: this error originates in the macro `bind_ref` (in Nightly builds, run with -Z macro-backtrace for more info)
help: make the implied reference pattern explicit
help: make the elided reference pattern explicit
|
LL | let &[bind_ref!(y)] = &[0];
| +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ LL | let [&bind_ref_mut!(x)] = &mut [0];
| |
| help: replace this `&` with `&mut`: `&mut`

error: binding modifiers may only be written when the default binding mode is `move`
error: explicit binding modifiers may not be written within elided reference patterns
--> $DIR/mixed-editions.rs:26:21
|
LL | let match_ctor!(ref x) = &[0];
| ^^^ binding modifier not allowed under `ref` default binding mode
| ^^^ explicit binding modifier not allowed within elided reference pattern
|
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
help: make the implied reference pattern explicit
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/match-ergonomics.html>
help: make the elided reference pattern explicit
--> $DIR/auxiliary/mixed-editions-macros.rs:11:9
|
LL | &[$p]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ fn assert_type_eq<T, U: Eq<T>>(_: T, _: U) {}
/// only when the binding is from edition 2024.
fn ref_binding_tests() {
let match_ctor!(ref x) = &[0];
//[classic2024,structural2024]~^ ERROR: binding modifiers may only be written when the default binding mode is `move`
//[classic2024,structural2024]~^ ERROR: explicit binding modifiers may not be written within elided reference patterns
#[cfg(any(classic2021, structural2021))] assert_type_eq(x, &0u32);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ LL | let [&bind_ref_mut!(x)] = &mut [0];
| |
| help: replace this `&` with `&mut`: `&mut`

error: binding modifiers may only be written when the default binding mode is `move`
error: explicit binding modifiers may not be written within elided reference patterns
--> $DIR/mixed-editions.rs:30:10
|
LL | let [bind_ref!(y)] = &[0];
| ^^^^^^^^^^^^ occurs within macro expansion
|
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
note: matching on a reference type with a non-reference pattern changes the default binding mode
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/match-ergonomics.html>
note: matching on a reference type with a non-reference pattern makes variables within bind by reference
--> $DIR/mixed-editions.rs:30:9
|
LL | let [bind_ref!(y)] = &[0];
| ^^^^^^^^^^^^^^ this matches on type `&_`
| ^^^^^^^^^^^^^^ this matches on a reference type `&_` without a reference pattern
= note: this error originates in the macro `bind_ref` (in Nightly builds, run with -Z macro-backtrace for more info)
help: make the implied reference pattern explicit
help: make the elided reference pattern explicit
|
LL | let &[bind_ref!(y)] = &[0];
| +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ LL | let [&bind_ref_mut!(x)] = &mut [0];
| |
| help: replace this `&` with `&mut`: `&mut`

error: binding modifiers may only be written when the default binding mode is `move`
error: explicit binding modifiers may not be written within elided reference patterns
--> $DIR/mixed-editions.rs:26:21
|
LL | let match_ctor!(ref x) = &[0];
| ^^^ binding modifier not allowed under `ref` default binding mode
| ^^^ explicit binding modifier not allowed within elided reference pattern
|
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
help: make the implied reference pattern explicit
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/match-ergonomics.html>
help: make the elided reference pattern explicit
--> $DIR/auxiliary/mixed-editions-macros.rs:11:9
|
LL | &[$p]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ LL - let [&mut ref x] = &[&mut 0];
LL + let [&ref x] = &[&mut 0];
|

error: binding modifiers may only be written when the default binding mode is `move`
error: explicit binding modifiers may not be written within elided reference patterns
--> $DIR/ref-binding-on-inh-ref-errors.rs:73:10
|
LL | let [ref mut x] = &[0];
| ^^^^^^^ binding modifier not allowed under `ref` default binding mode
| ^^^^^^^ explicit binding modifier not allowed within elided reference pattern
|
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
note: matching on a reference type with a non-reference pattern changes the default binding mode
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/match-ergonomics.html>
note: matching on a reference type with a non-reference pattern makes variables within bind by reference
--> $DIR/ref-binding-on-inh-ref-errors.rs:73:9
|
LL | let [ref mut x] = &[0];
| ^^^^^^^^^^^ this matches on type `&_`
help: make the implied reference pattern explicit
| ^^^^^^^^^^^ this matches on a reference type `&_` without a reference pattern
help: make the elided reference pattern explicit
|
LL | let &[ref mut x] = &[0];
| +
Expand All @@ -34,53 +34,53 @@ error[E0596]: cannot borrow data in a `&` reference as mutable
LL | let [ref mut x] = &[0];
| ^^^^^^^^^ cannot borrow as mutable

error: binding modifiers may only be written when the default binding mode is `move`
error: explicit binding modifiers may not be written within elided reference patterns
--> $DIR/ref-binding-on-inh-ref-errors.rs:81:10
|
LL | let [ref x] = &[0];
| ^^^ binding modifier not allowed under `ref` default binding mode
| ^^^ explicit binding modifier not allowed within elided reference pattern
|
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
note: matching on a reference type with a non-reference pattern changes the default binding mode
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/match-ergonomics.html>
note: matching on a reference type with a non-reference pattern makes variables within bind by reference
--> $DIR/ref-binding-on-inh-ref-errors.rs:81:9
|
LL | let [ref x] = &[0];
| ^^^^^^^ this matches on type `&_`
help: make the implied reference pattern explicit
| ^^^^^^^ this matches on a reference type `&_` without a reference pattern
help: make the elided reference pattern explicit
|
LL | let &[ref x] = &[0];
| +

error: binding modifiers may only be written when the default binding mode is `move`
error: explicit binding modifiers may not be written within elided reference patterns
--> $DIR/ref-binding-on-inh-ref-errors.rs:85:10
|
LL | let [ref x] = &mut [0];
| ^^^ binding modifier not allowed under `ref mut` default binding mode
| ^^^ explicit binding modifier not allowed within elided reference pattern
|
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
note: matching on a reference type with a non-reference pattern changes the default binding mode
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/match-ergonomics.html>
note: matching on a reference type with a non-reference pattern makes variables within bind by reference
--> $DIR/ref-binding-on-inh-ref-errors.rs:85:9
|
LL | let [ref x] = &mut [0];
| ^^^^^^^ this matches on type `&mut _`
help: make the implied reference pattern explicit
| ^^^^^^^ this matches on a reference type `&mut _` without a reference pattern
help: make the elided reference pattern explicit
|
LL | let &mut [ref x] = &mut [0];
| ++++

error: binding modifiers may only be written when the default binding mode is `move`
error: explicit binding modifiers may not be written within elided reference patterns
--> $DIR/ref-binding-on-inh-ref-errors.rs:89:10
|
LL | let [ref mut x] = &mut [0];
| ^^^^^^^ binding modifier not allowed under `ref mut` default binding mode
| ^^^^^^^ explicit binding modifier not allowed within elided reference pattern
|
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
note: matching on a reference type with a non-reference pattern changes the default binding mode
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/match-ergonomics.html>
note: matching on a reference type with a non-reference pattern makes variables within bind by reference
--> $DIR/ref-binding-on-inh-ref-errors.rs:89:9
|
LL | let [ref mut x] = &mut [0];
| ^^^^^^^^^^^ this matches on type `&mut _`
help: make the implied reference pattern explicit
| ^^^^^^^^^^^ this matches on a reference type `&mut _` without a reference pattern
help: make the elided reference pattern explicit
|
LL | let &mut [ref mut x] = &mut [0];
| ++++
Expand Down
Loading
Loading