Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion 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;

use rustc_front::intravisit::{self, FnKind, Visitor};
use rustc_front::hir;
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
17 changes: 17 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;
use ext::mtwt;
use fold::Folder;

Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid duplicating this heuristic everywhere? It was tricky (#26087) and we may want to update it. I don't want to block on this, but I'd block on having FIXME comment with filed issue or at least having cross reference to same heuristic in resolve, both here and in resolve. (Best to just fix it though.)

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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use fileline_help please. Printing the span multiple times is not useful and only contributes to more useless visual noise.

}
}
}

/// 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
File renamed without changes.
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`?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion should also include the exclamation. i.e. println! instead of println.

}