Simplify RvalueCandidateType.
There is no difference between the Patternand Borrow cases. Reduce it to a simple struct.
This commit is contained in:
parent
1da5e60ac5
commit
227f93395a
3 changed files with 19 additions and 34 deletions
|
@ -623,10 +623,7 @@ fn resolve_local<'tcx>(
|
||||||
if is_binding_pat(pat) {
|
if is_binding_pat(pat) {
|
||||||
visitor.scope_tree.record_rvalue_candidate(
|
visitor.scope_tree.record_rvalue_candidate(
|
||||||
expr.hir_id,
|
expr.hir_id,
|
||||||
RvalueCandidateType::Pattern {
|
RvalueCandidate { target: expr.hir_id.local_id, lifetime: blk_scope },
|
||||||
target: expr.hir_id.local_id,
|
|
||||||
lifetime: blk_scope,
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -731,10 +728,7 @@ fn resolve_local<'tcx>(
|
||||||
record_rvalue_scope_if_borrow_expr(visitor, subexpr, blk_id);
|
record_rvalue_scope_if_borrow_expr(visitor, subexpr, blk_id);
|
||||||
visitor.scope_tree.record_rvalue_candidate(
|
visitor.scope_tree.record_rvalue_candidate(
|
||||||
subexpr.hir_id,
|
subexpr.hir_id,
|
||||||
RvalueCandidateType::Borrow {
|
RvalueCandidate { target: subexpr.hir_id.local_id, lifetime: blk_id },
|
||||||
target: subexpr.hir_id.local_id,
|
|
||||||
lifetime: blk_id,
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
hir::ExprKind::Struct(_, fields, _) => {
|
hir::ExprKind::Struct(_, fields, _) => {
|
||||||
|
|
|
@ -2,7 +2,7 @@ use hir::Node;
|
||||||
use hir::def_id::DefId;
|
use hir::def_id::DefId;
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_middle::bug;
|
use rustc_middle::bug;
|
||||||
use rustc_middle::middle::region::{RvalueCandidateType, Scope, ScopeTree};
|
use rustc_middle::middle::region::{RvalueCandidate, Scope, ScopeTree};
|
||||||
use rustc_middle::ty::RvalueScopes;
|
use rustc_middle::ty::RvalueScopes;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
|
@ -55,15 +55,11 @@ fn record_rvalue_scope_rec(
|
||||||
fn record_rvalue_scope(
|
fn record_rvalue_scope(
|
||||||
rvalue_scopes: &mut RvalueScopes,
|
rvalue_scopes: &mut RvalueScopes,
|
||||||
expr: &hir::Expr<'_>,
|
expr: &hir::Expr<'_>,
|
||||||
candidate: &RvalueCandidateType,
|
candidate: &RvalueCandidate,
|
||||||
) {
|
) {
|
||||||
debug!("resolve_rvalue_scope(expr={expr:?}, candidate={candidate:?})");
|
debug!("resolve_rvalue_scope(expr={expr:?}, candidate={candidate:?})");
|
||||||
match candidate {
|
record_rvalue_scope_rec(rvalue_scopes, expr, candidate.lifetime)
|
||||||
RvalueCandidateType::Borrow { lifetime, .. }
|
// FIXME(@dingxiangfei2009): handle the candidates in the function call arguments
|
||||||
| RvalueCandidateType::Pattern { lifetime, .. } => {
|
|
||||||
record_rvalue_scope_rec(rvalue_scopes, expr, *lifetime)
|
|
||||||
} // FIXME(@dingxiangfei2009): handle the candidates in the function call arguments
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn resolve_rvalue_scopes<'a, 'tcx>(
|
pub(crate) fn resolve_rvalue_scopes<'a, 'tcx>(
|
||||||
|
|
|
@ -224,7 +224,7 @@ pub struct ScopeTree {
|
||||||
/// and not the enclosing *statement*. Expressions that are not present in this
|
/// and not the enclosing *statement*. Expressions that are not present in this
|
||||||
/// table are not rvalue candidates. The set of rvalue candidates is computed
|
/// table are not rvalue candidates. The set of rvalue candidates is computed
|
||||||
/// during type check based on a traversal of the AST.
|
/// during type check based on a traversal of the AST.
|
||||||
pub rvalue_candidates: HirIdMap<RvalueCandidateType>,
|
pub rvalue_candidates: HirIdMap<RvalueCandidate>,
|
||||||
|
|
||||||
/// Backwards incompatible scoping that will be introduced in future editions.
|
/// Backwards incompatible scoping that will be introduced in future editions.
|
||||||
/// This information is used later for linting to identify locals and
|
/// This information is used later for linting to identify locals and
|
||||||
|
@ -308,15 +308,14 @@ pub struct ScopeTree {
|
||||||
pub yield_in_scope: UnordMap<Scope, Vec<YieldData>>,
|
pub yield_in_scope: UnordMap<Scope, Vec<YieldData>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Identifies the reason that a given expression is an rvalue candidate
|
/// See the `rvalue_candidates` field for more information on rvalue
|
||||||
/// (see the `rvalue_candidates` field for more information what rvalue
|
/// candidates in general.
|
||||||
/// candidates in general). In constants, the `lifetime` field is None
|
/// The `lifetime` field is None to indicate that certain expressions escape
|
||||||
/// to indicate that certain expressions escape into 'static and
|
/// into 'static and should have no local cleanup scope.
|
||||||
/// should have no local cleanup scope.
|
|
||||||
#[derive(Debug, Copy, Clone, HashStable)]
|
#[derive(Debug, Copy, Clone, HashStable)]
|
||||||
pub enum RvalueCandidateType {
|
pub struct RvalueCandidate {
|
||||||
Borrow { target: hir::ItemLocalId, lifetime: Option<Scope> },
|
pub target: hir::ItemLocalId,
|
||||||
Pattern { target: hir::ItemLocalId, lifetime: Option<Scope> },
|
pub lifetime: Option<Scope>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, HashStable)]
|
#[derive(Debug, Copy, Clone, HashStable)]
|
||||||
|
@ -344,16 +343,12 @@ impl ScopeTree {
|
||||||
self.var_map.insert(var, lifetime);
|
self.var_map.insert(var, lifetime);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn record_rvalue_candidate(&mut self, var: HirId, candidate_type: RvalueCandidateType) {
|
pub fn record_rvalue_candidate(&mut self, var: HirId, candidate: RvalueCandidate) {
|
||||||
debug!("record_rvalue_candidate(var={var:?}, type={candidate_type:?})");
|
debug!("record_rvalue_candidate(var={var:?}, candidate={candidate:?})");
|
||||||
match &candidate_type {
|
if let Some(lifetime) = &candidate.lifetime {
|
||||||
RvalueCandidateType::Borrow { lifetime: Some(lifetime), .. }
|
assert!(var.local_id != lifetime.local_id)
|
||||||
| RvalueCandidateType::Pattern { lifetime: Some(lifetime), .. } => {
|
|
||||||
assert!(var.local_id != lifetime.local_id)
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
self.rvalue_candidates.insert(var, candidate_type);
|
self.rvalue_candidates.insert(var, candidate);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the narrowest scope that encloses `id`, if any.
|
/// Returns the narrowest scope that encloses `id`, if any.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue