Skip to content

Commit a5e5c67

Browse files
committed
Add suggestion of similar macro names to macro undefined error message
1 parent edf2198 commit a5e5c67

File tree

7 files changed

+21
-3
lines changed

7 files changed

+21
-3
lines changed

src/librustc/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ pub mod util {
152152
pub mod common;
153153
pub mod ppaux;
154154
pub mod nodemap;
155-
pub mod lev_distance;
156155
pub mod num;
157156
pub mod fs;
158157
}

src/librustc_resolve/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ use rustc::middle::privacy::*;
6363
use rustc::middle::subst::{ParamSpace, FnSpace, TypeSpace};
6464
use rustc::middle::ty::{Freevar, FreevarMap, TraitMap, GlobMap};
6565
use rustc::util::nodemap::{NodeMap, DefIdSet, FnvHashMap};
66-
use rustc::util::lev_distance::lev_distance;
6766

6867
use syntax::ast;
6968
use syntax::ast::{CRATE_NODE_ID, Ident, Name, NodeId, CrateNum, TyIs, TyI8, TyI16, TyI32, TyI64};
@@ -73,6 +72,7 @@ use syntax::ext::mtwt;
7372
use syntax::parse::token::{self, special_names, special_idents};
7473
use syntax::ptr::P;
7574
use syntax::codemap::{self, Span, Pos};
75+
use syntax::util::lev_distance::lev_distance;
7676

7777
use rustc_front::intravisit::{self, FnKind, Visitor};
7878
use rustc_front::hir;

src/librustc_typeck/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ use TypeAndSubsts;
110110
use lint;
111111
use util::common::{block_query, ErrorReported, indenter, loop_query};
112112
use util::nodemap::{DefIdMap, FnvHashMap, NodeMap};
113-
use util::lev_distance::lev_distance;
114113

115114
use std::cell::{Cell, Ref, RefCell};
116115
use std::collections::{HashSet};
@@ -123,6 +122,7 @@ use syntax::codemap::{self, Span, Spanned};
123122
use syntax::owned_slice::OwnedSlice;
124123
use syntax::parse::token::{self, InternedString};
125124
use syntax::ptr::P;
125+
use syntax::util::lev_distance::lev_distance;
126126

127127
use rustc_front::intravisit::{self, Visitor};
128128
use rustc_front::hir;

src/libsyntax/ext/base.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use parse::token;
2424
use parse::token::{InternedString, intern, str_to_ident};
2525
use ptr::P;
2626
use util::small_vector::SmallVector;
27+
use util::lev_distance::lev_distance;
2728
use ext::mtwt;
2829
use fold::Folder;
2930

@@ -776,6 +777,22 @@ impl<'a> ExtCtxt<'a> {
776777
pub fn name_of(&self, st: &str) -> ast::Name {
777778
token::intern(st)
778779
}
780+
781+
pub fn suggest_macro_name(&mut self, name: &str, span: Span) {
782+
use std::cmp::max;
783+
784+
let mut min: Option<(Name, usize)> = None;
785+
let max_dist = max(name.len() / 3, 1);
786+
for macro_name in self.syntax_env.names.iter() {
787+
let dist = lev_distance(name, &macro_name.as_str());
788+
if dist <= max_dist && (min.is_none() || min.unwrap().1 > dist) {
789+
min = Some((*macro_name, dist));
790+
}
791+
}
792+
if let Some((suggestion, _)) = min {
793+
self.span_help(span, &format!("did you mean `{}`?", suggestion));
794+
}
795+
}
779796
}
780797

781798
/// Extract a string literal from the macro expanded version of `expr`,

src/libsyntax/ext/expand.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ fn expand_mac_invoc<T, F, G>(mac: ast::Mac,
191191
pth.span,
192192
&format!("macro undefined: '{}!'",
193193
&extname));
194+
fld.cx.suggest_macro_name(&extname.as_str(), pth.span);
194195

195196
// let compilation continue
196197
None

src/libsyntax/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ macro_rules! panictry {
6565

6666
pub mod util {
6767
pub mod interner;
68+
pub mod lev_distance;
6869
pub mod node_count;
6970
pub mod parser;
7071
#[cfg(test)]

0 commit comments

Comments
 (0)