Add suggestion of similar macro names to macro undefined error message
This commit is contained in:
@@ -24,6 +24,7 @@ use parse::token;
|
||||
use parse::token::{InternedString, intern, str_to_ident};
|
||||
use ptr::P;
|
||||
use util::small_vector::SmallVector;
|
||||
use util::lev_distance::lev_distance;
|
||||
use ext::mtwt;
|
||||
use fold::Folder;
|
||||
|
||||
@@ -776,6 +777,22 @@ impl<'a> ExtCtxt<'a> {
|
||||
pub fn name_of(&self, st: &str) -> ast::Name {
|
||||
token::intern(st)
|
||||
}
|
||||
|
||||
pub fn suggest_macro_name(&mut self, name: &str, span: Span) {
|
||||
use std::cmp::max;
|
||||
|
||||
let mut min: Option<(Name, usize)> = None;
|
||||
let max_dist = max(name.len() / 3, 1);
|
||||
for macro_name in self.syntax_env.names.iter() {
|
||||
let dist = lev_distance(name, ¯o_name.as_str());
|
||||
if dist <= max_dist && (min.is_none() || min.unwrap().1 > dist) {
|
||||
min = Some((*macro_name, dist));
|
||||
}
|
||||
}
|
||||
if let Some((suggestion, _)) = min {
|
||||
self.span_help(span, &format!("did you mean `{}`?", suggestion));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract a string literal from the macro expanded version of `expr`,
|
||||
|
||||
@@ -191,6 +191,7 @@ fn expand_mac_invoc<T, F, G>(mac: ast::Mac,
|
||||
pth.span,
|
||||
&format!("macro undefined: '{}!'",
|
||||
&extname));
|
||||
fld.cx.suggest_macro_name(&extname.as_str(), pth.span);
|
||||
|
||||
// let compilation continue
|
||||
None
|
||||
|
||||
Reference in New Issue
Block a user