Skip to content

Commit b584c32

Browse files
authored
Rollup merge of #52740 - estebank:crate-name, r=petrochenkov
Suggest underscore when using dashes in crate namet push fork Fix #48437.
2 parents 7da2214 + 647d295 commit b584c32

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6514,6 +6514,39 @@ impl<'a> Parser<'a> {
65146514
})
65156515
}
65166516

6517+
fn parse_crate_name_with_dashes(&mut self) -> PResult<'a, ast::Ident> {
6518+
let error_msg = "crate name using dashes are not valid in `extern crate` statements";
6519+
let suggestion_msg = "if the original crate name uses dashes you need to use underscores \
6520+
in the code";
6521+
let mut ident = self.parse_ident()?;
6522+
let mut idents = vec![];
6523+
let mut replacement = vec![];
6524+
let mut fixed_crate_name = false;
6525+
// Accept `extern crate name-like-this` for better diagnostics
6526+
let dash = token::Token::BinOp(token::BinOpToken::Minus);
6527+
if self.token == dash { // Do not include `-` as part of the expected tokens list
6528+
while self.eat(&dash) {
6529+
fixed_crate_name = true;
6530+
replacement.push((self.prev_span, "_".to_string()));
6531+
idents.push(self.parse_ident()?);
6532+
}
6533+
}
6534+
if fixed_crate_name {
6535+
let fixed_name_sp = ident.span.to(idents.last().unwrap().span);
6536+
let mut fixed_name = format!("{}", ident.name);
6537+
for part in idents {
6538+
fixed_name.push_str(&format!("_{}", part.name));
6539+
}
6540+
ident = Ident::from_str(&fixed_name).with_span_pos(fixed_name_sp);
6541+
6542+
let mut err = self.struct_span_err(fixed_name_sp, error_msg);
6543+
err.span_label(fixed_name_sp, "dash-separated idents are not valid");
6544+
err.multipart_suggestion(suggestion_msg, replacement);
6545+
err.emit();
6546+
}
6547+
Ok(ident)
6548+
}
6549+
65176550
/// Parse extern crate links
65186551
///
65196552
/// # Examples
@@ -6525,7 +6558,8 @@ impl<'a> Parser<'a> {
65256558
visibility: Visibility,
65266559
attrs: Vec<Attribute>)
65276560
-> PResult<'a, P<Item>> {
6528-
let orig_name = self.parse_ident()?;
6561+
// Accept `extern crate name-like-this` for better diagnostics
6562+
let orig_name = self.parse_crate_name_with_dashes()?;
65296563
let (item_name, orig_name) = if let Some(rename) = self.parse_rename()? {
65306564
(rename, Some(orig_name.name))
65316565
} else {

src/test/ui/bad-crate-name.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
extern crate krate-name-here;
12+
//~^ ERROR crate name using dashes are not valid in `extern crate` statements
13+
//~| ERROR can't find crate for `krate_name_here`
14+
15+
fn main() {}

src/test/ui/bad-crate-name.stderr

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: crate name using dashes are not valid in `extern crate` statements
2+
--> $DIR/bad-crate-name.rs:11:14
3+
|
4+
LL | extern crate krate-name-here;
5+
| ^^^^^^^^^^^^^^^ dash-separated idents are not valid
6+
help: if the original crate name uses dashes you need to use underscores in the code
7+
|
8+
LL | extern crate krate_name_here;
9+
| ^ ^
10+
11+
error[E0463]: can't find crate for `krate_name_here`
12+
--> $DIR/bad-crate-name.rs:11:1
13+
|
14+
LL | extern crate krate-name-here;
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0463`.

0 commit comments

Comments
 (0)