Skip to content

Add help for crate arg when crate name is invalid #114058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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