Skip to content

Commit b889870

Browse files
committed
Auto merge of #150158 - Zalathar:rollup-dqazzrn, r=Zalathar
Rollup of 3 pull requests Successful merges: - #150121 (mir_build: Don't use a mixture of THIR pattern kinds for pin-patterns) - #150148 (mir_build: Remove unnecessary lifetime from THIR `PatCtxt`) - #150150 (move eii tests) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 1d8f9c5 + 77491e7 commit b889870

File tree

21 files changed

+62
-66
lines changed

21 files changed

+62
-66
lines changed

compiler/rustc_hir_typeck/src/expr_use_visitor.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_middle::hir::place::ProjectionKind;
2121
// Export these here so that Clippy can use them.
2222
pub use rustc_middle::hir::place::{Place, PlaceBase, PlaceWithHirId, Projection};
2323
use rustc_middle::mir::FakeReadCause;
24+
use rustc_middle::thir::DerefPatBorrowMode;
2425
use rustc_middle::ty::{
2526
self, BorrowKind, Ty, TyCtxt, TypeFoldable, TypeVisitableExt as _, adjustment,
2627
};
@@ -890,7 +891,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
890891
// Deref patterns on boxes don't borrow, so we ignore them here.
891892
// HACK: this could be a fake pattern corresponding to a deref inserted by match
892893
// ergonomics, in which case `pat.hir_id` will be the id of the subpattern.
893-
if let hir::ByRef::Yes(_, mutability) =
894+
if let DerefPatBorrowMode::Borrow(mutability) =
894895
self.cx.typeck_results().deref_pat_borrow_mode(place.place.ty(), subpattern)
895896
{
896897
let bk = ty::BorrowKind::from_mutbl(mutability);
@@ -1837,9 +1838,9 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
18371838
) -> Result<PlaceWithHirId<'tcx>, Cx::Error> {
18381839
match self.cx.typeck_results().deref_pat_borrow_mode(base_place.place.ty(), inner) {
18391840
// Deref patterns on boxes are lowered using a built-in deref.
1840-
hir::ByRef::No => self.cat_deref(hir_id, base_place),
1841+
DerefPatBorrowMode::Box => self.cat_deref(hir_id, base_place),
18411842
// For other types, we create a temporary to match on.
1842-
hir::ByRef::Yes(_, mutability) => {
1843+
DerefPatBorrowMode::Borrow(mutability) => {
18431844
let re_erased = self.cx.tcx().lifetimes.re_erased;
18441845
let ty = Ty::new_ref(self.cx.tcx(), re_erased, target_ty, mutability);
18451846
// A deref pattern stores the result of `Deref::deref` or `DerefMut::deref_mut` ...

compiler/rustc_middle/src/thir.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::ops::Index;
1414
use std::sync::Arc;
1515

1616
use rustc_abi::{FieldIdx, Integer, Size, VariantIdx};
17-
use rustc_ast::{AsmMacro, InlineAsmOptions, InlineAsmTemplatePiece};
17+
use rustc_ast::{AsmMacro, InlineAsmOptions, InlineAsmTemplatePiece, Mutability};
1818
use rustc_hir as hir;
1919
use rustc_hir::def_id::DefId;
2020
use rustc_hir::{BindingMode, ByRef, HirId, MatchSource, RangeEnd};
@@ -811,11 +811,11 @@ pub enum PatKind<'tcx> {
811811
DerefPattern {
812812
subpattern: Box<Pat<'tcx>>,
813813
/// Whether the pattern scrutinee needs to be borrowed in order to call `Deref::deref` or
814-
/// `DerefMut::deref_mut`, and if so, which. This is `ByRef::No` for deref patterns on
814+
/// `DerefMut::deref_mut`, and if so, which. This is `DerefPatBorrowMode::Box` for deref patterns on
815815
/// boxes; they are lowered using a built-in deref rather than a method call, thus they
816816
/// don't borrow the scrutinee.
817817
#[type_visitable(ignore)]
818-
borrow: ByRef,
818+
borrow: DerefPatBorrowMode,
819819
},
820820

821821
/// One of the following:
@@ -879,6 +879,12 @@ pub enum PatKind<'tcx> {
879879
Error(ErrorGuaranteed),
880880
}
881881

882+
#[derive(Copy, Clone, Debug, HashStable)]
883+
pub enum DerefPatBorrowMode {
884+
Borrow(Mutability),
885+
Box,
886+
}
887+
882888
/// A range pattern.
883889
/// The boundaries must be of the same type and that type must be numeric.
884890
#[derive(Clone, Debug, PartialEq, HashStable, TypeVisitable)]

compiler/rustc_middle/src/ty/typeck_results.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdMap};
1111
use rustc_hir::hir_id::OwnerId;
1212
use rustc_hir::{
1313
self as hir, BindingMode, ByRef, HirId, ItemLocalId, ItemLocalMap, ItemLocalSet, Mutability,
14-
Pinnedness,
1514
};
1615
use rustc_index::IndexVec;
1716
use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
@@ -21,6 +20,7 @@ use rustc_span::Span;
2120
use crate::hir::place::Place as HirPlace;
2221
use crate::infer::canonical::Canonical;
2322
use crate::mir::FakeReadCause;
23+
use crate::thir::DerefPatBorrowMode;
2424
use crate::traits::ObligationCause;
2525
use crate::ty::{
2626
self, BoundVar, CanonicalPolyFnSig, ClosureSizeProfileData, GenericArgKind, GenericArgs,
@@ -491,13 +491,18 @@ impl<'tcx> TypeckResults<'tcx> {
491491
/// In most cases, if the pattern recursively contains a `ref mut` binding, we find the inner
492492
/// pattern's scrutinee by calling `DerefMut::deref_mut`, and otherwise we call `Deref::deref`.
493493
/// However, for boxes we can use a built-in deref instead, which doesn't borrow the scrutinee;
494-
/// in this case, we return `ByRef::No`.
495-
pub fn deref_pat_borrow_mode(&self, pointer_ty: Ty<'_>, inner: &hir::Pat<'_>) -> ByRef {
494+
/// in this case, we return `DerefPatBorrowMode::Box`.
495+
pub fn deref_pat_borrow_mode(
496+
&self,
497+
pointer_ty: Ty<'_>,
498+
inner: &hir::Pat<'_>,
499+
) -> DerefPatBorrowMode {
496500
if pointer_ty.is_box() {
497-
ByRef::No
501+
DerefPatBorrowMode::Box
498502
} else {
499-
let mutable = self.pat_has_ref_mut_binding(inner);
500-
ByRef::Yes(Pinnedness::Not, if mutable { Mutability::Mut } else { Mutability::Not })
503+
let mutability =
504+
if self.pat_has_ref_mut_binding(inner) { Mutability::Mut } else { Mutability::Not };
505+
DerefPatBorrowMode::Borrow(mutability)
501506
}
502507
}
503508

compiler/rustc_mir_build/src/builder/matches/match_pair.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use std::sync::Arc;
22

33
use rustc_abi::FieldIdx;
4-
use rustc_hir::ByRef;
54
use rustc_middle::mir::*;
65
use rustc_middle::thir::*;
7-
use rustc_middle::ty::{self, Pinnedness, Ty, TypeVisitableExt};
6+
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
87

98
use crate::builder::Builder;
109
use crate::builder::expr::as_place::{PlaceBase, PlaceBuilder};
@@ -287,8 +286,9 @@ impl<'tcx> MatchPairTree<'tcx> {
287286
None
288287
}
289288

289+
// FIXME: Pin-patterns should probably have their own pattern kind,
290+
// instead of overloading `PatKind::Deref` via the pattern type.
290291
PatKind::Deref { ref subpattern }
291-
| PatKind::DerefPattern { ref subpattern, borrow: ByRef::Yes(Pinnedness::Pinned, _) }
292292
if let Some(ref_ty) = pattern.ty.pinned_ty()
293293
&& ref_ty.is_ref() =>
294294
{
@@ -302,12 +302,8 @@ impl<'tcx> MatchPairTree<'tcx> {
302302
None
303303
}
304304

305-
PatKind::DerefPattern { borrow: ByRef::Yes(Pinnedness::Pinned, _), .. } => {
306-
rustc_middle::bug!("RefPin pattern on non-`Pin` type {:?}", pattern.ty)
307-
}
308-
309305
PatKind::Deref { ref subpattern }
310-
| PatKind::DerefPattern { ref subpattern, borrow: ByRef::No } => {
306+
| PatKind::DerefPattern { ref subpattern, borrow: DerefPatBorrowMode::Box } => {
311307
MatchPairTree::for_pattern(
312308
place_builder.deref(),
313309
subpattern,
@@ -320,7 +316,7 @@ impl<'tcx> MatchPairTree<'tcx> {
320316

321317
PatKind::DerefPattern {
322318
ref subpattern,
323-
borrow: ByRef::Yes(Pinnedness::Not, mutability),
319+
borrow: DerefPatBorrowMode::Borrow(mutability),
324320
} => {
325321
// Create a new temporary for each deref pattern.
326322
// FIXME(deref_patterns): dedup temporaries to avoid multiple `deref()` calls?

compiler/rustc_mir_build/src/builder/matches/mod.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ use std::mem;
1111
use std::sync::Arc;
1212

1313
use itertools::{Itertools, Position};
14-
use rustc_abi::{FIRST_VARIANT, FieldIdx, VariantIdx};
14+
use rustc_abi::{FIRST_VARIANT, VariantIdx};
1515
use rustc_data_structures::fx::FxIndexMap;
1616
use rustc_data_structures::stack::ensure_sufficient_stack;
17-
use rustc_hir::{BindingMode, ByRef, LangItem, LetStmt, LocalSource, Node, Pinnedness};
17+
use rustc_hir::{BindingMode, ByRef, LangItem, LetStmt, LocalSource, Node};
1818
use rustc_middle::middle::region::{self, TempLifetime};
1919
use rustc_middle::mir::*;
2020
use rustc_middle::thir::{self, *};
@@ -920,10 +920,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
920920
visit_subpat(self, subpattern, &user_tys.deref(), f);
921921
}
922922

923-
PatKind::DerefPattern { ref subpattern, borrow: ByRef::Yes(Pinnedness::Pinned, _) } => {
924-
visit_subpat(self, subpattern, &user_tys.leaf(FieldIdx::ZERO).deref(), f);
925-
}
926-
927923
PatKind::DerefPattern { ref subpattern, .. } => {
928924
visit_subpat(self, subpattern, &ProjectedUserTypesNode::None, f);
929925
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::errors::{
2828
PointerPattern, TypeNotPartialEq, TypeNotStructural, UnionPattern, UnsizedPattern,
2929
};
3030

31-
impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
31+
impl<'tcx> PatCtxt<'tcx> {
3232
/// Converts a constant to a pattern (if possible).
3333
/// This means aggregate values (like structs and enums) are converted
3434
/// to a pattern that matches the value (as if you'd compared via structural equality).
@@ -64,7 +64,7 @@ struct ConstToPat<'tcx> {
6464
}
6565

6666
impl<'tcx> ConstToPat<'tcx> {
67-
fn new(pat_ctxt: &PatCtxt<'_, 'tcx>, id: hir::HirId, span: Span, c: ty::Const<'tcx>) -> Self {
67+
fn new(pat_ctxt: &PatCtxt<'tcx>, id: hir::HirId, span: Span, c: ty::Const<'tcx>) -> Self {
6868
trace!(?pat_ctxt.typeck_results.hir_owner);
6969
ConstToPat { tcx: pat_ctxt.tcx, typing_env: pat_ctxt.typing_env, span, id, c }
7070
}

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

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ use rustc_abi::{FieldIdx, Integer};
1111
use rustc_errors::codes::*;
1212
use rustc_hir::def::{CtorOf, DefKind, Res};
1313
use rustc_hir::pat_util::EnumerateAndAdjustIterator;
14-
use rustc_hir::{self as hir, ByRef, LangItem, Mutability, Pinnedness, RangeEnd};
14+
use rustc_hir::{self as hir, LangItem, RangeEnd};
1515
use rustc_index::Idx;
1616
use rustc_infer::infer::TyCtxtInferExt;
1717
use rustc_middle::mir::interpret::LitToConstInput;
1818
use rustc_middle::thir::{
19-
Ascription, FieldPat, LocalVarId, Pat, PatKind, PatRange, PatRangeBoundary,
19+
Ascription, DerefPatBorrowMode, FieldPat, LocalVarId, Pat, PatKind, PatRange, PatRangeBoundary,
2020
};
2121
use rustc_middle::ty::adjustment::{PatAdjust, PatAdjustment};
2222
use rustc_middle::ty::layout::IntegerExt;
@@ -30,19 +30,20 @@ pub(crate) use self::check_match::check_match;
3030
use self::migration::PatMigration;
3131
use crate::errors::*;
3232

33-
struct PatCtxt<'a, 'tcx> {
33+
/// Context for lowering HIR patterns to THIR patterns.
34+
struct PatCtxt<'tcx> {
3435
tcx: TyCtxt<'tcx>,
3536
typing_env: ty::TypingEnv<'tcx>,
36-
typeck_results: &'a ty::TypeckResults<'tcx>,
37+
typeck_results: &'tcx ty::TypeckResults<'tcx>,
3738

3839
/// Used by the Rust 2024 migration lint.
39-
rust_2024_migration: Option<PatMigration<'a>>,
40+
rust_2024_migration: Option<PatMigration<'tcx>>,
4041
}
4142

42-
pub(super) fn pat_from_hir<'a, 'tcx>(
43+
pub(super) fn pat_from_hir<'tcx>(
4344
tcx: TyCtxt<'tcx>,
4445
typing_env: ty::TypingEnv<'tcx>,
45-
typeck_results: &'a ty::TypeckResults<'tcx>,
46+
typeck_results: &'tcx ty::TypeckResults<'tcx>,
4647
pat: &'tcx hir::Pat<'tcx>,
4748
) -> Box<Pat<'tcx>> {
4849
let mut pcx = PatCtxt {
@@ -62,7 +63,7 @@ pub(super) fn pat_from_hir<'a, 'tcx>(
6263
result
6364
}
6465

65-
impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
66+
impl<'tcx> PatCtxt<'tcx> {
6667
fn lower_pattern(&mut self, pat: &'tcx hir::Pat<'tcx>) -> Box<Pat<'tcx>> {
6768
let adjustments: &[PatAdjustment<'tcx>] =
6869
self.typeck_results.pat_adjustments().get(pat.hir_id).map_or(&[], |v| &**v);
@@ -114,16 +115,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
114115
let borrow = self.typeck_results.deref_pat_borrow_mode(adjust.source, pat);
115116
PatKind::DerefPattern { subpattern: thir_pat, borrow }
116117
}
117-
PatAdjust::PinDeref => {
118-
let mutable = self.typeck_results.pat_has_ref_mut_binding(pat);
119-
PatKind::DerefPattern {
120-
subpattern: thir_pat,
121-
borrow: ByRef::Yes(
122-
Pinnedness::Pinned,
123-
if mutable { Mutability::Mut } else { Mutability::Not },
124-
),
125-
}
126-
}
118+
PatAdjust::PinDeref => PatKind::Deref { subpattern: thir_pat },
127119
};
128120
Box::new(Pat { span, ty: adjust.source, kind })
129121
});
@@ -334,7 +326,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
334326
}
335327
hir::PatKind::Box(subpattern) => PatKind::DerefPattern {
336328
subpattern: self.lower_pattern(subpattern),
337-
borrow: hir::ByRef::No,
329+
borrow: DerefPatBorrowMode::Box,
338330
},
339331

340332
hir::PatKind::Slice(prefix, slice, suffix) => {
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,107 @@
11
error: `#[foo]` is only valid on functions
2-
--> $DIR/wrong_target.rs:7:1
2+
--> $DIR/attribute_targets.rs:7:1
33
|
44
LL | #[foo]
55
| ^^^^^^
66

77
error: `#[eii]` is only valid on functions
8-
--> $DIR/wrong_target.rs:9:1
8+
--> $DIR/attribute_targets.rs:9:1
99
|
1010
LL | #[eii]
1111
| ^^^^^^
1212

1313
error: `#[foo]` is only valid on functions
14-
--> $DIR/wrong_target.rs:13:1
14+
--> $DIR/attribute_targets.rs:13:1
1515
|
1616
LL | #[foo]
1717
| ^^^^^^
1818

1919
error: `#[eii]` is only valid on functions
20-
--> $DIR/wrong_target.rs:15:1
20+
--> $DIR/attribute_targets.rs:15:1
2121
|
2222
LL | #[eii]
2323
| ^^^^^^
2424

2525
error: `#[foo]` is only valid on functions
26-
--> $DIR/wrong_target.rs:21:1
26+
--> $DIR/attribute_targets.rs:21:1
2727
|
2828
LL | #[foo]
2929
| ^^^^^^
3030

3131
error: `#[eii]` is only valid on functions
32-
--> $DIR/wrong_target.rs:23:1
32+
--> $DIR/attribute_targets.rs:23:1
3333
|
3434
LL | #[eii]
3535
| ^^^^^^
3636

3737
error: `#[foo]` is only valid on functions
38-
--> $DIR/wrong_target.rs:27:1
38+
--> $DIR/attribute_targets.rs:27:1
3939
|
4040
LL | #[foo]
4141
| ^^^^^^
4242

4343
error: `#[eii]` is only valid on functions
44-
--> $DIR/wrong_target.rs:29:1
44+
--> $DIR/attribute_targets.rs:29:1
4545
|
4646
LL | #[eii]
4747
| ^^^^^^
4848

4949
error: `#[foo]` is only valid on functions
50-
--> $DIR/wrong_target.rs:32:5
50+
--> $DIR/attribute_targets.rs:32:5
5151
|
5252
LL | #[foo]
5353
| ^^^^^^
5454

5555
error: `#[eii]` is only valid on functions
56-
--> $DIR/wrong_target.rs:34:5
56+
--> $DIR/attribute_targets.rs:34:5
5757
|
5858
LL | #[eii]
5959
| ^^^^^^
6060

6161
error: `#[foo]` is only valid on functions
62-
--> $DIR/wrong_target.rs:39:1
62+
--> $DIR/attribute_targets.rs:39:1
6363
|
6464
LL | #[foo]
6565
| ^^^^^^
6666

6767
error: `#[eii]` is only valid on functions
68-
--> $DIR/wrong_target.rs:41:1
68+
--> $DIR/attribute_targets.rs:41:1
6969
|
7070
LL | #[eii]
7171
| ^^^^^^
7272

7373
error: `#[foo]` is only valid on functions
74-
--> $DIR/wrong_target.rs:44:5
74+
--> $DIR/attribute_targets.rs:44:5
7575
|
7676
LL | #[foo]
7777
| ^^^^^^
7878

7979
error: `#[eii]` is only valid on functions
80-
--> $DIR/wrong_target.rs:46:5
80+
--> $DIR/attribute_targets.rs:46:5
8181
|
8282
LL | #[eii]
8383
| ^^^^^^
8484

8585
error: `#[foo]` is only valid on functions
86-
--> $DIR/wrong_target.rs:51:1
86+
--> $DIR/attribute_targets.rs:51:1
8787
|
8888
LL | #[foo]
8989
| ^^^^^^
9090

9191
error: `#[eii]` is only valid on functions
92-
--> $DIR/wrong_target.rs:53:1
92+
--> $DIR/attribute_targets.rs:53:1
9393
|
9494
LL | #[eii]
9595
| ^^^^^^
9696

9797
error: `#[foo]` is only valid on functions
98-
--> $DIR/wrong_target.rs:56:5
98+
--> $DIR/attribute_targets.rs:56:5
9999
|
100100
LL | #[foo]
101101
| ^^^^^^
102102

103103
error: `#[eii]` is only valid on functions
104-
--> $DIR/wrong_target.rs:58:5
104+
--> $DIR/attribute_targets.rs:58:5
105105
|
106106
LL | #[eii]
107107
| ^^^^^^

0 commit comments

Comments
 (0)