Skip to content

Commit

Permalink
Rollup merge of rust-lang#52740 - estebank:crate-name, r=petrochenkov
Browse files Browse the repository at this point in the history
Suggest underscore when using dashes in crate namet push fork

Fix rust-lang#48437.
  • Loading branch information
kennytm committed Jul 28, 2018
2 parents 7da2214 + 647d295 commit b584c32
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions src/test/ui/bad-crate-name.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 19 additions & 0 deletions src/test/ui/bad-crate-name.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit b584c32

Please sign in to comment.