Skip to content

Commit

Permalink
Auto merge of #30064 - fhartwig:macro-suggestions, r=sanxiyn
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Nov 27, 2015
2 parents ca63650 + 4bb7cf1 commit 5dc91a7
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 2 additions & 6 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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;
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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`,
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
14 changes: 14 additions & 0 deletions src/test/compile-fail/macro-name-typo.rs
Original file line number Diff line number Diff line change
@@ -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!`?
}

0 comments on commit 5dc91a7

Please sign in to comment.