Skip to content

Commit bdc6c4d

Browse files
committed
reword pattern migration diagnostic to make sense in all editions
This aligns the main error message a bit more with the phrasing in the Edition Guide and provides a bit more information on the labels to (hopefully!) aid in understanding.
1 parent 724b885 commit bdc6c4d

15 files changed

+224
-196
lines changed

compiler/rustc_hir_typeck/src/pat.rs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
804804

805805
// Determine the binding mode...
806806
let bm = match user_bind_annot {
807-
BindingMode(ByRef::No, Mutability::Mut) if matches!(def_br, ByRef::Yes(_)) => {
807+
BindingMode(ByRef::No, Mutability::Mut) if let ByRef::Yes(def_br_mutbl) = def_br => {
808808
// Only mention the experimental `mut_ref` feature if if we're in edition 2024 and
809809
// using other experimental matching features compatible with it.
810810
if pat.span.at_least_rust_2024()
@@ -826,22 +826,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
826826
// `mut` resets the binding mode on edition <= 2021
827827
self.add_rust_2024_migration_desugared_pat(
828828
pat_info.top_info.hir_id,
829-
pat.span,
829+
pat,
830830
ident.span,
831-
"requires binding by-value, but the implicit default is by-reference",
831+
def_br_mutbl,
832832
);
833833
BindingMode(ByRef::No, Mutability::Mut)
834834
}
835835
}
836836
BindingMode(ByRef::No, mutbl) => BindingMode(def_br, mutbl),
837837
BindingMode(ByRef::Yes(_), _) => {
838-
if matches!(def_br, ByRef::Yes(_)) {
838+
if let ByRef::Yes(def_br_mutbl) = def_br {
839839
// `ref`/`ref mut` overrides the binding mode on edition <= 2021
840840
self.add_rust_2024_migration_desugared_pat(
841841
pat_info.top_info.hir_id,
842-
pat.span,
842+
pat,
843843
ident.span,
844-
"cannot override to bind by-reference when that is the implicit default",
844+
def_br_mutbl,
845845
);
846846
}
847847
user_bind_annot
@@ -2378,9 +2378,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23782378
pat_info.binding_mode = ByRef::No;
23792379
self.add_rust_2024_migration_desugared_pat(
23802380
pat_info.top_info.hir_id,
2381-
pat.span,
2381+
pat,
23822382
inner.span,
2383-
"cannot implicitly match against multiple layers of reference",
2383+
inh_mut,
23842384
)
23852385
}
23862386
}
@@ -2770,33 +2770,40 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
27702770
fn add_rust_2024_migration_desugared_pat(
27712771
&self,
27722772
pat_id: HirId,
2773-
subpat_span: Span,
2773+
subpat: &'tcx Pat<'tcx>,
27742774
cutoff_span: Span,
2775-
detailed_label: &str,
2775+
def_br_mutbl: Mutability,
27762776
) {
27772777
// Try to trim the span we're labeling to just the `&` or binding mode that's an issue.
27782778
// If the subpattern's span is is from an expansion, the emitted label will not be trimmed.
27792779
let source_map = self.tcx.sess.source_map();
27802780
let cutoff_span = source_map
27812781
.span_extend_prev_while(cutoff_span, char::is_whitespace)
27822782
.unwrap_or(cutoff_span);
2783-
// Ensure we use the syntax context and thus edition of `subpat_span`; this will be a hard
2783+
// Ensure we use the syntax context and thus edition of `subpat.span`; this will be a hard
27842784
// error if the subpattern is of edition >= 2024.
2785-
let trimmed_span = subpat_span.until(cutoff_span).with_ctxt(subpat_span.ctxt());
2785+
let trimmed_span = subpat.span.until(cutoff_span).with_ctxt(subpat.span.ctxt());
27862786

27872787
// Only provide a detailed label if the problematic subpattern isn't from an expansion.
27882788
// In the case that it's from a macro, we'll add a more detailed note in the emitter.
2789-
let desc = if subpat_span.from_expansion() {
2790-
"default binding mode is reset within expansion"
2789+
let desc = if subpat.span.from_expansion() {
2790+
"occurs within expansion"
27912791
} else {
2792-
detailed_label
2792+
match def_br_mutbl {
2793+
Mutability::Not => "default binding mode is `ref`",
2794+
Mutability::Mut => "default binding mode is `ref mut`",
2795+
}
27932796
};
27942797

2795-
self.typeck_results
2796-
.borrow_mut()
2797-
.rust_2024_migration_desugared_pats_mut()
2798-
.entry(pat_id)
2799-
.or_default()
2800-
.push((trimmed_span, desc.to_owned()));
2798+
let mut typeck_results = self.typeck_results.borrow_mut();
2799+
let mut table = typeck_results.rust_2024_migration_desugared_pats_mut();
2800+
let info = table.entry(pat_id).or_default();
2801+
2802+
info.labels.push((trimmed_span, desc.to_owned()));
2803+
if matches!(subpat.kind, PatKind::Binding(_, _, _, _)) {
2804+
info.bad_modifiers |= true;
2805+
} else {
2806+
info.bad_ref_pats |= true;
2807+
}
28012808
}
28022809
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub use self::sty::{
9494
pub use self::trait_def::TraitDef;
9595
pub use self::typeck_results::{
9696
CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, IsIdentity,
97-
TypeckResults, UserType, UserTypeAnnotationIndex, UserTypeKind,
97+
Rust2024IncompatiblePatInfo, TypeckResults, UserType, UserTypeAnnotationIndex, UserTypeKind,
9898
};
9999
pub use self::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor};
100100
use crate::error::{OpaqueHiddenTypeMismatch, TypeMismatchReason};

compiler/rustc_middle/src/ty/typeck_results.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ pub struct TypeckResults<'tcx> {
7373
/// Stores the actual binding mode for all instances of [`BindingMode`].
7474
pat_binding_modes: ItemLocalMap<BindingMode>,
7575

76-
/// Top-level patterns whose match ergonomics need to be desugared by the Rust 2021 -> 2024
77-
/// migration lint. Problematic subpatterns are stored in the `Vec` for the lint to highlight.
78-
rust_2024_migration_desugared_pats: ItemLocalMap<Vec<(Span, String)>>,
76+
/// Top-level patterns incompatible with Rust 2024's match ergonomics. These will be translated
77+
/// to a form valid in all Editions, either as a lint diagnostic or hard error.
78+
rust_2024_migration_desugared_pats: ItemLocalMap<Rust2024IncompatiblePatInfo>,
7979

8080
/// Stores the types which were implicitly dereferenced in pattern binding modes
8181
/// for later usage in THIR lowering. For example,
@@ -420,7 +420,7 @@ impl<'tcx> TypeckResults<'tcx> {
420420

421421
pub fn rust_2024_migration_desugared_pats(
422422
&self,
423-
) -> LocalTableInContext<'_, Vec<(Span, String)>> {
423+
) -> LocalTableInContext<'_, Rust2024IncompatiblePatInfo> {
424424
LocalTableInContext {
425425
hir_owner: self.hir_owner,
426426
data: &self.rust_2024_migration_desugared_pats,
@@ -429,7 +429,7 @@ impl<'tcx> TypeckResults<'tcx> {
429429

430430
pub fn rust_2024_migration_desugared_pats_mut(
431431
&mut self,
432-
) -> LocalTableInContextMut<'_, Vec<(Span, String)>> {
432+
) -> LocalTableInContextMut<'_, Rust2024IncompatiblePatInfo> {
433433
LocalTableInContextMut {
434434
hir_owner: self.hir_owner,
435435
data: &mut self.rust_2024_migration_desugared_pats,
@@ -811,3 +811,15 @@ impl<'tcx> std::fmt::Display for UserTypeKind<'tcx> {
811811
}
812812
}
813813
}
814+
815+
/// Information on a pattern incompatible with Rust 2024, for use by the error/migration diagnostic
816+
/// emitted during THIR construction.
817+
#[derive(TyEncodable, TyDecodable, Debug, HashStable, Default)]
818+
pub struct Rust2024IncompatiblePatInfo {
819+
/// Labels for subpatterns incompatible with Rust 2024.
820+
pub labels: Vec<(Span, String)>,
821+
/// Whether any binding modifiers occur under a non-`move` default binding mode.
822+
pub bad_modifiers: bool,
823+
/// Whether any `&` or `&mut` patterns occur under a non-`move` default binding mode.
824+
pub bad_ref_pats: bool,
825+
}

compiler/rustc_mir_build/messages.ftl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,16 @@ mir_build_pointer_pattern = function pointers and raw pointers not derived from
285285
286286
mir_build_privately_uninhabited = pattern `{$witness_1}` is currently uninhabited, but this variant contains private fields which may become inhabited in the future
287287
288-
mir_build_rust_2024_incompatible_pat = this pattern relies on behavior which may change in edition 2024
288+
mir_build_rust_2024_incompatible_pat = {$bad_modifiers ->
289+
*[true] binding modifiers{$bad_ref_pats ->
290+
*[true] {" "}and reference patterns
291+
[false] {""}
292+
}
293+
[false] reference patterns
294+
} may only be written when the default binding mode is `move`{$is_hard_error ->
295+
*[true] {""}
296+
[false] {" "}in Rust 2024
297+
}
289298
290299
mir_build_static_in_pattern = statics cannot be referenced in patterns
291300
.label = can't be used in patterns

compiler/rustc_mir_build/src/errors.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,9 @@ pub(crate) enum MiscPatternSuggestion {
11001100
pub(crate) struct Rust2024IncompatiblePat {
11011101
#[subdiagnostic]
11021102
pub(crate) sugg: Rust2024IncompatiblePatSugg,
1103+
pub(crate) bad_modifiers: bool,
1104+
pub(crate) bad_ref_pats: bool,
1105+
pub(crate) is_hard_error: bool,
11031106
}
11041107

11051108
pub(crate) struct Rust2024IncompatiblePatSugg {

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,42 +44,50 @@ pub(super) fn pat_from_hir<'a, 'tcx>(
4444
typeck_results: &'a ty::TypeckResults<'tcx>,
4545
pat: &'tcx hir::Pat<'tcx>,
4646
) -> Box<Pat<'tcx>> {
47-
let migration_labels = typeck_results.rust_2024_migration_desugared_pats().get(pat.hir_id);
47+
let migration_info = typeck_results.rust_2024_migration_desugared_pats().get(pat.hir_id);
4848
let mut pcx = PatCtxt {
4949
tcx,
5050
typing_env,
5151
typeck_results,
52-
rust_2024_migration_suggestion: migration_labels.and(Some(Rust2024IncompatiblePatSugg {
52+
rust_2024_migration_suggestion: migration_info.and(Some(Rust2024IncompatiblePatSugg {
5353
suggestion: Vec::new(),
5454
ref_pattern_count: 0,
5555
binding_mode_count: 0,
5656
})),
5757
};
5858
let result = pcx.lower_pattern(pat);
5959
debug!("pat_from_hir({:?}) = {:?}", pat, result);
60-
if let Some(labels) = migration_labels {
60+
if let Some(info) = migration_info {
6161
let sugg = pcx.rust_2024_migration_suggestion.expect("suggestion should be present");
62-
let mut spans = MultiSpan::from_spans(labels.iter().map(|(span, _)| *span).collect());
63-
for (span, label) in labels {
62+
let mut spans = MultiSpan::from_spans(info.labels.iter().map(|(span, _)| *span).collect());
63+
for (span, label) in &info.labels {
6464
spans.push_span_label(*span, label.clone());
6565
}
6666
// If a relevant span is from at least edition 2024, this is a hard error.
6767
let is_hard_error = spans.primary_spans().iter().any(|span| span.at_least_rust_2024());
6868
if is_hard_error {
6969
let mut err =
7070
tcx.dcx().struct_span_err(spans, fluent::mir_build_rust_2024_incompatible_pat);
71-
if let Some(info) = lint::builtin::RUST_2024_INCOMPATIBLE_PAT.future_incompatible {
71+
if let Some(lint_info) = lint::builtin::RUST_2024_INCOMPATIBLE_PAT.future_incompatible {
7272
// provide the same reference link as the lint
73-
err.note(format!("for more information, see {}", info.reference));
73+
err.note(format!("for more information, see {}", lint_info.reference));
7474
}
75+
err.arg("bad_modifiers", info.bad_modifiers);
76+
err.arg("bad_ref_pats", info.bad_ref_pats);
77+
err.arg("is_hard_error", true);
7578
err.subdiagnostic(sugg);
7679
err.emit();
7780
} else {
7881
tcx.emit_node_span_lint(
7982
lint::builtin::RUST_2024_INCOMPATIBLE_PAT,
8083
pat.hir_id,
8184
spans,
82-
Rust2024IncompatiblePat { sugg },
85+
Rust2024IncompatiblePat {
86+
sugg,
87+
bad_modifiers: info.bad_modifiers,
88+
bad_ref_pats: info.bad_ref_pats,
89+
is_hard_error,
90+
},
8391
);
8492
}
8593
}

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.classic2024.stderr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0308]: mismatched types
2-
--> $DIR/ref-binding-on-inh-ref-errors.rs:60:10
2+
--> $DIR/ref-binding-on-inh-ref-errors.rs:54:10
33
|
44
LL | let [&mut ref x] = &[&mut 0];
55
| ^^^^^
@@ -10,11 +10,11 @@ help: replace this `&mut` pattern with `&`
1010
LL | let [&ref x] = &[&mut 0];
1111
| ~
1212

13-
error: this pattern relies on behavior which may change in edition 2024
14-
--> $DIR/ref-binding-on-inh-ref-errors.rs:74:10
13+
error: binding modifiers may only be written when the default binding mode is `move`
14+
--> $DIR/ref-binding-on-inh-ref-errors.rs:67:10
1515
|
1616
LL | let [ref mut x] = &[0];
17-
| ^^^^^^^ cannot override to bind by-reference when that is the implicit default
17+
| ^^^^^^^ default binding mode is `ref`
1818
|
1919
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
2020
help: make the implied reference pattern explicit
@@ -23,40 +23,40 @@ LL | let &[ref mut x] = &[0];
2323
| +
2424

2525
error[E0596]: cannot borrow data in a `&` reference as mutable
26-
--> $DIR/ref-binding-on-inh-ref-errors.rs:74:10
26+
--> $DIR/ref-binding-on-inh-ref-errors.rs:67:10
2727
|
2828
LL | let [ref mut x] = &[0];
2929
| ^^^^^^^^^ cannot borrow as mutable
3030

31-
error: this pattern relies on behavior which may change in edition 2024
32-
--> $DIR/ref-binding-on-inh-ref-errors.rs:83:10
31+
error: binding modifiers may only be written when the default binding mode is `move`
32+
--> $DIR/ref-binding-on-inh-ref-errors.rs:75:10
3333
|
3434
LL | let [ref x] = &[0];
35-
| ^^^ cannot override to bind by-reference when that is the implicit default
35+
| ^^^ default binding mode is `ref`
3636
|
3737
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
3838
help: make the implied reference pattern explicit
3939
|
4040
LL | let &[ref x] = &[0];
4141
| +
4242

43-
error: this pattern relies on behavior which may change in edition 2024
44-
--> $DIR/ref-binding-on-inh-ref-errors.rs:88:10
43+
error: binding modifiers may only be written when the default binding mode is `move`
44+
--> $DIR/ref-binding-on-inh-ref-errors.rs:79:10
4545
|
4646
LL | let [ref x] = &mut [0];
47-
| ^^^ cannot override to bind by-reference when that is the implicit default
47+
| ^^^ default binding mode is `ref mut`
4848
|
4949
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
5050
help: make the implied reference pattern explicit
5151
|
5252
LL | let &mut [ref x] = &mut [0];
5353
| ++++
5454

55-
error: this pattern relies on behavior which may change in edition 2024
56-
--> $DIR/ref-binding-on-inh-ref-errors.rs:93:10
55+
error: binding modifiers may only be written when the default binding mode is `move`
56+
--> $DIR/ref-binding-on-inh-ref-errors.rs:83:10
5757
|
5858
LL | let [ref mut x] = &mut [0];
59-
| ^^^^^^^ cannot override to bind by-reference when that is the implicit default
59+
| ^^^^^^^ default binding mode is `ref mut`
6060
|
6161
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
6262
help: make the implied reference pattern explicit

0 commit comments

Comments
 (0)