Skip to content

Commit 227f933

Browse files
committed
Simplify RvalueCandidateType.
There is no difference between the Patternand Borrow cases. Reduce it to a simple struct.
1 parent 1da5e60 commit 227f933

File tree

3 files changed

+19
-34
lines changed

3 files changed

+19
-34
lines changed

compiler/rustc_hir_analysis/src/check/region.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -623,10 +623,7 @@ fn resolve_local<'tcx>(
623623
if is_binding_pat(pat) {
624624
visitor.scope_tree.record_rvalue_candidate(
625625
expr.hir_id,
626-
RvalueCandidateType::Pattern {
627-
target: expr.hir_id.local_id,
628-
lifetime: blk_scope,
629-
},
626+
RvalueCandidate { target: expr.hir_id.local_id, lifetime: blk_scope },
630627
);
631628
}
632629
}
@@ -731,10 +728,7 @@ fn resolve_local<'tcx>(
731728
record_rvalue_scope_if_borrow_expr(visitor, subexpr, blk_id);
732729
visitor.scope_tree.record_rvalue_candidate(
733730
subexpr.hir_id,
734-
RvalueCandidateType::Borrow {
735-
target: subexpr.hir_id.local_id,
736-
lifetime: blk_id,
737-
},
731+
RvalueCandidate { target: subexpr.hir_id.local_id, lifetime: blk_id },
738732
);
739733
}
740734
hir::ExprKind::Struct(_, fields, _) => {

compiler/rustc_hir_typeck/src/rvalue_scopes.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use hir::Node;
22
use hir::def_id::DefId;
33
use rustc_hir as hir;
44
use rustc_middle::bug;
5-
use rustc_middle::middle::region::{RvalueCandidateType, Scope, ScopeTree};
5+
use rustc_middle::middle::region::{RvalueCandidate, Scope, ScopeTree};
66
use rustc_middle::ty::RvalueScopes;
77
use tracing::debug;
88

@@ -55,15 +55,11 @@ fn record_rvalue_scope_rec(
5555
fn record_rvalue_scope(
5656
rvalue_scopes: &mut RvalueScopes,
5757
expr: &hir::Expr<'_>,
58-
candidate: &RvalueCandidateType,
58+
candidate: &RvalueCandidate,
5959
) {
6060
debug!("resolve_rvalue_scope(expr={expr:?}, candidate={candidate:?})");
61-
match candidate {
62-
RvalueCandidateType::Borrow { lifetime, .. }
63-
| RvalueCandidateType::Pattern { lifetime, .. } => {
64-
record_rvalue_scope_rec(rvalue_scopes, expr, *lifetime)
65-
} // FIXME(@dingxiangfei2009): handle the candidates in the function call arguments
66-
}
61+
record_rvalue_scope_rec(rvalue_scopes, expr, candidate.lifetime)
62+
// FIXME(@dingxiangfei2009): handle the candidates in the function call arguments
6763
}
6864

6965
pub(crate) fn resolve_rvalue_scopes<'a, 'tcx>(

compiler/rustc_middle/src/middle/region.rs

+13-18
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ pub struct ScopeTree {
224224
/// and not the enclosing *statement*. Expressions that are not present in this
225225
/// table are not rvalue candidates. The set of rvalue candidates is computed
226226
/// during type check based on a traversal of the AST.
227-
pub rvalue_candidates: HirIdMap<RvalueCandidateType>,
227+
pub rvalue_candidates: HirIdMap<RvalueCandidate>,
228228

229229
/// Backwards incompatible scoping that will be introduced in future editions.
230230
/// This information is used later for linting to identify locals and
@@ -308,15 +308,14 @@ pub struct ScopeTree {
308308
pub yield_in_scope: UnordMap<Scope, Vec<YieldData>>,
309309
}
310310

311-
/// Identifies the reason that a given expression is an rvalue candidate
312-
/// (see the `rvalue_candidates` field for more information what rvalue
313-
/// candidates in general). In constants, the `lifetime` field is None
314-
/// to indicate that certain expressions escape into 'static and
315-
/// should have no local cleanup scope.
311+
/// See the `rvalue_candidates` field for more information on rvalue
312+
/// candidates in general.
313+
/// The `lifetime` field is None to indicate that certain expressions escape
314+
/// into 'static and should have no local cleanup scope.
316315
#[derive(Debug, Copy, Clone, HashStable)]
317-
pub enum RvalueCandidateType {
318-
Borrow { target: hir::ItemLocalId, lifetime: Option<Scope> },
319-
Pattern { target: hir::ItemLocalId, lifetime: Option<Scope> },
316+
pub struct RvalueCandidate {
317+
pub target: hir::ItemLocalId,
318+
pub lifetime: Option<Scope>,
320319
}
321320

322321
#[derive(Debug, Copy, Clone, HashStable)]
@@ -344,16 +343,12 @@ impl ScopeTree {
344343
self.var_map.insert(var, lifetime);
345344
}
346345

347-
pub fn record_rvalue_candidate(&mut self, var: HirId, candidate_type: RvalueCandidateType) {
348-
debug!("record_rvalue_candidate(var={var:?}, type={candidate_type:?})");
349-
match &candidate_type {
350-
RvalueCandidateType::Borrow { lifetime: Some(lifetime), .. }
351-
| RvalueCandidateType::Pattern { lifetime: Some(lifetime), .. } => {
352-
assert!(var.local_id != lifetime.local_id)
353-
}
354-
_ => {}
346+
pub fn record_rvalue_candidate(&mut self, var: HirId, candidate: RvalueCandidate) {
347+
debug!("record_rvalue_candidate(var={var:?}, candidate={candidate:?})");
348+
if let Some(lifetime) = &candidate.lifetime {
349+
assert!(var.local_id != lifetime.local_id)
355350
}
356-
self.rvalue_candidates.insert(var, candidate_type);
351+
self.rvalue_candidates.insert(var, candidate);
357352
}
358353

359354
/// Returns the narrowest scope that encloses `id`, if any.

0 commit comments

Comments
 (0)