From fd3b44184650b12ce30900e49e4d3ecb462ea540 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Wed, 28 Aug 2019 10:27:40 +0200 Subject: [PATCH] resolve: move `fresh_binding`. --- src/librustc_resolve/late.rs | 107 +++++++++++++++++------------------ 1 file changed, 52 insertions(+), 55 deletions(-) diff --git a/src/librustc_resolve/late.rs b/src/librustc_resolve/late.rs index 29264b3eedc..1b550bd7b0f 100644 --- a/src/librustc_resolve/late.rs +++ b/src/librustc_resolve/late.rs @@ -1272,18 +1272,56 @@ fn resolve_block(&mut self, block: &Block) { debug!("(resolving block) leaving block"); } - fn fresh_binding(&mut self, - ident: Ident, - pat_id: NodeId, - outer_pat_id: NodeId, - pat_src: PatternSource, - bindings: &mut IdentMap) - -> Res { - // Add the binding to the local ribs, if it - // doesn't already exist in the bindings map. (We - // must not add it if it's in the bindings map - // because that breaks the assumptions later - // passes make about or-patterns.) + fn resolve_pattern( + &mut self, + pat: &Pat, + pat_src: PatternSource, + // Maps idents to the node ID for the outermost pattern that binds them. + bindings: &mut IdentMap, + ) { + // Visit all direct subpatterns of this pattern. + let outer_pat_id = pat.id; + pat.walk(&mut |pat| { + debug!("resolve_pattern pat={:?} node={:?}", pat, pat.node); + match pat.node { + PatKind::Ident(bmode, ident, ref sub) => { + // First try to resolve the identifier as some existing entity, + // then fall back to a fresh binding. + let has_sub = sub.is_some(); + let res = self.try_resolve_as_non_binding(pat_src, pat, bmode, ident, has_sub) + .unwrap_or_else(|| { + self.fresh_binding(ident, pat.id, outer_pat_id, pat_src, bindings) + }); + self.r.record_partial_res(pat.id, PartialRes::new(res)); + } + PatKind::TupleStruct(ref path, ..) => { + self.smart_resolve_path(pat.id, None, path, PathSource::TupleStruct); + } + PatKind::Path(ref qself, ref path) => { + self.smart_resolve_path(pat.id, qself.as_ref(), path, PathSource::Pat); + } + PatKind::Struct(ref path, ..) => { + self.smart_resolve_path(pat.id, None, path, PathSource::Struct); + } + _ => {} + } + true + }); + + visit::walk_pat(self, pat); + } + + fn fresh_binding( + &mut self, + ident: Ident, + pat_id: NodeId, + outer_pat_id: NodeId, + pat_src: PatternSource, + bindings: &mut IdentMap, + ) -> Res { + // Add the binding to the local ribs, if it doesn't already exist in the bindings map. + // (We must not add it if it's in the bindings map because that breaks the assumptions + // later passes make about or-patterns.) let ident = ident.modern_and_legacy(); let mut res = Res::Local(pat_id); match bindings.get(&ident).cloned() { @@ -1291,16 +1329,14 @@ fn fresh_binding(&mut self, // `Variant(a, a)`, error self.r.report_error( ident.span, - ResolutionError::IdentifierBoundMoreThanOnceInSamePattern( - &ident.as_str()) + ResolutionError::IdentifierBoundMoreThanOnceInSamePattern(&ident.as_str()), ); } Some(..) if pat_src == PatternSource::FnParam => { // `fn f(a: u8, a: u8)`, error self.r.report_error( ident.span, - ResolutionError::IdentifierBoundMoreThanOnceInParameterList( - &ident.as_str()) + ResolutionError::IdentifierBoundMoreThanOnceInParameterList(&ident.as_str()), ); } Some(..) if pat_src == PatternSource::Match || @@ -1329,45 +1365,6 @@ fn innermost_rib_bindings(&mut self, ns: Namespace) -> &mut IdentMap { &mut self.ribs[ns].last_mut().unwrap().bindings } - fn resolve_pattern( - &mut self, - pat: &Pat, - pat_src: PatternSource, - // Maps idents to the node ID for the outermost pattern that binds them. - bindings: &mut IdentMap, - ) { - // Visit all direct subpatterns of this pattern. - let outer_pat_id = pat.id; - pat.walk(&mut |pat| { - debug!("resolve_pattern pat={:?} node={:?}", pat, pat.node); - match pat.node { - PatKind::Ident(bmode, ident, ref sub) => { - // First try to resolve the identifier as some existing entity, - // then fall back to a fresh binding. - let has_sub = sub.is_some(); - let res = self.try_resolve_as_non_binding(pat_src, pat, bmode, ident, has_sub) - .unwrap_or_else(|| { - self.fresh_binding(ident, pat.id, outer_pat_id, pat_src, bindings) - }); - self.r.record_partial_res(pat.id, PartialRes::new(res)); - } - PatKind::TupleStruct(ref path, ..) => { - self.smart_resolve_path(pat.id, None, path, PathSource::TupleStruct); - } - PatKind::Path(ref qself, ref path) => { - self.smart_resolve_path(pat.id, qself.as_ref(), path, PathSource::Pat); - } - PatKind::Struct(ref path, ..) => { - self.smart_resolve_path(pat.id, None, path, PathSource::Struct); - } - _ => {} - } - true - }); - - visit::walk_pat(self, pat); - } - fn try_resolve_as_non_binding( &mut self, pat_src: PatternSource, -- 2.44.0