Skip to content
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
9 changes: 9 additions & 0 deletions compiler/rustc_session/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,11 +435,20 @@ pub(crate) struct CrateNameEmpty {

#[derive(Diagnostic)]
#[diag("invalid character {$character} in crate name: `{$crate_name}`")]
#[note("crate names may only contain alphanumeric characters or underscores")]
pub(crate) struct InvalidCharacterInCrateName {
#[primary_span]
pub(crate) span: Option<Span>,
pub(crate) character: char,
pub(crate) crate_name: Symbol,
#[subdiagnostic]
pub(crate) suggestion: Option<InvalidCharacterInCrateNameSuggestion>,
}

#[derive(Subdiagnostic)]
#[help("you might have meant to use `--crate-name={$suggested_name}`")]
pub(crate) struct InvalidCharacterInCrateNameSuggestion {
pub(crate) suggested_name: String,
}

#[derive(Subdiagnostic)]
Expand Down
9 changes: 8 additions & 1 deletion compiler/rustc_session/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use rustc_span::{Span, Symbol};

use crate::Session;
use crate::config::{CrateType, OutFileName, OutputFilenames, OutputType};
use crate::diagnostics::{CrateNameEmpty, FileIsNotWriteable, InvalidCharacterInCrateName};
use crate::diagnostics::{
CrateNameEmpty, FileIsNotWriteable, InvalidCharacterInCrateName,
InvalidCharacterInCrateNameSuggestion,
};

pub fn out_filename(
sess: &Session,
Expand Down Expand Up @@ -68,6 +71,10 @@ pub fn validate_crate_name(sess: &Session, crate_name: Symbol, span: Option<Span
span,
character: c,
crate_name,
suggestion: span.is_none().then(|| InvalidCharacterInCrateNameSuggestion {
suggested_name:
crate_name.as_str().replace(|c: char| c != '_' && !c.is_alphanumeric(), "_"),
}),
Comment on lines +74 to +77

@estebank estebank Jul 20, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if suggesting replacing something like an emoji with _ will be always the best idea, but can't think of better behavior other than replacing all invalid chars with underscore, not just - dashes.

View changes since the review

}));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Invalid command-line crate names should suggest a sanitized replacement.

//@ compile-flags: --crate-name=my-crate

fn main() {}

//~? ERROR invalid character '-' in crate name
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: invalid character '-' in crate name: `my-crate`
|
= note: crate names may only contain alphanumeric characters or underscores
= help: you might have meant to use `--crate-name=my_crate`

error: aborting due to 1 previous error

Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
error: invalid character '$' in crate name: `need_crate_arg_ignore_tidy$x`
|
= note: crate names may only contain alphanumeric characters or underscores
= help: you might have meant to use `--crate-name=need_crate_arg_ignore_tidy_x`

error: aborting due to 1 previous error

Loading