Skip to content

Commit

Permalink
Rollup merge of rust-lang#54173 - phansch:suggest_valid_crate_type, r…
Browse files Browse the repository at this point in the history
…=estebank

Suggest valid crate type if invalid crate type is found

This adds a suggestion to the `invalid_crate_types` lint.

The suggestion is based on the Levenshtein distance to existing crate
types. If no suggestion is found it will show the lint without any
suggestions.

Closes rust-lang#53958
  • Loading branch information
kennytm committed Sep 14, 2018
2 parents 9c0f946 + 7249a1b commit d51c364
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 10 deletions.
9 changes: 9 additions & 0 deletions src/librustc/lint/builtin.rs
Expand Up @@ -422,6 +422,7 @@ pub enum BuiltinLintDiagnostics {
ProcMacroDeriveResolutionFallback(Span),
MacroExpandedMacroExportsAccessedByAbsolutePaths(Span),
ElidedLifetimesInPaths(usize, Span, bool, Span, String),
UnknownCrateTypes(Span, String, String),
}

impl BuiltinLintDiagnostics {
Expand Down Expand Up @@ -500,6 +501,14 @@ impl BuiltinLintDiagnostics {
Applicability::MachineApplicable
);
}
BuiltinLintDiagnostics::UnknownCrateTypes(span, note, sugg) => {
db.span_suggestion_with_applicability(
span,
&note,
sugg,
Applicability::MaybeIncorrect
);
}
}
}
}
Expand Down
48 changes: 41 additions & 7 deletions src/librustc_driver/driver.rs
Expand Up @@ -57,6 +57,8 @@ use syntax::ext::base::ExtCtxt;
use syntax::fold::Folder;
use syntax::parse::{self, PResult};
use syntax::util::node_count::NodeCounter;
use syntax::util::lev_distance::find_best_match_for_name;
use syntax::symbol::Symbol;
use syntax_pos::{FileName, hygiene};
use syntax_ext;

Expand Down Expand Up @@ -1508,13 +1510,45 @@ pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<c
Some(ref n) if *n == "staticlib" => Some(config::CrateType::Staticlib),
Some(ref n) if *n == "proc-macro" => Some(config::CrateType::ProcMacro),
Some(ref n) if *n == "bin" => Some(config::CrateType::Executable),
Some(_) => {
session.buffer_lint(
lint::builtin::UNKNOWN_CRATE_TYPES,
ast::CRATE_NODE_ID,
a.span,
"invalid `crate_type` value",
);
Some(ref n) => {
let crate_types = vec![
Symbol::intern("rlib"),
Symbol::intern("dylib"),
Symbol::intern("cdylib"),
Symbol::intern("lib"),
Symbol::intern("staticlib"),
Symbol::intern("proc-macro"),
Symbol::intern("bin")
];
if let ast::MetaItemKind::NameValue(spanned) = a.meta().unwrap().node {
let span = spanned.span;
let lev_candidate = find_best_match_for_name(
crate_types.iter(),
&n.as_str(),
None
);
if let Some(candidate) = lev_candidate {
session.buffer_lint_with_diagnostic(
lint::builtin::UNKNOWN_CRATE_TYPES,
ast::CRATE_NODE_ID,
span,
"invalid `crate_type` value",
lint::builtin::BuiltinLintDiagnostics::
UnknownCrateTypes(
span,
"did you mean".to_string(),
format!("\"{}\"", candidate)
)
);
} else {
session.buffer_lint(
lint::builtin::UNKNOWN_CRATE_TYPES,
ast::CRATE_NODE_ID,
span,
"invalid `crate_type` value"
);
}
}
None
}
_ => {
Expand Down
42 changes: 42 additions & 0 deletions src/test/ui/invalid/invalid-crate-type.rs
Expand Up @@ -11,6 +11,48 @@
// regression test for issue 11256
#![crate_type="foo"] //~ ERROR invalid `crate_type` value

// Tests for suggestions (#53958)

#![crate_type="statoclib"]
//~^ ERROR invalid `crate_type` value
//~| HELP did you mean
//~| SUGGESTION staticlib

#![crate_type="procmacro"]
//~^ ERROR invalid `crate_type` value
//~| HELP did you mean
//~| SUGGESTION proc-macro

#![crate_type="static-lib"]
//~^ ERROR invalid `crate_type` value
//~| HELP did you mean
//~| SUGGESTION staticlib

#![crate_type="drylib"]
//~^ ERROR invalid `crate_type` value
//~| HELP did you mean
//~| SUGGESTION dylib

#![crate_type="dlib"]
//~^ ERROR invalid `crate_type` value
//~| HELP did you mean
//~| SUGGESTION rlib

#![crate_type="lob"]
//~^ ERROR invalid `crate_type` value
//~| HELP did you mean
//~| SUGGESTION lib

#![crate_type="bon"]
//~^ ERROR invalid `crate_type` value
//~| HELP did you mean
//~| SUGGESTION bin

#![crate_type="cdalib"]
//~^ ERROR invalid `crate_type` value
//~| HELP did you mean
//~| SUGGESTION cdylib

fn main() {
return
}
54 changes: 51 additions & 3 deletions src/test/ui/invalid/invalid-crate-type.stderr
@@ -1,10 +1,58 @@
error: invalid `crate_type` value
--> $DIR/invalid-crate-type.rs:12:1
--> $DIR/invalid-crate-type.rs:12:15
|
LL | #![crate_type="foo"] //~ ERROR invalid `crate_type` value
| ^^^^^^^^^^^^^^^^^^^^
| ^^^^^
|
= note: #[deny(unknown_crate_types)] on by default

error: aborting due to previous error
error: invalid `crate_type` value
--> $DIR/invalid-crate-type.rs:16:15
|
LL | #![crate_type="statoclib"]
| ^^^^^^^^^^^ help: did you mean: `"staticlib"`

error: invalid `crate_type` value
--> $DIR/invalid-crate-type.rs:21:15
|
LL | #![crate_type="procmacro"]
| ^^^^^^^^^^^ help: did you mean: `"proc-macro"`

error: invalid `crate_type` value
--> $DIR/invalid-crate-type.rs:26:15
|
LL | #![crate_type="static-lib"]
| ^^^^^^^^^^^^ help: did you mean: `"staticlib"`

error: invalid `crate_type` value
--> $DIR/invalid-crate-type.rs:31:15
|
LL | #![crate_type="drylib"]
| ^^^^^^^^ help: did you mean: `"dylib"`

error: invalid `crate_type` value
--> $DIR/invalid-crate-type.rs:36:15
|
LL | #![crate_type="dlib"]
| ^^^^^^ help: did you mean: `"rlib"`

error: invalid `crate_type` value
--> $DIR/invalid-crate-type.rs:41:15
|
LL | #![crate_type="lob"]
| ^^^^^ help: did you mean: `"lib"`

error: invalid `crate_type` value
--> $DIR/invalid-crate-type.rs:46:15
|
LL | #![crate_type="bon"]
| ^^^^^ help: did you mean: `"bin"`

error: invalid `crate_type` value
--> $DIR/invalid-crate-type.rs:51:15
|
LL | #![crate_type="cdalib"]
| ^^^^^^^^ help: did you mean: `"cdylib"`

error: aborting due to 9 previous errors

0 comments on commit d51c364

Please sign in to comment.