Suggest underscore when using dashes in crate namet push fork
This commit is contained in:
parent
8dbbd81f7e
commit
a3bf27b1db
@ -6508,6 +6508,40 @@ impl<'a> Parser<'a> {
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_crate_name_with_dashes(
|
||||
&mut self,
|
||||
error_msg: &str,
|
||||
suggestion_msg: &str,
|
||||
) -> PResult<'a, ast::Ident> {
|
||||
let mut ident = self.parse_ident()?;
|
||||
let mut idents = vec![];
|
||||
let mut replacement = vec![];
|
||||
let mut fixed_crate_name = false;
|
||||
// Accept `extern crate name-like-this` for better diagnostics
|
||||
let dash = token::Token::BinOp(token::BinOpToken::Minus);
|
||||
if self.token == dash { // Do not include `-` as part of the expected tokens list
|
||||
while self.eat(&dash) {
|
||||
fixed_crate_name = true;
|
||||
replacement.push((self.prev_span, "_".to_string()));
|
||||
idents.push(self.parse_ident()?);
|
||||
}
|
||||
}
|
||||
if fixed_crate_name {
|
||||
let fixed_name_sp = ident.span.to(idents.last().unwrap().span);
|
||||
let mut fixed_name = format!("{}", ident.name);
|
||||
for part in idents {
|
||||
fixed_name.push_str(&format!("_{}", part.name));
|
||||
}
|
||||
ident = Ident::from_str(&fixed_name).with_span_pos(fixed_name_sp);
|
||||
|
||||
let mut err = self.struct_span_err(fixed_name_sp, error_msg);
|
||||
err.span_label(fixed_name_sp, "dash-separated idents are not valid");
|
||||
err.multipart_suggestion(suggestion_msg, replacement);
|
||||
err.emit();
|
||||
}
|
||||
Ok(ident)
|
||||
}
|
||||
|
||||
/// Parse extern crate links
|
||||
///
|
||||
/// # Examples
|
||||
@ -6519,11 +6553,15 @@ impl<'a> Parser<'a> {
|
||||
visibility: Visibility,
|
||||
attrs: Vec<Attribute>)
|
||||
-> PResult<'a, P<Item>> {
|
||||
let orig_name = self.parse_ident()?;
|
||||
// Accept `extern crate name-like-this` for better diagnostics
|
||||
let ident = self.parse_crate_name_with_dashes(
|
||||
"crate name using dashes are not valid in `extern crate` statements",
|
||||
"if the original crate name uses dashes you need to use underscores in the code",
|
||||
)?;
|
||||
let (item_name, orig_name) = if let Some(rename) = self.parse_rename()? {
|
||||
(rename, Some(orig_name.name))
|
||||
(rename, Some(ident.name))
|
||||
} else {
|
||||
(orig_name, None)
|
||||
(ident, None)
|
||||
};
|
||||
self.expect(&token::Semi)?;
|
||||
|
||||
|
15
src/test/ui/bad-crate-name.rs
Normal file
15
src/test/ui/bad-crate-name.rs
Normal file
@ -0,0 +1,15 @@
|
||||
// Copyright 2018 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.
|
||||
|
||||
extern crate krate-name-here;
|
||||
//~^ ERROR crate name using dashes are not valid in `extern crate` statements
|
||||
//~| ERROR can't find crate for `krate_name_here`
|
||||
|
||||
fn main() {}
|
19
src/test/ui/bad-crate-name.stderr
Normal file
19
src/test/ui/bad-crate-name.stderr
Normal file
@ -0,0 +1,19 @@
|
||||
error: crate name using dashes are not valid in `extern crate` statements
|
||||
--> $DIR/bad-crate-name.rs:11:14
|
||||
|
|
||||
LL | extern crate krate-name-here;
|
||||
| ^^^^^^^^^^^^^^^ dash-separated idents are not valid
|
||||
help: if the original crate name uses dashes you need to use underscores in the code
|
||||
|
|
||||
LL | extern crate krate_name_here;
|
||||
| ^ ^
|
||||
|
||||
error[E0463]: can't find crate for `krate_name_here`
|
||||
--> $DIR/bad-crate-name.rs:11:1
|
||||
|
|
||||
LL | extern crate krate-name-here;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0463`.
|
Loading…
x
Reference in New Issue
Block a user