Skip to content

Commit

Permalink
Rollup merge of rust-lang#114058 - chenyukang:yukang-fix-113981-crate…
Browse files Browse the repository at this point in the history
…-arg, r=fmease,oli-obk

Add help for crate arg when crate name is invalid

Fixes rust-lang#113981
  • Loading branch information
matthiaskrgr committed Jul 25, 2023
2 parents b7a1ff2 + e0c479e commit de5228e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_session/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ session_int_literal_too_large = integer literal is too large
.note = value exceeds limit of `{$limit}`
session_invalid_character_in_create_name = invalid character `{$character}` in crate name: `{$crate_name}`
session_invalid_character_in_create_name_help = you can either pass `--crate-name` on the command line or add `#![crate_name="…"]` to set the crate name
session_invalid_float_literal_suffix = invalid suffix `{$suffix}` for float literal
.label = invalid suffix `{$suffix}`
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_session/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ pub struct InvalidCharacterInCrateName {
pub span: Option<Span>,
pub character: char,
pub crate_name: Symbol,
#[subdiagnostic]
pub crate_name_help: Option<InvalidCrateNameHelp>,
}

#[derive(Subdiagnostic)]
pub enum InvalidCrateNameHelp {
#[help(session_invalid_character_in_create_name_help)]
AddCrateName,
}

#[derive(Subdiagnostic)]
Expand Down
13 changes: 11 additions & 2 deletions compiler/rustc_session/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::config::{CrateType, Input, OutFileName, OutputFilenames, OutputType};
use crate::errors::{
CrateNameDoesNotMatch, CrateNameEmpty, CrateNameInvalid, FileIsNotWriteable,
InvalidCharacterInCrateName,
InvalidCharacterInCrateName, InvalidCrateNameHelp,
};
use crate::Session;
use rustc_ast::{self as ast, attr};
Expand Down Expand Up @@ -101,7 +101,16 @@ pub fn validate_crate_name(sess: &Session, s: Symbol, sp: Option<Span>) {
continue;
}
err_count += 1;
sess.emit_err(InvalidCharacterInCrateName { span: sp, character: c, crate_name: s });
sess.emit_err(InvalidCharacterInCrateName {
span: sp,
character: c,
crate_name: s,
crate_name_help: if sp.is_none() {
Some(InvalidCrateNameHelp::AddCrateName)
} else {
None
},
});
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/tools/tidy/src/ui_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ pub fn check(path: &Path, bad: &mut bool) {
// must strip all of them.
let testname =
file_path.file_name().unwrap().to_str().unwrap().split_once('.').unwrap().0;
if !file_path.with_file_name(testname).with_extension("rs").exists() {
if !file_path.with_file_name(testname).with_extension("rs").exists()
&& !testname.contains("ignore-tidy")
{
tidy_error!(bad, "Stray file with UI testing output: {:?}", file_path);
}

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/command/need-crate-arg-ignore-tidy.x.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// issue: 113981
pub fn main() {}
6 changes: 6 additions & 0 deletions tests/ui/command/need-crate-arg-ignore-tidy.x.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error: invalid character `'.'` in crate name: `need_crate_arg_ignore_tidy.x`
|
= help: you can either pass `--crate-name` on the command line or add `#![crate_name="…"]` to set the crate name

error: aborting due to previous error

0 comments on commit de5228e

Please sign in to comment.