Auto merge of #30064 - fhartwig:macro-suggestions, r=sanxiyn

Fixes #13677
This does the same sort of suggestion for misspelt macros that we already do for misspelt identifiers.
Example. Compiling this program:

```rust
macro_rules! foo {
    ($e:expr) => ( $e )
}

fn main() {
    fob!("hello!");
}
```

gives the following error message:

```
/Users/mcp/temp/test.rs:7:5: 7:8 error: macro undefined: 'fob!'
/Users/mcp/temp/test.rs:7     fob!("hello!");
                              ^~~
/Users/mcp/temp/test.rs:7:5: 7:8 help: did you mean `foo`?
/Users/mcp/temp/test.rs:7     fob!("hello!");
```

I had to move the levenshtein distance function into libsyntax for this. Maybe this should live somewhere else (some utility crate?), but I couldn't find a crate to put it in that is imported by libsyntax and the other rustc crates.
This commit is contained in:
bors 2015-11-27 18:41:53 +00:00
commit 5dc91a74b1
8 changed files with 42 additions and 8 deletions

View File

@ -146,7 +146,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

@ -62,7 +62,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};
@ -72,6 +71,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, max_suggestion_distance};
use rustc_front::intravisit::{self, FnKind, Visitor};
use rustc_front::hir;
@ -3383,11 +3383,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
}
// As a loose rule to avoid obviously incorrect suggestions, clamp the
// maximum edit distance we will accept for a suggestion to one third of
// the typo'd name's length.
let max_distance = std::cmp::max(name.len(), 3) / 3;
let max_distance = max_suggestion_distance(name);
if !values.is_empty() && values[smallest] <= max_distance && name != &maybes[smallest][..] {
SuggestionType::Function(maybes[smallest].to_string())

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, max_suggestion_distance};
use ext::mtwt;
use fold::Folder;
@ -776,6 +777,20 @@ 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) {
let mut min: Option<(Name, usize)> = None;
let max_dist = max_suggestion_distance(name);
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.fileline_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)]

View File

@ -41,6 +41,14 @@ pub fn lev_distance(me: &str, t: &str) -> usize {
dcol[t_last + 1]
}
pub fn max_suggestion_distance(name: &str) -> usize {
use std::cmp::max;
// As a loose rule to avoid obviously incorrect suggestions, clamp the
// maximum edit distance we will accept for a suggestion to one third of
// the typo'd name's length.
max(name.len(), 3) / 3
}
#[test]
fn test_lev_distance() {
use std::char::{ from_u32, MAX };

View File

@ -0,0 +1,14 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn main() {
printlx!("oh noes!"); //~ ERROR macro undefined
//~^ HELP did you mean `println!`?
}