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 | 111 +++++++++++++++++------------------ 1 file changed, 54 insertions(+), 57 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,63 +1272,6 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> { 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.) - let ident = ident.modern_and_legacy(); - let mut res = Res::Local(pat_id); - match bindings.get(&ident).cloned() { - Some(id) if id == outer_pat_id => { - // `Variant(a, a)`, error - self.r.report_error( - ident.span, - 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()) - ); - } - Some(..) if pat_src == PatternSource::Match || - pat_src == PatternSource::Let => { - // `Variant1(a) | Variant2(a)`, ok - // Reuse definition from the first `a`. - res = self.innermost_rib_bindings(ValueNS)[&ident]; - } - Some(..) => { - span_bug!(ident.span, "two bindings with the same name from \ - unexpected pattern source {:?}", pat_src); - } - None => { - // A completely fresh binding, add to the lists if it's valid. - if ident.name != kw::Invalid { - bindings.insert(ident, outer_pat_id); - self.innermost_rib_bindings(ValueNS).insert(ident, res); - } - } - } - - res - } - - 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, @@ -1368,6 +1311,60 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> { 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() { + Some(id) if id == outer_pat_id => { + // `Variant(a, a)`, error + self.r.report_error( + ident.span, + 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()), + ); + } + Some(..) if pat_src == PatternSource::Match || + pat_src == PatternSource::Let => { + // `Variant1(a) | Variant2(a)`, ok + // Reuse definition from the first `a`. + res = self.innermost_rib_bindings(ValueNS)[&ident]; + } + Some(..) => { + span_bug!(ident.span, "two bindings with the same name from \ + unexpected pattern source {:?}", pat_src); + } + None => { + // A completely fresh binding, add to the lists if it's valid. + if ident.name != kw::Invalid { + bindings.insert(ident, outer_pat_id); + self.innermost_rib_bindings(ValueNS).insert(ident, res); + } + } + } + + res + } + + fn innermost_rib_bindings(&mut self, ns: Namespace) -> &mut IdentMap { + &mut self.ribs[ns].last_mut().unwrap().bindings + } + fn try_resolve_as_non_binding( &mut self, pat_src: PatternSource,