From c19841cdb261cd6a622d81e65133669f2c8a6fed Mon Sep 17 00:00:00 2001 From: dobkeratops Date: Fri, 14 Mar 2014 20:23:11 +0000 Subject: [PATCH 1/8] added 1st attempt at suggested symbol error message (when a symbol isn't found, it scans for the same symbol ident and shows their full path. much cleanup needed.. --- src/librustc/middle/resolve.rs | 114 ++++++++++++++++++++++++++++----- 1 file changed, 99 insertions(+), 15 deletions(-) diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index 77e44c137a983..ad7a2d8002299 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -12,6 +12,7 @@ use driver::session::Session; use metadata::csearch; +use metadata::cstore; use metadata::decoder::{DefLike, DlDef, DlField, DlImpl}; use middle::lang_items::LanguageItems; use middle::lint::{UnnecessaryQualification, UnusedImports}; @@ -153,7 +154,7 @@ enum NameDefinition { ImportNameDefinition(Def, LastPrivate) //< The name identifies an import. } -impl Visitor<()> for Resolver { +impl<'a> Visitor<()> for Resolver<'a> { fn visit_item(&mut self, item: &Item, _: ()) { self.resolve_item(item); } @@ -787,9 +788,10 @@ fn namespace_error_to_str(ns: NamespaceError) -> &'static str { } } -fn Resolver(session: Session, +fn Resolver<'a>(session: Session, lang_items: @LanguageItems, - crate_span: Span) -> Resolver { + crate_span: Span, + krate:&'a ast::Crate) -> Resolver<'a> { let graph_root = @NameBindings(); graph_root.define_module(NoParentLink, @@ -809,6 +811,7 @@ fn Resolver(session: Session, // AST. graph_root: graph_root, + resolver_krate:krate, method_map: @RefCell::new(HashMap::new()), structs: HashSet::new(), @@ -843,7 +846,7 @@ fn Resolver(session: Session, } /// The main resolver class. -struct Resolver { +struct Resolver<'a> { session: @Session, lang_items: @LanguageItems, @@ -852,6 +855,8 @@ struct Resolver { method_map: @RefCell>>, structs: HashSet, + resolver_krate: &'a ast::Crate, // for error message search-"did you mean..." + // The number of imports that are currently unresolved. unresolved_imports: uint, @@ -896,11 +901,11 @@ struct Resolver { used_imports: HashSet<(NodeId, Namespace)>, } -struct BuildReducedGraphVisitor<'a> { - resolver: &'a mut Resolver, +struct BuildReducedGraphVisitor<'a,'r> { + resolver: &'a mut Resolver<'r>, } -impl<'a> Visitor for BuildReducedGraphVisitor<'a> { +impl<'a,'r> Visitor for BuildReducedGraphVisitor<'a,'r> { fn visit_item(&mut self, item: &Item, context: ReducedGraphParent) { let p = self.resolver.build_reduced_graph_for_item(item, context); @@ -920,7 +925,7 @@ impl<'a> Visitor for BuildReducedGraphVisitor<'a> { fn visit_view_item(&mut self, view_item: &ViewItem, context: ReducedGraphParent) { self.resolver.build_reduced_graph_for_view_item(view_item, context); } - + fn visit_block(&mut self, block: &Block, context: ReducedGraphParent) { let np = self.resolver.build_reduced_graph_for_block(block, context); visit::walk_block(self, block, np); @@ -928,18 +933,19 @@ impl<'a> Visitor for BuildReducedGraphVisitor<'a> { } -struct UnusedImportCheckVisitor<'a> { resolver: &'a mut Resolver } +struct UnusedImportCheckVisitor<'a,'r> { resolver: &'a mut Resolver<'r> } -impl<'a> Visitor<()> for UnusedImportCheckVisitor<'a> { +impl<'a,'r> Visitor<()> for UnusedImportCheckVisitor<'a,'r> { fn visit_view_item(&mut self, vi: &ViewItem, _: ()) { self.resolver.check_for_item_unused_imports(vi); visit::walk_view_item(self, vi, ()); } } -impl Resolver { +impl<'r> Resolver<'r> { /// The main name resolution procedure. - fn resolve(&mut self, krate: &ast::Crate) { + fn resolve(&mut self, krate: &'r ast::Crate) { + self.resolver_krate = krate; self.build_reduced_graph(krate); self.session.abort_if_errors(); @@ -2860,7 +2866,7 @@ impl Resolver { } UseLexicalScope => { // This is not a crate-relative path. We resolve the - // first component of the path in the current lexical + // first component of the path in the current lexical // scope and then proceed to resolve below that. let result = self.resolve_module_in_lexical_scope( module_, @@ -5163,7 +5169,7 @@ impl Resolver { match self.find_best_match_for_name(wrong_name, 5) { Some(m) => { self.resolve_error(expr.span, - format!("unresolved name `{}`. \ + format!("unresolved name `{}`. \ Did you mean `{}`?", wrong_name, m)); } @@ -5171,6 +5177,7 @@ impl Resolver { self.resolve_error(expr.span, format!("unresolved name `{}`.", wrong_name)); + self.find_unresolved_symbol_elsewhere(path); } } } @@ -5560,8 +5567,85 @@ impl Resolver { debug!("* {}:{}{}", token::get_name(name), value_repr, type_repr); } } + + // Search other modules for this ident, and advise possible module paths + fn find_unresolved_symbol_elsewhere(&mut self, path:&Path) { + + let mut finder_visitor= FindSymbolVisitor{ + cstore:self.session.cstore, + curr_path_idents: ::std::vec_ng::Vec::new(), + path_to_find:path, + suggestions: ::std::vec_ng::Vec::new(), + max_suggestions: 5, + }; + + visit::walk_crate(&mut finder_visitor, self.resolver_krate, ()); + + if finder_visitor.suggestions.len()>0 { + let mut note=~"did you mean\n"; + for &(ref name,score) in finder_visitor.suggestions.iter() { + note.push_str(*name); + note.push_str("\n"); + } + self.session.span_note(path.span, note); + } + } } +struct FindSymbolVisitor<'a>{ + cstore:@cstore::CStore, + curr_path_idents: ::std::vec_ng::Vec, + path_to_find:&'a Path, + suggestions: ::std::vec_ng::Vec<(~str,int)>, + max_suggestions: uint, +} + +impl<'a,'r> Visitor<()> for FindSymbolVisitor<'a> { + fn visit_ident<'a>(&mut self, _sp:Span, ident:Ident, e: ()) { + // ::std::io::println(format!("visit ident {:?}", ident)); + } + fn visit_item<'a>(&mut self, i:&Item, e: ()) { + let find_path_last_seg:&PathSegment = self.path_to_find.segments.last().unwrap(); + + +// std::io::println(); + let mut cmp_path_str:~str=~"::"; + for &n in self.curr_path_idents.iter() { + cmp_path_str.push_str( + self.cstore.intr.get_ref(n)); + cmp_path_str.push_str(~"::"); + } + cmp_path_str.push_str(self.cstore.intr.get_ref(i.ident.name)); + + debug!("visit item: {:?} ?== {:s} ??", + self.cstore.intr.get_ref(find_path_last_seg.identifier.name), + cmp_path_str + ); + + if find_path_last_seg.identifier.name== i.ident.name && + self.suggestions.len() < self.max_suggestions { + // todo: score = num matched supplied paths, plus number of paths + // already in scope + // todo - prune lowest score if count too high + let score=0; + self.suggestions.push((cmp_path_str,score)); + } + + // visit sub nodes.. + self.curr_path_idents.push(i.ident.name); + visit::walk_item(self,i,e); + self.curr_path_idents.pop(); + } + + fn visit_mod(&mut self, m: &Mod, _s: Span, _n: NodeId, e: ()) { + visit::walk_mod(self, m, e) ; + } + + +} + + + pub struct CrateMap { def_map: DefMap, exp_map2: ExportMap2, @@ -5575,7 +5659,7 @@ pub fn resolve_crate(session: Session, lang_items: @LanguageItems, krate: &Crate) -> CrateMap { - let mut resolver = Resolver(session, lang_items, krate.span); + let mut resolver = Resolver(session, lang_items, krate.span, krate); resolver.resolve(krate); let Resolver { def_map, export_map2, trait_map, last_private, external_exports, .. } = resolver; From 9e5cc06c082cc6fd18dc10ad930d5cea492a0554 Mon Sep 17 00:00:00 2001 From: dobkeratops Date: Sun, 16 Mar 2014 20:07:30 +0000 Subject: [PATCH 2/8] fixed warnings --- src/librustc/middle/resolve.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index ad7a2d8002299..7cf8543d7449a 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -2692,7 +2692,7 @@ impl<'r> Resolver<'r> { /// Resolves the given module path from the given root `module_`. fn resolve_module_path_from_root(&mut self, - module_: @Module, + module_: @Module, module_path: &[Ident], index: uint, span: Span, @@ -5583,7 +5583,8 @@ impl<'r> Resolver<'r> { if finder_visitor.suggestions.len()>0 { let mut note=~"did you mean\n"; - for &(ref name,score) in finder_visitor.suggestions.iter() { + for &(ref name,_) in finder_visitor.suggestions.iter() { + note.push_str("\t\t"); note.push_str(*name); note.push_str("\n"); } @@ -5601,9 +5602,9 @@ struct FindSymbolVisitor<'a>{ } impl<'a,'r> Visitor<()> for FindSymbolVisitor<'a> { - fn visit_ident<'a>(&mut self, _sp:Span, ident:Ident, e: ()) { +// fn visit_ident<'a>(&mut self, _sp:Span, _:Ident, e: ()) { // ::std::io::println(format!("visit ident {:?}", ident)); - } +// } fn visit_item<'a>(&mut self, i:&Item, e: ()) { let find_path_last_seg:&PathSegment = self.path_to_find.segments.last().unwrap(); @@ -5613,7 +5614,7 @@ impl<'a,'r> Visitor<()> for FindSymbolVisitor<'a> { for &n in self.curr_path_idents.iter() { cmp_path_str.push_str( self.cstore.intr.get_ref(n)); - cmp_path_str.push_str(~"::"); + cmp_path_str.push_str("::"); } cmp_path_str.push_str(self.cstore.intr.get_ref(i.ident.name)); @@ -5640,8 +5641,6 @@ impl<'a,'r> Visitor<()> for FindSymbolVisitor<'a> { fn visit_mod(&mut self, m: &Mod, _s: Span, _n: NodeId, e: ()) { visit::walk_mod(self, m, e) ; } - - } From b26d014dba9bdd210f77d4d6aea580d4963a7dc0 Mon Sep 17 00:00:00 2001 From: dobkeratops Date: Sun, 16 Mar 2014 23:50:31 +0000 Subject: [PATCH 3/8] sorted items, cached Idents instead of strings --- src/librustc/middle/resolve.rs | 63 +++++++++++++++------------------- 1 file changed, 28 insertions(+), 35 deletions(-) diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index 7cf8543d7449a..c887f2a2c76ec 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -34,6 +34,7 @@ use syntax::visit::Visitor; use std::cell::{Cell, RefCell}; use std::uint; +use std::cmp; use std::mem::replace; use std::vec_ng::Vec; use collections::{HashMap, HashSet}; @@ -5573,20 +5574,18 @@ impl<'r> Resolver<'r> { let mut finder_visitor= FindSymbolVisitor{ cstore:self.session.cstore, - curr_path_idents: ::std::vec_ng::Vec::new(), + curr_path_idents: Vec::new(), path_to_find:path, - suggestions: ::std::vec_ng::Vec::new(), + suggestions: Vec::new(), max_suggestions: 5, }; visit::walk_crate(&mut finder_visitor, self.resolver_krate, ()); if finder_visitor.suggestions.len()>0 { - let mut note=~"did you mean\n"; + let mut note=~"did you mean:-\n"; for &(ref name,_) in finder_visitor.suggestions.iter() { - note.push_str("\t\t"); - note.push_str(*name); - note.push_str("\n"); + note.push_str("\t\t\t"+ "::"+name.map(|i|{token::get_ident(*i).get().to_str()}).connect("::")+"\n"); } self.session.span_note(path.span, note); } @@ -5595,46 +5594,40 @@ impl<'r> Resolver<'r> { struct FindSymbolVisitor<'a>{ cstore:@cstore::CStore, - curr_path_idents: ::std::vec_ng::Vec, + curr_path_idents: Vec, path_to_find:&'a Path, - suggestions: ::std::vec_ng::Vec<(~str,int)>, + suggestions: Vec<(Vec,uint)>, max_suggestions: uint, } + impl<'a,'r> Visitor<()> for FindSymbolVisitor<'a> { -// fn visit_ident<'a>(&mut self, _sp:Span, _:Ident, e: ()) { - // ::std::io::println(format!("visit ident {:?}", ident)); -// } - fn visit_item<'a>(&mut self, i:&Item, e: ()) { + fn visit_item<'a>(&mut self, item:&Item, e: ()) { let find_path_last_seg:&PathSegment = self.path_to_find.segments.last().unwrap(); + self.curr_path_idents.push(item.ident); -// std::io::println(); - let mut cmp_path_str:~str=~"::"; - for &n in self.curr_path_idents.iter() { - cmp_path_str.push_str( - self.cstore.intr.get_ref(n)); - cmp_path_str.push_str("::"); - } - cmp_path_str.push_str(self.cstore.intr.get_ref(i.ident.name)); - - debug!("visit item: {:?} ?== {:s} ??", - self.cstore.intr.get_ref(find_path_last_seg.identifier.name), - cmp_path_str - ); - - if find_path_last_seg.identifier.name== i.ident.name && - self.suggestions.len() < self.max_suggestions { - // todo: score = num matched supplied paths, plus number of paths - // already in scope - // todo - prune lowest score if count too high - let score=0; - self.suggestions.push((cmp_path_str,score)); + //todo: compare with current 'use' and uprate suggestions with more segs in scope. + if find_path_last_seg.identifier.name== item.ident.name && + self.suggestions.len() <= (self.max_suggestions) { + + let mut score=0; + for ref find_seg in self.path_to_find.segments.iter() { + for (ref index,ref ident) in self.curr_path_idents.iter().enumerate() { + if ident.name==find_seg.identifier.name {score+=*index;} + } + } + // sort such that highscore is first. + self.suggestions.push((self.curr_path_idents.clone(),score)); + self.suggestions.sort_by( + |&(_,ref score1),&(_,ref score2)| + if score1 self.max_suggestions {self.suggestions.pop();} } // visit sub nodes.. - self.curr_path_idents.push(i.ident.name); - visit::walk_item(self,i,e); + + visit::walk_item(self,item,e); self.curr_path_idents.pop(); } From 8091d0401843b88d3cc04e761f4ac2776a11cd91 Mon Sep 17 00:00:00 2001 From: dobkeratops Date: Mon, 17 Mar 2014 00:12:16 +0000 Subject: [PATCH 4/8] formatting, removed tabs --- src/librustc/middle/resolve.rs | 127 +++++++++++++++++---------------- 1 file changed, 66 insertions(+), 61 deletions(-) diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index c887f2a2c76ec..c340b15853360 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -792,7 +792,7 @@ fn namespace_error_to_str(ns: NamespaceError) -> &'static str { fn Resolver<'a>(session: Session, lang_items: @LanguageItems, crate_span: Span, - krate:&'a ast::Crate) -> Resolver<'a> { + krate:&'a ast::Crate) -> Resolver<'a> { let graph_root = @NameBindings(); graph_root.define_module(NoParentLink, @@ -812,7 +812,7 @@ fn Resolver<'a>(session: Session, // AST. graph_root: graph_root, - resolver_krate:krate, + resolver_krate:krate, method_map: @RefCell::new(HashMap::new()), structs: HashSet::new(), @@ -856,7 +856,7 @@ struct Resolver<'a> { method_map: @RefCell>>, structs: HashSet, - resolver_krate: &'a ast::Crate, // for error message search-"did you mean..." + resolver_krate: &'a ast::Crate, // for error message search-"did you mean..." // The number of imports that are currently unresolved. unresolved_imports: uint, @@ -926,7 +926,7 @@ impl<'a,'r> Visitor for BuildReducedGraphVisitor<'a,'r> { fn visit_view_item(&mut self, view_item: &ViewItem, context: ReducedGraphParent) { self.resolver.build_reduced_graph_for_view_item(view_item, context); } - + fn visit_block(&mut self, block: &Block, context: ReducedGraphParent) { let np = self.resolver.build_reduced_graph_for_block(block, context); visit::walk_block(self, block, np); @@ -946,7 +946,7 @@ impl<'a,'r> Visitor<()> for UnusedImportCheckVisitor<'a,'r> { impl<'r> Resolver<'r> { /// The main name resolution procedure. fn resolve(&mut self, krate: &'r ast::Crate) { - self.resolver_krate = krate; + self.resolver_krate = krate; self.build_reduced_graph(krate); self.session.abort_if_errors(); @@ -2693,7 +2693,7 @@ impl<'r> Resolver<'r> { /// Resolves the given module path from the given root `module_`. fn resolve_module_path_from_root(&mut self, - module_: @Module, + module_: @Module, module_path: &[Ident], index: uint, span: Span, @@ -2867,7 +2867,7 @@ impl<'r> Resolver<'r> { } UseLexicalScope => { // This is not a crate-relative path. We resolve the - // first component of the path in the current lexical + // first component of the path in the current lexical // scope and then proceed to resolve below that. let result = self.resolve_module_in_lexical_scope( module_, @@ -5178,7 +5178,7 @@ impl<'r> Resolver<'r> { self.resolve_error(expr.span, format!("unresolved name `{}`.", wrong_name)); - self.find_unresolved_symbol_elsewhere(path); + self.find_unresolved_symbol_elsewhere(path); } } } @@ -5569,71 +5569,76 @@ impl<'r> Resolver<'r> { } } - // Search other modules for this ident, and advise possible module paths - fn find_unresolved_symbol_elsewhere(&mut self, path:&Path) { + // Search other modules for this ident, and advise possible module paths + fn find_unresolved_symbol_elsewhere(&mut self, path:&Path) { - let mut finder_visitor= FindSymbolVisitor{ - cstore:self.session.cstore, - curr_path_idents: Vec::new(), - path_to_find:path, - suggestions: Vec::new(), - max_suggestions: 5, - }; + let mut finder_visitor= FindSymbolVisitor{ + cstore:self.session.cstore, + curr_path_idents: Vec::new(), + path_to_find:path, + suggestions: Vec::new(), + max_suggestions: 5, + }; - visit::walk_crate(&mut finder_visitor, self.resolver_krate, ()); + visit::walk_crate(&mut finder_visitor, self.resolver_krate, ()); - if finder_visitor.suggestions.len()>0 { - let mut note=~"did you mean:-\n"; - for &(ref name,_) in finder_visitor.suggestions.iter() { - note.push_str("\t\t\t"+ "::"+name.map(|i|{token::get_ident(*i).get().to_str()}).connect("::")+"\n"); - } - self.session.span_note(path.span, note); - } - } + if finder_visitor.suggestions.len()>0 { + let mut note=~"did you mean:-\n"; + for &(ref name,_) in finder_visitor.suggestions.iter() { + note.push_str( + "\t\t\t"+ "::"+ + name.map( + |i|{oken::get_ident(*i).get().to_str()}) + .connect("::")+"\n" + ); + } + self.session.span_note(path.span, note); + } + } } struct FindSymbolVisitor<'a>{ - cstore:@cstore::CStore, - curr_path_idents: Vec, - path_to_find:&'a Path, - suggestions: Vec<(Vec,uint)>, - max_suggestions: uint, + cstore:@cstore::CStore, + curr_path_idents: Vec, + path_to_find:&'a Path, + suggestions: Vec<(Vec,uint)>, + max_suggestions: uint, } impl<'a,'r> Visitor<()> for FindSymbolVisitor<'a> { - fn visit_item<'a>(&mut self, item:&Item, e: ()) { - let find_path_last_seg:&PathSegment = self.path_to_find.segments.last().unwrap(); - - self.curr_path_idents.push(item.ident); - - //todo: compare with current 'use' and uprate suggestions with more segs in scope. - if find_path_last_seg.identifier.name== item.ident.name && - self.suggestions.len() <= (self.max_suggestions) { - - let mut score=0; - for ref find_seg in self.path_to_find.segments.iter() { - for (ref index,ref ident) in self.curr_path_idents.iter().enumerate() { - if ident.name==find_seg.identifier.name {score+=*index;} - } - } - // sort such that highscore is first. - self.suggestions.push((self.curr_path_idents.clone(),score)); - self.suggestions.sort_by( - |&(_,ref score1),&(_,ref score2)| - if score1 self.max_suggestions {self.suggestions.pop();} - } - - // visit sub nodes.. - - visit::walk_item(self,item,e); - self.curr_path_idents.pop(); - } + fn visit_item<'a>(&mut self, item:&Item, e: ()) { + let find_path_last_seg:&PathSegment = self.path_to_find.segments.last().unwrap(); + + self.curr_path_idents.push(item.ident); + + //todo: compare with current 'use' and uprate suggestions with more segs in scope. + if find_path_last_seg.identifier.name== item.ident.name && + self.suggestions.len() <= (self.max_suggestions) { + + let mut score=0; + for ref find_seg in self.path_to_find.segments.iter() { + for (ref index,ref ident) in self.curr_path_idents.iter().enumerate() { + if ident.name==find_seg.identifier.name {score+=*index;} + } + } + // sort such that highscore is first. + self.suggestions.push((self.curr_path_idents.clone(),score)); + self.suggestions.sort_by( + |&(_,ref score1),&(_,ref score2)| + if score1 self.max_suggestions {self.suggestions.pop();} + } + + // visit sub nodes.. + + visit::walk_item(self,item,e); + self.curr_path_idents.pop(); + } fn visit_mod(&mut self, m: &Mod, _s: Span, _n: NodeId, e: ()) { - visit::walk_mod(self, m, e) ; - } + visit::walk_mod(self, m, e) ; + } } From b9be3aa51c52ead2e58662c8f28a7faa78809571 Mon Sep 17 00:00:00 2001 From: dobkeratops Date: Mon, 17 Mar 2014 00:22:25 +0000 Subject: [PATCH 5/8] fixed compile error --- src/librustc/middle/resolve.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index c340b15853360..4a19e322a2fab 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -5588,7 +5588,7 @@ impl<'r> Resolver<'r> { note.push_str( "\t\t\t"+ "::"+ name.map( - |i|{oken::get_ident(*i).get().to_str()}) + |i|{token::get_ident(*i).get().to_str()}) .connect("::")+"\n" ); } From 0d653b681caf36ab22f179f7c6c487717c50fb24 Mon Sep 17 00:00:00 2001 From: dobkeratops Date: Mon, 17 Mar 2014 01:12:22 +0000 Subject: [PATCH 6/8] renamed 'find_unresolved_symbol_suggestions --- src/librustc/middle/resolve.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index 4a19e322a2fab..06ec0d4f7f600 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -5178,7 +5178,7 @@ impl<'r> Resolver<'r> { self.resolve_error(expr.span, format!("unresolved name `{}`.", wrong_name)); - self.find_unresolved_symbol_elsewhere(path); + self.find_unresolved_symbol_suggestions(path); } } } @@ -5570,7 +5570,7 @@ impl<'r> Resolver<'r> { } // Search other modules for this ident, and advise possible module paths - fn find_unresolved_symbol_elsewhere(&mut self, path:&Path) { + fn find_unresolved_symbol_suggestions(&mut self, path:&Path) { let mut finder_visitor= FindSymbolVisitor{ cstore:self.session.cstore, From bb679a236429a7fde6e7c231ca96d7f0b85a1c38 Mon Sep 17 00:00:00 2001 From: dobkeratops Date: Mon, 17 Mar 2014 12:23:02 +0000 Subject: [PATCH 7/8] removed erroneous brinaies --- src/librustc/middle/resolve.rs | 38 ++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index 06ec0d4f7f600..0836bd2200aae 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -5164,23 +5164,25 @@ impl<'r> Resolver<'r> { wrong_name)); } - _ => - // limit search to 5 to reduce the number - // of stupid suggestions - match self.find_best_match_for_name(wrong_name, 5) { - Some(m) => { - self.resolve_error(expr.span, - format!("unresolved name `{}`. \ - Did you mean `{}`?", - wrong_name, m)); - } - None => { - self.resolve_error(expr.span, - format!("unresolved name `{}`.", - wrong_name)); - self.find_unresolved_symbol_suggestions(path); - } - } + _ => { + // limit search to 5 to reduce the number + // of stupid suggestions + match self.find_best_match_for_name(wrong_name, 5) { + Some(m) => { + self.resolve_error(expr.span, + format!("unresolved name `{}`. \ + Did you mean `{}`?", + wrong_name, m)); + } + None => { + self.resolve_error(expr.span, + format!("unresolved name `{}`.", + wrong_name)); + } + } + // additionally display exact symbol matches from other scopes. + self.show_suggestions_from_other_scopes(path); + } } } } @@ -5570,7 +5572,7 @@ impl<'r> Resolver<'r> { } // Search other modules for this ident, and advise possible module paths - fn find_unresolved_symbol_suggestions(&mut self, path:&Path) { + fn show_suggestions_from_other_scopes(&mut self, path:&Path) { let mut finder_visitor= FindSymbolVisitor{ cstore:self.session.cstore, From 9981b085e201534be46df3b6d14b0aa7235d6bcf Mon Sep 17 00:00:00 2001 From: dobkeratops Date: Mon, 17 Mar 2014 14:57:30 +0000 Subject: [PATCH 8/8] cleanup; removed suggestions that dont match all the given segs --- src/librustc/middle/resolve.rs | 46 +++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index 0836bd2200aae..811446347c727 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -5572,21 +5572,13 @@ impl<'r> Resolver<'r> { } // Search other modules for this ident, and advise possible module paths - fn show_suggestions_from_other_scopes(&mut self, path:&Path) { + fn show_suggestions_from_other_scopes(&self, path:&Path) { - let mut finder_visitor= FindSymbolVisitor{ - cstore:self.session.cstore, - curr_path_idents: Vec::new(), - path_to_find:path, - suggestions: Vec::new(), - max_suggestions: 5, - }; - - visit::walk_crate(&mut finder_visitor, self.resolver_krate, ()); + let suggestions = self.find_suggestions_from_other_scopes(path); - if finder_visitor.suggestions.len()>0 { + if suggestions.len()>0 { let mut note=~"did you mean:-\n"; - for &(ref name,_) in finder_visitor.suggestions.iter() { + for &(ref name,_) in suggestions.iter() { note.push_str( "\t\t\t"+ "::"+ name.map( @@ -5597,6 +5589,18 @@ impl<'r> Resolver<'r> { self.session.span_note(path.span, note); } } + + fn find_suggestions_from_other_scopes(&self, path:&Path)->Vec<(Vec,uint)> { + let mut finder_visitor= FindSymbolVisitor{ + cstore:self.session.cstore, + curr_path_idents: Vec::new(), + path_to_find:path, + suggestions: Vec::new(), + max_suggestions: 5, + }; + visit::walk_crate(&mut finder_visitor, self.resolver_krate, ()); + finder_visitor.suggestions + } } struct FindSymbolVisitor<'a>{ @@ -5607,7 +5611,6 @@ struct FindSymbolVisitor<'a>{ max_suggestions: uint, } - impl<'a,'r> Visitor<()> for FindSymbolVisitor<'a> { fn visit_item<'a>(&mut self, item:&Item, e: ()) { let find_path_last_seg:&PathSegment = self.path_to_find.segments.last().unwrap(); @@ -5619,19 +5622,22 @@ impl<'a,'r> Visitor<()> for FindSymbolVisitor<'a> { self.suggestions.len() <= (self.max_suggestions) { let mut score=0; + let mut segs_found=0; for ref find_seg in self.path_to_find.segments.iter() { for (ref index,ref ident) in self.curr_path_idents.iter().enumerate() { - if ident.name==find_seg.identifier.name {score+=*index;} + if ident.name==find_seg.identifier.name {score+=*index;segs_found+=1;} } } + if segs_found >=self.path_to_find.segments.len() { // sort such that highscore is first. - self.suggestions.push((self.curr_path_idents.clone(),score)); - self.suggestions.sort_by( - |&(_,ref score1),&(_,ref score2)| - if score1 self.max_suggestions {self.suggestions.pop();} + self.suggestions.push((self.curr_path_idents.clone(),score)); + self.suggestions.sort_by( + |&(_,ref score1),&(_,ref score2)| + if score1 self.max_suggestions {self.suggestions.pop();} + } } - // visit sub nodes.. visit::walk_item(self,item,e);