-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Add suggestion of similar macro names to macro undefined
error message
#30064
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
Changes from 2 commits
a5e5c67
ac0220c
9ba657c
4bb7cf1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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); | ||
for macro_name in self.syntax_env.names.iter() { | ||
let dist = lev_distance(name, ¯o_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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
} | ||
} | ||
} | ||
|
||
/// Extract a string literal from the macro expanded version of `expr`, | ||
|
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`? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion should also include the exclamation. i.e. |
||
} |
There was a problem hiding this comment.
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.)