From ef1d084c0b5b0ff7143bbca966442cd24151313b Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Fri, 5 Apr 2024 23:27:29 -0400 Subject: [PATCH 1/7] Match ergonomics 2024: `mut` doesn't reset binding mode --- compiler/rustc_feature/src/unstable.rs | 2 ++ compiler/rustc_hir_typeck/src/pat.rs | 16 +++++++--- compiler/rustc_span/src/symbol.rs | 1 + ...e-gate-mut_dont_reset_binding_mode_2024.rs | 14 +++++++++ ...te-mut_dont_reset_binding_mode_2024.stderr | 31 +++++++++++++++++++ .../mut_dont_reset_binding_mode_2021.rs | 15 +++++++++ .../mut_dont_reset_binding_mode_2021.stderr | 31 +++++++++++++++++++ .../mut_dont_reset_binding_mode_2024.rs | 15 +++++++++ 8 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 tests/ui/pattern/feature-gate-mut_dont_reset_binding_mode_2024.rs create mode 100644 tests/ui/pattern/feature-gate-mut_dont_reset_binding_mode_2024.stderr create mode 100644 tests/ui/pattern/mut_dont_reset_binding_mode_2021.rs create mode 100644 tests/ui/pattern/mut_dont_reset_binding_mode_2021.stderr create mode 100644 tests/ui/pattern/mut_dont_reset_binding_mode_2024.rs diff --git a/compiler/rustc_feature/src/unstable.rs b/compiler/rustc_feature/src/unstable.rs index e6b19817de385..2a753af938721 100644 --- a/compiler/rustc_feature/src/unstable.rs +++ b/compiler/rustc_feature/src/unstable.rs @@ -533,6 +533,8 @@ declare_features! ( (unstable, more_qualified_paths, "1.54.0", Some(86935)), /// Allows the `#[must_not_suspend]` attribute. (unstable, must_not_suspend, "1.57.0", Some(83310)), + /// Make `mut` not reset the binding mode on edition >= 2024. + (unstable, mut_dont_reset_binding_mode_2024, "CURRENT_RUSTC_VERSION", Some(123076)), /// Allows `mut ref` and `mut ref mut` identifier patterns. (incomplete, mut_ref, "CURRENT_RUSTC_VERSION", Some(123076)), /// Allows using `#[naked]` on functions. diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index db4bd132b7e30..06f6a7f6bf282 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -629,12 +629,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { expected: Ty<'tcx>, pat_info: PatInfo<'tcx, '_>, ) -> Ty<'tcx> { - let PatInfo { binding_mode: def_bm, top_info: ti, .. } = pat_info; + let PatInfo { binding_mode: BindingAnnotation(def_br, _), top_info: ti, .. } = pat_info; // Determine the binding mode... let bm = match ba { - BindingAnnotation(ByRef::No, Mutability::Not) => def_bm, - _ => ba, + BindingAnnotation(ByRef::No, Mutability::Mut) + if !(pat.span.at_least_rust_2024() + && self.tcx.features().mut_dont_reset_binding_mode_2024) + && matches!(def_br, ByRef::Yes(_)) => + { + // `mut x` resets the binding mode in edition <= 2021. + BindingAnnotation(ByRef::No, Mutability::Mut) + } + BindingAnnotation(ByRef::No, mutbl) => BindingAnnotation(def_br, mutbl), + BindingAnnotation(ByRef::Yes(_), _) => ba, }; // ...and store it in a side table: self.typeck_results.borrow_mut().pat_binding_modes_mut().insert(pat.hir_id, bm); @@ -743,7 +751,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } - // Precondition: pat is a Ref(_) pattern + /// Precondition: pat is a `Ref(_)` pattern fn borrow_pat_suggestion(&self, err: &mut Diag<'_>, pat: &Pat<'_>) { let tcx = self.tcx; if let PatKind::Ref(inner, mutbl) = pat.kind diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index bfd0f77c237b2..02550fd655cb2 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1194,6 +1194,7 @@ symbols! { multiple_supertrait_upcastable, must_not_suspend, must_use, + mut_dont_reset_binding_mode_2024, mut_ref, naked, naked_functions, diff --git a/tests/ui/pattern/feature-gate-mut_dont_reset_binding_mode_2024.rs b/tests/ui/pattern/feature-gate-mut_dont_reset_binding_mode_2024.rs new file mode 100644 index 0000000000000..15c542e6bf104 --- /dev/null +++ b/tests/ui/pattern/feature-gate-mut_dont_reset_binding_mode_2024.rs @@ -0,0 +1,14 @@ +//@ edition: 2024 +//@ compile-flags: -Zunstable-options + +struct Foo(u8); + +fn main() { + let Foo(mut a) = &Foo(0); + a = &42; + //~^ ERROR: mismatched types + + let Foo(mut a) = &mut Foo(0); + a = &mut 42; + //~^ ERROR: mismatched types +} diff --git a/tests/ui/pattern/feature-gate-mut_dont_reset_binding_mode_2024.stderr b/tests/ui/pattern/feature-gate-mut_dont_reset_binding_mode_2024.stderr new file mode 100644 index 0000000000000..1624883de60c1 --- /dev/null +++ b/tests/ui/pattern/feature-gate-mut_dont_reset_binding_mode_2024.stderr @@ -0,0 +1,31 @@ +error[E0308]: mismatched types + --> $DIR/feature-gate-mut_dont_reset_binding_mode_2024.rs:8:9 + | +LL | let Foo(mut a) = &Foo(0); + | ----- expected due to the type of this binding +LL | a = &42; + | ^^^ expected `u8`, found `&{integer}` + | +help: consider removing the borrow + | +LL - a = &42; +LL + a = 42; + | + +error[E0308]: mismatched types + --> $DIR/feature-gate-mut_dont_reset_binding_mode_2024.rs:12:9 + | +LL | let Foo(mut a) = &mut Foo(0); + | ----- expected due to the type of this binding +LL | a = &mut 42; + | ^^^^^^^ expected `u8`, found `&mut {integer}` + | +help: consider removing the borrow + | +LL - a = &mut 42; +LL + a = 42; + | + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/pattern/mut_dont_reset_binding_mode_2021.rs b/tests/ui/pattern/mut_dont_reset_binding_mode_2021.rs new file mode 100644 index 0000000000000..a9e12472734f0 --- /dev/null +++ b/tests/ui/pattern/mut_dont_reset_binding_mode_2021.rs @@ -0,0 +1,15 @@ +//@ edition: 2021 +//@ compile-flags: -Zunstable-options +#![feature(mut_dont_reset_binding_mode_2024)] + +struct Foo(u8); + +fn main() { + let Foo(mut a) = &Foo(0); + a = &42; + //~^ ERROR: mismatched types + + let Foo(mut a) = &mut Foo(0); + a = &mut 42; + //~^ ERROR: mismatched types +} diff --git a/tests/ui/pattern/mut_dont_reset_binding_mode_2021.stderr b/tests/ui/pattern/mut_dont_reset_binding_mode_2021.stderr new file mode 100644 index 0000000000000..16818c900b342 --- /dev/null +++ b/tests/ui/pattern/mut_dont_reset_binding_mode_2021.stderr @@ -0,0 +1,31 @@ +error[E0308]: mismatched types + --> $DIR/mut_dont_reset_binding_mode_2021.rs:9:9 + | +LL | let Foo(mut a) = &Foo(0); + | ----- expected due to the type of this binding +LL | a = &42; + | ^^^ expected `u8`, found `&{integer}` + | +help: consider removing the borrow + | +LL - a = &42; +LL + a = 42; + | + +error[E0308]: mismatched types + --> $DIR/mut_dont_reset_binding_mode_2021.rs:13:9 + | +LL | let Foo(mut a) = &mut Foo(0); + | ----- expected due to the type of this binding +LL | a = &mut 42; + | ^^^^^^^ expected `u8`, found `&mut {integer}` + | +help: consider removing the borrow + | +LL - a = &mut 42; +LL + a = 42; + | + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/pattern/mut_dont_reset_binding_mode_2024.rs b/tests/ui/pattern/mut_dont_reset_binding_mode_2024.rs new file mode 100644 index 0000000000000..9ac5ec50c74c9 --- /dev/null +++ b/tests/ui/pattern/mut_dont_reset_binding_mode_2024.rs @@ -0,0 +1,15 @@ +//@ run-pass +//@ edition: 2024 +//@ compile-flags: -Zunstable-options +#![feature(mut_dont_reset_binding_mode_2024)] +#![allow(unused)] + +struct Foo(u8); + +fn main() { + let Foo(mut a) = &Foo(0); + a = &42; + + let Foo(mut a) = &mut Foo(0); + a = &mut 42; +} From 83f330fbd45be04a693e89e9602f0ab2ffab16e2 Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Fri, 5 Apr 2024 23:28:34 -0400 Subject: [PATCH 2/7] Migration lint Rustfix remains TODO --- compiler/rustc_hir_typeck/messages.ftl | 4 +++ compiler/rustc_hir_typeck/src/errors.rs | 8 +++++ compiler/rustc_hir_typeck/src/pat.rs | 7 ++++ compiler/rustc_lint_defs/src/builtin.rs | 36 +++++++++++++++++++ .../mut_dont_reset_binding_mode_2024_lint.rs | 18 ++++++++++ ...t_dont_reset_binding_mode_2024_lint.stderr | 35 ++++++++++++++++++ 6 files changed, 108 insertions(+) create mode 100644 tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.rs create mode 100644 tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.stderr diff --git a/compiler/rustc_hir_typeck/messages.ftl b/compiler/rustc_hir_typeck/messages.ftl index 18d9d739dd696..07b4948872dd8 100644 --- a/compiler/rustc_hir_typeck/messages.ftl +++ b/compiler/rustc_hir_typeck/messages.ftl @@ -46,6 +46,10 @@ hir_typeck_ctor_is_private = tuple struct constructor `{$def}` is private hir_typeck_deref_is_empty = this expression `Deref`s to `{$deref_ty}` which implements `is_empty` +hir_typeck_dereferencing_mut_binding = dereferencing `mut` binding + .label = `mut` dereferences the type of this binding + .help = this will change in edition 2024 + hir_typeck_expected_default_return_type = expected `()` because of default return type hir_typeck_expected_return_type = expected `{$expected}` because of return type diff --git a/compiler/rustc_hir_typeck/src/errors.rs b/compiler/rustc_hir_typeck/src/errors.rs index d399730bf3df4..3dc9c7b86f713 100644 --- a/compiler/rustc_hir_typeck/src/errors.rs +++ b/compiler/rustc_hir_typeck/src/errors.rs @@ -632,3 +632,11 @@ pub enum SuggestBoxingForReturnImplTrait { ends: Vec, }, } + +#[derive(LintDiagnostic)] +#[diag(hir_typeck_dereferencing_mut_binding)] +pub struct DereferencingMutBinding { + #[label] + #[help] + pub span: Span, +} diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index 06f6a7f6bf282..252125aba7c14 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -10,6 +10,7 @@ use rustc_hir::pat_util::EnumerateAndAdjustIterator; use rustc_hir::{self as hir, BindingAnnotation, ByRef, HirId, Mutability, Pat, PatKind}; use rustc_infer::infer; use rustc_infer::infer::type_variable::TypeVariableOrigin; +use rustc_lint as lint; use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::ty::{self, Adt, Ty, TypeVisitableExt}; use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS; @@ -639,6 +640,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { && matches!(def_br, ByRef::Yes(_)) => { // `mut x` resets the binding mode in edition <= 2021. + self.tcx.emit_node_span_lint( + lint::builtin::DEREFERENCING_MUT_BINDING, + pat.hir_id, + pat.span, + errors::DereferencingMutBinding { span: pat.span }, + ); BindingAnnotation(ByRef::No, Mutability::Mut) } BindingAnnotation(ByRef::No, mutbl) => BindingAnnotation(def_br, mutbl), diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 2713690f8120a..bc36a587a9ec3 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -38,6 +38,7 @@ declare_lint_pass! { DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME, DEPRECATED_IN_FUTURE, DEPRECATED_WHERE_CLAUSE_LOCATION, + DEREFERENCING_MUT_BINDING, DUPLICATE_MACRO_ATTRIBUTES, ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT, ELIDED_LIFETIMES_IN_PATHS, @@ -1627,6 +1628,41 @@ declare_lint! { "detect mut variables which don't need to be mutable" } +declare_lint! { + /// The `dereferencing_mut_binding` lint detects a `mut x` pattern that resets the binding mode, + /// as this behavior will change in rust 2024. + /// + /// ### Example + /// + /// ```rust + /// # #![warn(dereferencing_mut_binding)] + /// let x = Some(123u32); + /// let _y = match &x { + /// Some(mut x) => { + /// x += 1; + /// x + /// } + /// None => 0, + /// }; + /// ``` + /// + /// {{produces}} + /// + /// ### Explanation + /// + /// Without the `mut`, `x` would have type `&u32`. Pre-2024, adding `mut` makes `x` have type + /// `u32`, which was deeped surprising. After edition 2024, adding `mut` will not change the + /// type of `x`. This lint warns users of editions before 2024 to update their code. + pub DEREFERENCING_MUT_BINDING, + Allow, + "detects `mut x` bindings that change the type of `x`", + @feature_gate = sym::mut_dont_reset_binding_mode_2024; + @future_incompatible = FutureIncompatibleInfo { + reason: FutureIncompatibilityReason::EditionSemanticsChange(Edition::Edition2024), + reference: "123076", + }; +} + declare_lint! { /// The `unconditional_recursion` lint detects functions that cannot /// return without calling themselves. diff --git a/tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.rs b/tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.rs new file mode 100644 index 0000000000000..2992e61fbbcdd --- /dev/null +++ b/tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.rs @@ -0,0 +1,18 @@ +//@ edition: 2021 +#![feature(mut_dont_reset_binding_mode_2024)] +#![allow(unused)] +#![forbid(dereferencing_mut_binding)] + +struct Foo(u8); + +fn main() { + let Foo(mut a) = &Foo(0); + //~^ ERROR: dereferencing `mut` binding + //~| WARN: this changes meaning in Rust 2024 + a = 42; + + let Foo(mut a) = &mut Foo(0); + //~^ ERROR: dereferencing `mut` binding + //~| WARN: this changes meaning in Rust 2024 + a = 42; +} diff --git a/tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.stderr b/tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.stderr new file mode 100644 index 0000000000000..72b72f0df5d95 --- /dev/null +++ b/tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.stderr @@ -0,0 +1,35 @@ +error: dereferencing `mut` binding + --> $DIR/mut_dont_reset_binding_mode_2024_lint.rs:9:13 + | +LL | let Foo(mut a) = &Foo(0); + | ^^^^^ `mut` dereferences the type of this binding + | + = warning: this changes meaning in Rust 2024 + = note: for more information, see 123076 +help: this will change in edition 2024 + --> $DIR/mut_dont_reset_binding_mode_2024_lint.rs:9:13 + | +LL | let Foo(mut a) = &Foo(0); + | ^^^^^ +note: the lint level is defined here + --> $DIR/mut_dont_reset_binding_mode_2024_lint.rs:4:11 + | +LL | #![forbid(dereferencing_mut_binding)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: dereferencing `mut` binding + --> $DIR/mut_dont_reset_binding_mode_2024_lint.rs:14:13 + | +LL | let Foo(mut a) = &mut Foo(0); + | ^^^^^ `mut` dereferences the type of this binding + | + = warning: this changes meaning in Rust 2024 + = note: for more information, see 123076 +help: this will change in edition 2024 + --> $DIR/mut_dont_reset_binding_mode_2024_lint.rs:14:13 + | +LL | let Foo(mut a) = &mut Foo(0); + | ^^^^^ + +error: aborting due to 2 previous errors + From d5d700d5c65df24dfeec32f6fbd9d1d7b81c70b2 Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Sat, 6 Apr 2024 00:29:35 -0400 Subject: [PATCH 3/7] Temporarily remove future compatibility label from migration lint The lint is unstable, and the lint group `rust_2024_compatibility` must keep working on stable --- compiler/rustc_lint_defs/src/builtin.rs | 5 +++-- tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.rs | 2 -- .../pattern/mut_dont_reset_binding_mode_2024_lint.stderr | 8 ++------ 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index bc36a587a9ec3..5859f7b4319a7 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -1657,10 +1657,11 @@ declare_lint! { Allow, "detects `mut x` bindings that change the type of `x`", @feature_gate = sym::mut_dont_reset_binding_mode_2024; - @future_incompatible = FutureIncompatibleInfo { + // FIXME uncomment below upon stabilization + /*@future_incompatible = FutureIncompatibleInfo { reason: FutureIncompatibilityReason::EditionSemanticsChange(Edition::Edition2024), reference: "123076", - }; + };*/ } declare_lint! { diff --git a/tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.rs b/tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.rs index 2992e61fbbcdd..2e8a82d12cd6a 100644 --- a/tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.rs +++ b/tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.rs @@ -8,11 +8,9 @@ struct Foo(u8); fn main() { let Foo(mut a) = &Foo(0); //~^ ERROR: dereferencing `mut` binding - //~| WARN: this changes meaning in Rust 2024 a = 42; let Foo(mut a) = &mut Foo(0); //~^ ERROR: dereferencing `mut` binding - //~| WARN: this changes meaning in Rust 2024 a = 42; } diff --git a/tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.stderr b/tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.stderr index 72b72f0df5d95..4db775f0f518b 100644 --- a/tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.stderr +++ b/tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.stderr @@ -4,8 +4,6 @@ error: dereferencing `mut` binding LL | let Foo(mut a) = &Foo(0); | ^^^^^ `mut` dereferences the type of this binding | - = warning: this changes meaning in Rust 2024 - = note: for more information, see 123076 help: this will change in edition 2024 --> $DIR/mut_dont_reset_binding_mode_2024_lint.rs:9:13 | @@ -18,15 +16,13 @@ LL | #![forbid(dereferencing_mut_binding)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: dereferencing `mut` binding - --> $DIR/mut_dont_reset_binding_mode_2024_lint.rs:14:13 + --> $DIR/mut_dont_reset_binding_mode_2024_lint.rs:13:13 | LL | let Foo(mut a) = &mut Foo(0); | ^^^^^ `mut` dereferences the type of this binding | - = warning: this changes meaning in Rust 2024 - = note: for more information, see 123076 help: this will change in edition 2024 - --> $DIR/mut_dont_reset_binding_mode_2024_lint.rs:14:13 + --> $DIR/mut_dont_reset_binding_mode_2024_lint.rs:13:13 | LL | let Foo(mut a) = &mut Foo(0); | ^^^^^ From 1b2e471b43610e74c7cc6e7a38d48293420e157f Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Sun, 14 Apr 2024 11:07:23 -0400 Subject: [PATCH 4/7] Fix typo Co-authored-by: Guillaume Boisseau --- compiler/rustc_lint_defs/src/builtin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 5859f7b4319a7..3d80eb1cec9e1 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -1651,7 +1651,7 @@ declare_lint! { /// ### Explanation /// /// Without the `mut`, `x` would have type `&u32`. Pre-2024, adding `mut` makes `x` have type - /// `u32`, which was deeped surprising. After edition 2024, adding `mut` will not change the + /// `u32`, which was deemed surprising. After edition 2024, adding `mut` will not change the /// type of `x`. This lint warns users of editions before 2024 to update their code. pub DEREFERENCING_MUT_BINDING, Allow, From e13911e6e8d0d85802789a2d75aeb0e4b49ebb33 Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Sun, 14 Apr 2024 11:12:52 -0400 Subject: [PATCH 5/7] Rename feature gate --- compiler/rustc_feature/src/unstable.rs | 2 +- compiler/rustc_hir_typeck/src/pat.rs | 2 +- compiler/rustc_lint_defs/src/builtin.rs | 2 +- compiler/rustc_span/src/symbol.rs | 2 +- ... => feature-gate-mut_preserve_binding_mode_2024.rs} | 0 ...feature-gate-mut_preserve_binding_mode_2024.stderr} | 4 ++-- ..._mode_2021.rs => mut_preserve_binding_mode_2021.rs} | 2 +- ...21.stderr => mut_preserve_binding_mode_2021.stderr} | 4 ++-- ..._mode_2024.rs => mut_preserve_binding_mode_2024.rs} | 2 +- ..._lint.rs => mut_preserve_binding_mode_2024_lint.rs} | 2 +- ...derr => mut_preserve_binding_mode_2024_lint.stderr} | 10 +++++----- 11 files changed, 16 insertions(+), 16 deletions(-) rename tests/ui/pattern/{feature-gate-mut_dont_reset_binding_mode_2024.rs => feature-gate-mut_preserve_binding_mode_2024.rs} (100%) rename tests/ui/pattern/{feature-gate-mut_dont_reset_binding_mode_2024.stderr => feature-gate-mut_preserve_binding_mode_2024.stderr} (84%) rename tests/ui/pattern/{mut_dont_reset_binding_mode_2021.rs => mut_preserve_binding_mode_2021.rs} (84%) rename tests/ui/pattern/{mut_dont_reset_binding_mode_2021.stderr => mut_preserve_binding_mode_2021.stderr} (87%) rename tests/ui/pattern/{mut_dont_reset_binding_mode_2024.rs => mut_preserve_binding_mode_2024.rs} (82%) rename tests/ui/pattern/{mut_dont_reset_binding_mode_2024_lint.rs => mut_preserve_binding_mode_2024_lint.rs} (86%) rename tests/ui/pattern/{mut_dont_reset_binding_mode_2024_lint.stderr => mut_preserve_binding_mode_2024_lint.stderr} (70%) diff --git a/compiler/rustc_feature/src/unstable.rs b/compiler/rustc_feature/src/unstable.rs index 2a753af938721..3fc05752dd1e2 100644 --- a/compiler/rustc_feature/src/unstable.rs +++ b/compiler/rustc_feature/src/unstable.rs @@ -534,7 +534,7 @@ declare_features! ( /// Allows the `#[must_not_suspend]` attribute. (unstable, must_not_suspend, "1.57.0", Some(83310)), /// Make `mut` not reset the binding mode on edition >= 2024. - (unstable, mut_dont_reset_binding_mode_2024, "CURRENT_RUSTC_VERSION", Some(123076)), + (unstable, mut_preserve_binding_mode_2024, "CURRENT_RUSTC_VERSION", Some(123076)), /// Allows `mut ref` and `mut ref mut` identifier patterns. (incomplete, mut_ref, "CURRENT_RUSTC_VERSION", Some(123076)), /// Allows using `#[naked]` on functions. diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index 252125aba7c14..cdc6c4d809d12 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -636,7 +636,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let bm = match ba { BindingAnnotation(ByRef::No, Mutability::Mut) if !(pat.span.at_least_rust_2024() - && self.tcx.features().mut_dont_reset_binding_mode_2024) + && self.tcx.features().mut_preserve_binding_mode_2024) && matches!(def_br, ByRef::Yes(_)) => { // `mut x` resets the binding mode in edition <= 2021. diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 3d80eb1cec9e1..e74cc388cab4c 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -1656,7 +1656,7 @@ declare_lint! { pub DEREFERENCING_MUT_BINDING, Allow, "detects `mut x` bindings that change the type of `x`", - @feature_gate = sym::mut_dont_reset_binding_mode_2024; + @feature_gate = sym::mut_preserve_binding_mode_2024; // FIXME uncomment below upon stabilization /*@future_incompatible = FutureIncompatibleInfo { reason: FutureIncompatibilityReason::EditionSemanticsChange(Edition::Edition2024), diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 02550fd655cb2..0a95d86ccc891 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1194,7 +1194,7 @@ symbols! { multiple_supertrait_upcastable, must_not_suspend, must_use, - mut_dont_reset_binding_mode_2024, + mut_preserve_binding_mode_2024, mut_ref, naked, naked_functions, diff --git a/tests/ui/pattern/feature-gate-mut_dont_reset_binding_mode_2024.rs b/tests/ui/pattern/feature-gate-mut_preserve_binding_mode_2024.rs similarity index 100% rename from tests/ui/pattern/feature-gate-mut_dont_reset_binding_mode_2024.rs rename to tests/ui/pattern/feature-gate-mut_preserve_binding_mode_2024.rs diff --git a/tests/ui/pattern/feature-gate-mut_dont_reset_binding_mode_2024.stderr b/tests/ui/pattern/feature-gate-mut_preserve_binding_mode_2024.stderr similarity index 84% rename from tests/ui/pattern/feature-gate-mut_dont_reset_binding_mode_2024.stderr rename to tests/ui/pattern/feature-gate-mut_preserve_binding_mode_2024.stderr index 1624883de60c1..6d0a034be21c7 100644 --- a/tests/ui/pattern/feature-gate-mut_dont_reset_binding_mode_2024.stderr +++ b/tests/ui/pattern/feature-gate-mut_preserve_binding_mode_2024.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/feature-gate-mut_dont_reset_binding_mode_2024.rs:8:9 + --> $DIR/feature-gate-mut_preserve_binding_mode_2024.rs:8:9 | LL | let Foo(mut a) = &Foo(0); | ----- expected due to the type of this binding @@ -13,7 +13,7 @@ LL + a = 42; | error[E0308]: mismatched types - --> $DIR/feature-gate-mut_dont_reset_binding_mode_2024.rs:12:9 + --> $DIR/feature-gate-mut_preserve_binding_mode_2024.rs:12:9 | LL | let Foo(mut a) = &mut Foo(0); | ----- expected due to the type of this binding diff --git a/tests/ui/pattern/mut_dont_reset_binding_mode_2021.rs b/tests/ui/pattern/mut_preserve_binding_mode_2021.rs similarity index 84% rename from tests/ui/pattern/mut_dont_reset_binding_mode_2021.rs rename to tests/ui/pattern/mut_preserve_binding_mode_2021.rs index a9e12472734f0..658ba2851ccb0 100644 --- a/tests/ui/pattern/mut_dont_reset_binding_mode_2021.rs +++ b/tests/ui/pattern/mut_preserve_binding_mode_2021.rs @@ -1,6 +1,6 @@ //@ edition: 2021 //@ compile-flags: -Zunstable-options -#![feature(mut_dont_reset_binding_mode_2024)] +#![feature(mut_preserve_binding_mode_2024)] struct Foo(u8); diff --git a/tests/ui/pattern/mut_dont_reset_binding_mode_2021.stderr b/tests/ui/pattern/mut_preserve_binding_mode_2021.stderr similarity index 87% rename from tests/ui/pattern/mut_dont_reset_binding_mode_2021.stderr rename to tests/ui/pattern/mut_preserve_binding_mode_2021.stderr index 16818c900b342..9487aa64b4f6f 100644 --- a/tests/ui/pattern/mut_dont_reset_binding_mode_2021.stderr +++ b/tests/ui/pattern/mut_preserve_binding_mode_2021.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/mut_dont_reset_binding_mode_2021.rs:9:9 + --> $DIR/mut_preserve_binding_mode_2021.rs:9:9 | LL | let Foo(mut a) = &Foo(0); | ----- expected due to the type of this binding @@ -13,7 +13,7 @@ LL + a = 42; | error[E0308]: mismatched types - --> $DIR/mut_dont_reset_binding_mode_2021.rs:13:9 + --> $DIR/mut_preserve_binding_mode_2021.rs:13:9 | LL | let Foo(mut a) = &mut Foo(0); | ----- expected due to the type of this binding diff --git a/tests/ui/pattern/mut_dont_reset_binding_mode_2024.rs b/tests/ui/pattern/mut_preserve_binding_mode_2024.rs similarity index 82% rename from tests/ui/pattern/mut_dont_reset_binding_mode_2024.rs rename to tests/ui/pattern/mut_preserve_binding_mode_2024.rs index 9ac5ec50c74c9..a5d7ac9d8e6d1 100644 --- a/tests/ui/pattern/mut_dont_reset_binding_mode_2024.rs +++ b/tests/ui/pattern/mut_preserve_binding_mode_2024.rs @@ -1,7 +1,7 @@ //@ run-pass //@ edition: 2024 //@ compile-flags: -Zunstable-options -#![feature(mut_dont_reset_binding_mode_2024)] +#![feature(mut_preserve_binding_mode_2024)] #![allow(unused)] struct Foo(u8); diff --git a/tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.rs b/tests/ui/pattern/mut_preserve_binding_mode_2024_lint.rs similarity index 86% rename from tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.rs rename to tests/ui/pattern/mut_preserve_binding_mode_2024_lint.rs index 2e8a82d12cd6a..d3e3ffffcc5e2 100644 --- a/tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.rs +++ b/tests/ui/pattern/mut_preserve_binding_mode_2024_lint.rs @@ -1,5 +1,5 @@ //@ edition: 2021 -#![feature(mut_dont_reset_binding_mode_2024)] +#![feature(mut_preserve_binding_mode_2024)] #![allow(unused)] #![forbid(dereferencing_mut_binding)] diff --git a/tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.stderr b/tests/ui/pattern/mut_preserve_binding_mode_2024_lint.stderr similarity index 70% rename from tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.stderr rename to tests/ui/pattern/mut_preserve_binding_mode_2024_lint.stderr index 4db775f0f518b..e8d11acd83e50 100644 --- a/tests/ui/pattern/mut_dont_reset_binding_mode_2024_lint.stderr +++ b/tests/ui/pattern/mut_preserve_binding_mode_2024_lint.stderr @@ -1,28 +1,28 @@ error: dereferencing `mut` binding - --> $DIR/mut_dont_reset_binding_mode_2024_lint.rs:9:13 + --> $DIR/mut_preserve_binding_mode_2024_lint.rs:9:13 | LL | let Foo(mut a) = &Foo(0); | ^^^^^ `mut` dereferences the type of this binding | help: this will change in edition 2024 - --> $DIR/mut_dont_reset_binding_mode_2024_lint.rs:9:13 + --> $DIR/mut_preserve_binding_mode_2024_lint.rs:9:13 | LL | let Foo(mut a) = &Foo(0); | ^^^^^ note: the lint level is defined here - --> $DIR/mut_dont_reset_binding_mode_2024_lint.rs:4:11 + --> $DIR/mut_preserve_binding_mode_2024_lint.rs:4:11 | LL | #![forbid(dereferencing_mut_binding)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: dereferencing `mut` binding - --> $DIR/mut_dont_reset_binding_mode_2024_lint.rs:13:13 + --> $DIR/mut_preserve_binding_mode_2024_lint.rs:13:13 | LL | let Foo(mut a) = &mut Foo(0); | ^^^^^ `mut` dereferences the type of this binding | help: this will change in edition 2024 - --> $DIR/mut_dont_reset_binding_mode_2024_lint.rs:13:13 + --> $DIR/mut_preserve_binding_mode_2024_lint.rs:13:13 | LL | let Foo(mut a) = &mut Foo(0); | ^^^^^ From 1b6d435cf3e94548f3da6a0409c5786c4accfaa4 Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Sun, 14 Apr 2024 11:17:58 -0400 Subject: [PATCH 6/7] Mark gate as incomplete --- compiler/rustc_feature/src/unstable.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_feature/src/unstable.rs b/compiler/rustc_feature/src/unstable.rs index 3fc05752dd1e2..eaaf4026cd759 100644 --- a/compiler/rustc_feature/src/unstable.rs +++ b/compiler/rustc_feature/src/unstable.rs @@ -534,7 +534,7 @@ declare_features! ( /// Allows the `#[must_not_suspend]` attribute. (unstable, must_not_suspend, "1.57.0", Some(83310)), /// Make `mut` not reset the binding mode on edition >= 2024. - (unstable, mut_preserve_binding_mode_2024, "CURRENT_RUSTC_VERSION", Some(123076)), + (incomplete, mut_preserve_binding_mode_2024, "CURRENT_RUSTC_VERSION", Some(123076)), /// Allows `mut ref` and `mut ref mut` identifier patterns. (incomplete, mut_ref, "CURRENT_RUSTC_VERSION", Some(123076)), /// Allows using `#[naked]` on functions. From 7a3211726bb101ab0c0138b2dcff44a175ff74c4 Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Sun, 14 Apr 2024 13:33:11 -0400 Subject: [PATCH 7/7] Fix tests --- tests/ui/pattern/mut_preserve_binding_mode_2021.rs | 1 + tests/ui/pattern/mut_preserve_binding_mode_2021.stderr | 4 ++-- tests/ui/pattern/mut_preserve_binding_mode_2024.rs | 2 +- tests/ui/pattern/mut_preserve_binding_mode_2024_lint.rs | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/ui/pattern/mut_preserve_binding_mode_2021.rs b/tests/ui/pattern/mut_preserve_binding_mode_2021.rs index 658ba2851ccb0..befa49fdc2472 100644 --- a/tests/ui/pattern/mut_preserve_binding_mode_2021.rs +++ b/tests/ui/pattern/mut_preserve_binding_mode_2021.rs @@ -1,6 +1,7 @@ //@ edition: 2021 //@ compile-flags: -Zunstable-options #![feature(mut_preserve_binding_mode_2024)] +#![allow(incomplete_features)] struct Foo(u8); diff --git a/tests/ui/pattern/mut_preserve_binding_mode_2021.stderr b/tests/ui/pattern/mut_preserve_binding_mode_2021.stderr index 9487aa64b4f6f..b800cc4a0f454 100644 --- a/tests/ui/pattern/mut_preserve_binding_mode_2021.stderr +++ b/tests/ui/pattern/mut_preserve_binding_mode_2021.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/mut_preserve_binding_mode_2021.rs:9:9 + --> $DIR/mut_preserve_binding_mode_2021.rs:10:9 | LL | let Foo(mut a) = &Foo(0); | ----- expected due to the type of this binding @@ -13,7 +13,7 @@ LL + a = 42; | error[E0308]: mismatched types - --> $DIR/mut_preserve_binding_mode_2021.rs:13:9 + --> $DIR/mut_preserve_binding_mode_2021.rs:14:9 | LL | let Foo(mut a) = &mut Foo(0); | ----- expected due to the type of this binding diff --git a/tests/ui/pattern/mut_preserve_binding_mode_2024.rs b/tests/ui/pattern/mut_preserve_binding_mode_2024.rs index a5d7ac9d8e6d1..5454962e16ce1 100644 --- a/tests/ui/pattern/mut_preserve_binding_mode_2024.rs +++ b/tests/ui/pattern/mut_preserve_binding_mode_2024.rs @@ -2,7 +2,7 @@ //@ edition: 2024 //@ compile-flags: -Zunstable-options #![feature(mut_preserve_binding_mode_2024)] -#![allow(unused)] +#![allow(incomplete_features, unused)] struct Foo(u8); diff --git a/tests/ui/pattern/mut_preserve_binding_mode_2024_lint.rs b/tests/ui/pattern/mut_preserve_binding_mode_2024_lint.rs index d3e3ffffcc5e2..249f251d2cd2e 100644 --- a/tests/ui/pattern/mut_preserve_binding_mode_2024_lint.rs +++ b/tests/ui/pattern/mut_preserve_binding_mode_2024_lint.rs @@ -1,6 +1,6 @@ //@ edition: 2021 #![feature(mut_preserve_binding_mode_2024)] -#![allow(unused)] +#![allow(incomplete_features, unused)] #![forbid(dereferencing_mut_binding)] struct Foo(u8);