Add suggestion of similar macro names to macro undefined error message

This commit is contained in:
Florian Hartwig 2015-11-26 00:21:38 +01:00
parent edf2198f5f
commit a5e5c67756
7 changed files with 21 additions and 3 deletions

View File

@ -152,7 +152,6 @@ pub mod util {
pub mod common;
pub mod ppaux;
pub mod nodemap;
pub mod lev_distance;
pub mod num;
pub mod fs;
}

View File

@ -63,7 +63,6 @@ use rustc::middle::privacy::*;
use rustc::middle::subst::{ParamSpace, FnSpace, TypeSpace};
use rustc::middle::ty::{Freevar, FreevarMap, TraitMap, GlobMap};
use rustc::util::nodemap::{NodeMap, DefIdSet, FnvHashMap};
use rustc::util::lev_distance::lev_distance;
use syntax::ast;
use syntax::ast::{CRATE_NODE_ID, Ident, Name, NodeId, CrateNum, TyIs, TyI8, TyI16, TyI32, TyI64};
@ -73,6 +72,7 @@ use syntax::ext::mtwt;
use syntax::parse::token::{self, special_names, special_idents};
use syntax::ptr::P;
use syntax::codemap::{self, Span, Pos};
use syntax::util::lev_distance::lev_distance;
use rustc_front::intravisit::{self, FnKind, Visitor};
use rustc_front::hir;

View File

@ -110,7 +110,6 @@ use TypeAndSubsts;
use lint;
use util::common::{block_query, ErrorReported, indenter, loop_query};
use util::nodemap::{DefIdMap, FnvHashMap, NodeMap};
use util::lev_distance::lev_distance;
use std::cell::{Cell, Ref, RefCell};
use std::collections::{HashSet};
@ -123,6 +122,7 @@ use syntax::codemap::{self, Span, Spanned};
use syntax::owned_slice::OwnedSlice;
use syntax::parse::token::{self, InternedString};
use syntax::ptr::P;
use syntax::util::lev_distance::lev_distance;
use rustc_front::intravisit::{self, Visitor};
use rustc_front::hir;

View File

@ -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, &macro_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`,

View File

@ -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

View File

@ -65,6 +65,7 @@ macro_rules! panictry {
pub mod util {
pub mod interner;
pub mod lev_distance;
pub mod node_count;
pub mod parser;
#[cfg(test)]