Skip to content

Commit fd3b441

Browse files
committed
resolve: move fresh_binding.
1 parent f343e84 commit fd3b441

File tree

1 file changed

+52
-55
lines changed

1 file changed

+52
-55
lines changed

src/librustc_resolve/late.rs

Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,35 +1272,71 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
12721272
debug!("(resolving block) leaving block");
12731273
}
12741274

1275-
fn fresh_binding(&mut self,
1276-
ident: Ident,
1277-
pat_id: NodeId,
1278-
outer_pat_id: NodeId,
1279-
pat_src: PatternSource,
1280-
bindings: &mut IdentMap<NodeId>)
1281-
-> Res {
1282-
// Add the binding to the local ribs, if it
1283-
// doesn't already exist in the bindings map. (We
1284-
// must not add it if it's in the bindings map
1285-
// because that breaks the assumptions later
1286-
// passes make about or-patterns.)
1275+
fn resolve_pattern(
1276+
&mut self,
1277+
pat: &Pat,
1278+
pat_src: PatternSource,
1279+
// Maps idents to the node ID for the outermost pattern that binds them.
1280+
bindings: &mut IdentMap<NodeId>,
1281+
) {
1282+
// Visit all direct subpatterns of this pattern.
1283+
let outer_pat_id = pat.id;
1284+
pat.walk(&mut |pat| {
1285+
debug!("resolve_pattern pat={:?} node={:?}", pat, pat.node);
1286+
match pat.node {
1287+
PatKind::Ident(bmode, ident, ref sub) => {
1288+
// First try to resolve the identifier as some existing entity,
1289+
// then fall back to a fresh binding.
1290+
let has_sub = sub.is_some();
1291+
let res = self.try_resolve_as_non_binding(pat_src, pat, bmode, ident, has_sub)
1292+
.unwrap_or_else(|| {
1293+
self.fresh_binding(ident, pat.id, outer_pat_id, pat_src, bindings)
1294+
});
1295+
self.r.record_partial_res(pat.id, PartialRes::new(res));
1296+
}
1297+
PatKind::TupleStruct(ref path, ..) => {
1298+
self.smart_resolve_path(pat.id, None, path, PathSource::TupleStruct);
1299+
}
1300+
PatKind::Path(ref qself, ref path) => {
1301+
self.smart_resolve_path(pat.id, qself.as_ref(), path, PathSource::Pat);
1302+
}
1303+
PatKind::Struct(ref path, ..) => {
1304+
self.smart_resolve_path(pat.id, None, path, PathSource::Struct);
1305+
}
1306+
_ => {}
1307+
}
1308+
true
1309+
});
1310+
1311+
visit::walk_pat(self, pat);
1312+
}
1313+
1314+
fn fresh_binding(
1315+
&mut self,
1316+
ident: Ident,
1317+
pat_id: NodeId,
1318+
outer_pat_id: NodeId,
1319+
pat_src: PatternSource,
1320+
bindings: &mut IdentMap<NodeId>,
1321+
) -> Res {
1322+
// Add the binding to the local ribs, if it doesn't already exist in the bindings map.
1323+
// (We must not add it if it's in the bindings map because that breaks the assumptions
1324+
// later passes make about or-patterns.)
12871325
let ident = ident.modern_and_legacy();
12881326
let mut res = Res::Local(pat_id);
12891327
match bindings.get(&ident).cloned() {
12901328
Some(id) if id == outer_pat_id => {
12911329
// `Variant(a, a)`, error
12921330
self.r.report_error(
12931331
ident.span,
1294-
ResolutionError::IdentifierBoundMoreThanOnceInSamePattern(
1295-
&ident.as_str())
1332+
ResolutionError::IdentifierBoundMoreThanOnceInSamePattern(&ident.as_str()),
12961333
);
12971334
}
12981335
Some(..) if pat_src == PatternSource::FnParam => {
12991336
// `fn f(a: u8, a: u8)`, error
13001337
self.r.report_error(
13011338
ident.span,
1302-
ResolutionError::IdentifierBoundMoreThanOnceInParameterList(
1303-
&ident.as_str())
1339+
ResolutionError::IdentifierBoundMoreThanOnceInParameterList(&ident.as_str()),
13041340
);
13051341
}
13061342
Some(..) if pat_src == PatternSource::Match ||
@@ -1329,45 +1365,6 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
13291365
&mut self.ribs[ns].last_mut().unwrap().bindings
13301366
}
13311367

1332-
fn resolve_pattern(
1333-
&mut self,
1334-
pat: &Pat,
1335-
pat_src: PatternSource,
1336-
// Maps idents to the node ID for the outermost pattern that binds them.
1337-
bindings: &mut IdentMap<NodeId>,
1338-
) {
1339-
// Visit all direct subpatterns of this pattern.
1340-
let outer_pat_id = pat.id;
1341-
pat.walk(&mut |pat| {
1342-
debug!("resolve_pattern pat={:?} node={:?}", pat, pat.node);
1343-
match pat.node {
1344-
PatKind::Ident(bmode, ident, ref sub) => {
1345-
// First try to resolve the identifier as some existing entity,
1346-
// then fall back to a fresh binding.
1347-
let has_sub = sub.is_some();
1348-
let res = self.try_resolve_as_non_binding(pat_src, pat, bmode, ident, has_sub)
1349-
.unwrap_or_else(|| {
1350-
self.fresh_binding(ident, pat.id, outer_pat_id, pat_src, bindings)
1351-
});
1352-
self.r.record_partial_res(pat.id, PartialRes::new(res));
1353-
}
1354-
PatKind::TupleStruct(ref path, ..) => {
1355-
self.smart_resolve_path(pat.id, None, path, PathSource::TupleStruct);
1356-
}
1357-
PatKind::Path(ref qself, ref path) => {
1358-
self.smart_resolve_path(pat.id, qself.as_ref(), path, PathSource::Pat);
1359-
}
1360-
PatKind::Struct(ref path, ..) => {
1361-
self.smart_resolve_path(pat.id, None, path, PathSource::Struct);
1362-
}
1363-
_ => {}
1364-
}
1365-
true
1366-
});
1367-
1368-
visit::walk_pat(self, pat);
1369-
}
1370-
13711368
fn try_resolve_as_non_binding(
13721369
&mut self,
13731370
pat_src: PatternSource,

0 commit comments

Comments
 (0)