From 550b0ad0367ba3ade3f672bf46aa7b61fe0236a8 Mon Sep 17 00:00:00 2001 From: dianne Date: Mon, 23 Dec 2024 01:59:32 -0800 Subject: [PATCH 01/16] make experimental pattern typing features mutually exclusive This aims to reduce the complexity needed in the boolean logic for telling which rules we're using to type patterns. If we still want the functionality this removes, we can re-add it later, after some cleanup to pattern typing. --- compiler/rustc_feature/src/unstable.rs | 9 +- .../ref_pat_eat_one_layer_2021.rs | 6 +- .../ref_pat_eat_one_layer_2024.rs | 10 +- ...ef_pat_eat_one_layer_2024_fail.both.stderr | 169 ------------------ .../ref_pat_eat_one_layer_2024_fail.rs | 6 +- 5 files changed, 16 insertions(+), 184 deletions(-) delete mode 100644 tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.both.stderr diff --git a/compiler/rustc_feature/src/unstable.rs b/compiler/rustc_feature/src/unstable.rs index 8cc4c18c02abf..a30a797beffaa 100644 --- a/compiler/rustc_feature/src/unstable.rs +++ b/compiler/rustc_feature/src/unstable.rs @@ -718,7 +718,8 @@ impl Features { /// Some features are not allowed to be used together at the same time, if /// the two are present, produce an error. -/// -/// Currently empty, but we will probably need this again in the future, -/// so let's keep it in for now. -pub const INCOMPATIBLE_FEATURES: &[(Symbol, Symbol)] = &[]; +pub const INCOMPATIBLE_FEATURES: &[(Symbol, Symbol)] = &[ + // Experimental match ergonomics rulesets are incompatible with each other, to simplify the + // boolean logic required to tell which typing rules to use. + (sym::ref_pat_eat_one_layer_2024, sym::ref_pat_eat_one_layer_2024_structural), +]; diff --git a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2021.rs b/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2021.rs index afea249ffef0b..9372049a2b283 100644 --- a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2021.rs +++ b/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2021.rs @@ -1,9 +1,9 @@ //@ run-pass //@ edition: 2021 -//@ revisions: classic structural both +//@ revisions: classic structural #![allow(incomplete_features)] -#![cfg_attr(any(classic, both), feature(ref_pat_eat_one_layer_2024))] -#![cfg_attr(any(structural, both), feature(ref_pat_eat_one_layer_2024_structural))] +#![cfg_attr(classic, feature(ref_pat_eat_one_layer_2024))] +#![cfg_attr(structural, feature(ref_pat_eat_one_layer_2024_structural))] pub fn main() { if let &Some(Some(x)) = &Some(&mut Some(0)) { diff --git a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024.rs b/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024.rs index b145446de0a1c..70add6f4d44a7 100644 --- a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024.rs +++ b/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024.rs @@ -1,9 +1,9 @@ //@ run-pass //@ edition: 2024 -//@ revisions: classic structural both +//@ revisions: classic structural #![allow(incomplete_features)] -#![cfg_attr(any(classic, both), feature(ref_pat_eat_one_layer_2024))] -#![cfg_attr(any(structural, both), feature(ref_pat_eat_one_layer_2024_structural))] +#![cfg_attr(classic, feature(ref_pat_eat_one_layer_2024))] +#![cfg_attr(structural, feature(ref_pat_eat_one_layer_2024_structural))] pub fn main() { if let Some(Some(&x)) = &Some(&Some(0)) { @@ -54,11 +54,11 @@ pub fn main() { if let Some(&Some(x)) = &mut Some(Some(0)) { let _: u32 = x; } - #[cfg(any(classic, both))] + #[cfg(classic)] if let Some(&mut x) = &mut Some(&0) { let _: &u32 = x; } - #[cfg(any(structural, both))] + #[cfg(structural)] if let Some(&mut x) = &Some(&mut 0) { let _: &u32 = x; } diff --git a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.both.stderr b/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.both.stderr deleted file mode 100644 index f8672d755b959..0000000000000 --- a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.both.stderr +++ /dev/null @@ -1,169 +0,0 @@ -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:8:17 - | -LL | if let Some(&mut Some(&_)) = &Some(&Some(0)) { - | ^^^^^^^^^^^^^ --------------- this expression has type `&Option<&Option<{integer}>>` - | | - | types differ in mutability - | - = note: expected reference `&Option<{integer}>` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:11:23 - | -LL | if let Some(&Some(&mut _)) = &Some(&mut Some(0)) { - | ^^^^^^ ------------------- this expression has type `&Option<&mut Option<{integer}>>` - | | - | expected integer, found `&mut _` - | - = note: expected type `{integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:15:27 - | -LL | let _: &mut u32 = x; - | -------- ^ types differ in mutability - | | - | expected due to this - | - = note: expected mutable reference `&mut u32` - found reference `&{integer}` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:18:23 - | -LL | if let Some(&Some(&mut _)) = &mut Some(&Some(0)) { - | ^^^^^^ ------------------- this expression has type `&mut Option<&Option<{integer}>>` - | | - | expected integer, found `&mut _` - | - = note: expected type `{integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:21:29 - | -LL | if let Some(&Some(Some((&mut _)))) = &Some(Some(&mut Some(0))) { - | ^^^^^^ ------------------------- this expression has type `&Option>>` - | | - | expected integer, found `&mut _` - | - = note: expected type `{integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:24:17 - | -LL | if let Some(&mut Some(x)) = &Some(Some(0)) { - | ^^^^^^^^^^^^ -------------- this expression has type `&Option>` - | | - | expected `Option<{integer}>`, found `&mut _` - | - = note: expected enum `Option<{integer}>` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:27:17 - | -LL | if let Some(&mut Some(x)) = &Some(Some(0)) { - | ^^^^^^^^^^^^ -------------- this expression has type `&Option>` - | | - | expected `Option<{integer}>`, found `&mut _` - | - = note: expected enum `Option<{integer}>` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:31:9 - | -LL | let &mut _ = &&0; - | ^^^^^^ --- this expression has type `&&{integer}` - | | - | types differ in mutability - | - = note: expected reference `&&{integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:34:9 - | -LL | let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&0; - | ^^^^^^ ----------------------------- this expression has type `&&&&&&&&&&&&&&&&&&&&&&&&&&&&{integer}` - | | - | types differ in mutability - | - = note: expected reference `&&&&&&&&&&&&&&&&&&&&&&&&&&&&{integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:45:9 - | -LL | let &mut _ = &&mut 0; - | ^^^^^^ ------- this expression has type `&&mut {integer}` - | | - | types differ in mutability - | - = note: expected reference `&&mut {integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:48:9 - | -LL | let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&mut 0; - | ^^^^^^ --------------------------------- this expression has type `&&&&&&&&&&&&&&&&&&&&&&&&&&&&mut {integer}` - | | - | types differ in mutability - | - = note: expected reference `&&&&&&&&&&&&&&&&&&&&&&&&&&&&mut {integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:51:14 - | -LL | let &mut &mut &mut &mut _ = &mut &&&&mut &&&mut &mut 0; - | ^^^^^^^^^^^^^^^^ -------------------------- this expression has type `&mut &&&&mut &&&mut &mut {integer}` - | | - | types differ in mutability - | - = note: expected reference `&&&&mut &&&mut &mut {integer}` - found mutable reference `&mut _` - -error[E0658]: binding cannot be both mutable and by-reference - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:60:13 - | -LL | let Foo(mut a) = &Foo(0); - | ^^^^ - | - = note: see issue #123076 for more information - = help: add `#![feature(mut_ref)]` to the crate attributes to enable - = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date - -error[E0658]: binding cannot be both mutable and by-reference - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:64:13 - | -LL | let Foo(mut a) = &mut Foo(0); - | ^^^^ - | - = note: see issue #123076 for more information - = help: add `#![feature(mut_ref)]` to the crate attributes to enable - = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date - -error[E0277]: the trait bound `&_: main::Ref` is not satisfied - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:82:14 - | -LL | let &_ = generic(); - | ^^^^^^^^^ the trait `main::Ref` is not implemented for `&_` - | - = help: the trait `main::Ref` is implemented for `&'static mut [(); 0]` -note: required by a bound in `generic` - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:68:19 - | -LL | fn generic() -> R { - | ^^^ required by this bound in `generic` - -error: aborting due to 15 previous errors - -Some errors have detailed explanations: E0277, E0308, E0658. -For more information about an error, try `rustc --explain E0277`. diff --git a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.rs b/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.rs index fd616807b285c..ef4a92eb4b3ce 100644 --- a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.rs +++ b/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.rs @@ -1,8 +1,8 @@ //@ edition: 2024 -//@ revisions: classic structural both +//@ revisions: classic structural #![allow(incomplete_features)] -#![cfg_attr(any(classic, both), feature(ref_pat_eat_one_layer_2024))] -#![cfg_attr(any(structural, both), feature(ref_pat_eat_one_layer_2024_structural))] +#![cfg_attr(classic, feature(ref_pat_eat_one_layer_2024))] +#![cfg_attr(structural, feature(ref_pat_eat_one_layer_2024_structural))] pub fn main() { if let Some(&mut Some(&_)) = &Some(&Some(0)) { From ff165d54cbfc4734ae889e861ef9fd94ecfc5375 Mon Sep 17 00:00:00 2001 From: dianne Date: Tue, 31 Dec 2024 17:59:36 -0800 Subject: [PATCH 02/16] pattern typing cleanup: remove a redundant assignment As far as I can tell, the assignment removed here will never do anything. `pat_info.max_ref_mutbl` starts at `MutblCap::Mut` for the top-level pattern and is only changed if feature gates are enabled that would result in the statement not being executed. Regardless of what new pattern typing rules are adopted, I don't imagine we want to conditionally reset `max_ref_mutbl` based on edition either, since it'd have consequences for subpatterns in other editions. --- compiler/rustc_hir_typeck/src/pat.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index 4870e6193c3f1..5e6cca51c925f 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -2216,13 +2216,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let pat_prefix_span = inner.span.find_ancestor_inside(pat.span).map(|end| pat.span.until(end)); - if no_ref_mut_behind_and { - if pat_mutbl == Mutability::Not { - // Prevent the inner pattern from binding with `ref mut`. - pat_info.max_ref_mutbl = pat_info.max_ref_mutbl.cap_to_weakly_not(pat_prefix_span); - } - } else { - pat_info.max_ref_mutbl = MutblCap::Mut; + if no_ref_mut_behind_and && pat_mutbl == Mutability::Not { + // Prevent the inner pattern from binding with `ref mut`. + pat_info.max_ref_mutbl = pat_info.max_ref_mutbl.cap_to_weakly_not(pat_prefix_span); } expected = self.try_structurally_resolve_type(pat.span, expected); From e2f3ce956809dd98adf271afe0b024c7febdf47f Mon Sep 17 00:00:00 2001 From: dianne Date: Wed, 1 Jan 2025 15:19:11 -0800 Subject: [PATCH 03/16] always track patterns' `MutblCap` If Rules 3 or 5 are adopted in any edition, we'll need to track the `MutblCap` in all editions for macro hygiene purposes. Previously, the check for whether to track it was conflated with the checks for whether to apply Rules 3 and 5, so to make it a bit clearer, this always tracks the `MutblCap`. If needed, we could check if Rules 3 or 5 are present in any edition before tracking the `MutblCap`, but since it's not that much more expensive to always track it, I've figured that's simplest. My main concern with removing the checks is that it may not be clear that the `MutblCap` is tracked for those specific purposes. To try and mitigate this, I've made its doc comment a bit more precise regarding the extent of how and why it's used. This leaves the condition untouched on the `cap_to_weakly_not` call needed for Rule 5, since it's only needed for that and it can affect diagnostics. --- compiler/rustc_hir_typeck/src/pat.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index 5e6cca51c925f..391acce97bd9d 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -168,15 +168,16 @@ enum AdjustMode { Pass, } -/// `ref mut` patterns (explicit or match-ergonomics) -/// are not allowed behind an `&` reference. +/// `ref mut` bindings (explicit or match-ergonomics) are not allowed behind an `&` reference. +/// Normally, the borrow checker enforces this, but for (currently experimental) match ergonomics, +/// we track this when typing patterns for two purposes: /// -/// This includes explicit `ref mut` behind `&` patterns -/// that match against `&mut` references, -/// where the code would have compiled -/// had the pattern been written as `&mut`. -/// However, the borrow checker will not catch -/// this last case, so we need to throw an error ourselves. +/// - For RFC 3627's Rule 3, when this would prevent us from binding with `ref mut`, we limit the +/// default binding mode to be by shared `ref` when it would otherwise be `ref mut`. +/// +/// - For RFC 3627's Rule 5, we allow `&` patterns to match against `&mut` references, treating them +/// as if they were shared references. Since the scrutinee is mutable in this case, the borrow +/// checker won't catch if we bind with `ref mut`, so we need to throw an error ourselves. #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum MutblCap { /// Mutability restricted to immutable. @@ -477,9 +478,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if features.ref_pat_eat_one_layer_2024() || features.ref_pat_eat_one_layer_2024_structural() { def_br = def_br.cap_ref_mutability(max_ref_mutbl.as_mutbl()); - if def_br == ByRef::Yes(Mutability::Not) { - max_ref_mutbl = MutblCap::Not; - } + } + if def_br == ByRef::Yes(Mutability::Not) { + max_ref_mutbl = MutblCap::Not; } if !pat_adjustments.is_empty() { @@ -2292,7 +2293,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if (no_ref_mut_behind_and && r_mutbl >= pat_mutbl) || r_mutbl == pat_mutbl => { - if no_ref_mut_behind_and && r_mutbl == Mutability::Not { + if r_mutbl == Mutability::Not { pat_info.max_ref_mutbl = MutblCap::Not; } From c0e8bada734d41fa9941fd7989f277639bcf0c23 Mon Sep 17 00:00:00 2001 From: dianne Date: Mon, 23 Dec 2024 02:31:54 -0800 Subject: [PATCH 04/16] cleanup: de-tangle experimental pattern typing rules some The goal of this cleanup is to make it more apparent which feature gates correspond to which typing rules, and which typing rules correspond to what code. My intent is for calls to the "which typing rules do we have?" functions to be replaced by comments (and edition checks, as appropriate), but as long as we're experimenting with multiple rulesets, this seemed to me to be the easiest to document and read. There's still some nontrivial control flow, but I've added comments to try and make it clearer. There's some logic that looks like it could be de-duplicated across different ways of matching against inherited references; however, the duplication is intentional. Once we choose which rulesets we want, we can make this more clever, but until then, my priorities are clarity and ease of modification/extension. That said, I think the diagnostics could use some work; factoring out commonalities there (and separating them from the typing logic) would be ideal. I've opted not to include that here both since it'd make this refactor less obvious and since it affects test output. Also, this doesn't get quite as fine-grained as Typing Rust Patterns, so there's some instances where certain rules are conflated. I'd prefer to minimize dead/untested codepaths for rulesets we're not interested in, so as a compromise I've added comments wherever some aspect of the typing rules is assumed from another. I'm not totally happy with it, but I think it's at least better than plain checks against the feature gates and edition. --- compiler/rustc_hir_typeck/src/pat.rs | 173 +++++++++++++++++++-------- 1 file changed, 120 insertions(+), 53 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index 391acce97bd9d..d1153821e6212 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -21,6 +21,7 @@ use rustc_middle::{bug, span_bug}; use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS; use rustc_session::parse::feature_err; use rustc_span::edit_distance::find_best_match_for_name; +use rustc_span::edition::Edition; use rustc_span::hygiene::DesugaringKind; use rustc_span::source_map::Spanned; use rustc_span::{BytePos, DUMMY_SP, Ident, Span, kw, sym}; @@ -213,7 +214,62 @@ impl MutblCap { } } +/// Variations on RFC 3627's Rule 4: when do reference patterns match against inherited references? +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum InheritedRefMatchRule { + /// Reference patterns try to consume the inherited reference first. + /// This assumes reference patterns can always match against an inherited reference. + EatOuter, + /// Reference patterns consume inherited references if matching against a non-reference type. + /// This assumes reference patterns can always match against an inherited reference. + EatInner, + /// Reference patterns consume both layers of reference. + /// Currently, this assumes the stable Rust behavior of not allowing reference patterns to eat + /// an inherited reference alone. This will need an additional field or variant to represent the + /// planned edition <= 2021 behavior of experimental match ergonomics, which does allow that. + EatBoth, +} + impl<'a, 'tcx> FnCtxt<'a, 'tcx> { + /// Experimental pattern feature: after matching against a shared reference, do we limit the + /// default binding mode in subpatterns to be `ref` when it would otherwise be `ref mut`? + /// This corresponds to Rule 3 of RFC 3627. + fn downgrade_mut_inside_shared(&self) -> bool { + // NB: RFC 3627 proposes stabilizing Rule 3 in all editions. If we adopt the same behavior + // across all editions, this may be removed. + self.tcx.features().ref_pat_eat_one_layer_2024() + || self.tcx.features().ref_pat_eat_one_layer_2024_structural() + } + + /// Experimental pattern feature: when do reference patterns match against inherited references? + /// This corresponds to variations on Rule 4 of RFC 3627. + fn ref_pat_matches_inherited_ref(&self, edition: Edition) -> InheritedRefMatchRule { + // NB: The particular rule used here is likely to differ across editions, so calls to this + // may need to become edition checks after match ergonomics stabilize. + if edition.at_least_rust_2024() { + if self.tcx.features().ref_pat_eat_one_layer_2024() { + InheritedRefMatchRule::EatOuter + } else if self.tcx.features().ref_pat_eat_one_layer_2024_structural() { + InheritedRefMatchRule::EatInner + } else { + // Currently, matching against an inherited ref on edition 2024 is an error. + // Use `EatBoth` as a fallback to be similar to stable Rust. + InheritedRefMatchRule::EatBoth + } + } else { + InheritedRefMatchRule::EatBoth + } + } + + /// Experimental pattern feature: do `&` patterns match against `&mut` references, treating them + /// as if they were shared references? This corresponds to Rule 5 of RFC 3627. + fn ref_pat_matches_mut_ref(&self) -> bool { + // NB: RFC 3627 proposes stabilizing Rule 5 in all editions. If we adopt the same behavior + // across all editions, this may be removed. + self.tcx.features().ref_pat_eat_one_layer_2024() + || self.tcx.features().ref_pat_eat_one_layer_2024_structural() + } + /// Type check the given top level pattern against the `expected` type. /// /// If a `Some(span)` is provided and `origin_expr` holds, @@ -474,9 +530,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { }); } - let features = self.tcx.features(); - if features.ref_pat_eat_one_layer_2024() || features.ref_pat_eat_one_layer_2024_structural() - { + if self.downgrade_mut_inside_shared() { def_br = def_br.cap_ref_mutability(max_ref_mutbl.as_mutbl()); } if def_br == ByRef::Yes(Mutability::Not) { @@ -708,6 +762,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // Determine the binding mode... let bm = match user_bind_annot { BindingMode(ByRef::No, Mutability::Mut) if matches!(def_br, ByRef::Yes(_)) => { + // Only mention the experimental `mut_ref` feature if if we're in edition 2024 and + // using other experimental matching features compatible with it. if pat.span.at_least_rust_2024() && (self.tcx.features().ref_pat_eat_one_layer_2024() || self.tcx.features().ref_pat_eat_one_layer_2024_structural()) @@ -2205,51 +2261,68 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { mut pat_info: PatInfo<'_, 'tcx>, ) -> Ty<'tcx> { let tcx = self.tcx; - let features = tcx.features(); - let ref_pat_eat_one_layer_2024 = features.ref_pat_eat_one_layer_2024(); - let ref_pat_eat_one_layer_2024_structural = - features.ref_pat_eat_one_layer_2024_structural(); - - let no_ref_mut_behind_and = - ref_pat_eat_one_layer_2024 || ref_pat_eat_one_layer_2024_structural; - let new_match_ergonomics = pat.span.at_least_rust_2024() && no_ref_mut_behind_and; let pat_prefix_span = inner.span.find_ancestor_inside(pat.span).map(|end| pat.span.until(end)); - if no_ref_mut_behind_and && pat_mutbl == Mutability::Not { - // Prevent the inner pattern from binding with `ref mut`. + let ref_pat_matches_mut_ref = self.ref_pat_matches_mut_ref(); + if ref_pat_matches_mut_ref && pat_mutbl == Mutability::Not { + // If `&` patterns can match against mutable reference types (RFC 3627, Rule 5), we need + // to prevent subpatterns from binding with `ref mut`. Subpatterns of a shared reference + // pattern should have read-only access to the scrutinee, and the borrow checker won't + // catch it in this case. pat_info.max_ref_mutbl = pat_info.max_ref_mutbl.cap_to_weakly_not(pat_prefix_span); } expected = self.try_structurally_resolve_type(pat.span, expected); - if new_match_ergonomics { - if let ByRef::Yes(inh_mut) = pat_info.binding_mode { - if !ref_pat_eat_one_layer_2024 && let ty::Ref(_, _, r_mutbl) = *expected.kind() { - // Don't attempt to consume inherited reference - pat_info.binding_mode = pat_info.binding_mode.cap_ref_mutability(r_mutbl); - } else { + // Determine whether we're consuming an inherited reference and resetting the default + // binding mode, based on edition and enabled experimental features. + if let ByRef::Yes(inh_mut) = pat_info.binding_mode { + match self.ref_pat_matches_inherited_ref(pat.span.edition()) { + InheritedRefMatchRule::EatOuter => { // ref pattern attempts to consume inherited reference if pat_mutbl > inh_mut { // Tried to match inherited `ref` with `&mut` - if !ref_pat_eat_one_layer_2024_structural { - let err_msg = "mismatched types"; - let err = if let Some(span) = pat_prefix_span { - let mut err = self.dcx().struct_span_err(span, err_msg); - err.code(E0308); - err.note("cannot match inherited `&` with `&mut` pattern"); - err.span_suggestion_verbose( - span, - "replace this `&mut` pattern with `&`", - "&", - Applicability::MachineApplicable, - ); - err - } else { - self.dcx().struct_span_err(pat.span, err_msg) - }; - err.emit(); + // NB: This assumes that `&` patterns can match against mutable references + // (RFC 3627, Rule 5). If we implement a pattern typing ruleset with Rule 4E + // but not Rule 5, we'll need to check that here. + let err_msg = "mismatched types"; + let err = if let Some(span) = pat_prefix_span { + let mut err = self.dcx().struct_span_err(span, err_msg); + err.code(E0308); + err.note("cannot match inherited `&` with `&mut` pattern"); + err.span_suggestion_verbose( + span, + "replace this `&mut` pattern with `&`", + "&", + Applicability::MachineApplicable, + ); + err + } else { + self.dcx().struct_span_err(pat.span, err_msg) + }; + err.emit(); + } + pat_info.binding_mode = ByRef::No; + self.typeck_results.borrow_mut().skipped_ref_pats_mut().insert(pat.hir_id); + self.check_pat(inner, expected, pat_info); + return expected; + } + InheritedRefMatchRule::EatInner => { + if let ty::Ref(_, _, r_mutbl) = *expected.kind() { + // Match against the reference type; don't consume the inherited ref. + pat_info.binding_mode = pat_info.binding_mode.cap_ref_mutability(r_mutbl); + } else { + // The expected type isn't a reference, so match against the inherited ref. + if pat_mutbl > inh_mut { + // We can't match an inherited shared reference with `&mut`. This will + // be a type error later, since we're matching a reference pattern + // against a non-reference type. + // NB: This assumes that `&` patterns can match against mutable + // references (RFC 3627, Rule 5). If we implement a pattern typing + // ruleset with Rule 4 but not Rule 5, we'll need to check that here. + } else { pat_info.binding_mode = ByRef::No; self.typeck_results .borrow_mut() @@ -2258,24 +2331,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.check_pat(inner, expected, pat_info); return expected; } - } else { - pat_info.binding_mode = ByRef::No; - self.typeck_results.borrow_mut().skipped_ref_pats_mut().insert(pat.hir_id); - self.check_pat(inner, expected, pat_info); - return expected; } } - } - } else { - // Reset binding mode on old editions - if pat_info.binding_mode != ByRef::No { - pat_info.binding_mode = ByRef::No; - self.add_rust_2024_migration_desugared_pat( - pat_info.top_info.hir_id, - pat.span, - inner.span, - "cannot implicitly match against multiple layers of reference", - ) + InheritedRefMatchRule::EatBoth => { + // Reset binding mode on old editions + pat_info.binding_mode = ByRef::No; + self.add_rust_2024_migration_desugared_pat( + pat_info.top_info.hir_id, + pat.span, + inner.span, + "cannot implicitly match against multiple layers of reference", + ) + } } } @@ -2290,7 +2357,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { debug!("check_pat_ref: expected={:?}", expected); match *expected.kind() { ty::Ref(_, r_ty, r_mutbl) - if (no_ref_mut_behind_and && r_mutbl >= pat_mutbl) + if (ref_pat_matches_mut_ref && r_mutbl >= pat_mutbl) || r_mutbl == pat_mutbl => { if r_mutbl == Mutability::Not { From 5dfb972693236a67316c6ed6a3014a5d1006406c Mon Sep 17 00:00:00 2001 From: dianne Date: Mon, 23 Dec 2024 14:07:39 -0800 Subject: [PATCH 05/16] move the experimental match ergonomics tests to be with the other rfc 3627 tests --- .../experimental}/feature-gate-ref_pat_eat_one_layer_2024.rs | 0 .../experimental}/feature-gate-ref_pat_eat_one_layer_2024.stderr | 0 .../experimental}/ref_pat_eat_one_layer_2021.rs | 0 .../experimental}/ref_pat_eat_one_layer_2021_fail.rs | 0 .../experimental}/ref_pat_eat_one_layer_2021_fail.stderr | 0 .../experimental}/ref_pat_eat_one_layer_2024.rs | 0 .../experimental}/ref_pat_eat_one_layer_2024_fail.classic.stderr | 0 .../experimental}/ref_pat_eat_one_layer_2024_fail.rs | 0 .../ref_pat_eat_one_layer_2024_fail.structural.stderr | 0 .../experimental}/ref_pat_eat_one_layer_2024_fail2.rs | 0 .../experimental}/ref_pat_eat_one_layer_2024_fail2.stderr | 0 .../ref_pat_eat_one_layer_2024_ref_mut_inside_and.fixed | 0 .../ref_pat_eat_one_layer_2024_ref_mut_inside_and.rs | 0 .../ref_pat_eat_one_layer_2024_ref_mut_inside_and.stderr | 0 14 files changed, 0 insertions(+), 0 deletions(-) rename tests/ui/{match/ref_pat_eat_one_layer_2024 => pattern/rfc-3627-match-ergonomics-2024/experimental}/feature-gate-ref_pat_eat_one_layer_2024.rs (100%) rename tests/ui/{match/ref_pat_eat_one_layer_2024 => pattern/rfc-3627-match-ergonomics-2024/experimental}/feature-gate-ref_pat_eat_one_layer_2024.stderr (100%) rename tests/ui/{match/ref_pat_eat_one_layer_2024 => pattern/rfc-3627-match-ergonomics-2024/experimental}/ref_pat_eat_one_layer_2021.rs (100%) rename tests/ui/{match/ref_pat_eat_one_layer_2024 => pattern/rfc-3627-match-ergonomics-2024/experimental}/ref_pat_eat_one_layer_2021_fail.rs (100%) rename tests/ui/{match/ref_pat_eat_one_layer_2024 => pattern/rfc-3627-match-ergonomics-2024/experimental}/ref_pat_eat_one_layer_2021_fail.stderr (100%) rename tests/ui/{match/ref_pat_eat_one_layer_2024 => pattern/rfc-3627-match-ergonomics-2024/experimental}/ref_pat_eat_one_layer_2024.rs (100%) rename tests/ui/{match/ref_pat_eat_one_layer_2024 => pattern/rfc-3627-match-ergonomics-2024/experimental}/ref_pat_eat_one_layer_2024_fail.classic.stderr (100%) rename tests/ui/{match/ref_pat_eat_one_layer_2024 => pattern/rfc-3627-match-ergonomics-2024/experimental}/ref_pat_eat_one_layer_2024_fail.rs (100%) rename tests/ui/{match/ref_pat_eat_one_layer_2024 => pattern/rfc-3627-match-ergonomics-2024/experimental}/ref_pat_eat_one_layer_2024_fail.structural.stderr (100%) rename tests/ui/{match/ref_pat_eat_one_layer_2024 => pattern/rfc-3627-match-ergonomics-2024/experimental}/ref_pat_eat_one_layer_2024_fail2.rs (100%) rename tests/ui/{match/ref_pat_eat_one_layer_2024 => pattern/rfc-3627-match-ergonomics-2024/experimental}/ref_pat_eat_one_layer_2024_fail2.stderr (100%) rename tests/ui/{match/ref_pat_eat_one_layer_2024 => pattern/rfc-3627-match-ergonomics-2024/experimental}/ref_pat_eat_one_layer_2024_ref_mut_inside_and.fixed (100%) rename tests/ui/{match/ref_pat_eat_one_layer_2024 => pattern/rfc-3627-match-ergonomics-2024/experimental}/ref_pat_eat_one_layer_2024_ref_mut_inside_and.rs (100%) rename tests/ui/{match/ref_pat_eat_one_layer_2024 => pattern/rfc-3627-match-ergonomics-2024/experimental}/ref_pat_eat_one_layer_2024_ref_mut_inside_and.stderr (100%) diff --git a/tests/ui/match/ref_pat_eat_one_layer_2024/feature-gate-ref_pat_eat_one_layer_2024.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/feature-gate-ref_pat_eat_one_layer_2024.rs similarity index 100% rename from tests/ui/match/ref_pat_eat_one_layer_2024/feature-gate-ref_pat_eat_one_layer_2024.rs rename to tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/feature-gate-ref_pat_eat_one_layer_2024.rs diff --git a/tests/ui/match/ref_pat_eat_one_layer_2024/feature-gate-ref_pat_eat_one_layer_2024.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/feature-gate-ref_pat_eat_one_layer_2024.stderr similarity index 100% rename from tests/ui/match/ref_pat_eat_one_layer_2024/feature-gate-ref_pat_eat_one_layer_2024.stderr rename to tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/feature-gate-ref_pat_eat_one_layer_2024.stderr diff --git a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2021.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2021.rs similarity index 100% rename from tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2021.rs rename to tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2021.rs diff --git a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2021_fail.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2021_fail.rs similarity index 100% rename from tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2021_fail.rs rename to tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2021_fail.rs diff --git a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2021_fail.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2021_fail.stderr similarity index 100% rename from tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2021_fail.stderr rename to tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2021_fail.stderr diff --git a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024.rs similarity index 100% rename from tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024.rs rename to tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024.rs diff --git a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.classic.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail.classic.stderr similarity index 100% rename from tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.classic.stderr rename to tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail.classic.stderr diff --git a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail.rs similarity index 100% rename from tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.rs rename to tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail.rs diff --git a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.structural.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail.structural.stderr similarity index 100% rename from tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.structural.stderr rename to tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail.structural.stderr diff --git a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail2.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail2.rs similarity index 100% rename from tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail2.rs rename to tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail2.rs diff --git a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail2.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail2.stderr similarity index 100% rename from tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail2.stderr rename to tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail2.stderr diff --git a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_ref_mut_inside_and.fixed b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_ref_mut_inside_and.fixed similarity index 100% rename from tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_ref_mut_inside_and.fixed rename to tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_ref_mut_inside_and.fixed diff --git a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_ref_mut_inside_and.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_ref_mut_inside_and.rs similarity index 100% rename from tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_ref_mut_inside_and.rs rename to tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_ref_mut_inside_and.rs diff --git a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_ref_mut_inside_and.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_ref_mut_inside_and.stderr similarity index 100% rename from tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_ref_mut_inside_and.stderr rename to tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_ref_mut_inside_and.stderr From b350d948779efa0cd2d139868ac62ba14afa5838 Mon Sep 17 00:00:00 2001 From: dianne Date: Mon, 23 Dec 2024 18:38:14 -0800 Subject: [PATCH 06/16] reorganize and comment some of the experimental pattern typing tests This only includes previously existing tests (with a couple duplicates removed). I plan on adding more comprarisons where the rules differ once I've updated the pattern typing rules. I also haven't touched the tests for new rules in old editions; I'll see how best to handle that once those rules are updated as well. --- ....stderr => borrowck-errors.classic.stderr} | 4 +- ...layer_2024_fail2.rs => borrowck-errors.rs} | 5 +- .../borrowck-errors.structural.stderr | 25 +++ ...ot-mutably-deref-shared-ref.classic.stderr | 58 +++++ .../cannot-mutably-deref-shared-ref.rs | 23 ++ ...mutably-deref-shared-ref.structural.stderr | 58 +++++ .../experimental/mut-ref-mut.classic.stderr | 23 ++ .../experimental/mut-ref-mut.rs | 18 ++ .../mut-ref-mut.structural.stderr | 23 ++ .../pattern-errors.classic.stderr | 111 ++++++++++ .../experimental/pattern-errors.rs | 46 ++++ .../pattern-errors.structural.stderr | 89 ++++++++ ...pat_eat_one_layer_2024_fail.classic.stderr | 199 ------------------ .../ref_pat_eat_one_layer_2024_fail.rs | 83 -------- ..._eat_one_layer_2024_fail.structural.stderr | 180 ---------------- .../experimental/trait-selection-sanity.rs | 30 +++ ...trait-selection-sanity.without_impl.stderr | 16 ++ ...yer_2024.rs => well-typed-edition-2024.rs} | 35 +-- 18 files changed, 529 insertions(+), 497 deletions(-) rename tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/{ref_pat_eat_one_layer_2024_fail2.stderr => borrowck-errors.classic.stderr} (88%) rename tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/{ref_pat_eat_one_layer_2024_fail2.rs => borrowck-errors.rs} (54%) create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.structural.stderr create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/cannot-mutably-deref-shared-ref.classic.stderr create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/cannot-mutably-deref-shared-ref.rs create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/cannot-mutably-deref-shared-ref.structural.stderr create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.classic.stderr create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.rs create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.structural.stderr create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic.stderr create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.rs create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural.stderr delete mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail.classic.stderr delete mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail.rs delete mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail.structural.stderr create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/trait-selection-sanity.rs create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/trait-selection-sanity.without_impl.stderr rename tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/{ref_pat_eat_one_layer_2024.rs => well-typed-edition-2024.rs} (71%) diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail2.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.classic.stderr similarity index 88% rename from tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail2.stderr rename to tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.classic.stderr index a8b813941109c..c62461140750b 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail2.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.classic.stderr @@ -1,5 +1,5 @@ error[E0507]: cannot move out of a shared reference - --> $DIR/ref_pat_eat_one_layer_2024_fail2.rs:6:29 + --> $DIR/borrowck-errors.rs:9:29 | LL | if let Some(&Some(x)) = Some(&Some(&mut 0)) { | - ^^^^^^^^^^^^^^^^^^^ @@ -14,7 +14,7 @@ LL + if let Some(Some(x)) = Some(&Some(&mut 0)) { | error[E0596]: cannot borrow data in a `&` reference as mutable - --> $DIR/ref_pat_eat_one_layer_2024_fail2.rs:11:10 + --> $DIR/borrowck-errors.rs:14:10 | LL | let &ref mut x = &0; | ^^^^^^^^^ cannot borrow as mutable diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail2.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.rs similarity index 54% rename from tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail2.rs rename to tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.rs index 79403b1936528..a01e9ca265760 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail2.rs +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.rs @@ -1,6 +1,9 @@ //@ edition: 2024 +//@ revisions: classic structural +//! Tests for pattern errors not handled by the pattern typing rules, but by borrowck. #![allow(incomplete_features)] -#![feature(ref_pat_eat_one_layer_2024)] +#![cfg_attr(classic, feature(ref_pat_eat_one_layer_2024))] +#![cfg_attr(structural, feature(ref_pat_eat_one_layer_2024_structural))] pub fn main() { if let Some(&Some(x)) = Some(&Some(&mut 0)) { diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.structural.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.structural.stderr new file mode 100644 index 0000000000000..c62461140750b --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.structural.stderr @@ -0,0 +1,25 @@ +error[E0507]: cannot move out of a shared reference + --> $DIR/borrowck-errors.rs:9:29 + | +LL | if let Some(&Some(x)) = Some(&Some(&mut 0)) { + | - ^^^^^^^^^^^^^^^^^^^ + | | + | data moved here + | move occurs because `x` has type `&mut u32`, which does not implement the `Copy` trait + | +help: consider removing the borrow + | +LL - if let Some(&Some(x)) = Some(&Some(&mut 0)) { +LL + if let Some(Some(x)) = Some(&Some(&mut 0)) { + | + +error[E0596]: cannot borrow data in a `&` reference as mutable + --> $DIR/borrowck-errors.rs:14:10 + | +LL | let &ref mut x = &0; + | ^^^^^^^^^ cannot borrow as mutable + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0507, E0596. +For more information about an error, try `rustc --explain E0507`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/cannot-mutably-deref-shared-ref.classic.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/cannot-mutably-deref-shared-ref.classic.stderr new file mode 100644 index 0000000000000..89a52ba7d1ddb --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/cannot-mutably-deref-shared-ref.classic.stderr @@ -0,0 +1,58 @@ +error[E0308]: mismatched types + --> $DIR/cannot-mutably-deref-shared-ref.rs:9:9 + | +LL | let &mut _ = &&0; + | ^^^^^^ --- this expression has type `&&{integer}` + | | + | types differ in mutability + | + = note: expected reference `&&{integer}` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/cannot-mutably-deref-shared-ref.rs:12:9 + | +LL | let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&0; + | ^^^^^^ ----------------------------- this expression has type `&&&&&&&&&&&&&&&&&&&&&&&&&&&&{integer}` + | | + | types differ in mutability + | + = note: expected reference `&&&&&&&&&&&&&&&&&&&&&&&&&&&&{integer}` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/cannot-mutably-deref-shared-ref.rs:15:9 + | +LL | let &mut _ = &&mut 0; + | ^^^^^^ ------- this expression has type `&&mut {integer}` + | | + | types differ in mutability + | + = note: expected reference `&&mut {integer}` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/cannot-mutably-deref-shared-ref.rs:18:9 + | +LL | let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&mut 0; + | ^^^^^^ --------------------------------- this expression has type `&&&&&&&&&&&&&&&&&&&&&&&&&&&&mut {integer}` + | | + | types differ in mutability + | + = note: expected reference `&&&&&&&&&&&&&&&&&&&&&&&&&&&&mut {integer}` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/cannot-mutably-deref-shared-ref.rs:21:14 + | +LL | let &mut &mut &mut &mut _ = &mut &&&&mut &&&mut &mut 0; + | ^^^^^^^^^^^^^^^^ -------------------------- this expression has type `&mut &&&&mut &&&mut &mut {integer}` + | | + | types differ in mutability + | + = note: expected reference `&&&&mut &&&mut &mut {integer}` + found mutable reference `&mut _` + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/cannot-mutably-deref-shared-ref.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/cannot-mutably-deref-shared-ref.rs new file mode 100644 index 0000000000000..e22bd1f8f6c99 --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/cannot-mutably-deref-shared-ref.rs @@ -0,0 +1,23 @@ +//@ edition: 2024 +//@ revisions: classic structural +//! Test that `&mut` patterns don't match shared reference types under new typing rules in Rust 2024 +#![allow(incomplete_features)] +#![cfg_attr(classic, feature(ref_pat_eat_one_layer_2024))] +#![cfg_attr(structural, feature(ref_pat_eat_one_layer_2024_structural))] + +pub fn main() { + let &mut _ = &&0; + //~^ ERROR: mismatched types + + let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&0; + //~^ ERROR: mismatched types + + let &mut _ = &&mut 0; + //~^ ERROR: mismatched types + + let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&mut 0; + //~^ ERROR: mismatched types + + let &mut &mut &mut &mut _ = &mut &&&&mut &&&mut &mut 0; + //~^ ERROR: mismatched types +} diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/cannot-mutably-deref-shared-ref.structural.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/cannot-mutably-deref-shared-ref.structural.stderr new file mode 100644 index 0000000000000..89a52ba7d1ddb --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/cannot-mutably-deref-shared-ref.structural.stderr @@ -0,0 +1,58 @@ +error[E0308]: mismatched types + --> $DIR/cannot-mutably-deref-shared-ref.rs:9:9 + | +LL | let &mut _ = &&0; + | ^^^^^^ --- this expression has type `&&{integer}` + | | + | types differ in mutability + | + = note: expected reference `&&{integer}` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/cannot-mutably-deref-shared-ref.rs:12:9 + | +LL | let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&0; + | ^^^^^^ ----------------------------- this expression has type `&&&&&&&&&&&&&&&&&&&&&&&&&&&&{integer}` + | | + | types differ in mutability + | + = note: expected reference `&&&&&&&&&&&&&&&&&&&&&&&&&&&&{integer}` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/cannot-mutably-deref-shared-ref.rs:15:9 + | +LL | let &mut _ = &&mut 0; + | ^^^^^^ ------- this expression has type `&&mut {integer}` + | | + | types differ in mutability + | + = note: expected reference `&&mut {integer}` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/cannot-mutably-deref-shared-ref.rs:18:9 + | +LL | let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&mut 0; + | ^^^^^^ --------------------------------- this expression has type `&&&&&&&&&&&&&&&&&&&&&&&&&&&&mut {integer}` + | | + | types differ in mutability + | + = note: expected reference `&&&&&&&&&&&&&&&&&&&&&&&&&&&&mut {integer}` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/cannot-mutably-deref-shared-ref.rs:21:14 + | +LL | let &mut &mut &mut &mut _ = &mut &&&&mut &&&mut &mut 0; + | ^^^^^^^^^^^^^^^^ -------------------------- this expression has type `&mut &&&&mut &&&mut &mut {integer}` + | | + | types differ in mutability + | + = note: expected reference `&&&&mut &&&mut &mut {integer}` + found mutable reference `&mut _` + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.classic.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.classic.stderr new file mode 100644 index 0000000000000..43560a1803026 --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.classic.stderr @@ -0,0 +1,23 @@ +error[E0658]: binding cannot be both mutable and by-reference + --> $DIR/mut-ref-mut.rs:11:13 + | +LL | let Foo(mut a) = &Foo(0); + | ^^^^ + | + = note: see issue #123076 for more information + = help: add `#![feature(mut_ref)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0658]: binding cannot be both mutable and by-reference + --> $DIR/mut-ref-mut.rs:15:13 + | +LL | let Foo(mut a) = &mut Foo(0); + | ^^^^ + | + = note: see issue #123076 for more information + = help: add `#![feature(mut_ref)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0658`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.rs new file mode 100644 index 0000000000000..786587984ba41 --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.rs @@ -0,0 +1,18 @@ +//@ edition: 2024 +//@ revisions: classic structural +//! Test diagnostics for binding with `mut` when the default binding mode is by-ref. +#![allow(incomplete_features)] +#![cfg_attr(classic, feature(ref_pat_eat_one_layer_2024))] +#![cfg_attr(structural, feature(ref_pat_eat_one_layer_2024_structural))] + +pub fn main() { + struct Foo(u8); + + let Foo(mut a) = &Foo(0); + //~^ ERROR: binding cannot be both mutable and by-reference + a = &42; + + let Foo(mut a) = &mut Foo(0); + //~^ ERROR: binding cannot be both mutable and by-reference + a = &mut 42; +} diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.structural.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.structural.stderr new file mode 100644 index 0000000000000..43560a1803026 --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.structural.stderr @@ -0,0 +1,23 @@ +error[E0658]: binding cannot be both mutable and by-reference + --> $DIR/mut-ref-mut.rs:11:13 + | +LL | let Foo(mut a) = &Foo(0); + | ^^^^ + | + = note: see issue #123076 for more information + = help: add `#![feature(mut_ref)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error[E0658]: binding cannot be both mutable and by-reference + --> $DIR/mut-ref-mut.rs:15:13 + | +LL | let Foo(mut a) = &mut Foo(0); + | ^^^^ + | + = note: see issue #123076 for more information + = help: add `#![feature(mut_ref)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0658`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic.stderr new file mode 100644 index 0000000000000..2bc3ecb7636d0 --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic.stderr @@ -0,0 +1,111 @@ +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:15:17 + | +LL | if let Some(&mut x) = &Some(&mut 0) { + | ^^^^^ + | + = note: cannot match inherited `&` with `&mut` pattern +help: replace this `&mut` pattern with `&` + | +LL | if let Some(&x) = &Some(&mut 0) { + | ~ + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:19:17 + | +LL | if let Some(&mut Some(&x)) = &Some(&mut Some(0)) { + | ^^^^^ + | + = note: cannot match inherited `&` with `&mut` pattern +help: replace this `&mut` pattern with `&` + | +LL | if let Some(&Some(&x)) = &Some(&mut Some(0)) { + | ~ + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:23:22 + | +LL | if let Some(Some(&mut x)) = &Some(Some(&mut 0)) { + | ^^^^^ + | + = note: cannot match inherited `&` with `&mut` pattern +help: replace this `&mut` pattern with `&` + | +LL | if let Some(Some(&x)) = &Some(Some(&mut 0)) { + | ~ + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:28:17 + | +LL | if let Some(&mut Some(&_)) = &Some(&Some(0)) { + | ^^^^^ + | + = note: cannot match inherited `&` with `&mut` pattern +help: replace this `&mut` pattern with `&` + | +LL | if let Some(&Some(&_)) = &Some(&Some(0)) { + | ~ + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:31:23 + | +LL | if let Some(&Some(&mut _)) = &Some(&mut Some(0)) { + | ^^^^^ + | + = note: cannot match inherited `&` with `&mut` pattern +help: replace this `&mut` pattern with `&` + | +LL | if let Some(&Some(&_)) = &Some(&mut Some(0)) { + | ~ + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:34:23 + | +LL | if let Some(&Some(&mut _)) = &mut Some(&Some(0)) { + | ^^^^^ + | + = note: cannot match inherited `&` with `&mut` pattern +help: replace this `&mut` pattern with `&` + | +LL | if let Some(&Some(&_)) = &mut Some(&Some(0)) { + | ~ + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:37:29 + | +LL | if let Some(&Some(Some((&mut _)))) = &Some(Some(&mut Some(0))) { + | ^^^^^ + | + = note: cannot match inherited `&` with `&mut` pattern +help: replace this `&mut` pattern with `&` + | +LL | if let Some(&Some(Some((&_)))) = &Some(Some(&mut Some(0))) { + | ~ + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:40:17 + | +LL | if let Some(&mut Some(x)) = &Some(Some(0)) { + | ^^^^^ + | + = note: cannot match inherited `&` with `&mut` pattern +help: replace this `&mut` pattern with `&` + | +LL | if let Some(&Some(x)) = &Some(Some(0)) { + | ~ + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:43:17 + | +LL | if let Some(&mut Some(x)) = &Some(Some(0)) { + | ^^^^^ + | + = note: cannot match inherited `&` with `&mut` pattern +help: replace this `&mut` pattern with `&` + | +LL | if let Some(&Some(x)) = &Some(Some(0)) { + | ~ + +error: aborting due to 9 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.rs new file mode 100644 index 0000000000000..3535ba9c7019a --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.rs @@ -0,0 +1,46 @@ +//@ edition: 2024 +//@ revisions: classic structural +//! Test cases for poorly-typed patterns in edition 2024 which are caught by HIR typeck. These must +//! be separate from cases caught by MIR borrowck or the latter errors may not be emitted. +#![allow(incomplete_features)] +#![cfg_attr(classic, feature(ref_pat_eat_one_layer_2024))] +#![cfg_attr(structural, feature(ref_pat_eat_one_layer_2024_structural))] + +pub fn main() { + if let Some(&mut x) = &mut Some(&0) { + //[structural]~^ ERROR: mismatched types + let _: &u32 = x; + } + + if let Some(&mut x) = &Some(&mut 0) { + //[classic]~^ ERROR: mismatched types + let _: &u32 = x; + } + if let Some(&mut Some(&x)) = &Some(&mut Some(0)) { + //[classic]~^ ERROR: mismatched types + let _: u32 = x; + } + if let Some(Some(&mut x)) = &Some(Some(&mut 0)) { + //[classic]~^ ERROR: mismatched types + let _: &u32 = x; + } + + if let Some(&mut Some(&_)) = &Some(&Some(0)) { + //~^ ERROR: mismatched types + } + if let Some(&Some(&mut _)) = &Some(&mut Some(0)) { + //~^ ERROR: mismatched types + } + if let Some(&Some(&mut _)) = &mut Some(&Some(0)) { + //~^ ERROR: mismatched types + } + if let Some(&Some(Some((&mut _)))) = &Some(Some(&mut Some(0))) { + //~^ ERROR: mismatched types + } + if let Some(&mut Some(x)) = &Some(Some(0)) { + //~^ ERROR: mismatched types + } + if let Some(&mut Some(x)) = &Some(Some(0)) { + //~^ ERROR: mismatched types + } +} diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural.stderr new file mode 100644 index 0000000000000..59d65553fae8f --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural.stderr @@ -0,0 +1,89 @@ +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:10:17 + | +LL | if let Some(&mut x) = &mut Some(&0) { + | ^^^^^^ ------------- this expression has type `&mut Option<&{integer}>` + | | + | types differ in mutability + | + = note: expected reference `&{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/pattern-errors.rs:10:17 + | +LL | if let Some(&mut x) = &mut Some(&0) { + | ^^^^^^ +help: consider removing `&mut` from the pattern + | +LL | if let Some(x) = &mut Some(&0) { + | ~ + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:28:17 + | +LL | if let Some(&mut Some(&_)) = &Some(&Some(0)) { + | ^^^^^^^^^^^^^ --------------- this expression has type `&Option<&Option<{integer}>>` + | | + | types differ in mutability + | + = note: expected reference `&Option<{integer}>` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:31:23 + | +LL | if let Some(&Some(&mut _)) = &Some(&mut Some(0)) { + | ^^^^^^ ------------------- this expression has type `&Option<&mut Option<{integer}>>` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:34:23 + | +LL | if let Some(&Some(&mut _)) = &mut Some(&Some(0)) { + | ^^^^^^ ------------------- this expression has type `&mut Option<&Option<{integer}>>` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:37:29 + | +LL | if let Some(&Some(Some((&mut _)))) = &Some(Some(&mut Some(0))) { + | ^^^^^^ ------------------------- this expression has type `&Option>>` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:40:17 + | +LL | if let Some(&mut Some(x)) = &Some(Some(0)) { + | ^^^^^^^^^^^^ -------------- this expression has type `&Option>` + | | + | expected `Option<{integer}>`, found `&mut _` + | + = note: expected enum `Option<{integer}>` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:43:17 + | +LL | if let Some(&mut Some(x)) = &Some(Some(0)) { + | ^^^^^^^^^^^^ -------------- this expression has type `&Option>` + | | + | expected `Option<{integer}>`, found `&mut _` + | + = note: expected enum `Option<{integer}>` + found mutable reference `&mut _` + +error: aborting due to 7 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail.classic.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail.classic.stderr deleted file mode 100644 index a37316b3097b0..0000000000000 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail.classic.stderr +++ /dev/null @@ -1,199 +0,0 @@ -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:8:17 - | -LL | if let Some(&mut Some(&_)) = &Some(&Some(0)) { - | ^^^^^ - | - = note: cannot match inherited `&` with `&mut` pattern -help: replace this `&mut` pattern with `&` - | -LL | if let Some(&Some(&_)) = &Some(&Some(0)) { - | ~ - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:11:23 - | -LL | if let Some(&Some(&mut _)) = &Some(&mut Some(0)) { - | ^^^^^ - | - = note: cannot match inherited `&` with `&mut` pattern -help: replace this `&mut` pattern with `&` - | -LL | if let Some(&Some(&_)) = &Some(&mut Some(0)) { - | ~ - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:15:27 - | -LL | let _: &mut u32 = x; - | -------- ^ types differ in mutability - | | - | expected due to this - | - = note: expected mutable reference `&mut u32` - found reference `&{integer}` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:18:23 - | -LL | if let Some(&Some(&mut _)) = &mut Some(&Some(0)) { - | ^^^^^ - | - = note: cannot match inherited `&` with `&mut` pattern -help: replace this `&mut` pattern with `&` - | -LL | if let Some(&Some(&_)) = &mut Some(&Some(0)) { - | ~ - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:21:29 - | -LL | if let Some(&Some(Some((&mut _)))) = &Some(Some(&mut Some(0))) { - | ^^^^^ - | - = note: cannot match inherited `&` with `&mut` pattern -help: replace this `&mut` pattern with `&` - | -LL | if let Some(&Some(Some((&_)))) = &Some(Some(&mut Some(0))) { - | ~ - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:24:17 - | -LL | if let Some(&mut Some(x)) = &Some(Some(0)) { - | ^^^^^ - | - = note: cannot match inherited `&` with `&mut` pattern -help: replace this `&mut` pattern with `&` - | -LL | if let Some(&Some(x)) = &Some(Some(0)) { - | ~ - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:27:17 - | -LL | if let Some(&mut Some(x)) = &Some(Some(0)) { - | ^^^^^ - | - = note: cannot match inherited `&` with `&mut` pattern -help: replace this `&mut` pattern with `&` - | -LL | if let Some(&Some(x)) = &Some(Some(0)) { - | ~ - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:31:9 - | -LL | let &mut _ = &&0; - | ^^^^^^ --- this expression has type `&&{integer}` - | | - | types differ in mutability - | - = note: expected reference `&&{integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:34:9 - | -LL | let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&0; - | ^^^^^^ ----------------------------- this expression has type `&&&&&&&&&&&&&&&&&&&&&&&&&&&&{integer}` - | | - | types differ in mutability - | - = note: expected reference `&&&&&&&&&&&&&&&&&&&&&&&&&&&&{integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:37:17 - | -LL | if let Some(&mut Some(&_)) = &Some(&mut Some(0)) { - | ^^^^^ - | - = note: cannot match inherited `&` with `&mut` pattern -help: replace this `&mut` pattern with `&` - | -LL | if let Some(&Some(&_)) = &Some(&mut Some(0)) { - | ~ - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:41:22 - | -LL | if let Some(Some(&mut x)) = &Some(Some(&mut 0)) { - | ^^^^^ - | - = note: cannot match inherited `&` with `&mut` pattern -help: replace this `&mut` pattern with `&` - | -LL | if let Some(Some(&x)) = &Some(Some(&mut 0)) { - | ~ - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:45:9 - | -LL | let &mut _ = &&mut 0; - | ^^^^^^ ------- this expression has type `&&mut {integer}` - | | - | types differ in mutability - | - = note: expected reference `&&mut {integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:48:9 - | -LL | let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&mut 0; - | ^^^^^^ --------------------------------- this expression has type `&&&&&&&&&&&&&&&&&&&&&&&&&&&&mut {integer}` - | | - | types differ in mutability - | - = note: expected reference `&&&&&&&&&&&&&&&&&&&&&&&&&&&&mut {integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:51:14 - | -LL | let &mut &mut &mut &mut _ = &mut &&&&mut &&&mut &mut 0; - | ^^^^^^^^^^^^^^^^ -------------------------- this expression has type `&mut &&&&mut &&&mut &mut {integer}` - | | - | types differ in mutability - | - = note: expected reference `&&&&mut &&&mut &mut {integer}` - found mutable reference `&mut _` - -error[E0658]: binding cannot be both mutable and by-reference - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:60:13 - | -LL | let Foo(mut a) = &Foo(0); - | ^^^^ - | - = note: see issue #123076 for more information - = help: add `#![feature(mut_ref)]` to the crate attributes to enable - = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date - -error[E0658]: binding cannot be both mutable and by-reference - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:64:13 - | -LL | let Foo(mut a) = &mut Foo(0); - | ^^^^ - | - = note: see issue #123076 for more information - = help: add `#![feature(mut_ref)]` to the crate attributes to enable - = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date - -error[E0277]: the trait bound `&_: main::Ref` is not satisfied - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:82:14 - | -LL | let &_ = generic(); - | ^^^^^^^^^ the trait `main::Ref` is not implemented for `&_` - | - = help: the trait `main::Ref` is implemented for `&'static mut [(); 0]` -note: required by a bound in `generic` - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:68:19 - | -LL | fn generic() -> R { - | ^^^ required by this bound in `generic` - -error: aborting due to 17 previous errors - -Some errors have detailed explanations: E0277, E0308, E0658. -For more information about an error, try `rustc --explain E0277`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail.rs deleted file mode 100644 index ef4a92eb4b3ce..0000000000000 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail.rs +++ /dev/null @@ -1,83 +0,0 @@ -//@ edition: 2024 -//@ revisions: classic structural -#![allow(incomplete_features)] -#![cfg_attr(classic, feature(ref_pat_eat_one_layer_2024))] -#![cfg_attr(structural, feature(ref_pat_eat_one_layer_2024_structural))] - -pub fn main() { - if let Some(&mut Some(&_)) = &Some(&Some(0)) { - //~^ ERROR: mismatched types - } - if let Some(&Some(&mut _)) = &Some(&mut Some(0)) { - //~^ ERROR: mismatched types - } - if let Some(&Some(x)) = &mut Some(&Some(0)) { - let _: &mut u32 = x; - //~^ ERROR: mismatched types - } - if let Some(&Some(&mut _)) = &mut Some(&Some(0)) { - //~^ ERROR: mismatched types - } - if let Some(&Some(Some((&mut _)))) = &Some(Some(&mut Some(0))) { - //~^ ERROR: mismatched types - } - if let Some(&mut Some(x)) = &Some(Some(0)) { - //~^ ERROR: mismatched types - } - if let Some(&mut Some(x)) = &Some(Some(0)) { - //~^ ERROR: mismatched types - } - - let &mut _ = &&0; - //~^ ERROR: mismatched types - - let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&0; - //~^ ERROR: mismatched types - - if let Some(&mut Some(&_)) = &Some(&mut Some(0)) { - //[classic]~^ ERROR: mismatched types - } - - if let Some(Some(&mut x)) = &Some(Some(&mut 0)) { - //[classic]~^ ERROR: mismatched types - } - - let &mut _ = &&mut 0; - //~^ ERROR: mismatched types - - let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&mut 0; - //~^ ERROR: mismatched types - - let &mut &mut &mut &mut _ = &mut &&&&mut &&&mut &mut 0; - //~^ ERROR: mismatched types - - if let Some(&mut _) = &mut Some(&0) { - //[structural]~^ ERROR - } - - struct Foo(u8); - - let Foo(mut a) = &Foo(0); - //~^ ERROR: binding cannot be both mutable and by-reference - a = &42; - - let Foo(mut a) = &mut Foo(0); - //~^ ERROR: binding cannot be both mutable and by-reference - a = &mut 42; - - fn generic() -> R { - R::meow() - } - - trait Ref: Sized { - fn meow() -> Self; - } - - impl Ref for &'static mut [(); 0] { - fn meow() -> Self { - &mut [] - } - } - - let &_ = generic(); //~ERROR: the trait bound `&_: main::Ref` is not satisfied [E0277] -} diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail.structural.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail.structural.stderr deleted file mode 100644 index 2f62e9974fa3f..0000000000000 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_fail.structural.stderr +++ /dev/null @@ -1,180 +0,0 @@ -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:8:17 - | -LL | if let Some(&mut Some(&_)) = &Some(&Some(0)) { - | ^^^^^^^^^^^^^ --------------- this expression has type `&Option<&Option<{integer}>>` - | | - | types differ in mutability - | - = note: expected reference `&Option<{integer}>` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:11:23 - | -LL | if let Some(&Some(&mut _)) = &Some(&mut Some(0)) { - | ^^^^^^ ------------------- this expression has type `&Option<&mut Option<{integer}>>` - | | - | expected integer, found `&mut _` - | - = note: expected type `{integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:15:27 - | -LL | let _: &mut u32 = x; - | -------- ^ types differ in mutability - | | - | expected due to this - | - = note: expected mutable reference `&mut u32` - found reference `&{integer}` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:18:23 - | -LL | if let Some(&Some(&mut _)) = &mut Some(&Some(0)) { - | ^^^^^^ ------------------- this expression has type `&mut Option<&Option<{integer}>>` - | | - | expected integer, found `&mut _` - | - = note: expected type `{integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:21:29 - | -LL | if let Some(&Some(Some((&mut _)))) = &Some(Some(&mut Some(0))) { - | ^^^^^^ ------------------------- this expression has type `&Option>>` - | | - | expected integer, found `&mut _` - | - = note: expected type `{integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:24:17 - | -LL | if let Some(&mut Some(x)) = &Some(Some(0)) { - | ^^^^^^^^^^^^ -------------- this expression has type `&Option>` - | | - | expected `Option<{integer}>`, found `&mut _` - | - = note: expected enum `Option<{integer}>` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:27:17 - | -LL | if let Some(&mut Some(x)) = &Some(Some(0)) { - | ^^^^^^^^^^^^ -------------- this expression has type `&Option>` - | | - | expected `Option<{integer}>`, found `&mut _` - | - = note: expected enum `Option<{integer}>` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:31:9 - | -LL | let &mut _ = &&0; - | ^^^^^^ --- this expression has type `&&{integer}` - | | - | types differ in mutability - | - = note: expected reference `&&{integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:34:9 - | -LL | let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&0; - | ^^^^^^ ----------------------------- this expression has type `&&&&&&&&&&&&&&&&&&&&&&&&&&&&{integer}` - | | - | types differ in mutability - | - = note: expected reference `&&&&&&&&&&&&&&&&&&&&&&&&&&&&{integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:45:9 - | -LL | let &mut _ = &&mut 0; - | ^^^^^^ ------- this expression has type `&&mut {integer}` - | | - | types differ in mutability - | - = note: expected reference `&&mut {integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:48:9 - | -LL | let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&mut 0; - | ^^^^^^ --------------------------------- this expression has type `&&&&&&&&&&&&&&&&&&&&&&&&&&&&mut {integer}` - | | - | types differ in mutability - | - = note: expected reference `&&&&&&&&&&&&&&&&&&&&&&&&&&&&mut {integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:51:14 - | -LL | let &mut &mut &mut &mut _ = &mut &&&&mut &&&mut &mut 0; - | ^^^^^^^^^^^^^^^^ -------------------------- this expression has type `&mut &&&&mut &&&mut &mut {integer}` - | | - | types differ in mutability - | - = note: expected reference `&&&&mut &&&mut &mut {integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:54:17 - | -LL | if let Some(&mut _) = &mut Some(&0) { - | ^^^^^^ ------------- this expression has type `&mut Option<&{integer}>` - | | - | types differ in mutability - | - = note: expected reference `&{integer}` - found mutable reference `&mut _` - -error[E0658]: binding cannot be both mutable and by-reference - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:60:13 - | -LL | let Foo(mut a) = &Foo(0); - | ^^^^ - | - = note: see issue #123076 for more information - = help: add `#![feature(mut_ref)]` to the crate attributes to enable - = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date - -error[E0658]: binding cannot be both mutable and by-reference - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:64:13 - | -LL | let Foo(mut a) = &mut Foo(0); - | ^^^^ - | - = note: see issue #123076 for more information - = help: add `#![feature(mut_ref)]` to the crate attributes to enable - = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date - -error[E0277]: the trait bound `&_: main::Ref` is not satisfied - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:82:14 - | -LL | let &_ = generic(); - | ^^^^^^^^^ the trait `main::Ref` is not implemented for `&_` - | - = help: the trait `main::Ref` is implemented for `&'static mut [(); 0]` -note: required by a bound in `generic` - --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:68:19 - | -LL | fn generic() -> R { - | ^^^ required by this bound in `generic` - -error: aborting due to 16 previous errors - -Some errors have detailed explanations: E0277, E0308, E0658. -For more information about an error, try `rustc --explain E0277`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/trait-selection-sanity.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/trait-selection-sanity.rs new file mode 100644 index 0000000000000..cb8fdb489c028 --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/trait-selection-sanity.rs @@ -0,0 +1,30 @@ +//@ edition: 2024 +//@ revisions: with_impl without_impl +//@[with_impl] run-pass +//! Sanity check that experimental new pattern typing rules work as expected with trait selection + +fn main() { + fn generic() -> (R, bool) { + R::meow() + } + + trait Ref: Sized { + fn meow() -> (Self, bool); + } + + #[cfg(with_impl)] + impl Ref for &'static [(); 0] { + fn meow() -> (Self, bool) { + (&[], false) + } + } + + impl Ref for &'static mut [(); 0] { + fn meow() -> (Self, bool) { + (&mut [], true) + } + } + + let (&_, b) = generic(); //[without_impl]~ ERROR: the trait bound `&_: main::Ref` is not satisfied [E0277] + assert!(!b); +} diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/trait-selection-sanity.without_impl.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/trait-selection-sanity.without_impl.stderr new file mode 100644 index 0000000000000..83e45221facc7 --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/trait-selection-sanity.without_impl.stderr @@ -0,0 +1,16 @@ +error[E0277]: the trait bound `&_: main::Ref` is not satisfied + --> $DIR/trait-selection-sanity.rs:28:19 + | +LL | let (&_, b) = generic(); + | ^^^^^^^^^ the trait `main::Ref` is not implemented for `&_` + | + = help: the trait `main::Ref` is implemented for `&'static mut [(); 0]` +note: required by a bound in `generic` + --> $DIR/trait-selection-sanity.rs:7:19 + | +LL | fn generic() -> (R, bool) { + | ^^^ required by this bound in `generic` + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.rs similarity index 71% rename from tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024.rs rename to tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.rs index 70add6f4d44a7..077b52d8f274e 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024.rs +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.rs @@ -1,6 +1,8 @@ -//@ run-pass //@ edition: 2024 //@ revisions: classic structural +//@ run-pass +//! Test cases for well-typed patterns in edition 2024. These are in their own file to ensure we +//! pass both HIR typeck and MIR borrowck, as we may skip the latter if grouped with failing tests. #![allow(incomplete_features)] #![cfg_attr(classic, feature(ref_pat_eat_one_layer_2024))] #![cfg_attr(structural, feature(ref_pat_eat_one_layer_2024_structural))] @@ -54,35 +56,4 @@ pub fn main() { if let Some(&Some(x)) = &mut Some(Some(0)) { let _: u32 = x; } - #[cfg(classic)] - if let Some(&mut x) = &mut Some(&0) { - let _: &u32 = x; - } - #[cfg(structural)] - if let Some(&mut x) = &Some(&mut 0) { - let _: &u32 = x; - } - - fn generic() -> (R, bool) { - R::meow() - } - - trait Ref: Sized { - fn meow() -> (Self, bool); - } - - impl Ref for &'static [(); 0] { - fn meow() -> (Self, bool) { - (&[], false) - } - } - - impl Ref for &'static mut [(); 0] { - fn meow() -> (Self, bool) { - (&mut [], true) - } - } - - let (&_, b) = generic(); - assert!(!b); } From 93a19501c2bad56e73f0ac9f3ae062b614d30832 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 9 Jan 2025 23:13:00 +0000 Subject: [PATCH 07/16] Always force non-trimming of path in `unreachable_patterns` lint Creating a "trimmed DefID path" when no error is being emitted is an ICE (on purpose). If we create a trimmed path for a lint that is then silenced before being emitted causes a known ICE. This side-steps the issue by always using `with_no_trimmed_path!`. This was verified to fix https://github.com/quinn-rs/quinn/, but couldn't write a repro case for the test suite. Fix #135289. --- compiler/rustc_mir_build/src/thir/pattern/check_match.rs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index a13b00e192167..6631086720068 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -1086,14 +1086,7 @@ fn find_fallback_pattern_typo<'tcx>( let vis = cx.tcx.visibility(item.owner_id); if vis.is_accessible_from(parent, cx.tcx) { accessible.push(item_name); - let path = if item_name == name { - // We know that the const wasn't in scope because it has the exact - // same name, so we suggest the full path. - with_no_trimmed_paths!(cx.tcx.def_path_str(item.owner_id)) - } else { - // The const is likely just typoed, and nothing else. - cx.tcx.def_path_str(item.owner_id) - }; + let path = with_no_trimmed_paths!(cx.tcx.def_path_str(item.owner_id)); accessible_path.push(path); } else if name == item_name { // The const exists somewhere in this crate, but it can't be imported From d8f975cc23395597dd3d0f68c11873b1698cedcf Mon Sep 17 00:00:00 2001 From: dianne Date: Fri, 3 Jan 2025 19:50:38 -0800 Subject: [PATCH 08/16] rename and comment the test for "Rule 5"-related mutability errors This also makes it test the "structural" ruleset, in preparation for additional tests where the rulesets disagree. --- ...f-mut-inside-shared-ref-pat.classic.fixed} | 6 ++- ...-mut-inside-shared-ref-pat.classic.stderr} | 10 ++--- ...nd.rs => ref-mut-inside-shared-ref-pat.rs} | 6 ++- ...mut-inside-shared-ref-pat.structural.fixed | 33 ++++++++++++++ ...ut-inside-shared-ref-pat.structural.stderr | 43 +++++++++++++++++++ 5 files changed, 91 insertions(+), 7 deletions(-) rename tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/{ref_pat_eat_one_layer_2024_ref_mut_inside_and.fixed => ref-mut-inside-shared-ref-pat.classic.fixed} (73%) rename tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/{ref_pat_eat_one_layer_2024_ref_mut_inside_and.stderr => ref-mut-inside-shared-ref-pat.classic.stderr} (79%) rename tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/{ref_pat_eat_one_layer_2024_ref_mut_inside_and.rs => ref-mut-inside-shared-ref-pat.rs} (73%) create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural.fixed create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural.stderr diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_ref_mut_inside_and.fixed b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic.fixed similarity index 73% rename from tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_ref_mut_inside_and.fixed rename to tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic.fixed index e69d169966b52..4f4941975d804 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_ref_mut_inside_and.fixed +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic.fixed @@ -1,7 +1,11 @@ //@ edition: 2024 //@ run-rustfix +//@ revisions: classic structural +//! Tests for `&` patterns matched against `&mut` reference types where the inner pattern attempts +//! to bind by mutable reference. #![allow(incomplete_features)] -#![feature(ref_pat_eat_one_layer_2024)] +#![cfg_attr(classic, feature(ref_pat_eat_one_layer_2024))] +#![cfg_attr(structural, feature(ref_pat_eat_one_layer_2024_structural))] pub fn main() { if let Some(&mut Some(ref mut x)) = &mut Some(Some(0)) { diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_ref_mut_inside_and.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic.stderr similarity index 79% rename from tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_ref_mut_inside_and.stderr rename to tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic.stderr index 8e135b65253fd..6c384a51fac14 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_ref_mut_inside_and.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic.stderr @@ -1,5 +1,5 @@ error[E0596]: cannot borrow as mutable inside an `&` pattern - --> $DIR/ref_pat_eat_one_layer_2024_ref_mut_inside_and.rs:7:31 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:11:31 | LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { | - ^ @@ -7,7 +7,7 @@ LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { | help: replace this `&` with `&mut`: `&mut` error[E0596]: cannot borrow as mutable inside an `&` pattern - --> $DIR/ref_pat_eat_one_layer_2024_ref_mut_inside_and.rs:12:31 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:16:31 | LL | if let &Some(Some(ref mut x)) = &mut Some(Some(0)) { | - ^ @@ -15,7 +15,7 @@ LL | if let &Some(Some(ref mut x)) = &mut Some(Some(0)) { | help: replace this `&` with `&mut`: `&mut` error[E0596]: cannot borrow as mutable inside an `&` pattern - --> $DIR/ref_pat_eat_one_layer_2024_ref_mut_inside_and.rs:20:15 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:24:15 | LL | let &pat!(x) = &mut 0; | - ^ @@ -23,7 +23,7 @@ LL | let &pat!(x) = &mut 0; | help: replace this `&` with `&mut`: `&mut` error[E0596]: cannot borrow as mutable inside an `&` pattern - --> $DIR/ref_pat_eat_one_layer_2024_ref_mut_inside_and.rs:24:19 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:28:19 | LL | let &(ref mut a, ref mut b) = &mut (true, false); | - ^ @@ -31,7 +31,7 @@ LL | let &(ref mut a, ref mut b) = &mut (true, false); | help: replace this `&` with `&mut`: `&mut` error[E0596]: cannot borrow as mutable inside an `&` pattern - --> $DIR/ref_pat_eat_one_layer_2024_ref_mut_inside_and.rs:24:30 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:28:30 | LL | let &(ref mut a, ref mut b) = &mut (true, false); | - ^ diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_ref_mut_inside_and.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.rs similarity index 73% rename from tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_ref_mut_inside_and.rs rename to tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.rs index a300cbcd4df52..b29bff7603f7a 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2024_ref_mut_inside_and.rs +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.rs @@ -1,7 +1,11 @@ //@ edition: 2024 //@ run-rustfix +//@ revisions: classic structural +//! Tests for `&` patterns matched against `&mut` reference types where the inner pattern attempts +//! to bind by mutable reference. #![allow(incomplete_features)] -#![feature(ref_pat_eat_one_layer_2024)] +#![cfg_attr(classic, feature(ref_pat_eat_one_layer_2024))] +#![cfg_attr(structural, feature(ref_pat_eat_one_layer_2024_structural))] pub fn main() { if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural.fixed b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural.fixed new file mode 100644 index 0000000000000..4f4941975d804 --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural.fixed @@ -0,0 +1,33 @@ +//@ edition: 2024 +//@ run-rustfix +//@ revisions: classic structural +//! Tests for `&` patterns matched against `&mut` reference types where the inner pattern attempts +//! to bind by mutable reference. +#![allow(incomplete_features)] +#![cfg_attr(classic, feature(ref_pat_eat_one_layer_2024))] +#![cfg_attr(structural, feature(ref_pat_eat_one_layer_2024_structural))] + +pub fn main() { + if let Some(&mut Some(ref mut x)) = &mut Some(Some(0)) { + //~^ ERROR: cannot borrow as mutable inside an `&` pattern + let _: &mut u8 = x; + } + + if let &mut Some(Some(ref mut x)) = &mut Some(Some(0)) { + //~^ ERROR: cannot borrow as mutable inside an `&` pattern + let _: &mut u8 = x; + } + + macro_rules! pat { + ($var:ident) => { ref mut $var }; + } + let &mut pat!(x) = &mut 0; + //~^ ERROR: cannot borrow as mutable inside an `&` pattern + let _: &mut u8 = x; + + let &mut (ref mut a, ref mut b) = &mut (true, false); + //~^ ERROR: cannot borrow as mutable inside an `&` pattern + //~| ERROR: cannot borrow as mutable inside an `&` pattern + let _: &mut bool = a; + let _: &mut bool = b; +} diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural.stderr new file mode 100644 index 0000000000000..6c384a51fac14 --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural.stderr @@ -0,0 +1,43 @@ +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/ref-mut-inside-shared-ref-pat.rs:11:31 + | +LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/ref-mut-inside-shared-ref-pat.rs:16:31 + | +LL | if let &Some(Some(ref mut x)) = &mut Some(Some(0)) { + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/ref-mut-inside-shared-ref-pat.rs:24:15 + | +LL | let &pat!(x) = &mut 0; + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/ref-mut-inside-shared-ref-pat.rs:28:19 + | +LL | let &(ref mut a, ref mut b) = &mut (true, false); + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/ref-mut-inside-shared-ref-pat.rs:28:30 + | +LL | let &(ref mut a, ref mut b) = &mut (true, false); + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0596`. From 562c522b947cb026aef70ed77041850fa067e571 Mon Sep 17 00:00:00 2001 From: dianne Date: Sun, 12 Jan 2025 22:01:17 -0800 Subject: [PATCH 09/16] remove `tests/ui/pattern/no_ref_mut_behind_and.rs` This was a test for `ref_pat_eat_one_layer_2024` downgrading a `mut ref` default binding mode to `ref` within a shared reference pattern (i.e. Rule 3) on edition 2021 specifically. Since it's near-identical to another existing test (currently in `ref_pat_eat_one_layer_2021.rs` in the "experimental" rfc 3627 subdirectory) and that particular feature gate's typing rulesets are planned to no longer have Rule 3, I'm opting to remove it rather than continue maintaining it separately. --- tests/ui/pattern/no_ref_mut_behind_and.rs | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 tests/ui/pattern/no_ref_mut_behind_and.rs diff --git a/tests/ui/pattern/no_ref_mut_behind_and.rs b/tests/ui/pattern/no_ref_mut_behind_and.rs deleted file mode 100644 index c18d64904d035..0000000000000 --- a/tests/ui/pattern/no_ref_mut_behind_and.rs +++ /dev/null @@ -1,9 +0,0 @@ -//@ edition: 2021 -//@ run-pass -#![allow(incomplete_features)] -#![feature(ref_pat_eat_one_layer_2024)] - -fn main() { - let &[[x]] = &[&mut [42]]; - let _: &i32 = x; -} From 3a0554a44578afb2f69babdae39f8880d23560ae Mon Sep 17 00:00:00 2001 From: klensy Date: Mon, 13 Jan 2025 21:00:57 +0300 Subject: [PATCH 10/16] further improve panic_immediate_abort by removing rtprintpanic messages --- library/std/src/rt.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/library/std/src/rt.rs b/library/std/src/rt.rs index b2492238bd37b..b08ce4ed08919 100644 --- a/library/std/src/rt.rs +++ b/library/std/src/rt.rs @@ -32,9 +32,14 @@ use crate::{mem, panic, sys}; // - nothing (so this macro is a no-op) macro_rules! rtprintpanic { ($($t:tt)*) => { + #[cfg(not(feature = "panic_immediate_abort"))] if let Some(mut out) = crate::sys::stdio::panic_output() { let _ = crate::io::Write::write_fmt(&mut out, format_args!($($t)*)); } + #[cfg(feature = "panic_immediate_abort")] + { + let _ = format_args!($($t)*); + } } } From c2ed28443592adf74c6375514adac49a0d801a8a Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 14 Jan 2025 17:10:44 +0100 Subject: [PATCH 11/16] remove unnecessary rustc_allowed_through_unstable_modules --- library/std/src/os/fd/raw.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/library/std/src/os/fd/raw.rs b/library/std/src/os/fd/raw.rs index 22f5528248a32..03dff94350dad 100644 --- a/library/std/src/os/fd/raw.rs +++ b/library/std/src/os/fd/raw.rs @@ -19,11 +19,9 @@ use crate::sys_common::{AsInner, IntoInner}; use crate::{fs, io}; /// Raw file descriptors. -#[rustc_allowed_through_unstable_modules] #[stable(feature = "rust1", since = "1.0.0")] #[cfg(not(target_os = "hermit"))] pub type RawFd = raw::c_int; -#[rustc_allowed_through_unstable_modules] #[stable(feature = "rust1", since = "1.0.0")] #[cfg(target_os = "hermit")] pub type RawFd = i32; @@ -33,7 +31,6 @@ pub type RawFd = i32; /// This is only available on unix and WASI platforms and must be imported in /// order to call the method. Windows platforms have a corresponding /// `AsRawHandle` and `AsRawSocket` set of traits. -#[rustc_allowed_through_unstable_modules] #[stable(feature = "rust1", since = "1.0.0")] pub trait AsRawFd { /// Extracts the raw file descriptor. @@ -67,7 +64,6 @@ pub trait AsRawFd { /// A trait to express the ability to construct an object from a raw file /// descriptor. -#[rustc_allowed_through_unstable_modules] #[stable(feature = "from_raw_os", since = "1.1.0")] pub trait FromRawFd { /// Constructs a new instance of `Self` from the given raw file @@ -112,7 +108,6 @@ pub trait FromRawFd { /// A trait to express the ability to consume an object and acquire ownership of /// its raw file descriptor. -#[rustc_allowed_through_unstable_modules] #[stable(feature = "into_raw_os", since = "1.4.0")] pub trait IntoRawFd { /// Consumes this object, returning the raw underlying file descriptor. From f3cf39f3bebf5bbbe5be5631fb56332f16805c57 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 14 Jan 2025 17:10:56 +0100 Subject: [PATCH 12/16] wasi/io: remove dead files --- library/std/src/os/wasi/io/fd.rs | 9 --------- library/std/src/os/wasi/io/mod.rs | 4 ++++ library/std/src/os/wasi/io/raw.rs | 20 -------------------- library/std/src/os/wasi/io/{fd => }/tests.rs | 0 4 files changed, 4 insertions(+), 29 deletions(-) delete mode 100644 library/std/src/os/wasi/io/fd.rs delete mode 100644 library/std/src/os/wasi/io/raw.rs rename library/std/src/os/wasi/io/{fd => }/tests.rs (100%) diff --git a/library/std/src/os/wasi/io/fd.rs b/library/std/src/os/wasi/io/fd.rs deleted file mode 100644 index 930aca887e3c4..0000000000000 --- a/library/std/src/os/wasi/io/fd.rs +++ /dev/null @@ -1,9 +0,0 @@ -//! Owned and borrowed file descriptors. - -#![unstable(feature = "wasi_ext", issue = "71213")] - -// Tests for this module -#[cfg(test)] -mod tests; - -pub use crate::os::fd::owned::*; diff --git a/library/std/src/os/wasi/io/mod.rs b/library/std/src/os/wasi/io/mod.rs index 4e123a1eec8ae..5f9a735db085e 100644 --- a/library/std/src/os/wasi/io/mod.rs +++ b/library/std/src/os/wasi/io/mod.rs @@ -4,3 +4,7 @@ #[stable(feature = "io_safety_wasi", since = "1.65.0")] pub use crate::os::fd::*; + +// Tests for this module +#[cfg(test)] +mod tests; diff --git a/library/std/src/os/wasi/io/raw.rs b/library/std/src/os/wasi/io/raw.rs deleted file mode 100644 index da3b36adad409..0000000000000 --- a/library/std/src/os/wasi/io/raw.rs +++ /dev/null @@ -1,20 +0,0 @@ -//! WASI-specific extensions to general I/O primitives. - -#![unstable(feature = "wasi_ext", issue = "71213")] - -// NOTE: despite the fact that this module is unstable, -// stable Rust had the capability to access the stable -// re-exported items from os::fd::raw through this -// unstable module. -// In PR #95956 the stability checker was changed to check -// all path segments of an item rather than just the last, -// which caused the aforementioned stable usage to regress -// (see issue #99502). -// As a result, the items in os::fd::raw were given the -// rustc_allowed_through_unstable_modules attribute. -// No regression tests were added to ensure this property, -// as CI is not configured to test wasm32-wasi. -// If this module is stabilized, -// you may want to remove those attributes -// (assuming no other unstable modules need them). -pub use crate::os::fd::raw::*; diff --git a/library/std/src/os/wasi/io/fd/tests.rs b/library/std/src/os/wasi/io/tests.rs similarity index 100% rename from library/std/src/os/wasi/io/fd/tests.rs rename to library/std/src/os/wasi/io/tests.rs From 5bd20b0608c7d2fe134a8924d0de119e44ad7182 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 13 Dec 2024 18:42:24 +0100 Subject: [PATCH 13/16] fully de-stabilize all custom inner attributes --- compiler/rustc_resolve/src/macros.rs | 51 ++++--------------- .../proc-macro/inner-attr-non-inline-mod.rs | 1 - .../inner-attr-non-inline-mod.stderr | 35 +++++-------- .../inner-attr-non-inline-mod.stdout | 30 +++++------ tests/ui/proc-macro/proc-macro-gates.rs | 1 - tests/ui/proc-macro/proc-macro-gates.stderr | 19 ++----- 6 files changed, 40 insertions(+), 97 deletions(-) diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 25e35fead7e83..b6b2971263a63 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -6,15 +6,14 @@ use std::mem; use rustc_ast::attr::AttributeExt; use rustc_ast::expand::StrippedCfgItem; -use rustc_ast::{self as ast, Crate, Inline, ItemKind, ModKind, NodeId, attr}; +use rustc_ast::{self as ast, Crate, NodeId, attr}; use rustc_ast_pretty::pprust; use rustc_attr_parsing::StabilityLevel; use rustc_data_structures::intern::Interned; use rustc_data_structures::sync::Lrc; use rustc_errors::{Applicability, StashKey}; use rustc_expand::base::{ - Annotatable, DeriveResolution, Indeterminate, ResolverExpand, SyntaxExtension, - SyntaxExtensionKind, + DeriveResolution, Indeterminate, ResolverExpand, SyntaxExtension, SyntaxExtensionKind, }; use rustc_expand::compile_declarative_macro; use rustc_expand::expand::{ @@ -26,8 +25,8 @@ use rustc_middle::middle::stability; use rustc_middle::ty::{RegisteredTools, TyCtxt, Visibility}; use rustc_session::lint::BuiltinLintDiag; use rustc_session::lint::builtin::{ - LEGACY_DERIVE_HELPERS, OUT_OF_SCOPE_MACRO_CALLS, SOFT_UNSTABLE, - UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES, UNUSED_MACRO_RULES, UNUSED_MACROS, + LEGACY_DERIVE_HELPERS, OUT_OF_SCOPE_MACRO_CALLS, UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES, + UNUSED_MACRO_RULES, UNUSED_MACROS, }; use rustc_session::parse::feature_err; use rustc_span::edit_distance::edit_distance; @@ -157,26 +156,6 @@ pub(crate) fn registered_tools(tcx: TyCtxt<'_>, (): ()) -> RegisteredTools { registered_tools } -// Some feature gates for inner attributes are reported as lints for backward compatibility. -fn soft_custom_inner_attributes_gate(path: &ast::Path, invoc: &Invocation) -> bool { - match &path.segments[..] { - // `#![test]` - [seg] if seg.ident.name == sym::test => return true, - // `#![rustfmt::skip]` on out-of-line modules - [seg1, seg2] if seg1.ident.name == sym::rustfmt && seg2.ident.name == sym::skip => { - if let InvocationKind::Attr { item, .. } = &invoc.kind { - if let Annotatable::Item(item) = item { - if let ItemKind::Mod(_, ModKind::Loaded(_, Inline::No, _, _)) = item.kind { - return true; - } - } - } - } - _ => {} - } - false -} - impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> { fn next_node_id(&mut self) -> NodeId { self.next_node_id() @@ -317,7 +296,6 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> { parent_scope, node_id, force, - soft_custom_inner_attributes_gate(path, invoc), deleg_impl, looks_like_invoc_in_mod_inert_attr, )?; @@ -549,7 +527,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { parent_scope: &ParentScope<'ra>, node_id: NodeId, force: bool, - soft_custom_inner_attributes_gate: bool, deleg_impl: Option, invoc_in_mod_inert_attr: Option, ) -> Result<(Lrc, Res), Indeterminate> { @@ -667,22 +644,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { Res::NonMacroAttr(..) => false, _ => unreachable!(), }; - if soft_custom_inner_attributes_gate { - self.tcx.sess.psess.buffer_lint( - SOFT_UNSTABLE, - path.span, - node_id, - BuiltinLintDiag::InnerAttributeUnstable { is_macro }, - ); + let msg = if is_macro { + "inner macro attributes are unstable" } else { - // FIXME: deduplicate with rustc_lint (`BuiltinLintDiag::InnerAttributeUnstable`) - let msg = if is_macro { - "inner macro attributes are unstable" - } else { - "custom inner attributes are unstable" - }; - feature_err(&self.tcx.sess, sym::custom_inner_attributes, path.span, msg).emit(); - } + "custom inner attributes are unstable" + }; + feature_err(&self.tcx.sess, sym::custom_inner_attributes, path.span, msg).emit(); } if res == Res::NonMacroAttr(NonMacroAttrKind::Tool) diff --git a/tests/ui/proc-macro/inner-attr-non-inline-mod.rs b/tests/ui/proc-macro/inner-attr-non-inline-mod.rs index 714463b622508..d4336a7f3e1e0 100644 --- a/tests/ui/proc-macro/inner-attr-non-inline-mod.rs +++ b/tests/ui/proc-macro/inner-attr-non-inline-mod.rs @@ -1,7 +1,6 @@ //@ compile-flags: -Z span-debug //@ error-pattern:custom inner attributes are unstable //@ error-pattern:inner macro attributes are unstable -//@ error-pattern:this was previously accepted //@ proc-macro: test-macros.rs #![no_std] // Don't load unnecessary hygiene information from std diff --git a/tests/ui/proc-macro/inner-attr-non-inline-mod.stderr b/tests/ui/proc-macro/inner-attr-non-inline-mod.stderr index ccc967aaff971..025eec248188b 100644 --- a/tests/ui/proc-macro/inner-attr-non-inline-mod.stderr +++ b/tests/ui/proc-macro/inner-attr-non-inline-mod.stderr @@ -1,3 +1,13 @@ +error[E0658]: custom inner attributes are unstable + --> $DIR/module_with_attrs.rs:3:4 + | +LL | #![rustfmt::skip] + | ^^^^^^^^^^^^^ + | + = note: see issue #54726 for more information + = help: add `#![feature(custom_inner_attributes)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + error[E0658]: inner macro attributes are unstable --> $DIR/module_with_attrs.rs:4:4 | @@ -9,7 +19,7 @@ LL | #![print_attr] = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date error[E0658]: non-inline modules in proc macro input are unstable - --> $DIR/inner-attr-non-inline-mod.rs:14:1 + --> $DIR/inner-attr-non-inline-mod.rs:13:1 | LL | mod module_with_attrs; | ^^^^^^^^^^^^^^^^^^^^^^ @@ -19,7 +29,7 @@ LL | mod module_with_attrs; = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date error[E0658]: custom inner attributes are unstable - --> $DIR/inner-attr-non-inline-mod.rs:14:1 + --> $DIR/inner-attr-non-inline-mod.rs:13:1 | LL | mod module_with_attrs; | ^^^^^^^^^^^^^^^^^^^^^^ @@ -28,27 +38,6 @@ LL | mod module_with_attrs; = help: add `#![feature(custom_inner_attributes)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date -error: custom inner attributes are unstable - --> $DIR/module_with_attrs.rs:3:4 - | -LL | #![rustfmt::skip] - | ^^^^^^^^^^^^^ - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #64266 - = note: `#[deny(soft_unstable)]` on by default - error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0658`. -Future incompatibility report: Future breakage diagnostic: -error: custom inner attributes are unstable - --> $DIR/module_with_attrs.rs:3:4 - | -LL | #![rustfmt::skip] - | ^^^^^^^^^^^^^ - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #64266 - = note: `#[deny(soft_unstable)]` on by default - diff --git a/tests/ui/proc-macro/inner-attr-non-inline-mod.stdout b/tests/ui/proc-macro/inner-attr-non-inline-mod.stdout index aaec40669e67e..450542f68c65c 100644 --- a/tests/ui/proc-macro/inner-attr-non-inline-mod.stdout +++ b/tests/ui/proc-macro/inner-attr-non-inline-mod.stdout @@ -4,35 +4,35 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [ Punct { ch: '#', spacing: Alone, - span: $DIR/inner-attr-non-inline-mod.rs:14:1: 14:23 (#0), + span: $DIR/inner-attr-non-inline-mod.rs:13:1: 13:23 (#0), }, Group { delimiter: Bracket, stream: TokenStream [ Ident { ident: "deny", - span: $DIR/inner-attr-non-inline-mod.rs:14:1: 14:23 (#0), + span: $DIR/inner-attr-non-inline-mod.rs:13:1: 13:23 (#0), }, Group { delimiter: Parenthesis, stream: TokenStream [ Ident { ident: "unused_attributes", - span: $DIR/inner-attr-non-inline-mod.rs:14:1: 14:23 (#0), + span: $DIR/inner-attr-non-inline-mod.rs:13:1: 13:23 (#0), }, ], - span: $DIR/inner-attr-non-inline-mod.rs:14:1: 14:23 (#0), + span: $DIR/inner-attr-non-inline-mod.rs:13:1: 13:23 (#0), }, ], - span: $DIR/inner-attr-non-inline-mod.rs:14:1: 14:23 (#0), + span: $DIR/inner-attr-non-inline-mod.rs:13:1: 13:23 (#0), }, Ident { ident: "mod", - span: $DIR/inner-attr-non-inline-mod.rs:14:1: 14:23 (#0), + span: $DIR/inner-attr-non-inline-mod.rs:13:1: 13:23 (#0), }, Ident { ident: "module_with_attrs", - span: $DIR/inner-attr-non-inline-mod.rs:14:1: 14:23 (#0), + span: $DIR/inner-attr-non-inline-mod.rs:13:1: 13:23 (#0), }, Group { delimiter: Brace, @@ -40,38 +40,38 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [ Punct { ch: '#', spacing: Joint, - span: $DIR/inner-attr-non-inline-mod.rs:14:1: 14:23 (#0), + span: $DIR/inner-attr-non-inline-mod.rs:13:1: 13:23 (#0), }, Punct { ch: '!', spacing: Alone, - span: $DIR/inner-attr-non-inline-mod.rs:14:1: 14:23 (#0), + span: $DIR/inner-attr-non-inline-mod.rs:13:1: 13:23 (#0), }, Group { delimiter: Bracket, stream: TokenStream [ Ident { ident: "rustfmt", - span: $DIR/inner-attr-non-inline-mod.rs:14:1: 14:23 (#0), + span: $DIR/inner-attr-non-inline-mod.rs:13:1: 13:23 (#0), }, Punct { ch: ':', spacing: Joint, - span: $DIR/inner-attr-non-inline-mod.rs:14:1: 14:23 (#0), + span: $DIR/inner-attr-non-inline-mod.rs:13:1: 13:23 (#0), }, Punct { ch: ':', spacing: Alone, - span: $DIR/inner-attr-non-inline-mod.rs:14:1: 14:23 (#0), + span: $DIR/inner-attr-non-inline-mod.rs:13:1: 13:23 (#0), }, Ident { ident: "skip", - span: $DIR/inner-attr-non-inline-mod.rs:14:1: 14:23 (#0), + span: $DIR/inner-attr-non-inline-mod.rs:13:1: 13:23 (#0), }, ], - span: $DIR/inner-attr-non-inline-mod.rs:14:1: 14:23 (#0), + span: $DIR/inner-attr-non-inline-mod.rs:13:1: 13:23 (#0), }, ], - span: $DIR/inner-attr-non-inline-mod.rs:14:1: 14:23 (#0), + span: $DIR/inner-attr-non-inline-mod.rs:13:1: 13:23 (#0), }, ] diff --git a/tests/ui/proc-macro/proc-macro-gates.rs b/tests/ui/proc-macro/proc-macro-gates.rs index bf384bc479b51..04e097eb2f745 100644 --- a/tests/ui/proc-macro/proc-macro-gates.rs +++ b/tests/ui/proc-macro/proc-macro-gates.rs @@ -47,7 +47,6 @@ fn attrs() { fn test_case() { #![test] //~ ERROR inner macro attributes are unstable - //~| WARN this was previously accepted } fn main() {} diff --git a/tests/ui/proc-macro/proc-macro-gates.stderr b/tests/ui/proc-macro/proc-macro-gates.stderr index a05a7d0b185e6..3607b062a5fcb 100644 --- a/tests/ui/proc-macro/proc-macro-gates.stderr +++ b/tests/ui/proc-macro/proc-macro-gates.stderr @@ -84,27 +84,16 @@ LL | let _x = #[identity_attr] println!(); = help: add `#![feature(proc_macro_hygiene)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date -error: inner macro attributes are unstable +error[E0658]: inner macro attributes are unstable --> $DIR/proc-macro-gates.rs:49:8 | LL | #![test] | ^^^^ | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #64266 - = note: `#[deny(soft_unstable)]` on by default + = note: see issue #54726 for more information + = help: add `#![feature(custom_inner_attributes)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date error: aborting due to 10 previous errors For more information about this error, try `rustc --explain E0658`. -Future incompatibility report: Future breakage diagnostic: -error: inner macro attributes are unstable - --> $DIR/proc-macro-gates.rs:49:8 - | -LL | #![test] - | ^^^^ - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #64266 - = note: `#[deny(soft_unstable)]` on by default - From 48e023a56fcdb4647ae3e7ba10c61887e0bd5990 Mon Sep 17 00:00:00 2001 From: dianne Date: Sun, 19 Jan 2025 15:26:14 -0800 Subject: [PATCH 14/16] Add comments explaining inherited references This adds explanation of inherited references and how they relate to the default binding mode. Co-authored-by: Nadrieril --- compiler/rustc_hir_typeck/src/pat.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index d1153821e6212..b60908ed210c8 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -215,6 +215,10 @@ impl MutblCap { } /// Variations on RFC 3627's Rule 4: when do reference patterns match against inherited references? +/// +/// "Inherited reference" designates the `&`/`&mut` types that arise from using match ergonomics, i.e. +/// from matching a reference type with a non-reference pattern. E.g. when `Some(x)` matches on +/// `&mut Option<&T>`, `x` gets type `&mut &T` and the outer `&mut` is considered "inherited". #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum InheritedRefMatchRule { /// Reference patterns try to consume the inherited reference first. @@ -223,7 +227,7 @@ enum InheritedRefMatchRule { /// Reference patterns consume inherited references if matching against a non-reference type. /// This assumes reference patterns can always match against an inherited reference. EatInner, - /// Reference patterns consume both layers of reference. + /// Reference patterns consume both layers of reference, i.e. reset the binding mode when consuming a reference type. /// Currently, this assumes the stable Rust behavior of not allowing reference patterns to eat /// an inherited reference alone. This will need an additional field or variant to represent the /// planned edition <= 2021 behavior of experimental match ergonomics, which does allow that. From bfa0e1e0ad2356b0745c348276d1f4aa828aaf88 Mon Sep 17 00:00:00 2001 From: dianne Date: Sun, 19 Jan 2025 15:32:37 -0800 Subject: [PATCH 15/16] Add debug-assertions for assumptions about enabled typing rules --- compiler/rustc_hir_typeck/src/pat.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index b60908ed210c8..cff24672db6cb 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -2290,6 +2290,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // NB: This assumes that `&` patterns can match against mutable references // (RFC 3627, Rule 5). If we implement a pattern typing ruleset with Rule 4E // but not Rule 5, we'll need to check that here. + debug_assert!(ref_pat_matches_mut_ref); let err_msg = "mismatched types"; let err = if let Some(span) = pat_prefix_span { let mut err = self.dcx().struct_span_err(span, err_msg); @@ -2326,6 +2327,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // NB: This assumes that `&` patterns can match against mutable // references (RFC 3627, Rule 5). If we implement a pattern typing // ruleset with Rule 4 but not Rule 5, we'll need to check that here. + debug_assert!(ref_pat_matches_mut_ref); } else { pat_info.binding_mode = ByRef::No; self.typeck_results From 0263772bac97b00c242bb13844a90e1a6cd652f3 Mon Sep 17 00:00:00 2001 From: dianne Date: Sun, 19 Jan 2025 16:03:15 -0800 Subject: [PATCH 16/16] elaborate/clarify the comments on `InheritedRefMatchRule`'s variants --- compiler/rustc_hir_typeck/src/pat.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index cff24672db6cb..1913bc0e741cd 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -221,16 +221,17 @@ impl MutblCap { /// `&mut Option<&T>`, `x` gets type `&mut &T` and the outer `&mut` is considered "inherited". #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum InheritedRefMatchRule { - /// Reference patterns try to consume the inherited reference first. - /// This assumes reference patterns can always match against an inherited reference. + /// Reference patterns consume only the inherited reference if possible, regardless of whether + /// the underlying type being matched against is a reference type. If there is no inherited + /// reference, a reference will be consumed from the underlying type. EatOuter, - /// Reference patterns consume inherited references if matching against a non-reference type. - /// This assumes reference patterns can always match against an inherited reference. + /// Reference patterns consume only a reference from the underlying type if possible. If the + /// underlying type is not a reference type, the inherited reference will be consumed. EatInner, - /// Reference patterns consume both layers of reference, i.e. reset the binding mode when consuming a reference type. - /// Currently, this assumes the stable Rust behavior of not allowing reference patterns to eat - /// an inherited reference alone. This will need an additional field or variant to represent the - /// planned edition <= 2021 behavior of experimental match ergonomics, which does allow that. + /// When the underlying type is a reference type, reference patterns consume both layers of + /// reference, i.e. they both reset the binding mode and consume the reference type. Reference + /// patterns are not permitted when there is no underlying reference type, i.e. they can't eat + /// only an inherited reference. This is the current stable Rust behavior. EatBoth, }