Skip to content

Commit 753e682

Browse files
committed
parse pinned local variable declarations
1 parent f44efbf commit 753e682

File tree

50 files changed

+450
-93
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+450
-93
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -720,18 +720,22 @@ impl ByRef {
720720
/// Used for both the explicit binding annotations given in the HIR for a binding
721721
/// and the final binding mode that we infer after type inference/match ergonomics.
722722
/// `.0` is the by-reference mode (`ref`, `ref mut`, or by value),
723-
/// `.1` is the mutability of the binding.
723+
/// `.1` is the pinnedness of the binding,
724+
/// `.2` is the mutability of the binding.
724725
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
725726
#[derive(Encodable, Decodable, HashStable_Generic)]
726-
pub struct BindingMode(pub ByRef, pub Mutability);
727+
pub struct BindingMode(pub ByRef, pub Pinnedness, pub Mutability);
727728

728729
impl BindingMode {
729-
pub const NONE: Self = Self(ByRef::No, Mutability::Not);
730-
pub const REF: Self = Self(ByRef::Yes(Mutability::Not), Mutability::Not);
731-
pub const MUT: Self = Self(ByRef::No, Mutability::Mut);
732-
pub const REF_MUT: Self = Self(ByRef::Yes(Mutability::Mut), Mutability::Not);
733-
pub const MUT_REF: Self = Self(ByRef::Yes(Mutability::Not), Mutability::Mut);
734-
pub const MUT_REF_MUT: Self = Self(ByRef::Yes(Mutability::Mut), Mutability::Mut);
730+
pub const NONE: Self = Self(ByRef::No, Pinnedness::Not, Mutability::Not);
731+
pub const REF: Self = Self(ByRef::Yes(Mutability::Not), Pinnedness::Not, Mutability::Not);
732+
pub const MUT: Self = Self(ByRef::No, Pinnedness::Not, Mutability::Mut);
733+
pub const REF_MUT: Self = Self(ByRef::Yes(Mutability::Mut), Pinnedness::Not, Mutability::Not);
734+
pub const MUT_REF: Self = Self(ByRef::Yes(Mutability::Not), Pinnedness::Not, Mutability::Mut);
735+
pub const MUT_REF_MUT: Self =
736+
Self(ByRef::Yes(Mutability::Mut), Pinnedness::Not, Mutability::Mut);
737+
pub const PIN_CONST: Self = Self(ByRef::No, Pinnedness::Pinned, Mutability::Not);
738+
pub const PIN_MUT: Self = Self(ByRef::No, Pinnedness::Pinned, Mutability::Mut);
735739

736740
pub fn prefix_str(self) -> &'static str {
737741
match self {
@@ -741,6 +745,9 @@ impl BindingMode {
741745
Self::REF_MUT => "ref mut ",
742746
Self::MUT_REF => "mut ref ",
743747
Self::MUT_REF_MUT => "mut ref mut ",
748+
Self::PIN_CONST => "pin const ",
749+
Self::PIN_MUT => "pin mut ",
750+
Self(_, Pinnedness::Pinned, _) => panic!("unsupported pinned binding mode"),
744751
}
745752
}
746753
}
@@ -2604,7 +2611,9 @@ pub type ExplicitSelf = Spanned<SelfKind>;
26042611
impl Param {
26052612
/// Attempts to cast parameter to `ExplicitSelf`.
26062613
pub fn to_self(&self) -> Option<ExplicitSelf> {
2607-
if let PatKind::Ident(BindingMode(ByRef::No, mutbl), ident, _) = self.pat.kind {
2614+
if let PatKind::Ident(BindingMode(ByRef::No, Pinnedness::Not, mutbl), ident, _) =
2615+
self.pat.kind
2616+
{
26082617
if ident.name == kw::SelfLower {
26092618
return match self.ty.kind {
26102619
TyKind::ImplicitSelf => Some(respan(self.pat.span, SelfKind::Value(mutbl))),
@@ -2659,7 +2668,11 @@ impl Param {
26592668
attrs,
26602669
pat: P(Pat {
26612670
id: DUMMY_NODE_ID,
2662-
kind: PatKind::Ident(BindingMode(ByRef::No, mutbl), eself_ident, None),
2671+
kind: PatKind::Ident(
2672+
BindingMode(ByRef::No, Pinnedness::Not, mutbl),
2673+
eself_ident,
2674+
None,
2675+
),
26632676
span,
26642677
tokens: None,
26652678
}),

compiler/rustc_ast_ir/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,12 @@ pub enum Pinnedness {
8686
Not,
8787
Pinned,
8888
}
89+
90+
impl Pinnedness {
91+
pub fn is_pin(self) -> bool {
92+
matches!(self, Self::Pinned)
93+
}
94+
pub fn is_not(self) -> bool {
95+
matches!(self, Self::Not)
96+
}
97+
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
12781278
// Check if this is a binding pattern, if so, we can optimize and avoid adding a
12791279
// `let <pat> = __argN;` statement. In this case, we do not rename the parameter.
12801280
let (ident, is_simple_parameter) = match parameter.pat.kind {
1281-
hir::PatKind::Binding(hir::BindingMode(ByRef::No, _), _, ident, _) => (ident, true),
1281+
hir::PatKind::Binding(hir::BindingMode(ByRef::No, _, _), _, ident, _) => {
1282+
(ident, true)
1283+
}
12821284
// For `ref mut` or wildcard arguments, we can't reuse the binding, but
12831285
// we can keep the same name for the parameter.
12841286
// This lets rustdoc render it correctly in documentation.

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1629,7 +1629,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16291629
implicit_self: decl.inputs.get(0).map_or(hir::ImplicitSelfKind::None, |arg| {
16301630
let is_mutable_pat = matches!(
16311631
arg.pat.kind,
1632-
PatKind::Ident(hir::BindingMode(_, Mutability::Mut), ..)
1632+
PatKind::Ident(hir::BindingMode(_, _, Mutability::Mut), ..)
16331633
);
16341634

16351635
match &arg.ty.kind {

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1616,7 +1616,10 @@ impl<'a> State<'a> {
16161616
match &pat.kind {
16171617
PatKind::Wild => self.word("_"),
16181618
PatKind::Never => self.word("!"),
1619-
PatKind::Ident(BindingMode(by_ref, mutbl), ident, sub) => {
1619+
PatKind::Ident(BindingMode(by_ref, pin, mutbl), ident, sub) => {
1620+
if pin.is_pin() {
1621+
self.word_nbsp("pin");
1622+
}
16201623
if mutbl.is_mut() {
16211624
self.word_nbsp("mut");
16221625
}

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
305305
{
306306
match *decl.local_info() {
307307
LocalInfo::User(BindingForm::Var(mir::VarBindingForm {
308-
binding_mode: BindingMode(ByRef::No, Mutability::Not),
308+
binding_mode: BindingMode(ByRef::No, _, Mutability::Not),
309309
opt_ty_info: Some(sp),
310310
opt_match_place: _,
311311
pat_span: _,
@@ -732,7 +732,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
732732
debug!("local_decl: {:?}", local_decl);
733733
let pat_span = match *local_decl.local_info() {
734734
LocalInfo::User(BindingForm::Var(mir::VarBindingForm {
735-
binding_mode: BindingMode(ByRef::No, Mutability::Not),
735+
binding_mode: BindingMode(ByRef::No, _, Mutability::Not),
736736
opt_ty_info: _,
737737
opt_match_place: _,
738738
pat_span,
@@ -1145,7 +1145,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
11451145
}
11461146

11471147
LocalInfo::User(mir::BindingForm::Var(mir::VarBindingForm {
1148-
binding_mode: BindingMode(ByRef::No, _),
1148+
binding_mode: BindingMode(ByRef::No, _, _),
11491149
opt_ty_info,
11501150
..
11511151
})) => {
@@ -1224,7 +1224,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
12241224
}
12251225

12261226
LocalInfo::User(mir::BindingForm::Var(mir::VarBindingForm {
1227-
binding_mode: BindingMode(ByRef::Yes(_), _),
1227+
binding_mode: BindingMode(ByRef::Yes(_), _, _),
12281228
..
12291229
})) => {
12301230
let pattern_span: Span = local_decl.source_info.span;
@@ -1439,7 +1439,7 @@ fn mut_borrow_of_mutable_ref(local_decl: &LocalDecl<'_>, local_name: Option<Symb
14391439
match *local_decl.local_info() {
14401440
// Check if mutably borrowing a mutable reference.
14411441
LocalInfo::User(mir::BindingForm::Var(mir::VarBindingForm {
1442-
binding_mode: BindingMode(ByRef::No, Mutability::Not),
1442+
binding_mode: BindingMode(ByRef::No, _, Mutability::Not),
14431443
..
14441444
})) => matches!(local_decl.ty.kind(), ty::Ref(_, _, hir::Mutability::Mut)),
14451445
LocalInfo::User(mir::BindingForm::ImplicitSelf(kind)) => {

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ pub(crate) use SubstructureFields::*;
183183
use rustc_ast::ptr::P;
184184
use rustc_ast::{
185185
self as ast, AnonConst, BindingMode, ByRef, EnumDef, Expr, GenericArg, GenericParamKind,
186-
Generics, Mutability, PatKind, VariantData,
186+
Generics, Mutability, PatKind, Pinnedness, VariantData,
187187
};
188188
use rustc_attr_parsing as attr;
189189
use rustc_expand::base::{Annotatable, ExtCtxt};
@@ -1470,7 +1470,11 @@ impl<'a> TraitDef<'a> {
14701470
struct_field.ident,
14711471
cx.pat(
14721472
path.span,
1473-
PatKind::Ident(BindingMode(by_ref, Mutability::Not), path, None),
1473+
PatKind::Ident(
1474+
BindingMode(by_ref, Pinnedness::Not, Mutability::Not),
1475+
path,
1476+
None,
1477+
),
14741478
),
14751479
)
14761480
});

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_ast::{
1111
};
1212
pub use rustc_ast::{
1313
BinOp, BinOpKind, BindingMode, BorrowKind, BoundConstness, BoundPolarity, ByRef, CaptureBy,
14-
ImplPolarity, IsAuto, Movability, Mutability, UnOp, UnsafeBinderCastKind,
14+
ImplPolarity, IsAuto, Movability, Mutability, Pinnedness, UnOp, UnsafeBinderCastKind,
1515
};
1616
use rustc_data_structures::fingerprint::Fingerprint;
1717
use rustc_data_structures::sorted_map::SortedMap;

compiler/rustc_hir/src/pat_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl hir::Pat<'_> {
9595

9696
pub fn simple_ident(&self) -> Option<Ident> {
9797
match self.kind {
98-
PatKind::Binding(BindingMode(ByRef::No, _), _, ident, None) => Some(ident),
98+
PatKind::Binding(BindingMode(ByRef::No, _, _), _, ident, None) => Some(ident),
9999
_ => None,
100100
}
101101
}

compiler/rustc_hir_analysis/src/check/region.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ fn resolve_local<'tcx>(
681681
// & expression, and its lifetime would be extended to the end of the block (due
682682
// to a different rule, not the below code).
683683
match pat.kind {
684-
PatKind::Binding(hir::BindingMode(hir::ByRef::Yes(_), _), ..) => true,
684+
PatKind::Binding(hir::BindingMode(hir::ByRef::Yes(_), _, _), ..) => true,
685685

686686
PatKind::Struct(_, field_pats, _) => field_pats.iter().any(|fp| is_binding_pat(fp.pat)),
687687

@@ -700,7 +700,7 @@ fn resolve_local<'tcx>(
700700
}
701701

702702
PatKind::Ref(_, _)
703-
| PatKind::Binding(hir::BindingMode(hir::ByRef::No, _), ..)
703+
| PatKind::Binding(hir::BindingMode(hir::ByRef::No, _, _), ..)
704704
| PatKind::Wild
705705
| PatKind::Never
706706
| PatKind::Expr(_)

0 commit comments

Comments
 (0)