Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add suggestion of similar macro names to macro undefined error message #30064

Merged
merged 4 commits into from
Nov 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
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 @@ -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};
Expand All @@ -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, max_suggestion_distance};

use rustc_front::intravisit::{self, FnKind, Visitor};
use rustc_front::hir;
Expand Down Expand Up @@ -3384,11 +3384,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!`?
}