From e0c479eea2382930bb550bbb7ddb98a841f8c54b Mon Sep 17 00:00:00 2001 From: yukang Date: Mon, 24 Jul 2023 13:09:52 +0800 Subject: [PATCH] Add help for crate arg when crate name is invalid --- compiler/rustc_session/messages.ftl | 1 + compiler/rustc_session/src/errors.rs | 8 ++++++++ compiler/rustc_session/src/output.rs | 13 +++++++++++-- src/tools/tidy/src/ui_tests.rs | 4 +++- tests/ui/command/need-crate-arg-ignore-tidy.x.rs | 2 ++ .../ui/command/need-crate-arg-ignore-tidy.x.stderr | 6 ++++++ 6 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 tests/ui/command/need-crate-arg-ignore-tidy.x.rs create mode 100644 tests/ui/command/need-crate-arg-ignore-tidy.x.stderr diff --git a/compiler/rustc_session/messages.ftl b/compiler/rustc_session/messages.ftl index ee24c6d902f..d4042a2e61a 100644 --- a/compiler/rustc_session/messages.ftl +++ b/compiler/rustc_session/messages.ftl @@ -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}` diff --git a/compiler/rustc_session/src/errors.rs b/compiler/rustc_session/src/errors.rs index dd15ad45145..1ffee01b2f1 100644 --- a/compiler/rustc_session/src/errors.rs +++ b/compiler/rustc_session/src/errors.rs @@ -199,6 +199,14 @@ pub struct InvalidCharacterInCrateName { pub span: Option, pub character: char, pub crate_name: Symbol, + #[subdiagnostic] + pub crate_name_help: Option, +} + +#[derive(Subdiagnostic)] +pub enum InvalidCrateNameHelp { + #[help(session_invalid_character_in_create_name_help)] + AddCrateName, } #[derive(Subdiagnostic)] diff --git a/compiler/rustc_session/src/output.rs b/compiler/rustc_session/src/output.rs index 2088744bc5b..c0884fb21cd 100644 --- a/compiler/rustc_session/src/output.rs +++ b/compiler/rustc_session/src/output.rs @@ -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}; @@ -101,7 +101,16 @@ pub fn validate_crate_name(sess: &Session, s: Symbol, sp: Option) { 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 + }, + }); } } diff --git a/src/tools/tidy/src/ui_tests.rs b/src/tools/tidy/src/ui_tests.rs index c3a63952841..18cee7d3d5c 100644 --- a/src/tools/tidy/src/ui_tests.rs +++ b/src/tools/tidy/src/ui_tests.rs @@ -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); } diff --git a/tests/ui/command/need-crate-arg-ignore-tidy.x.rs b/tests/ui/command/need-crate-arg-ignore-tidy.x.rs new file mode 100644 index 00000000000..b1ac4a4ae21 --- /dev/null +++ b/tests/ui/command/need-crate-arg-ignore-tidy.x.rs @@ -0,0 +1,2 @@ +// issue: 113981 +pub fn main() {} diff --git a/tests/ui/command/need-crate-arg-ignore-tidy.x.stderr b/tests/ui/command/need-crate-arg-ignore-tidy.x.stderr new file mode 100644 index 00000000000..305f76694f7 --- /dev/null +++ b/tests/ui/command/need-crate-arg-ignore-tidy.x.stderr @@ -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 +