Rollup merge of #52740 - estebank:crate-name, r=petrochenkov

Suggest underscore when using dashes in crate namet push fork

Fix #48437.
This commit is contained in:
kennytm 2018-07-28 16:24:58 +08:00 committed by GitHub
commit b584c3227d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 1 deletions

View File

@ -6514,6 +6514,39 @@ impl<'a> Parser<'a> {
})
}
fn parse_crate_name_with_dashes(&mut self) -> PResult<'a, ast::Ident> {
let error_msg = "crate name using dashes are not valid in `extern crate` statements";
let suggestion_msg = "if the original crate name uses dashes you need to use underscores \
in the code";
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
@ -6525,7 +6558,8 @@ 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 orig_name = self.parse_crate_name_with_dashes()?;
let (item_name, orig_name) = if let Some(rename) = self.parse_rename()? {
(rename, Some(orig_name.name))
} else {

View 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() {}

View 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`.